-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
160 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM grpc/cxx | ||
LABEL maintainer="[email protected]" | ||
ENV PROJECT_DIR=/server | ||
|
||
COPY ./ $PROJECT_DIR/ | ||
|
||
RUN apt-get update && apt-get install -y cmake && apt-get install -y build-essential && apt-get clean | ||
|
||
WORKDIR $PROJECT_DIR/build | ||
|
||
RUN cmake .. && make && cd .. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
# A KV high-performance mini-database based on memory and C++17 | ||
**This project is inspired by Redis source code.** | ||
|
||
# Command line tools | ||
Developed command line tool **kvctl**. | ||
value type:string | ||
```shell | ||
yunfei@ubuntu:~/MiniKV/build$ ./kvctl --key qjx --operate set --value hello | ||
yunfei@ubuntu:~/MiniKV/build$ ./kvctl --key qjx --operate get | ||
|
||
hello | ||
``` | ||
|
||
value type:list | ||
```shell | ||
yunfei@ubuntu:~/MiniKV/build$ ./kvctl --key zyf --operate set --value hello --encoding list | ||
yunfei@ubuntu:~/MiniKV/build$ ./kvctl --key zyf --operate set --value world --encoding list | ||
|
||
yunfei@ubuntu:~/MiniKV/build$ ./kvctl --key zyf --operate get | ||
|
||
world hello | ||
``` | ||
|
||
|
||
# build | ||
**Dependencies: grpc, protobuf, gflags** | ||
In the project dir, do: | ||
```shell | ||
cd build && cmake .. && make | ||
``` | ||
then you can get `kvserver` and `kvclient`. | ||
then you can get `kvserver` and `kvclient`. | ||
|
||
# run | ||
```shell | ||
./kvserver | ||
``` | ||
|
||
# About | ||
- This project is based on the gRPC framework to implement the memory-based KV cache middleware, which realizes the client and server respectively. Support list, string type KV cache, data snapshot and progressive Rehash. | ||
|
||
- Communication between client and server is realized based on gRPC, and concurrency security is realized based on read-write lock; The client supports the addition, deletion, modification and query of keys. | ||
|
||
- Support data snapshot, background thread timing persistence, based on user-defined data frame format; Service start automaticly to read snapshot. | ||
|
||
- The underlying storage structure mimics the design of Redis hash table, solves hash conflicts with zipper method, and realizes automatic memory management with intelligent pointer. | ||
|
||
- Imitate Redis to implement the expired key based on the expired hash table, and clean up the expired key through lazy deletion. | ||
|
||
- Simulate the design of Redis double hash table, and implement progressive rehash after the background thread calculates the load factor regularly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "gflags/gflags.h" | ||
#include "../client/kvclient.h" | ||
|
||
DEFINE_string(ip, "localhost", "ip for admin node"); | ||
DEFINE_int32(port, 6789, "port for admin node"); | ||
DEFINE_string(key, "", "key"); | ||
DEFINE_string(value, "", "value"); | ||
DEFINE_string(encoding, "string", "encoding, see in encoding.h"); | ||
DEFINE_string(operate, "set", "operate, e.g. set, del.."); | ||
DEFINE_uint64(expires, 5000, "expire time"); | ||
|
||
|
||
DECLARE_string(ip); | ||
DECLARE_int32(port); | ||
DECLARE_string(key); | ||
DECLARE_string(value); | ||
DECLARE_string(encoding); | ||
DECLARE_string(operate); | ||
DECLARE_uint64(expires); | ||
|
||
#include <unordered_map> | ||
std::unordered_map<std::string, int> oper { | ||
{"set", KV_SET}, | ||
{"del", KV_DEL}, | ||
{"get", KV_GET}, | ||
{"expire", KV_EXPIRE} | ||
}; | ||
|
||
std::unordered_map<std::string, int> enMap { | ||
{"string", MiniKV_STRING}, | ||
{"list", MiniKV_LIST}, | ||
}; | ||
|
||
int main(int argc, char* argv[]) { | ||
gflags::ParseCommandLineFlags(&argc, &argv, true); | ||
KVClient* kvclient = new KVClient(FLAGS_ip, FLAGS_port); | ||
std::string encoding = FLAGS_encoding; | ||
std::string operation = FLAGS_operate; | ||
std::string key = FLAGS_key; | ||
std::string val = FLAGS_value; | ||
uint64_t expires = FLAGS_expires; | ||
|
||
switch (oper[operation]) { | ||
case KV_SET : { | ||
kvclient->setKV(key, val, enMap[encoding]); | ||
break; | ||
} | ||
case KV_DEL : { | ||
kvclient->delK(key); | ||
break; | ||
} | ||
case KV_GET : { | ||
auto res = kvclient->getK(key); | ||
for (int i = 0; i < res.data.size(); ++i) { | ||
std::cout << res.data[i] << " "; | ||
} std::cout << std::endl; | ||
break; | ||
} | ||
case KV_EXPIRE : { | ||
kvclient->setExpires(key, expires); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters