From 630935826294f2f7aaa4760330dbdeca16003ccd Mon Sep 17 00:00:00 2001 From: shufeng2012 <277665039@qq.com> Date: Sun, 9 Aug 2026 20:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20=E5=86=99=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E5=8D=8A=E4=BA=86memdb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memdb/include/memdb.hpp | 15 +++++++----- memdb/src/main/memdb.cpp | 52 ++++++++++++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/memdb/include/memdb.hpp b/memdb/include/memdb.hpp index 8985a60..7b57367 100644 --- a/memdb/include/memdb.hpp +++ b/memdb/include/memdb.hpp @@ -35,16 +35,19 @@ private: std::list db; std::unordered_map keys; std::priority_queue time_touch; - size_t __size; + size_t __size = 0; + int MAX_TIME = 60 * 10; // 10分钟 void flush(); public: using key_t = T; using value_t = U; - static constexpr int npos = -1; + static constexpr bool fail = false; + static constexpr bool succ = true; Memdb(); + Memdb(int max_time); Memdb& operator=(const Memdb& to_be_cp_memdb); ~Memdb(); @@ -53,8 +56,8 @@ public: const std::list & get_db() const; const std::priority_queue & 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; + bool insert(key_t to_be_ins_k, value_t ins_v); + bool remove(key_t to_be_del_k); + bool update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false); + std::optional search(key_t to_be_sch_k); }; \ No newline at end of file diff --git a/memdb/src/main/memdb.cpp b/memdb/src/main/memdb.cpp index 9209ec6..d2e3e53 100644 --- a/memdb/src/main/memdb.cpp +++ b/memdb/src/main/memdb.cpp @@ -5,21 +5,27 @@ template bool Memdb ::index_timestamp::operator<(const index_timestamp& a) const { - return this -> item -> timestamp > a.item -> timestamp; + return this -> item -> timestamp < a.item -> timestamp; } template void Memdb ::flush() { + if (this -> time_touch.empty() == true) { + return; + } std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now(); std::list ::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--; + + if (this -> time_touch.empty() == true) { + return; + } it = this -> time_touch.front().item; now_time = std::chrono::system_clock::now(); } @@ -30,6 +36,11 @@ template Memdb ::Memdb() = default; +template + +Memdb ::Memdb(int max_time) : MAX_TIME(max_time) {} + + template Memdb & Memdb ::operator=(const Memdb & to_be_cp_memdb) = default; @@ -70,15 +81,18 @@ const std::priority_queue ::index_timestamp>& Memdb template -void Memdb ::insert(key_t to_be_ins_k, value_t ins_v) { +bool Memdb ::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; + new_column.timestamp = now_time + std::chrono::seconds(MAX_TIME); this -> db.push_back(new_column); Memdb::index_keys new_key; @@ -89,38 +103,56 @@ void Memdb ::insert(key_t to_be_ins_k, value_t ins_v) { Memdb::index_timestamp new_time_touch; new_time_touch.item = std::prev(this -> db.end()); this -> time_touch.push(new_time_touch); + + this -> __size++; + + return this -> succ; } template -void Memdb ::remove(key_t to_be_del_k) { +bool Memdb ::remove(key_t to_be_del_k) { this -> flush(); + if (this -> keys.find(to_be_del_k) == this -> keys.end()) { + return this -> fail; + } std::list ::iterator it = this -> keys[to_be_del_k].item; it -> be_del = true; + this -> keys.erase(it -> key); + + return this -> succ; } template -void Memdb ::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false) { +bool Memdb ::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch) { this -> flush(); + if (this -> keys.find(to_be_udt_k) == this -> keys.end()) { + return this -> fail; + } - std::list ::iterator it = this -> keys[to_be_udt_k]; //! 这边会默认插入 + std::list ::iterator it = this -> keys[to_be_udt_k].item; 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; + it -> timestamp = now_time + std::chrono::seconds(MAX_TIME); //! 排序失效 } + + return this -> succ; } template -Memdb ::value_t Memdb ::search(key_t to_be_sch_k) const { +std::optional Memdb ::search(key_t to_be_sch_k){ this -> flush(); + if (this -> keys.find(to_be_sch_k) == this -> keys.end()) { + return std::nullopt; + } - std::list ::iterator it = this -> keys[to_be_sch_k]; //! 这边会默认插入 + std::list ::iterator it = this -> keys[to_be_sch_k].item; return it -> value; }