-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
131 lines (102 loc) · 3.01 KB
/
handler.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"sync"
)
// Handlers maps command strings to their respective handler functions.
var Handlers = map[string]func([]Value) Value{
"PING": ping,
"SET": set,
"GET": get,
"HSET": hset,
"HGET": hget,
"HGETALL": hgetall,
}
// ping responds to the PING command, optionally echoing back any additional argument.
func ping(args []Value) Value {
if len(args) == 0 {
return Value{typ: "string", str: "PONG"}
}
return Value{typ: "string", str: args[0].bulk}
}
// SETs stores key-value pairs for the SET and GET commands.
var SETs = map[string]string{}
var SETsMu sync.RWMutex // Protects SETs
// set handles the SET command to store a key-value pair.
func set(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'set' command"}
}
key := args[0].bulk
value := args[1].bulk
SETsMu.Lock()
SETs[key] = value
SETsMu.Unlock()
return Value{typ: "string", str: "OK"}
}
// get retrieves a value for a key using the GET command.
func get(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'get' command"}
}
key := args[0].bulk
SETsMu.RLock()
value, ok := SETs[key]
SETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
// HSETs stores nested hash map structures for the HSET, HGET, and HGETALL commands.
var HSETs = map[string]map[string]string{}
var HSETsMu sync.RWMutex // Protects HSETs
// hset handles the HSET command to store a key-value pair in a hash map.
func hset(args []Value) Value {
if len(args) != 3 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hset' command"}
}
hash := args[0].bulk
key := args[1].bulk
value := args[2].bulk
HSETsMu.Lock()
if _, ok := HSETs[hash]; !ok {
HSETs[hash] = map[string]string{}
}
HSETs[hash][key] = value
HSETsMu.Unlock()
return Value{typ: "string", str: "OK"}
}
// hget retrieves a value for a key within a hash using the HGET command.
func hget(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hget' command"}
}
hash := args[0].bulk
key := args[1].bulk
HSETsMu.RLock()
value, ok := HSETs[hash][key]
HSETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
// hgetall retrieves all key-value pairs within a hash using the HGETALL command.
func hgetall(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hgetall' command"}
}
hash := args[0].bulk
HSETsMu.RLock()
value, ok := HSETs[hash]
HSETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
var values []Value
for k, v := range value {
values = append(values, Value{typ: "bulk", bulk: k})
values = append(values, Value{typ: "bulk", bulk: v})
}
return Value{typ: "array", array: values}
}