-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (85 loc) · 2.94 KB
/
main.go
File metadata and controls
102 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"net"
"strings"
)
func main() {
// Start the server and listen on TCP port 6379.
fmt.Println("Listening on port :6379")
listener, err := net.Listen("tcp", ":6379")
if err != nil {
fmt.Println("Failed to listen on port 6379:", err)
return
}
defer listener.Close() // Ensure the listener is closed on function exit.
// Initialize the Append-Only File (AOF) to store database modifications.
aof, err := NewAof("database.aof")
if err != nil {
fmt.Println("Failed to initialize AOF:", err)
return
}
defer aof.Close() // Ensure the AOF file is closed on function exit.
// Load the initial data into memory by processing each stored command.
aof.Read(func(value Value) {
command := strings.ToUpper(value.array[0].bulk) // Convert command to uppercase.
args := value.array[1:] // Extract arguments from the value.
// Retrieve the handler function for the command.
handler, ok := Handlers[command]
if !ok {
fmt.Println("Invalid command received:", command)
return
}
handler(args) // Execute the handler with arguments.
})
// Handle incoming connections in a loop.
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Failed to accept connection:", err)
continue
}
defer conn.Close() // Ensure the connection is closed when the loop exits.
// Process commands received from the connection.
handleConnection(conn, aof)
}
}
// handleConnection processes commands from the connection and responds.
func handleConnection(conn net.Conn, aof *Aof) {
resp := NewResp(conn) // Initialize a new RESP (Redis Serialization Protocol) decoder.
for {
value, err := resp.Read()
if err != nil {
fmt.Println("Error reading from connection:", err)
return
}
if value.typ != "array" || len(value.array) == 0 {
fmt.Println("Invalid request, expected non-empty array")
continue
}
command := strings.ToUpper(value.array[0].bulk)
args := value.array[1:]
// Process the command.
processCommand(conn, command, args, aof)
}
}
// processCommand executes the command received via the network.
func processCommand(conn net.Conn, command string, args []Value, aof *Aof) {
writer := NewWriter(conn) // Initialize a new RESP writer.
handler, ok := Handlers[command]
if !ok {
fmt.Println("Invalid command:", command)
writer.Write(Value{typ: "string", str: ""})
return
}
// Handle special commands like "SET" or "HSET" that modify the database.
if command == "SET" || command == "HSET" {
// Manually constructing the array slice to include command and args.
values := make([]Value, len(args)+1)
values[0] = Value{typ: "bulk", bulk: command}
copy(values[1:], args)
aof.Write(Value{typ: "array", array: values})
}
result := handler(args) // Execute the handler and get the result.
writer.Write(result) // Write the result back to the client.
}