diff --git a/memdb/include/memdb.hpp b/memdb/include/memdb.hpp new file mode 100644 index 0000000..8985a60 --- /dev/null +++ b/memdb/include/memdb.hpp @@ -0,0 +1,60 @@ +#pragma once +#include +#include +#include +#include +#include + + + +template + +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 ::iterator item; +}; + +struct index_timestamp { + std::list ::iterator item; + + bool operator<(const index_timestamp& a) const; +}; + + +private: + std::list db; + std::unordered_map keys; + std::priority_queue 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 & get_keys() const; + 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; +}; \ No newline at end of file diff --git a/memdb/src/main.cpp b/memdb/src/main.cpp new file mode 100644 index 0000000..e69de29 diff --git a/memdb/src/main/memdb.cpp b/memdb/src/main/memdb.cpp new file mode 100644 index 0000000..9209ec6 --- /dev/null +++ b/memdb/src/main/memdb.cpp @@ -0,0 +1,126 @@ +#include "memdb.hpp" + + + +template + +bool Memdb ::index_timestamp::operator<(const index_timestamp& a) const { + return this -> item -> timestamp > a.item -> timestamp; +} + + +template + +void Memdb ::flush() { + 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--; + it = this -> time_touch.front().item; + now_time = std::chrono::system_clock::now(); + } +} + +template + +Memdb ::Memdb() = default; + + +template + +Memdb & Memdb ::operator=(const Memdb & to_be_cp_memdb) = default; + + +template + +Memdb ::~Memdb() = default; + + +template + +size_t Memdb ::size() const { + return this -> __size; +} + + +template + +const std::unordered_map ::index_keys>& Memdb ::get_keys() const { + return this -> keys; +} + + +template + +const std::list ::memdb_column>& Memdb ::get_db() const { + return this -> db; +} + + +template + +const std::priority_queue ::index_timestamp>& Memdb ::get_time_touch() const { + return this -> time_touch; +} + + +template + +void Memdb ::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 + +void Memdb ::remove(key_t to_be_del_k) { + this -> flush(); + + std::list ::iterator it = this -> keys[to_be_del_k].item; + it -> be_del = true; +} + + +template + +void Memdb ::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false) { + this -> flush(); + + std::list ::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 + +Memdb ::value_t Memdb ::search(key_t to_be_sch_k) const { + this -> flush(); + + std::list ::iterator it = this -> keys[to_be_sch_k]; //! 这边会默认插入 + return it -> value; +}