Looking for wildcards inside a Boost.MultiIndex data structure?

I am trying to optimize my application by reducing the number of hits to my database. As part of this effort, I was moving some of the tables into memory, storing them as Boost.MultiIndex .

As a side effect of this process, I lost the ability to perform wild-card matching on my lines. For example, when the table was saved in MySQL, I could do this:

SELECT * FROM m_table WHERE myString LIKE "foo%"

      

However, since I am now using the Boost.MultiIndex container with the key myString, it seems that I have lost this ability.

Obviously, I can use the equal_range () function to find exactly all entries that match a particular string:

std::pair< typename T::template index<by_name>::type::iterator,
           typename T::template index<by_name>::type::iterator > p
  = m_table.get<by_name>().equal_range(myString);

while (p.first != p.second )
{
  // do something with the EXACT matching entry
  ++p.first;
}

      

But it looks like the only way to do wild-card matching is to go through the whole structure and compare each key with boost :: regex with boost :: regex_match ().

std::pair< typename T::template index<by_name>::type::iterator,
           typename T::template index<by_name>::type::iterator > p
  = std::make_pair(m_table.get<by_name>().begin(),m_table.get<by_name>().end());

while (p.first != p.second )
{
  boost::regex e(myRegex);
  if ( boost::regex_match(p.first->myString, e ) )
  {
     // Do something with the REGEX matching entry
  }
  ++p.first;
}

      

Is there a better way?

+2


source to share


2 answers


Well, you don't need to use boost :: regex at first, if the wildcard is simple enough you can get away by folding your own unary operator. I'd like to point out that Boost.Regex is one of the few parts of the library that actually requires linking (not just the header).

As for the problem of walking all over the structure, I'm sorry, but there isn't much you can do here ... unless you know the advance searches.



If you know the parameters to look for in advance, then you can create a special kind of Multi-Index container suitable for this task using a dedicated comparator / hashing (for example, one that only takes into account the first 3 characters).

If you were hoping for more, please provide more information about the types of wildcards you want to use and the circumstances.

+1


source


In your particular case, you can do lower_bound ("foo") and then step forward to search for matches until you hit something that doesn't match or reach the end of the container. I don't think there is a general way to do this search though.



+1


source







All Articles