🚧 写了一半了memdb
This commit is contained in:
1 parent
7342de421b
commit
6309358262
2 files changed
+51
-16
No files matched your search
@@ -35,16 +35,19 @@ private:
|
|||||||
std::list <memdb_column> db;
|
std::list <memdb_column> db;
|
||||||
std::unordered_map <T, index_keys> keys;
|
std::unordered_map <T, index_keys> keys;
|
||||||
std::priority_queue <index_timestamp> time_touch;
|
std::priority_queue <index_timestamp> time_touch;
|
||||||
size_t __size;
|
size_t __size = 0;
|
||||||
|
int MAX_TIME = 60 * 10; // 10分钟
|
||||||
void flush();
|
void flush();
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using key_t = T;
|
using key_t = T;
|
||||||
using value_t = U;
|
using value_t = U;
|
||||||
static constexpr int npos = -1;
|
static constexpr bool fail = false;
|
||||||
|
static constexpr bool succ = true;
|
||||||
|
|
||||||
Memdb();
|
Memdb();
|
||||||
|
Memdb(int max_time);
|
||||||
Memdb& operator=(const Memdb& to_be_cp_memdb);
|
Memdb& operator=(const Memdb& to_be_cp_memdb);
|
||||||
~Memdb();
|
~Memdb();
|
||||||
|
|
||||||
@@ -53,8 +56,8 @@ public:
|
|||||||
const std::list <memdb_column>& get_db() const;
|
const std::list <memdb_column>& get_db() const;
|
||||||
const std::priority_queue <index_timestamp>& get_time_touch() const;
|
const std::priority_queue <index_timestamp>& get_time_touch() const;
|
||||||
|
|
||||||
void insert(key_t to_be_ins_k, value_t ins_v); //TODO 要加返回值
|
bool insert(key_t to_be_ins_k, value_t ins_v);
|
||||||
void remove(key_t to_be_del_k); //TODO 要加返回值
|
bool remove(key_t to_be_del_k);
|
||||||
void update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false);//TODO 要加返回值
|
bool update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false);
|
||||||
value_t search(key_t to_be_sch_k) const;
|
std::optional<U> search(key_t to_be_sch_k);
|
||||||
};
|
};
|
||||||
+42
-10
@@ -5,21 +5,27 @@
|
|||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
bool Memdb <T, U>::index_timestamp::operator<(const index_timestamp& a) const {
|
bool Memdb <T, U>::index_timestamp::operator<(const index_timestamp& a) const {
|
||||||
return this -> item -> timestamp > a.item -> timestamp;
|
return this -> item -> timestamp < a.item -> timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
void Memdb <T, U>::flush() {
|
void Memdb <T, U>::flush() {
|
||||||
|
if (this -> time_touch.empty() == true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
||||||
std::list <memdb_column>::iterator it = this -> time_touch.front().item;
|
std::list <memdb_column>::iterator it = this -> time_touch.front().item;
|
||||||
|
|
||||||
while (now_time > it -> timestamp || it -> be_del == true) {
|
while (now_time > it -> timestamp || it -> be_del == true) {
|
||||||
this -> keys.erase(it -> key);
|
|
||||||
this -> db.erase(it);
|
this -> db.erase(it);
|
||||||
this -> time_touch.pop();
|
this -> time_touch.pop();
|
||||||
this -> __size--;
|
this -> __size--;
|
||||||
|
|
||||||
|
if (this -> time_touch.empty() == true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
it = this -> time_touch.front().item;
|
it = this -> time_touch.front().item;
|
||||||
now_time = std::chrono::system_clock::now();
|
now_time = std::chrono::system_clock::now();
|
||||||
}
|
}
|
||||||
@@ -30,6 +36,11 @@ template <typename T, typename U>
|
|||||||
Memdb <T, U>::Memdb() = default;
|
Memdb <T, U>::Memdb() = default;
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
|
||||||
|
Memdb <T, U>::Memdb(int max_time) : MAX_TIME(max_time) {}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
Memdb <T, U>& Memdb <T, U>::operator=(const Memdb <T, U>& to_be_cp_memdb) = default;
|
Memdb <T, U>& Memdb <T, U>::operator=(const Memdb <T, U>& to_be_cp_memdb) = default;
|
||||||
@@ -70,15 +81,18 @@ const std::priority_queue <typename Memdb <T, U>::index_timestamp>& Memdb <T, U>
|
|||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
void Memdb <T, U>::insert(key_t to_be_ins_k, value_t ins_v) {
|
bool Memdb <T, U>::insert(key_t to_be_ins_k, value_t ins_v) {
|
||||||
this -> flush();
|
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();
|
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
||||||
|
|
||||||
Memdb::memdb_column new_column;
|
Memdb::memdb_column new_column;
|
||||||
new_column.key = to_be_ins_k;
|
new_column.key = to_be_ins_k;
|
||||||
new_column.value = ins_v;
|
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);
|
this -> db.push_back(new_column);
|
||||||
|
|
||||||
Memdb::index_keys new_key;
|
Memdb::index_keys new_key;
|
||||||
@@ -89,38 +103,56 @@ void Memdb <T, U>::insert(key_t to_be_ins_k, value_t ins_v) {
|
|||||||
Memdb::index_timestamp new_time_touch;
|
Memdb::index_timestamp new_time_touch;
|
||||||
new_time_touch.item = std::prev(this -> db.end());
|
new_time_touch.item = std::prev(this -> db.end());
|
||||||
this -> time_touch.push(new_time_touch);
|
this -> time_touch.push(new_time_touch);
|
||||||
|
|
||||||
|
this -> __size++;
|
||||||
|
|
||||||
|
return this -> succ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
void Memdb <T, U>::remove(key_t to_be_del_k) {
|
bool Memdb <T, U>::remove(key_t to_be_del_k) {
|
||||||
this -> flush();
|
this -> flush();
|
||||||
|
if (this -> keys.find(to_be_del_k) == this -> keys.end()) {
|
||||||
|
return this -> fail;
|
||||||
|
}
|
||||||
|
|
||||||
std::list <memdb_column>::iterator it = this -> keys[to_be_del_k].item;
|
std::list <memdb_column>::iterator it = this -> keys[to_be_del_k].item;
|
||||||
it -> be_del = true;
|
it -> be_del = true;
|
||||||
|
this -> keys.erase(it -> key);
|
||||||
|
|
||||||
|
return this -> succ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename U>
|
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) {
|
bool Memdb <T, U>::update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch) {
|
||||||
this -> flush();
|
this -> flush();
|
||||||
|
if (this -> keys.find(to_be_udt_k) == this -> keys.end()) {
|
||||||
|
return this -> fail;
|
||||||
|
}
|
||||||
|
|
||||||
std::list <memdb_column>::iterator it = this -> keys[to_be_udt_k]; //! 这边会默认插入
|
std::list <memdb_column>::iterator it = this -> keys[to_be_udt_k].item;
|
||||||
it -> value = udt_v;
|
it -> value = udt_v;
|
||||||
if (need_to_udt_timetouch == true) {
|
if (need_to_udt_timetouch == true) {
|
||||||
std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now();
|
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 <typename T, typename U>
|
template <typename T, typename U>
|
||||||
|
|
||||||
Memdb <T, U>::value_t Memdb <T, U>::search(key_t to_be_sch_k) const {
|
std::optional<U> Memdb <T, U>::search(key_t to_be_sch_k){
|
||||||
this -> flush();
|
this -> flush();
|
||||||
|
if (this -> keys.find(to_be_sch_k) == this -> keys.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
std::list <memdb_column>::iterator it = this -> keys[to_be_sch_k]; //! 这边会默认插入
|
std::list <memdb_column>::iterator it = this -> keys[to_be_sch_k].item;
|
||||||
return it -> value;
|
return it -> value;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user