148 lines
4.0 KiB
Plaintext
148 lines
4.0 KiB
Plaintext
#pragma once
|
|
/**
|
|
* @file SerializeMap.h
|
|
* @brief
|
|
* @author your name (you@domain.com)
|
|
* @version 0.1
|
|
* @date 2023-12-22
|
|
*
|
|
* Copyright: Baosight Co. Ltd.
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|
*
|
|
*/
|
|
#include <boost/archive/text_iarchive.hpp>
|
|
#include <boost/archive/text_oarchive.hpp>
|
|
#include <boost/serialization/vector.hpp>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include "shm_header.h"
|
|
using std::cout;
|
|
using std::endl;
|
|
namespace SerializeMapShm {
|
|
|
|
using namespace ShmHeader;
|
|
|
|
const static std::string dir_path = "/users/dsc/shm";
|
|
const static std::string shm_file = "SerializeMap"; ///<映射文件名
|
|
const double data_size = 500; ///< 数据大小 MB
|
|
static managed_mapped_file_t obj_mapped_file(
|
|
bipc::open_or_create, (dir_path + "/" + shm_file + "_boost.mmap").c_str(),
|
|
mix_cc::data_size::MB(data_size));
|
|
static void_allocator default_allocator(
|
|
obj_mapped_file.get_segment_manager()); ///<默认分配器
|
|
|
|
static char_string key_object("", default_allocator);
|
|
static char_string value_object("", default_allocator);
|
|
///< key 是string的情况
|
|
typedef std::pair<const char_string, bipc::string> pair_ss; ///< key-value
|
|
typedef bipc::node_allocator<pair_ss, mapped_segment_manager_t>
|
|
allocator_ss; ///<映射文件
|
|
typedef std::less<char_string> less_s;
|
|
typedef bipc::map<char_string, bipc::string, less_s, allocator_ss> SerializeMap;
|
|
typedef SerializeMap::iterator SerializeMapiter;
|
|
|
|
namespace {
|
|
bipc::offset_ptr<SerializeMap> SerializeMapPtr =
|
|
obj_mapped_file.find_or_construct<SerializeMap>(shm_file.c_str())(
|
|
less_s(), obj_mapped_file.get_segment_manager());
|
|
|
|
std::mutex local_mutext{}; ///<共享锁
|
|
} // namespace
|
|
|
|
/**
|
|
* @brief 反序列化的对象一定要确保带有空参构造函数,这是一个线程安全的版本
|
|
* @tparam Tp
|
|
* @param data My Param doc
|
|
* @return std::string
|
|
*/
|
|
template <typename Tp>
|
|
std::string serialize(Tp data) {
|
|
std::ostringstream oss;
|
|
boost::archive::text_oarchive oa(oss);
|
|
boost::serialization::save(oa, data, 0);
|
|
return oss.str();
|
|
}
|
|
|
|
/**
|
|
* @brief 反序列化的对象一定要确保带有空参构造函数
|
|
* @tparam Tp
|
|
* @param str My Param doc
|
|
* @return Tp
|
|
*/
|
|
template <typename Tp>
|
|
Tp deserialize(std::string str) {
|
|
Tp data;
|
|
std::stringstream ss;
|
|
ss << str;
|
|
boost::archive::text_iarchive ia(ss);
|
|
boost::serialization::load(ia, data, 0);
|
|
return data;
|
|
}
|
|
|
|
// /**
|
|
// * @brief 反序列化的对象一定要确保带有空参构造函数,这是一个线程安全的版本
|
|
// * @tparam Tp
|
|
// * @param data My Param doc
|
|
// * @return std::string
|
|
// */
|
|
// template <typename Tp>
|
|
// std::string serialize(Tp data) {
|
|
// std::stringstream ss;
|
|
// boost::archive::text_oarchive oa(ss);
|
|
// oa << data;
|
|
// return ss.str();
|
|
// }
|
|
|
|
// /**
|
|
// * @brief 反序列化的对象一定要确保带有空参构造函数
|
|
// * @tparam Tp
|
|
// * @param str My Param doc
|
|
// * @return Tp
|
|
// */
|
|
// template <typename Tp>
|
|
// Tp deserialize(std::string str) {
|
|
// std::stringstream ss;
|
|
// ss << str;
|
|
// boost::archive::text_iarchive ia(ss);
|
|
// Tp i;
|
|
// ia >> i;
|
|
// return i;
|
|
// }
|
|
|
|
bool delete_data(const std::string& key) {
|
|
key_object = key.c_str();
|
|
if (SerializeMapPtr.get()->find(key_object) == SerializeMapPtr.get()->end()) {
|
|
return false;
|
|
} else {
|
|
SerializeMapPtr.get()->erase(key_object);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
bool insert(const std::string& key, T data) {
|
|
key_object = key.c_str();
|
|
// std::lock_guard<std::mutex> guard(local_mutext);
|
|
std::string res = serialize(data);
|
|
cout << res << endl;
|
|
if (res.size() != 0) {
|
|
try {
|
|
SerializeMapPtr.get()->operator[](key_object) = res;
|
|
return true;
|
|
} catch (const std::exception& e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
T get_data(const std::string& key) {
|
|
key_object = key.c_str();
|
|
// std::lock_guard<std::mutex> guard(local_mutext);
|
|
std::string res = SerializeMapPtr.get()->operator[](key_object);
|
|
|
|
return deserialize<T>(res);
|
|
}
|
|
|
|
} // namespace SerializeMapShm |