Compare commits

...
3 Commits
Author SHA1 Message Date
shufeng2012 96d9f96e6d 🎨 更改为libmemdb 2026-08-12 20:40:48 +08:00
shufeng2012 f606f7ec56 ✅ 通过黑盒测试 2026-08-12 20:11:33 +08:00
shufeng2012 02d95a2bd9 ⚡️ 将queue切换为set 2026-08-12 20:11:07 +08:00
10 changed files with 258 additions and 387 deletions

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Linker files
*.ilk
# Debugger Files
*.pdb
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
*.so.*
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# Build directories
build/
Build/
build-*/
# CMake generated files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
#Makefile
install_manifest.txt
compile_commands.json
# Temporary files
*.tmp
*.log
*.bak
*.swp
# vcpkg
vcpkg_installed/
# debug information files
*.dwo
# test output & cache
Testing/
.cache/
+25
View File
@@ -0,0 +1,25 @@
VERSION := 1.0.0
FRAMEWORK_NAME := us
LIB_NAME := libus_memdb
INSTL_TDIR := /lib
INCLU_TDIR := /usr/include
INCLU_SDIR := inc
CPP := g++
INCS := -I inc
CPPFLAGS := --std=c++20 -O3 -pipe -Wall -Werror
LIBS :=
BUILD_TDIR := build
$(shell mkdir -p $(BUILD_TDIR))
install:
@echo "install UniServe_libmemdb..."
@mkdir -vp $(INCLU_TDIR)/${FRAMEWORK_NAME}
@cp -vf $(INCLU_SDIR)/$(FRAMEWORK_NAME)/memdb.hpp $(INCLU_TDIR)/$(FRAMEWORK_NAME)/
@echo "done."
uninstall:
@echo "uninstall UniServe_libmemdb..."
@rm -vf $(INCLU_TDIR)/$(FRAMEWORK_NAME)/memdb.hpp
@echo "done."
+5
View File
@@ -0,0 +1,5 @@
# UniServe_libmemdb
UniServe Framework memdb Library
# Dependancy
libmemdb-dev
+3
View File
@@ -0,0 +1,3 @@
#!/bin/bash
cd "$(dirname "$0")" || exit
make install
+156
View File
@@ -0,0 +1,156 @@
#pragma once
#include <list>
#include <unordered_map>
#include <chrono>
#include <set>
#include <optional>
#include <cmath>
namespace shufeng2012 {
template <typename T, typename U>
class Memdb {
struct memdb_column {
T key;
U value;
std::chrono::system_clock::time_point timestamp;
};
struct index_keys {
T key;
std::list <memdb_column>::iterator item;
};
struct index_timestamp {
std::list <memdb_column>::iterator item;
bool operator<(const index_timestamp& a) const {
return this -> item -> timestamp < a.item -> timestamp;
}
};
private:
std::list <memdb_column> db;
std::unordered_map <T, index_keys> keys;
std::multiset <index_timestamp> time_touch;
size_t __size = 0;
int MAX_TIME = 60 * 10; // 10分钟
void flush();
public:
using key_t = T;
using value_t = U;
static constexpr bool fail = false;
static constexpr bool succ = true;
Memdb() = default;
Memdb(int max_time) : MAX_TIME(std::max(0, max_time)) {}
Memdb& operator=(const Memdb& to_be_cp_memdb) = default;
~Memdb() = default;
size_t size() const {
return this -> __size;
}
const std::unordered_map <T, index_keys>& get_keys() const {
return this -> keys;
}
const std::list <memdb_column>& get_db() const {
return this -> db;
}
const std::multiset <typename Memdb <T, U>::index_timestamp>& get_time_touch() const {
return this -> time_touch;
}
bool 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;
}
bool 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;
this -> keys.erase(it -> key);
this -> time_touch.erase({it});
this -> db.erase(it);
this -> __size--;
return this -> succ;
}
bool update(key_t to_be_udt_k, value_t udt_v, bool need_to_udt_timetouch=false) {
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();
T copy_k = it -> key;
U copy_v = it -> value;
this -> remove(copy_k);
this -> insert(copy_k, copy_v);
}
return this -> succ;
}
std::optional<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;
}
};
}
-3
View File
@@ -1,3 +0,0 @@
*.bin
*.obj
build/
-2
View File
@@ -1,2 +0,0 @@
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
-77
View File
@@ -1,77 +0,0 @@
#pragma once
#include <memory>
#include <list>
#include <unordered_map>
#include <chrono>
#include <queue>
#include <set>
#include <optional>
template <typename T, typename U>
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 <memdb_column>::iterator item;
};
struct index_timestamp {
std::list <memdb_column>::iterator item;
bool operator<(const index_timestamp& a) const;
};
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;
using value_t = U;
static constexpr bool fail = false;
static constexpr bool succ = true;
Memdb();
Memdb(int max_time);
Memdb& operator=(const Memdb& to_be_cp_memdb);
~Memdb();
size_t size() const;
const std::unordered_map <T, index_keys>& get_keys() const;
const std::list <memdb_column>& get_db() const;
const std::priority_queue <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);
// 内测版
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);
};
-13
View File
@@ -1,13 +0,0 @@
#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;
}
-292
View File
@@ -1,292 +0,0 @@
#include "memdb.hpp"
template <typename T, typename U>
bool Memdb <T, U>::index_timestamp::operator<(const index_timestamp& a) const {
return this -> item -> timestamp > a.item -> timestamp;
}
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.top().item;
while (now_time > it -> timestamp || it -> be_del == true) {
if (it -> be_del != true) this -> __size--;
this -> keys.erase(it -> key);
this -> time_touch.pop();
this -> db.erase(it);
if (this -> time_touch.empty() == true) {
return;
}
it = this -> time_touch.top().item;
now_time = std::chrono::system_clock::now();
}
}
template <typename T, typename U>
void Memdb <T, U>::update_queue() {
std::priority_queue <index_timestamp> 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 <typename T, typename U>
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>
Memdb <T, U>& Memdb <T, U>::operator=(const Memdb <T, U>& to_be_cp_memdb) = default;
template <typename T, typename U>
Memdb <T, U>::~Memdb() = default;
template <typename T, typename U>
size_t Memdb <T, U>::size() const {
return this -> __size;
}
template <typename T, typename U>
const std::unordered_map <T, typename Memdb <T, U>::index_keys>& Memdb <T, U>::get_keys() const {
return this -> keys;
}
template <typename T, typename U>
const std::list <typename Memdb <T, U>::memdb_column>& Memdb <T, U>::get_db() const {
return this -> db;
}
template <typename T, typename U>
const std::priority_queue <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.push(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);
this -> update_queue();
}
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;
}
/*
内测版
*/
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;
}