I'm trying to query results from a multi index container where the value type is a struct of three elements. The first value is given, but the second and third have to be greater or less than a query parameter.
After searching around, I found that a custom Key extractor has to be implemented, and some of the links here suggest the same, but I am unable to implement it:
- boost::multi_index user-defined key extractor and composite key
- https://www.boost.org/doc/libs/1_62_0/libs/multi_index/test/test_composite_key.cpp
Can anyone help me get it working?
Below is my struct and multiindex implementation:
#define RANKFILTERVIEW 0
struct TPQ {
int UID;
int Value;
int Rank;
TPQ():UID(0),Value(0),Rank(0)
{ }
TPQ(int _T, int _V, int _R):UID(_T),Value(_V),Rank(_R)
{ }
};
typedef bip::allocator<
TPQ,bip::managed_shared_memory::segment_manager
> shared_struct_allocator;
typedef bmi::multi_index_container<
TPQ,
bmi::indexed_by<
bmi::ordered_unique<bmi::tag<struct Composite>,
bmi::composite_key<TPQ,
bmi::member<TPQ, int,&TPQ::UID>,
bmi::member<TPQ, int,&TPQ::Value>,
bmi::member<TPQ, int,&TPQ::Rank>
> >
>,
shared_struct_allocator
> Rank_Set;
typedef nth_index<Rank_Set, RANKFILTERVIEW>::type Rank_view;
int main()
{
bip::managed_shared_memory segment(bip::open_only,"RANKSOTRE");
int UID =52478;
std::pair<Rank_Set*, std::size_t> RankOrderRecord=segment.find<Rank_Set>("RANKDATARECORD");
/// Here I want the result as stated below.
auto range = RankOrderRecord.first->get<Composite>().equal_range(boost::make_tuple(UID,_2>500,_3>5));
}
I have a set of instruction that sorting or re-arranging shall be ignored.
You almost got it. Indeed, in order indices you can query by partial key. Since the fields are regular integral types it's pretty easy to come up with a good lower and upper bound:
Here's a full demo:
Live On Coliru
Which will only print, as expected: