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; }