A custom implementation for Redis using C++ and AVL Tree .
- Environment
- Description.
- Usage tips.
- Implementation Details.
- Testing Details.
- Command Line Interface.
Language: C++ 11
OS: macOS Catalina 10.15.3
Tools: VS code (1.41.1)
Implemented following Redis commands:
- GET: Get the value of
key
- SET: Set
key
to hold the stringvalue
- EXPIRE: Set a timeout on
key
. After the timeout has expired, the key will automatically be deleted - ZADD: Adds all the specified members with the specified scores to the sorted set stored at
key
- ZRANK: Returns the rank of
member
in the sorted set stored atkey
, with the scores ordered from low to high - ZRANGE: Returns the specified range of elements in the sorted set stored at
key
- ZREVRANK: Returns the rank of
member
in the sorted set stored atkey
, with the scores ordered from high to low - ZCARD: Returns the sorted set cardinality (number of elements) of the sorted set stored at
key
. - ZSCORE: Returns the score of
member
in the sorted set atkey
Step 1: Clone Repo
git clone https://github.com/nemish11/redis-cpp
Step 2: Compile and Run client.cpp file
g++ --std=c++11 client.cpp
./a.out
Step 3: Change content of client.cpp and perform step 2
Note: Threading is used to clear expired keys. In the case of segmentation fault, running the code twice will create the thread.
To manage sorted set of keys AVL tree is implemented.
AVL support insert, delete, getRank all operations in O(logN)
.
In Case of segmentation fault, comment following code in redis.h
:
Redis()
{
threadStatus = true; // set thread status
t1 = thread(&Redis::freeExpiredKey, this); // start thread for cleaning expire key
t1.detach(); // run thread in background
}
~Redis()
{
threadStatus = false; // complete execution of thread
t1.~thread();
}
test_redis.cpp
contains unit test cases for each command. To run test cases enter below commands:
g++ --std=c++11 test_redis.cpp
./a.out
testGET( ) method will test GET command.
same naming convension for other commands.
New Redis instance will be created per method.
To use CLI run following commands:
g++ --std=c++11 command_line_interface.cpp
./a.out
Type HELP command to check usage of each command.