Persistent key-value store database library. The KV-Storage database is a high-performance system designed to store key-value pairs and allow efficient retrieval based on the key. This system is inspired by modern databases like LevelDB and RocksDB, and supports multiple data types using C++ templates and Protocol Buffers.
Initializes the database system, setting up the necessary files and directories (including SSTs and related data). Can be initialized with a custom Memtable size or default size of 1e4
.
// Initialize with default memtable size
auto MyDBDefault = std::make_unique<kvdb::API>();
MyDBDefault->Open("database_name");
// Initialize with a custom memtable size
int memtableSize = 1e3;
auto MyDBCustom = std::make_unique<kvdb::API>(memtableSize);
MyDBCustom->Open("database_name");
Inserts a key-value pair into the database, where both the key and value can be of various types (int, double, string, etc.).
// Example of inserting different data types
auto MyDB = std::make_unique<kvdb::API>();
MyDB->Open("database_name");
MyDB->Put(1, 100); // int -> int
MyDB->Put(1.5, 'A'); // double -> char
MyDB->Put("Hello", 1e8LL); // string -> long long
Retrieves a value from the database based on the key. Supports multiple data types.
// Example of retrieving values
auto MyDB = std::make_unique<kvdb::API>();
MyDB->Open("database_name");
MyDB->Put(1, 100);
MyDB->Put(1.5, 'A');
MyDB->Put("Hello", 1e8LL);
// Retrieve the value by key
auto kv = MyDB->Get("Hello");
// Retrieve the value by `KeyValueWrapper` instance
auto kv = MyDB->Get(KeyValueWrapper("Hello", ""));
// Expected output: { key: "Hello", value: 1e8LL }
Closes the database, flushing any data in memory (Memtable) to disk and storing it in SSTs.
// Close the database and flush the Memtable to disk
auto MyDB = std::make_unique<kvdb::API>();
MyDB->Open("database_name");
MyDB->Close();
Scans the database for key-value pairs within a specified key range. The results are returned in sorted key order.
// Scan for key-value pairs within a range
auto MyDB = std::make_unique<kvdb::API>();
MyDB->Open("database_name");
// Scan by key
std::set<KeyValueWrapper> results = MyDB->Scan(1, 10);
// Scan by `KeyValueWrapper` instance
std::set<KeyValueWrapper> results = MyDB->Scan(KeyValueWrapper(1, ""), KeyValueWrapper(10, ""));
Set/reset buffer pool size_t::
capacity and EvictionPolicy::
policy (LRU
, CLOCK
, RANDOM
)
EvictionPolicy newPolicy = EvictionPolicy::LRU;
EvictionPolicy newPolicy = EvictionPolicy::CLOCK;
EvictionPolicy newPolicy = EvictionPolicy::RANDOM;
This method will clear all the previous cache in the buffer pool.
// Open the database
auto MyDB = std::make_unique<kvdb::API>();
MyDB->Open("database_name");
// Set buffer pool parameters
size_t Capacity = 20;
EvictionPolicy Policy = EvictionPolicy::CLOCK;
MyDB->SetBufferPoolParameters(newCapacity, Policy);
// Reset
size_t newCapacity = 100;
EvictionPolicy Policy = EvictionPolicy::LRU;
MyDB->SetBufferPoolParameters(newCapacity, Policy);
// Perform database operations
MyDB->Put(1, "value1");
KeyValueWrapper value = MyDB->Get(1);
// Close the database
MyDB->Close();
This will allow the updating of key-value pairs within the database.
This will allow the deletion of key-value pairs from the database.
2024-09-12 Restructure with
Protobuf
2024-09-09 Support Template<typename K, typename V>
enum KeyValueType { INT, LONG, DOUBLE, CHAR, STRING };
2024-08-28 Support <int_64, int_64>
Using Protocol Buffer for data serialization
syntax = "proto3";
message KeyValue {
oneof key {
int32 int_key = 1;
int64 long_key = 2;
double double_key = 3;
string string_key = 4;
string char_key = 5; // Protobuf doesn't have a char type, use a single-character string
}
oneof value {
int32 int_value = 6;
int64 long_value = 7;
double double_value = 8;
string string_value = 9;
string char_value = 10;
}
enum KeyValueType {
INT = 0;
LONG = 1;
DOUBLE = 2;
CHAR = 3;
STRING = 4;
}
KeyValueType key_type = 11;
KeyValueType value_type = 12;
}
The KV-Store system has been tested across multiple platforms and compilers. Below is the current support status:
Platform | Compiler | Status |
---|---|---|
Ubuntu ARM64 | GCC | ✅ |
Ubuntu ARM64 | Clang | ✅ |
Windows x86 | MSVC (cl) | ✅ |
KeyValueWrapper
class for multi-type key-value storage.<int64, int64>
pairs.cd KvDB/protobuf
protoc --proto_path=. --cpp_out=. className.proto