Search code examples
boostboost-multi-indexconst-iterator

Min & Max Value from Projected boost MultiIndex iterator


I am facing a sorting issue based on a Projected value from an iterator in the MultiIndex container. Below is the complete code. What I have removed here is the call of SellOrderRecord because the function called is same GenerateData but the expected result is different i.e; with top 10 Min Records while in BuyOrderRecord should result in Top 10 Max Records.

I am forcefully iterating to 500 assuming that I will get Max/Min value within that range which is wrong. What I need to fetch is Top 10 unique Price and sum its Qty if Price is repeated.

Ex. Price 10 Qty 5; Price 9 Qty 4; Price 8 Qty 7 .... n

Same is the case when I am trying to fetch top 10 Min Price with Qty.

The result generated with using printf (commented in Generate data) shows even if I have sorted it with std::less One record has Max at the end of Buy record while another holder has Min result at the end. What I think is either both should be min to max or max to min. The complete actual code is shared.

namespace bip = boost::interprocess;
namespace bmi = boost::multi_index;

struct MIOrder_Message /// For Data
{
    // STREAM_HEADER Global_Header;
    // char Message_Type;

    MIOrder_Message(Order_Message _Ord)
        : Timestamp(_Ord.Timestamp), Order_Id(_Ord.Order_Id), Token(_Ord.Token),
        Order_Type(_Ord.Order_Type), Price(_Ord.Price),
        Quantity(_Ord.Quantity) {
            //  std::cout << " Insert data for Token "<< _Ord.Token<<std::endl;
        }

    long Timestamp;
    double Order_Id;
    int Token;
    char Order_Type;
    int Price;
    int Quantity;
};
/// Order_Message and MIOrder_Message are almost identical
typedef bip::allocator<MIOrder_Message,
                       bip::managed_shared_memory::segment_manager>
    shared_struct_allocator;

enum {
    ORDERVIEW,
    TOKENVIEW,
    PRICEVIEW,
    TYPEVIEW,
};

typedef bmi::multi_index_container<
    MIOrder_Message,
    bmi::indexed_by<
        bmi::ordered_unique<bmi::tag<struct Order_Id>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, double, MIOrder_Message::Order_Id)>,
        bmi::ordered_non_unique<bmi::tag<struct Token>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Token)>,
        bmi::ordered_non_unique<bmi::tag<struct Price>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Price), std::less<int>>,
        bmi::ordered_non_unique<bmi::tag<struct Order_Type>, BOOST_MULTI_INDEX_MEMBER(MIOrder_Message, char, Order_Type)> >,
        shared_struct_allocator
    > Order_Set;

typedef bmi::nth_index<Order_Set, ORDERVIEW>::type Order_view;
typedef bmi::nth_index<Order_Set, TOKENVIEW>::type Token_View;
typedef bmi::nth_index<Order_Set, PRICEVIEW>::type Price_View;

typedef std::map<int, int> _PricePoint;

_PricePoint GenerateData(int _TKN, std::pair<Order_Set *, std::size_t> OrderRecord, bool IsReverse = false) {

  _PricePoint CurrentPrice;

  if (OrderRecord.second > 0) {

    Token_View::const_iterator t1 =
        OrderRecord.first->get<TOKENVIEW>().find(_TKN);

    Price_View::const_iterator it2 = OrderRecord.first->project<PRICEVIEW>(t1);

    int icount = 0;

    int Price = 0;

    while (icount < 500)
    /// I am forcefully iterating to 500 assuming that i will
    /// get Max value within that range. What i need to fetch is
    /// Top 10 unique Price and sum its Qty if Price is repeated.
    /// Ex. Price 10 Qty 5 ; Price 9 Qty 4; Price 8 Qty 7 .... n
    /// Same is the case when I am trying to fetch top 10 Min Price with Qty.
    {
      auto val = std::next(it2, icount);

      if (val->Token == _TKN) {
        // printf("  Bid Data Found for token %d , Price %d , Qty %d , Time %li
        // , OrderNumber %16f , icount %d  \n
        // ",val->Token,val->Price,val->Quantity,val->Timestamp,val->Order_Id,icount);

        CurrentPrice[val->Price] += val->Quantity;

        Price = val->Price;
      }
      icount++;
    }
    std::cout << " Bid Price " << Price << std::endl;
  }

  return CurrentPrice;
}

int main() {

  bip::managed_shared_memory segment(bip::open_or_create, "mySharedMemory", 20ull<<20);

  Order_Set::allocator_type alloc(segment.get_segment_manager());
  Order_Set * BuyOrderRecord = segment.find_or_construct<Order_Set>("MCASTPORT0BUY")(alloc);

  if (BuyOrderRecord->empty()) {
  }

  while (true) {
    int _TKN = 49732;

    _PricePoint CurrentPrice = GenerateData(_TKN, std::make_pair(BuyOrderRecord, 1), true);

    std::cout << "=========================================================" << std::endl;
    sleep(2);
  }

  return 0;
}

Solution

  • You never check whether the result of std::next(it2, icount) is valid.

    Chances are, it isn't. After all it2 is just the element found by _TKN, projected on the ordered price index. If _TKN happens to have the highest price in the Order_Set, then it2 is already the last element at

    Price_View::const_iterator it2 = OrderRecord.first->project<PRICEVIEW>(t1);
    

    This means that event just incrementing by 1 would return end() of the price index.

    It looks to me like you mistakenly thought "projecting" an iterator would project into a filtered subset, which isn't the case. It just projects onto the other index. Of the full set.

    If you indeed want to be able to iterate all records matching a token for the highest price, you will prefer to use a composite key:

    typedef bmi::multi_index_container<
        MIOrder_Message,
        bmi::indexed_by<
            bmi::ordered_unique<bmi::tag<struct Order_Id>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, double, MIOrder_Message::Order_Id)>,
            bmi::ordered_non_unique<bmi::tag<struct Token>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Token)>,
            bmi::ordered_non_unique<bmi::tag<struct Price>, BOOST_MULTI_INDEX_MEMBER( MIOrder_Message, int, MIOrder_Message::Price), std::less<int>>,
            bmi::ordered_non_unique<bmi::tag<struct Order_Type>, BOOST_MULTI_INDEX_MEMBER(MIOrder_Message, char, Order_Type)>
            , bmi::ordered_non_unique<bmi::tag<struct Composite>, 
                bmi::composite_key<MIOrder_Message, 
                    bmi::member<MIOrder_Message, int, &MIOrder_Message::Token>,
                    bmi::member<MIOrder_Message, int, &MIOrder_Message::Price> 
                >
            >
            >,
            shared_struct_allocator
        > Order_Set;
    

    Ordered composite keys accept partial keys, so you can do:

    auto range = OrderRecord.first->get<Composite>().equal_range(boost::make_tuple(_TKN));