From 8fe2afdfdec721dcef30195cf6cd074ec1bf8898 Mon Sep 17 00:00:00 2001 From: shufeng2012 <277665039@qq.com> Date: Mon, 10 Aug 2026 11:15:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20=E8=83=BD=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E4=BA=86=EF=BC=8Cmakefile=E8=BF=98=E6=B2=A1=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memdb/Makefile | 0 memdb/include/memdb.hpp | 1 + memdb/src/main/memdb.cpp | 33 +++++++++++++++++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 memdb/Makefile diff --git a/memdb/Makefile b/memdb/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/memdb/include/memdb.hpp b/memdb/include/memdb.hpp index 7b57367..e5528cc 100644 --- a/memdb/include/memdb.hpp +++ b/memdb/include/memdb.hpp @@ -38,6 +38,7 @@ private: size_t __size = 0; int MAX_TIME = 60 * 10; // 10分钟 void flush(); + void update_queue(); public: diff --git a/memdb/src/main/memdb.cpp b/memdb/src/main/memdb.cpp index d2e3e53..2c0d3b6 100644 --- a/memdb/src/main/memdb.cpp +++ b/memdb/src/main/memdb.cpp @@ -16,21 +16,36 @@ void Memdb ::flush() { return; } std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now(); - std::list ::iterator it = this -> time_touch.front().item; + typename std::list ::iterator it = this -> time_touch.top().item; while (now_time > it -> timestamp || it -> be_del == true) { + if (it -> be_del != true) this -> __size--; + key_t to_be_del_k = it -> key; this -> db.erase(it); + this -> keys.erase(to_be_del_k); this -> time_touch.pop(); - this -> __size--; if (this -> time_touch.empty() == true) { return; } - it = this -> time_touch.front().item; + it = this -> time_touch.top().item; now_time = std::chrono::system_clock::now(); } } + +template + +void Memdb ::update_queue() { + std::priority_queue new_time_touch; + while (this -> time_touch.empty() == false) { + new_time_touch.push(this -> time_touch.top()); + this -> time_touch.pop(); + } + this -> time_touch = new_time_touch; +} + + template Memdb ::Memdb() = default; @@ -98,7 +113,7 @@ bool Memdb ::insert(key_t to_be_ins_k, value_t ins_v) { 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); + this -> keys[to_be_ins_k] = new_key; Memdb::index_timestamp new_time_touch; new_time_touch.item = std::prev(this -> db.end()); @@ -118,9 +133,10 @@ bool Memdb ::remove(key_t to_be_del_k) { return this -> fail; } - std::list ::iterator it = this -> keys[to_be_del_k].item; + typename std::list ::iterator it = this -> keys[to_be_del_k].item; it -> be_del = true; this -> keys.erase(it -> key); + this -> __size--; return this -> succ; } @@ -134,11 +150,12 @@ bool Memdb ::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_tim return this -> fail; } - std::list ::iterator it = this -> keys[to_be_udt_k].item; + typename 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 + std::chrono::seconds(MAX_TIME); //! 排序失效 + it -> timestamp = now_time + std::chrono::seconds(MAX_TIME); + this -> update_queue(); } return this -> succ; @@ -153,6 +170,6 @@ std::optional Memdb ::search(key_t to_be_sch_k){ return std::nullopt; } - std::list ::iterator it = this -> keys[to_be_sch_k].item; + typename std::list ::iterator it = this -> keys[to_be_sch_k].item; return it -> value; }