The key value (Key-value) stores the database, a nosql (non-relational database) model whose data is organized, indexed, and stored in the form of key-value pairs. KV storage is ideal for business data that does not involve too much data-relational business relationships, while reducing the number of reads and writes to the disk, with better read and write performance than SQL database storage.
KV database has leveldb, Redis, Rocksdb and so on a lot of widely used and very reliable open source implementation, but there is a very simple implementation of their own.
1, external interface, basic and Redis common interface consistent
Get, put, Del, save, size
2, provide Db_manager
Responsible for parsing the configuration file, resulting in multiple db db_name, Db_file, max_size, and Expire_time
Provides get_db, save, and Terminate methods
3. Db_mem_impl has two locks for different scenes
pthread_rwlock_t _rwlock;
Read and write operations for interfaces
pthread_mutex_t _dumplock;
For Save, _restore, and special stats requirements
4. Operation when Save and _restore
Save to write temporary files before renaming
Read and write only node that has not timed out
5, put when the operation
If key exists, update node and update _memdb_list
If the size is exceeded, or if the trailing data in the _memdb_list times out, the _memdb_list's trailing data is removed from the _memdb_map
6, definitions of some structures
struct cfb_key_t {
uint64_t Ukey;
uint64_t Dkey;
cfb_key_t (uint64_t user_key, uint64_t doc_key): Ukey (User_key), Dkey (Doc_key) {};
cfb_key_t (): Ukey (0), Dkey (0) {};
};
struct cfb_key_hash_t
{
std::size_t operator () (const cfb_key_t& key) const{
return std::hash<uint64_t> () (key.ukey) ^
&N BSP; (Std::hash<uint64_t> () (Key.dkey) << 1);
}
};
typedef std::unordered_map< cfb_key_t, cfb_value_t, cfb_key_hash_t,cfb_key_equal_t > cfb_dictionary;
struct memdb_value_t {
cfb_key_t key;
cfb_value_t Val;
uint32_t Expire_ts;
};
struct key_hasher_t {
std::size_t operator () (const cfb_key_t& k) Const {
Return (K.ukey << | k.ukey >> +) ^ K.dkey;
}
};
typedef XXX::d list_t<memdb_value_t> memdb_list_t;
typedef std::unordered_map<cfb_key_t, memdb_list_t::node_t*, key_hasher_t, key_equaler_t> memdb_map_t;
The back-end programmer's Road 8, a memory KV database implementation