1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| bool otskv_init_shmdb(const std::string db_path, const int key_count, const int value_size) { int ret = shmdb::init(db_path, key_count, value_size, true, false); if (ret != 0) return false; return true; }
bool otskv_set_string(const std::string k, const std::string v) { pupa_str_t key, value; key.len = static_cast<int>(k.length()); key.data = const_cast<char*>(k.c_str()); value.len = static_cast<int>(v.length()); value.data = const_cast<char*>(v.c_str());
int ret = pupa_set(&key, &value); if (ret != PUPA_OK) { printf("Failed to set %.*s.\n", key.len, key.data); return true; } return false; }
std::string otskv_get_string(const std::string& k) { pupa_str_t key, value; key.len = static_cast<int>(k.length()); key.data = const_cast<char*>(k.c_str());
int ret = pupa_get(&key, &value); if (ret != PUPA_OK) { return "null"; }
std::string v = value.data; return v; }
double otskv_get_double(const double tm, const std::string k) { if (!db_state) { bool ret = otskv_init_shmdb(db_path, 1024, 128); db_state = ret; }
pupa_str_t key, value; key.len = static_cast<int>(k.length()); key.data = const_cast<char*>(k.c_str());
int ret = pupa_get(&key, &value); if (ret != PUPA_OK) { return -999; }
std::string v = value.data; double num_double = std::stod(v); return num_double; }
bool otskv_set_double(const std::string k, const double v) { pupa_str_t key, value; key.len = static_cast<int>(k.length()); key.data = const_cast<char*>(k.c_str());
std::string s_v = std::to_string(v); value.len = static_cast<int>(s_v.length()); value.data = const_cast<char*>(s_v.c_str());
int ret = pupa_set(&key, &value); if (ret != PUPA_OK) { printf("Failed to set %.*s.\n", key.len, key.data); return true; } return false; }
|