✅ 内测版通过测试
This commit is contained in:
1 parent
4d6e1e1fd4
commit
82875b3021
4 files changed
+149
-4
No files matched your search
@@ -0,0 +1,2 @@
|
||||
test: src/main.cpp src/main/memdb.cpp include/memdb.hpp
|
||||
g++ src/main.cpp -o test.bin -I include/ -std=c++20 -O3 -g
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <optional>
|
||||
|
||||
|
||||
|
||||
@@ -35,11 +37,15 @@ private:
|
||||
std::list <memdb_column> db;
|
||||
std::unordered_map <T, index_keys> keys;
|
||||
std::priority_queue <index_timestamp> time_touch;
|
||||
std::multiset <index_timestamp> time_touch_;
|
||||
size_t __size = 0;
|
||||
int MAX_TIME = 60 * 10; // 10分钟
|
||||
void flush();
|
||||
void update_queue();
|
||||
|
||||
// 内测版
|
||||
void flush_();
|
||||
|
||||
|
||||
public:
|
||||
using key_t = T;
|
||||
@@ -61,4 +67,11 @@ public:
|
||||
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<U> search(key_t to_be_sch_k);
|
||||
|
||||
// 内测版
|
||||
const std::multiset <index_timestamp>& get_time_touch_() 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<U> search_(key_t to_be_sch_k);
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <bits/stdc++.h>
|
||||
#include "main/memdb.cpp"
|
||||
using namespace std;
|
||||
Memdb<string,string> testdb(10);
|
||||
int main(){
|
||||
testdb.insert_("key1","value1");
|
||||
auto pack=testdb.search_("key1");
|
||||
cout<<(pack==nullopt?"未找到":pack.value())<<endl;
|
||||
this_thread::sleep_for(std::chrono::seconds(10));
|
||||
auto pack2=testdb.search_("key1");
|
||||
cout<<(pack2==nullopt?"未找到":pack2.value())<<endl;
|
||||
return 0;
|
||||
}
|
||||
+121
-4
@@ -5,7 +5,7 @@
|
||||
template <typename T, typename U>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,9 @@ void Memdb <T, U>::flush() {
|
||||
|
||||
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 -> keys.erase(it -> key);
|
||||
this -> time_touch.pop();
|
||||
this -> db.erase(it);
|
||||
|
||||
if (this -> time_touch.empty() == true) {
|
||||
return;
|
||||
@@ -94,6 +93,7 @@ const std::priority_queue <typename Memdb <T, U>::index_timestamp>& Memdb <T, U>
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
bool Memdb <T, U>::insert(key_t to_be_ins_k, value_t ins_v) {
|
||||
@@ -173,3 +173,120 @@ std::optional<U> Memdb <T, U>::search(key_t to_be_sch_k){
|
||||
typename std::list <memdb_column>::iterator it = this -> keys[to_be_sch_k].item;
|
||||
return it -> value;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
内测版
|
||||
*/
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
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();
|
||||
typename std::list <memdb_column>::iterator it = this -> time_touch_.begin() -> 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_.erase(this -> time_touch_.begin());
|
||||
|
||||
if (this -> time_touch_.empty() == true) {
|
||||
return;
|
||||
}
|
||||
it = this -> time_touch_.begin() -> item;
|
||||
now_time = std::chrono::system_clock::now();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
const std::multiset <typename Memdb <T, U>::index_timestamp>& Memdb <T, U>::get_time_touch_() const {
|
||||
return this -> time_touch_;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
bool Memdb <T, U>::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 + std::chrono::seconds(MAX_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[to_be_ins_k] = new_key;
|
||||
|
||||
Memdb::index_timestamp new_time_touch;
|
||||
new_time_touch.item = std::prev(this -> db.end());
|
||||
this -> time_touch_.insert(new_time_touch);
|
||||
|
||||
this -> __size++;
|
||||
|
||||
return this -> succ;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
bool Memdb <T, U>::remove_(key_t to_be_del_k) {
|
||||
this -> flush_();
|
||||
if (this -> keys.find(to_be_del_k) == this -> keys.end()) {
|
||||
return this -> fail;
|
||||
}
|
||||
|
||||
typename std::list <memdb_column>::iterator it = this -> keys[to_be_del_k].item;
|
||||
it -> be_del = true;
|
||||
this -> keys.erase(it -> key);
|
||||
this -> __size--;
|
||||
|
||||
return this -> succ;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
bool Memdb <T, U>::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;
|
||||
}
|
||||
|
||||
typename std::list <memdb_column>::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);
|
||||
}
|
||||
|
||||
return this -> succ;
|
||||
}
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
|
||||
std::optional<U> Memdb <T, U>::search_(key_t to_be_sch_k){
|
||||
this -> flush_();
|
||||
if (this -> keys.find(to_be_sch_k) == this -> keys.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
typename std::list <memdb_column>::iterator it = this -> keys[to_be_sch_k].item;
|
||||
return it -> value;
|
||||
}
|
||||
Reference in new issue
Block a user