Boost logo

Boost Users :

Subject: Re: [Boost-users] How to search multi_index container using composite key with date ranges
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2013-02-11 02:30:27


Jeff <jeffro59 <at> gmail.com> writes:

> Hi,
>
> I am having syntax issues and wish to
> search a bmi container by the composite index defined
> below.  I have read about boost::lambdas and understand how they work.  I
> simply have gotten stuck in the syntax to perform the get on the index and
> how to create the tuple(s) containing the proper objects.
>
> I want to search such that searchKey1=”key1” and searchKey2=”key2”
> and ValidFrom is <= '2013-03-01' and ValidTo >= '2013-02-08'.  Can anyone
> help me please?

Hi Jeff,

There's no way to retrieve this range with a single lookup operation,
basically because the elements satisfying the four criteria are not
sorted to appear adjacently in the idx_test index. The closest you can
get is

* lookup for elements with searchKey1=”key1”, searchKey2=”key2”,
  ValidFrom <= '2013-03-01'
* filter out from the latter range those elements with ValidTo >= '2013-02-08'

which would look roughly like this (not tested):

TestCache tc;
...
date valid_to=date(from_string("2013-02-08"));
for(
  auto it= tc.lower_bound(boost:make_tuple("key1","key2")),
       it_end=tc.upper_bound(
         boost:make_tuple("key1","key2",from_string("2013-03-01"))),
  it!=it_end;++it){
  if(it->ValidTo>=valid_to){...}
}

HTH,

Joaquín M López Muñoz
Telefónica Digital


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net