Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boost多索引关系数据库_C++_Boost_Boost Multi Index - Fatal编程技术网

C++ 使用boost多索引关系数据库

C++ 使用boost多索引关系数据库,c++,boost,boost-multi-index,C++,Boost,Boost Multi Index,以下是我试图模拟的情况: COL1 Col2 Col3 CBT.151.5.T.FEED S1 t1 CBT.151.5.T.FEED s2 t2 CBT.151.5.T.FEED s3 t3 CBT.151.5.T.FEED s4 t4 CBT.151.5.T.FEED s5 t1 CBT.151.8.T.FEED s

以下是我试图模拟的情况:

  COL1                 Col2     Col3
CBT.151.5.T.FEED       S1       t1
CBT.151.5.T.FEED       s2       t2
CBT.151.5.T.FEED       s3       t3
CBT.151.5.T.FEED       s4       t4
CBT.151.5.T.FEED       s5       t1

CBT.151.8.T.FEED       s7       t1
CBT.151.5.Q.FEED       s8       t3
COL1-是ID,对于给定的ID,可以有多个符号。
COL2-符号,它们是唯一的
COL3-一个符号的更新时间,两个不同的符号可能同时更新,因此它们不是唯一的。

我的目标是获得最活跃的股票代码,比如说在过去60秒内更新的符号。为此,我使用了boost多索引

头文件:

#ifndef __TICKER_INFO_MANAGER_IMPL__
#define __TICKER_INFO_MANAGER_IMPL__

#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <TickerInfoManagerConstants.h>
#include <TickerInfo.h>

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

struct id_index{};
struct symbol_index{};
struct last_update_time_index{};

struct Less {
  template<class T, class U>
    bool operator()(T const& t, U const& u) const {
      return t < u;
    }
};


typedef bmi::multi_index_container<
tickerUpdateInfoT,
  bmi::indexed_by<
  bmi::ordered_unique
  <bmi::tag<id_index>,  BOOST_MULTI_INDEX_MEMBER( tickerUpdateInfo, shm_string, m_id), Less>,
  bmi::ordered_unique<
  bmi::tag<symbol_index>,BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, shm_string, m_symbol), Less>,
  bmi::ordered_non_unique
  <bmi::tag<last_update_time_index>, BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, int, m_last_update_time), Less> >,
  bip::managed_shared_memory::allocator<tickerUpdateInfo>::type
  > ticker_update_info_set;

  class tickerInfoMangerImplementation {

    public:
      tickerInfoMangerImplementation( const sharedMemoryNameT & name );

      bool put_records( const tickerUpdateInfoT & record );

      int get_active_ticker_count( const thresholdT seconds );

      void print_contents();

      bip::managed_shared_memory& get_managed_memory_segment() {
        return m_managed_memory_segment;
      }

    private:
      const sharedMemoryNameT    m_name;
      bip::managed_shared_memory m_managed_memory_segment;
      ticker_update_info_set     *p_ticker_info_set;
  };
#endif
假设t1>t2>t3>t4,那么我想找出时间大于t3的集合,并想找出这些符号的计数。我如何继续进行相同的操作,我已经能够插入,但我仍被检索部分卡住了。请帮忙

我已将您的(复杂得可笑的)模型简化为:

enum TimePoints { // Lets assume t1 > t2 > t3 > t4
    t1 = 100,
    t2 = 80,
    t3 = 70,
    t4 = 20,
};

using IdType = std::string;
using Symbol = std::string;
using TimeT  = unsigned int;

struct tickerUpdateInfo {
    IdType m_id;
    Symbol m_symbol;
    TimeT  m_last_update_time;

    friend std::ostream& operator<<(std::ostream& os, tickerUpdateInfo const& tui) {
        return os << "T[" << tui.m_id << ",\t" << tui.m_symbol << ",\t" << tui.m_last_update_time << "]";
    }
} static const data[] = {
    { "CBT.151.5.T.FEED", "S1", t1 },
    { "CBT.151.5.T.FEED", "s2", t2 },
    { "CBT.151.5.T.FEED", "s3", t3 },
    { "CBT.151.5.T.FEED", "s4", t4 },
    { "CBT.151.5.T.FEED", "s5", t1 },
    { "CBT.151.8.T.FEED", "s7", t1 },
    { "CBT.151.5.Q.FEED", "s8", t3 },
};
enum时间点{//假设t1>t2>t3>t4
t1=100,
t2=80,
t3=70,
t4=20,
};
使用IdType=std::string;
使用Symbol=std::string;
使用TimeT=unsigned int;
结构tickerUpdateInfo{
IdType m_id;
符号m_符号;
TimeT m_上次更新时间;
friend std::ostream&operator_last_update_time,lb->m_symbol));
历史[lb->m_符号]+=std::距离(lb,ub);
lb=ub;
}
回归历史;
}
我不确定这是否会成为更快的方法(您的档案分析人员会知道)。看到它了吗

重新思考设计? TBH由于插入时间不理想以及迭代记录时缺少引用的局部性,整个多索引问题可能会使您的速度减慢

我建议你看看

  • 按更新时间排序的单个平面多映射
  • 甚至一个(固定大小)的线性环形缓冲顺序。这将非常有意义,因为您很可能会以递增的时间顺序接收事件,因此您可以在末尾继续追加(并在历史记录窗口已满时结束)。这一次消除了所有重新分配的需要(假定您为ringbuffer选择了适当的最大容量),并为您在遍历统计列表时提供最佳缓存预取性能
一旦使用Boost Lockfree的
spsc_队列
产品实现了ringbuffer,第二种方法应该确实有一些优点。为什么?因为您可以将其托管在共享内存中


如果您的代码是自包含的,则复杂性将得到保证。遗憾的是,事实并非如此。我不得不把它修剪一下,以使它发挥作用。显然,这是在删除了所有行号之后:)

我将您的(极其复杂的)模型简化为:

enum TimePoints { // Lets assume t1 > t2 > t3 > t4
    t1 = 100,
    t2 = 80,
    t3 = 70,
    t4 = 20,
};

using IdType = std::string;
using Symbol = std::string;
using TimeT  = unsigned int;

struct tickerUpdateInfo {
    IdType m_id;
    Symbol m_symbol;
    TimeT  m_last_update_time;

    friend std::ostream& operator<<(std::ostream& os, tickerUpdateInfo const& tui) {
        return os << "T[" << tui.m_id << ",\t" << tui.m_symbol << ",\t" << tui.m_last_update_time << "]";
    }
} static const data[] = {
    { "CBT.151.5.T.FEED", "S1", t1 },
    { "CBT.151.5.T.FEED", "s2", t2 },
    { "CBT.151.5.T.FEED", "s3", t3 },
    { "CBT.151.5.T.FEED", "s4", t4 },
    { "CBT.151.5.T.FEED", "s5", t1 },
    { "CBT.151.8.T.FEED", "s7", t1 },
    { "CBT.151.5.Q.FEED", "s8", t3 },
};
enum时间点{//假设t1>t2>t3>t4
t1=100,
t2=80,
t3=70,
t4=20,
};
使用IdType=std::string;
使用Symbol=std::string;
使用TimeT=unsigned int;
结构tickerUpdateInfo{
IdType m_id;
符号m_符号;
TimeT m_上次更新时间;
friend std::ostream&operator_last_update_time,lb->m_symbol));
历史[lb->m_符号]+=std::距离(lb,ub);
lb=ub;
}
回归历史;
}
我不确定这是否会成为更快的方法(您的档案分析人员会知道)。看到它了吗

重新思考设计? TBH由于插入时间不理想以及迭代记录时缺少引用的局部性,整个多索引问题可能会使您的速度减慢

我建议你看看

  • 按更新时间排序的单个平面多映射
  • 甚至一个(固定大小)的线性环形缓冲顺序。这将非常有意义,因为您很可能会以递增的时间顺序接收事件,因此您可以在末尾继续追加(并在历史记录窗口已满时结束)。这一次消除了所有重新分配的需要(假定您为ringbuffer选择了适当的最大容量),并为您在遍历统计列表时提供最佳缓存预取性能
一旦使用Boost Lockfree的
spsc_队列
产品实现了ringbuffer,第二种方法应该确实有一些优点。为什么?因为您可以将其托管在共享内存中



如果您的代码是自包含的,则复杂性将得到保证。遗憾的是,事实并非如此。我不得不把它修剪一下,以使它发挥作用。显然,这是在删除了所有行号之后:)

请不要让人们不得不编辑样本中的每一行。它应该是工作代码@sehe会注意您下次的反馈,非常感谢您的指导。我将探索您推荐的所有选项。请不要让人们编辑您样本中的每一行。它应该是工作代码@sehe会注意您下次的反馈,非常感谢您的指导。我将探索您推荐的所有选项
   S1       t1
   s2       t2
   s3       t3
   s4       t4
   s5       t1
enum TimePoints { // Lets assume t1 > t2 > t3 > t4
    t1 = 100,
    t2 = 80,
    t3 = 70,
    t4 = 20,
};

using IdType = std::string;
using Symbol = std::string;
using TimeT  = unsigned int;

struct tickerUpdateInfo {
    IdType m_id;
    Symbol m_symbol;
    TimeT  m_last_update_time;

    friend std::ostream& operator<<(std::ostream& os, tickerUpdateInfo const& tui) {
        return os << "T[" << tui.m_id << ",\t" << tui.m_symbol << ",\t" << tui.m_last_update_time << "]";
    }
} static const data[] = {
    { "CBT.151.5.T.FEED", "S1", t1 },
    { "CBT.151.5.T.FEED", "s2", t2 },
    { "CBT.151.5.T.FEED", "s3", t3 },
    { "CBT.151.5.T.FEED", "s4", t4 },
    { "CBT.151.5.T.FEED", "s5", t1 },
    { "CBT.151.8.T.FEED", "s7", t1 },
    { "CBT.151.5.Q.FEED", "s8", t3 },
};
typedef bmi::multi_index_container<tickerUpdateInfo,
    bmi::indexed_by<
        bmi::ordered_non_unique<bmi::tag<struct most_active_index>,
            bmi::composite_key<tickerUpdateInfo,
                BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, TimeT,  m_last_update_time),
                BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, Symbol, m_symbol),
                BOOST_MULTI_INDEX_MEMBER(tickerUpdateInfo, IdType, m_id)
        > > >
    > ticker_update_info_set;
std::map<Symbol, size_t> activity_histo(ticker_update_info_set const& tuis, TimeT since)
{
    std::map<Symbol, size_t> histo;
    auto const& index = tuis.get<most_active_index>();

    auto lb = index.upper_bound(since); // for greater-than-inclusive use lower_bound
    for (auto& rec : boost::make_iterator_range(lb, index.end()))
        histo[rec.m_symbol]++;

    return histo;
}
std::map<Symbol, size_t> activity_histo_ex(ticker_update_info_set const& tuis, TimeT since)
{
    std::map<Symbol, size_t> histo;
    auto const& index = tuis.get<most_active_index>();

    for (auto lb = index.upper_bound(since), end = tuis.end(); lb != end;) // for greater-than-inclusive use lower_bound
    {
        auto ub = index.upper_bound(boost::make_tuple(lb->m_last_update_time, lb->m_symbol));
        histo[lb->m_symbol] += std::distance(lb, ub);

        lb = ub;
    }

    return histo;
}