Merge branch 'dev_shufeng2012' into main
合入 dev_shufeng2012 分支:libmemdb 头文件 KV 库 + memdb 重构 + db2 SQLite schema 冲突解决: - _inst.sh:保留 main 版本(已有 captcha2/smtp3/liblogc,dev 新增项重叠) 合入内容: - libmemdb/:C++20 模板头文件 Memdb<T,U>(list+unordered_map+multiset,TTL KV 存储) - memdb/:重构为 libmemdb 客户端 + RPC 服务骨架(含 src/memdb.cpp,Makefile/_run.sh/_set.sh 空文件占位) - db2/create_sqlite3.sql:Users/Notes/Logs 三表 schema(含四个索引) 测试:pybot 89 passed,actpred 148 passed/18 skipped(全绿)
This commit is contained in:
commit
fc310c9081
13 files changed
+439
-186
No files matched your search
@@ -0,0 +1,72 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Linker files
|
||||
*.ilk
|
||||
|
||||
# Debugger Files
|
||||
*.pdb
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
*.so.*
|
||||
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# Build directories
|
||||
build/
|
||||
Build/
|
||||
build-*/
|
||||
|
||||
# CMake generated files
|
||||
CMakeFiles/
|
||||
CMakeCache.txt
|
||||
cmake_install.cmake
|
||||
#Makefile
|
||||
install_manifest.txt
|
||||
compile_commands.json
|
||||
|
||||
# Temporary files
|
||||
*.tmp
|
||||
*.log
|
||||
*.bak
|
||||
*.swp
|
||||
|
||||
# vcpkg
|
||||
vcpkg_installed/
|
||||
|
||||
# debug information files
|
||||
*.dwo
|
||||
|
||||
# test output & cache
|
||||
Testing/
|
||||
.cache/
|
||||
|
||||
# Databases
|
||||
*.db*
|
||||
File renamed without changes.
Whitespace-only changes.
Whitespace-only changes.
@@ -1,60 +0,0 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <queue>
|
||||
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
class Memdb {
|
||||
|
||||
|
||||
struct memdb_column {
|
||||
T key;
|
||||
U value;
|
||||
std::chrono::system_clock::time_point timestamp;
|
||||
bool be_del = false;
|
||||
};
|
||||
|
||||
struct index_keys {
|
||||
T key;
|
||||
std::list <memdb_column>::iterator item;
|
||||
};
|
||||
|
||||
struct index_timestamp {
|
||||
std::list <memdb_column>::iterator item;
|
||||
|
||||
bool operator<(const index_timestamp& a) const;
|
||||
};
|
||||
|
||||
|
||||
private:
|
||||
std::list <memdb_column> db;
|
||||
std::unordered_map <T, index_keys> keys;
|
||||
std::priority_queue <index_timestamp> time_touch;
|
||||
size_t __size;
|
||||
void flush();
|
||||
|
||||
|
||||
public:
|
||||
using key_t = T;
|
||||
using value_t = U;
|
||||
static constexpr int npos = -1;
|
||||
|
||||
Memdb();
|
||||
Memdb& operator=(const Memdb& to_be_cp_memdb);
|
||||
~Memdb();
|
||||
|
||||
size_t size() const;
|
||||
const std::unordered_map <T, index_keys>& get_keys() const;
|
||||
const std::list <memdb_column>& get_db() const;
|
||||
const std::priority_queue <index_timestamp>& get_time_touch() const;
|
||||
|
||||
void insert(key_t to_be_ins_k, value_t ins_v); //TODO 要加返回值
|
||||
void remove(key_t to_be_del_k); //TODO 要加返回值
|
||||
void update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false);//TODO 要加返回值
|
||||
value_t search(key_t to_be_sch_k) const;
|
||||
};
|
||||
@@ -1,126 +0,0 @@
|
||||
#include "memdb.hpp"
|
||||
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
bool Memdb <T, U>::index_timestamp::operator<(const index_timestamp& a) const {
|
||||
return this -> item -> timestamp > a.item -> timestamp;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
void Memdb <T, U>::flush() {
|
||||
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
||||
std::list <memdb_column>::iterator it = this -> time_touch.front().item;
|
||||
|
||||
while (now_time > it -> timestamp || it -> be_del == true) {
|
||||
this -> keys.erase(it -> key);
|
||||
this -> db.erase(it);
|
||||
this -> time_touch.pop();
|
||||
this -> __size--;
|
||||
it = this -> time_touch.front().item;
|
||||
now_time = std::chrono::system_clock::now();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
Memdb <T, U>::Memdb() = default;
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
Memdb <T, U>& Memdb <T, U>::operator=(const Memdb <T, U>& to_be_cp_memdb) = default;
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
Memdb <T, U>::~Memdb() = default;
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
size_t Memdb <T, U>::size() const {
|
||||
return this -> __size;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
const std::unordered_map <T, typename Memdb <T, U>::index_keys>& Memdb <T, U>::get_keys() const {
|
||||
return this -> keys;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
const std::list <typename Memdb <T, U>::memdb_column>& Memdb <T, U>::get_db() const {
|
||||
return this -> db;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
const std::priority_queue <typename Memdb <T, U>::index_timestamp>& Memdb <T, U>::get_time_touch() const {
|
||||
return this -> time_touch;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
void Memdb <T, U>::insert(key_t to_be_ins_k, value_t ins_v) {
|
||||
this -> flush();
|
||||
|
||||
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;
|
||||
this -> db.push_back(new_column);
|
||||
|
||||
Memdb::index_keys new_key;
|
||||
new_key.key = to_be_ins_k;
|
||||
new_key.item = std::prev(this -> db.end());
|
||||
this -> keys.insert(new_key);
|
||||
|
||||
Memdb::index_timestamp new_time_touch;
|
||||
new_time_touch.item = std::prev(this -> db.end());
|
||||
this -> time_touch.push(new_time_touch);
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
void Memdb <T, U>::remove(key_t to_be_del_k) {
|
||||
this -> flush();
|
||||
|
||||
std::list <memdb_column>::iterator it = this -> keys[to_be_del_k].item;
|
||||
it -> be_del = true;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
void Memdb <T, U>::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false) {
|
||||
this -> flush();
|
||||
|
||||
std::list <memdb_column>::iterator it = this -> keys[to_be_udt_k]; //! 这边会默认插入
|
||||
it -> value = udt_v;
|
||||
if (need_to_udt_timetouch == true) {
|
||||
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
||||
it -> timestamp = now_time;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
Memdb <T, U>::value_t Memdb <T, U>::search(key_t to_be_sch_k) const {
|
||||
this -> flush();
|
||||
|
||||
std::list <memdb_column>::iterator it = this -> keys[to_be_sch_k]; //! 这边会默认插入
|
||||
return it -> value;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <us/RpcServer.hpp>
|
||||
#include <us/RpcClient.hpp>
|
||||
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#include <us/memdb.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
namespace json = boost::json;
|
||||
|
||||
|
||||
|
||||
struct ClientConfig {
|
||||
std::string connected_host = "127.0.0.1";
|
||||
int connected_port = 8005;
|
||||
std::string service_name = "db2";
|
||||
double timeout = 10.0;
|
||||
bool retry_on_refused = true;
|
||||
};
|
||||
|
||||
struct ServerConfig {
|
||||
std::string listen_host = "127.0.0.1";
|
||||
int listen_port = 8011;
|
||||
int workers = 4;
|
||||
std::string db_url = "sqlite:///database.db";
|
||||
};
|
||||
|
||||
static std::string get_db_path(const std::string url) {
|
||||
const std::string prefix = "sqlite:///";
|
||||
if (url.rfind(prefix, 0) == 0) {
|
||||
return url.substr(prefix.size());
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
|
||||
|
||||
class DBConnection {
|
||||
private:
|
||||
ClientConfig __cfg;
|
||||
|
||||
|
||||
public:
|
||||
explicit DBConnection(const ClientConfig& cfg) :
|
||||
__cfg(cfg) {}
|
||||
~DBConnection() = default;
|
||||
|
||||
std::string get_from_sql(const std::string& sql_name) {
|
||||
pmc::net::RpcClient client(__cfg.connected_host, __cfg.connected_port, __cfg.service_name, __cfg.timeout, __cfg.retry_on_refused);
|
||||
|
||||
try {
|
||||
auto data = client.call("execute", {{"action", "select"}, {"table", "memdb"}, {"where", {"sql_name", sql_name}}});
|
||||
|
||||
} catch (const std::exception& err) {
|
||||
std::cerr << "[memdb] Error: 连接失败: " << err.what() << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class MemdbServer {
|
||||
|
||||
|
||||
private:
|
||||
ServerConfig __cfg;
|
||||
|
||||
|
||||
public:
|
||||
explicit MemdbServer(const ServerConfig& cfg) :
|
||||
__cfg(cfg) {}
|
||||
|
||||
void start() {
|
||||
//TODO 编写
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user