Files
blackbird/libmemdb/inc/us/memdb.hpp
T
qing 26de527a09 feat(memdb×libmemdb): memdb 服务化落地——RPC 服务端 + 构建/测试/部署闭环
- libmemdb: 补全 flush() 惰性 TTL 驱逐;修复 explicit 误用/头文件缺失/
  时间戳碰撞误删;size()/get_db() 前 flush 消除脏读;三处双重哈希查找
  合并;index_keys 去冗余字段;update(refresh_ttl) 改原地重索引,
  免 remove+insert 的拷贝与重复 flush
- memdb: 重写 RPC 服务端(libmsg RpcServer, :8011, 9 路由, mutex 保护);
  补全 Makefile/_set.sh/_run.sh 与 --instance/--log-level CLI;
  SIGINT/SIGTERM 优雅退出;注册 services.yaml
- 测试: libmemdb 单元测试 22 例 + memdb RPC 集成测试 12 场景(含并发)
- 清理: 删除误合入的 db2/create_sqlite3.sql 旧版(scripts/ 下为权威版)
2026-08-15 09:27:12 +08:00

183 lines
4.9 KiB
C++

#pragma once
#include <list>
#include <unordered_map>
#include <chrono>
#include <set>
#include <optional>
#include <algorithm>
template <typename T, typename U>
class Memdb {
struct memdb_column {
T key;
U value;
std::chrono::system_clock::time_point timestamp;
};
struct index_keys {
std::list <memdb_column>::iterator item;
};
struct index_timestamp {
std::list <memdb_column>::iterator item;
bool operator<(const index_timestamp& a) const {
return this -> item -> timestamp < a.item -> timestamp;
}
};
private:
std::list <memdb_column> db;
std::unordered_map <T, index_keys> keys;
std::multiset <index_timestamp> time_touch;
size_t __size = 0;
int MAX_TIME = 60 * 10; // 10分钟
void flush() {
if (this -> time_touch.empty()) {
return;
}
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
auto it = this -> time_touch.begin() -> item;
while (now_time >= it -> timestamp) {
this -> __size--;
key_t to_be_del_k = it -> key;
this -> db.erase(it);
this -> keys.erase(to_be_del_k);
this -> time_touch.erase(this -> time_touch.begin());
if (this -> time_touch.empty()) {
return;
}
it = this -> time_touch.begin() -> item;
now_time = std::chrono::system_clock::now();
}
}
public:
using key_t = T;
using value_t = U;
static constexpr bool fail = false;
static constexpr bool succ = true;
explicit Memdb() = default;
explicit Memdb(int max_time) : MAX_TIME(std::max(0, max_time)) {}
Memdb& operator=(const Memdb& to_be_cp_memdb) = default;
~Memdb() = default;
size_t size() {
this -> flush();
return this -> __size;
}
const std::unordered_map <T, index_keys>& get_keys() const {
return this -> keys;
}
const std::list<memdb_column>& get_db() {
this -> flush();
return this -> db;
}
const std::multiset <typename Memdb <T, U>::index_timestamp>& get_time_touch() const {
return this -> time_touch;
}
bool insert(key_t to_be_ins_k, value_t ins_v) {
this -> flush();
if (this -> keys.find(to_be_ins_k) != this -> keys.end()) {
return this -> fail;
}
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
Memdb::memdb_column new_column;
new_column.key = to_be_ins_k;
new_column.value = ins_v;
new_column.timestamp = now_time + std::chrono::seconds(MAX_TIME);
this -> db.push_back(new_column);
Memdb::index_keys new_key;
new_key.item = std::prev(this -> db.end());
this -> keys.emplace(to_be_ins_k, new_key);
Memdb::index_timestamp new_time_touch;
new_time_touch.item = std::prev(this -> db.end());
this -> time_touch.insert(new_time_touch);
this -> __size++;
return this -> succ;
}
bool remove(key_t to_be_del_k) {
this -> flush();
auto kit = this -> keys.find(to_be_del_k);
if (kit == this -> keys.end()) {
return this -> fail;
}
typename std::list <memdb_column>::iterator it = kit -> second.item;
this -> keys.erase(kit);
auto range = this -> time_touch.equal_range({it});
for (auto sit = range.first; sit != range.second; ++sit) {
if (sit -> item == it) {
this -> time_touch.erase(sit);
break;
}
}
this -> db.erase(it);
this -> __size--;
return this -> succ;
}
bool update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false) {
this -> flush();
auto kit = this -> keys.find(to_be_udt_k);
if (kit == this -> keys.end()) {
return this -> fail;
}
typename std::list <memdb_column>::iterator it = kit -> second.item;
it -> value = udt_v;
if (need_to_udt_timetouch == true) {
auto range = this -> time_touch.equal_range({it});
for (auto sit = range.first; sit != range.second; ++sit) {
if (sit -> item == it) {
this -> time_touch.erase(sit);
break;
}
}
it -> timestamp = std::chrono::system_clock::now()
+ std::chrono::seconds(MAX_TIME);
Memdb::index_timestamp new_time_touch;
new_time_touch.item = it;
this -> time_touch.insert(new_time_touch);
}
return this -> succ;
}
std::optional<U> search(key_t to_be_sch_k) {
this -> flush();
auto kit = this -> keys.find(to_be_sch_k);
if (kit == this -> keys.end()) {
return std::nullopt;
}
return kit -> second.item -> value;
}
};