Skip to content

Commit 808fa44

Browse files
authored
Merge pull request #4 from itsfuad/main
Typo in command fix
2 parents 23917e7 + 6e41353 commit 808fa44

File tree

15 files changed

+388
-159
lines changed

15 files changed

+388
-159
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"deno.enable": true
3+
}

dump.rdb

191 Bytes
Binary file not shown.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ SMEMBERS myset # returns x,z
2525
KEYS
2626
INFO
2727
FLUSHDB
28-
KEYS
29-
INFO
3028

3129
# Ping
3230
PING

examples/crud_demo.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"net"
7+
"strings"
8+
)
9+
10+
func sendCommand(conn net.Conn, cmd string) string {
11+
fmt.Fprintf(conn, "%s\n", cmd)
12+
reader := bufio.NewReader(conn)
13+
resp, _ := reader.ReadString('\n')
14+
return strings.TrimSpace(resp)
15+
}
16+
17+
func main() {
18+
conn, err := net.Dial("tcp", "127.0.0.1:7070")
19+
if err != nil {
20+
fmt.Println("Error connecting:", err)
21+
return
22+
}
23+
defer conn.Close()
24+
25+
fmt.Println("Connected to FurrDB")
26+
27+
// CREATE
28+
fmt.Println("[CREATE] Set user:1 name and email")
29+
fmt.Println(sendCommand(conn, "SET user:1:name Alice"))
30+
fmt.Println(sendCommand(conn, "SET user:1:email alice@example.com"))
31+
32+
// READ
33+
fmt.Println("\n[READ] Get user:1 name and email")
34+
fmt.Println("Name:", sendCommand(conn, "GET user:1:name"))
35+
fmt.Println("Email:", sendCommand(conn, "GET user:1:email"))
36+
37+
// UPDATE
38+
fmt.Println("\n[UPDATE] Update user:1 name")
39+
fmt.Println(sendCommand(conn, "SET user:1:name Alicia"))
40+
fmt.Println("Updated Name:", sendCommand(conn, "GET user:1:name"))
41+
42+
// EXISTS
43+
fmt.Println("\n[EXISTS] Check if user:1:email exists")
44+
fmt.Println("Exists:", sendCommand(conn, "EXISTS user:1:email"))
45+
46+
// DELETE
47+
fmt.Println("\n[DELETE] Delete user:1:email")
48+
fmt.Println(sendCommand(conn, "DEL user:1:email"))
49+
fmt.Println("Email after delete:", sendCommand(conn, "GET user:1:email"))
50+
51+
// KEYS
52+
fmt.Println("\n[KEYS] List all keys")
53+
fmt.Println(sendCommand(conn, "KEYS"))
54+
55+
// EXIT
56+
sendCommand(conn, "EXIT")
57+
fmt.Println("\nConnection closed")
58+
}

examples/crud_demo.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import socket
2+
3+
HOST = '127.0.0.1'
4+
PORT = 7070
5+
6+
def send_command(sock, cmd):
7+
sock.sendall((cmd + '\n').encode())
8+
resp = b''
9+
while not resp.endswith(b'\n'):
10+
chunk = sock.recv(4096)
11+
if not chunk:
12+
break
13+
resp += chunk
14+
return resp.decode().strip()
15+
16+
with socket.create_connection((HOST, PORT)) as s:
17+
print('Connected to FurrDB\n')
18+
19+
# CREATE
20+
print('[CREATE] Set user:1 name and email')
21+
print(send_command(s, 'SET user:1:name Alice'))
22+
print(send_command(s, 'SET user:1:email alice@example.com'))
23+
24+
# READ
25+
print('\n[READ] Get user:1 name and email')
26+
print('Name:', send_command(s, 'GET user:1:name'))
27+
print('Email:', send_command(s, 'GET user:1:email'))
28+
29+
# UPDATE
30+
print('\n[UPDATE] Update user:1 name')
31+
print(send_command(s, 'SET user:1:name Alicia'))
32+
print('Updated Name:', send_command(s, 'GET user:1:name'))
33+
34+
# EXISTS
35+
print('\n[EXISTS] Check if user:1:email exists')
36+
print('Exists:', send_command(s, 'EXISTS user:1:email'))
37+
38+
# DELETE
39+
print('\n[DELETE] Delete user:1:email')
40+
print(send_command(s, 'DEL user:1:email'))
41+
print('Email after delete:', send_command(s, 'GET user:1:email'))
42+
43+
# KEYS
44+
print('\n[KEYS] List all keys')
45+
print(send_command(s, 'KEYS'))
46+
47+
# EXIT
48+
send_command(s, 'EXIT')
49+
print('\nConnection closed')

examples/crud_demo.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// FurrDB CRUD Demo (Deno TypeScript)
2+
// Demonstrates basic CRUD operations for a user profile
3+
4+
const HOST = "127.0.0.1";
5+
const PORT = 7070;
6+
7+
// Helper to send a command and receive a response
8+
async function sendCommand(conn: Deno.Conn, cmd: string): Promise<string> {
9+
const encoder = new TextEncoder();
10+
const decoder = new TextDecoder();
11+
await conn.write(encoder.encode(cmd + "\n"));
12+
let resp = "";
13+
while (!resp.endsWith("\n")) {
14+
const buf = new Uint8Array(1024);
15+
const n = await conn.read(buf);
16+
if (n === null) break;
17+
resp += decoder.decode(buf.subarray(0, n));
18+
}
19+
return resp.trim();
20+
}
21+
22+
// Main demo logic
23+
const conn = await Deno.connect({ hostname: HOST, port: PORT });
24+
console.log("Connected to FurrDB\n");
25+
26+
// Create (SET user:1 name and email)
27+
console.log("[CREATE] Set user:1 name and email");
28+
console.log(await sendCommand(conn, 'SET user:1:name Alice'));
29+
console.log(await sendCommand(conn, 'SET user:1:email alice@example.com'));
30+
31+
// Read (GET user:1 name and email)
32+
console.log("\n[READ] Get user:1 name and email");
33+
console.log('Name:', await sendCommand(conn, 'GET user:1:name'));
34+
console.log('Email:', await sendCommand(conn, 'GET user:1:email'));
35+
36+
// Update (SET user:1 name)
37+
console.log("\n[UPDATE] Update user:1 name");
38+
console.log(await sendCommand(conn, 'SET user:1:name Alicia'));
39+
console.log('Updated Name:', await sendCommand(conn, 'GET user:1:name'));
40+
41+
// Exists
42+
console.log("\n[EXISTS] Check if user:1:email exists");
43+
console.log('Exists:', await sendCommand(conn, 'EXISTS user:1:email'));
44+
45+
// Delete (DEL user:1 email)
46+
console.log("\n[DELETE] Delete user:1:email");
47+
console.log(await sendCommand(conn, 'DEL user:1:email'));
48+
console.log('Email after delete:', await sendCommand(conn, 'GET user:1:email'));
49+
50+
// List all keys
51+
console.log("\n[KEYS] List all keys");
52+
console.log(await sendCommand(conn, 'KEYS'));
53+
54+
// Exit
55+
await sendCommand(conn, 'EXIT');
56+
conn.close();
57+
console.log("\nConnection closed");
58+
59+
export {};

internal/db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var Commands = map[string]HandlerFunc{
5757
"INFO": infoHandler,
5858
"EXPIRE": expireHandler,
5959
"TTL": ttlHandler,
60-
"SNAPSHOT": snapshotHandler,
60+
"SAVE": snapshotHandler,
6161
}
6262

6363
func (s *Store) ttlCleaner() {

internal/db/db_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"time"
88
)
99

10+
const (
11+
TEST_FILE = "test_dump.rdb"
12+
)
13+
1014
func TestStoreBasicOps(t *testing.T) {
1115
db := NewStore()
1216
db.mu.Lock()
@@ -127,7 +131,7 @@ func TestSnapshotSaveLoad(t *testing.T) {
127131
_, _ = saddHandler([]string{"snapset", "a", "b"})
128132
_, _ = expireHandler([]string{"snapkey", "10"})
129133

130-
err := SaveSnapshot("test_dump.rdb")
134+
err := SaveSnapshot(TEST_FILE)
131135
if err != nil {
132136
t.Fatalf("SaveSnapshot failed: %v", err)
133137
}
@@ -139,7 +143,7 @@ func TestSnapshotSaveLoad(t *testing.T) {
139143
t.Errorf("expected empty after clear, got %s", val)
140144
}
141145

142-
err = LoadSnapshot("test_dump.rdb")
146+
err = LoadSnapshot(TEST_FILE)
143147
if err != nil {
144148
t.Fatalf("LoadSnapshot failed: %v", err)
145149
}
@@ -151,5 +155,5 @@ func TestSnapshotSaveLoad(t *testing.T) {
151155
if !(strings.Contains(members, "a") && strings.Contains(members, "b")) {
152156
t.Errorf("expected set members a and b, got %s", members)
153157
}
154-
_ = os.Remove("test_dump.rdb")
158+
_ = os.Remove(TEST_FILE)
155159
}

internal/engine/test_aof.log

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ SET testkey testval
99
SET testkey testval
1010
SET testkey testval
1111
SET testkey testval
12+
SET testkey testval
13+
SET testkey testval
14+
SET testkey testval
15+
SET testkey testval

internal/handlers/script_handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func evalHandler(args []string) (string, error) {
3030
return "", fmt.Errorf("missing argument for EVAL")
3131
}
3232
scriptStr := strings.Join(args, " ")
33-
return script.EvalScript(scriptStr, nil)
33+
return script.EvalScript(scriptStr)
3434
}
3535

3636
func init() {

0 commit comments

Comments
 (0)