我一直在尝试一些解决方案,使用带有映射的boost进程间库,现在共享内存中有一个multi_index_container。使用multi_index_container,除了迭代equal_range返回的值之外,还有其他方法吗。我希望从我的非唯一索引(站名,例如ST0012345)中检索一个子集结果,然后找到/获得我需要的实际测量类型(例如温度)。
我需要检索测量点的唯一点ID值(例如ST0012345SD10000456.VoltsA=pointID-45789),以便将测量值插入数据历史记录。我喜欢multi_index_container的想法,因为我们的消息有效载荷包含阵列中一个站点的大约100到200个测量值,所以我想我可以对包含500000多个项目的共享内存容器进行一次调用,然后使用长而唯一的字符串名从一个小得多的列表中查找每个测量点。
从我所做的阅读来看,我可能只能对multi_index_container返回的较小列表进行迭代,而不是进行get/fiund。
如果是这样的话,我是不是应该坚持我最初的共享内存映射解决方案(我正在工作),正如我所说,该解决方案包含500000多个长字符串项目,用于检索我们的数据历史记录所需的pointID。每秒处理200个点的数据速率很高(我发现在测试环境中,我的数据映射查找可以达到每秒2000个查找)。
此外,如果我真的使用了地图,那么在共享内存中有几张地图会有什么害处吗?例如,悉尼车站的地图(约300000点),纽卡斯尔车站的地图)(约200000点)。
下面是我的程序的代码,该程序从创建multi_index_container的单独进程中检索项目。
using namespace boost::interprocess;
using namespace System;
namespace bmi = boost::multi_index;
typedef managed_shared_memory::allocator<char>::type char_allocator;
typedef basic_string<char, std::char_traits<char>, char_allocator>shm_string;
struct tag
{
shm_string name;
shm_string stn;
shm_string mtype;
int id;
tag( const char *name_
, const char_allocator &a
, const char *stn_
, const char_allocator &b
, const char *mtype_
, const char_allocator &d
, int id_)
: name(name_, a),
stn(stn_, b),
mtype(mtype_, d),
id(id_)
{}
};
struct name{};
struct stn{};
struct mtype{};
struct id{};
typedef bmi::multi_index_container<
tag,
bmi::indexed_by<
bmi::ordered_unique
<bmi::tag<name>, BOOST_MULTI_INDEX_MEMBER(tag,shm_string,name)>,
bmi::ordered_non_unique<
bmi::tag<stn>,BOOST_MULTI_INDEX_MEMBER(tag,shm_string,stn)> >,
managed_shared_memory::allocator<tag>::type
> tag_set;
typedef tag_set::iterator iterator;
int main(array<System::String ^> ^args)
{
try{
managed_shared_memory segment(open_only,"MySharedMemory");
offset_ptr<tag_set> es = segment.find<tag_set>("My MultiIndex Container").first;
char_allocator alloc_inst(segment.get_allocator<char>());
shm_string key_object(alloc_inst);
key_object = "ST0012345SMD10000456";
std::pair<tag_set::index<stn>::type::iterator, tag_set::index<stn>::type::iterator> values = es->get<stn>().equal_range(key_object);
while(values.first != values.second) {
std::cout << values.first->name << " -- " <<" ("<<values.first->stn<<","<<values.first->mtype<<")n";
++values.first;
}
char_allocator alloc_inst2(segment.get_allocator<char>());
shm_string key_object2(alloc_inst2);
key_object2 = "ST0012345SMD10000456.VoltsA";
///// is there any way something like the following can be done rather than the above iterator ?????
iterator it = values->get<name>(key_object2); <------- ????????????????????????
}
catch(const std::exception &exc){
std::cerr << "Exception caught: " << exc.what();
throw;
}
Sleep(3000);
return 0;
}
考虑对站名和温度使用一个复合键(可以单独根据站名或在涉及这两个键的操作中使用该索引进行查找)。