This is a simple tree-based in-memory database that organizes data hierarchically, similar to a filesystem.
- Nodes (directories) represent hierarchical paths (
/a
,/a/b
). - Leaves (key-value pairs) store actual data (
/a/b/c = "MOHIT"
). - Dynamic node creation ensures missing parent directories are automatically created.
- Supports TCP communication for client-server interaction.
The database is structured as a tree, where:
- Root (
/
) is the starting point. - Each directory (
/a
,/a/b
) is a node. - Each key-value pair (
/a/b/c = "MOHIT"
) is a leaf.
(root)
├── /a
├── /a/b
├── c -> "MOHIT"
Component | Description |
---|---|
tree.c |
Implements the in-memory database using a tree structure. |
redis.c |
Handles TCP communication and processes client commands. |
Client |
Sends commands like ADD and FIND to interact with the database. |
Nodes are created dynamically when a user adds a new path.
Node *create_node(Node *parent, int8 *path);
Node *users = create_node((Node *)&root, "/users");
Creates /users
under root.
Key-value pairs are stored as leaves under a node.
Leaf *create_leaf(Node *parent, int8 *key, int8 *value, int16 count);
create_leaf(users, "john", "25", 2);
Stores john -> 25
under /users
.
Users can retrieve values using linear lookup.
int8 *lookup_linear(int8 *path, int8 *key);
lookup_linear("/users", "john");
🔹 Returns: "25"
Users interact with the database over TCP using commands.
Command | Description |
---|---|
ADD /a/b key value |
Adds a key-value pair under /a/b . |
FIND /a/b key |
Retrieves the value of key under /a/b . |
add /users/john age 25
Added: john -> 25 under /users
find /users john
Found: john -> 25
The system automatically creates missing parent directories when adding data.
❌ Fails if /a
is missing before /a/b
is created.
✅ Automatically creates /a
when adding /a/b
.
Node *create_node(Node *parent, int8 *path)
{
if (!parent) {
char parent_path[256];
strncpy(parent_path, path, 255);
char *last_slash = strrchr(parent_path, '/');
if (last_slash) {
*last_slash = '\0';
parent = find_node_linear(parent_path);
if (!parent) {
parent = create_node((Node *)&root, parent_path);
}
}
}
...
}
make
./redis
telnet localhost 6379
add /users/john age 25
find /users john
- Tree-based hierarchical database
- Stores key-value pairs inside nodes
- Handles TCP requests for
ADD
andFIND
- Automatically creates missing parent directories
- Optimizable for larger datasets
Would you like hash-based lookups or disk storage integration next? 🚀