-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
273 lines (249 loc) · 6.62 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"strconv"
"sync"
)
var handelers = map[string]func([]Value) Value{
"PING": ping,
"SET": set,
"GET": get,
"DEL": del,
"HSET": hset,
"HGET": hget,
"HGETALL": hgetall,
"MGET": mget,
"MSET": mset,
"APPEND": append_,
"INCR": incr,
"INCRBY": incrby,
"DECR": decr,
"DECRBY": decrby,
}
func ping(args []Value) Value {
if len(args) == 0 {
return Value{typ: "string", str: "PONG"}
}
return Value{typ: "string", str: "PONG " + args[0].bulk}
}
// init some data structure we gonna need
// for set and get commands
var SETs = map[string]string{}
var SETSMutex = sync.RWMutex{}
// for hset and hget commands
var HSETS = map[string]map[string]string{}
var HSETSMutex = sync.RWMutex{}
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
val := args[1].bulk
SETSMutex.Lock()
defer SETSMutex.Unlock()
SETs[key] = val
return Value{typ: "string", str: "OK"}
}
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
SETSMutex.RLock()
defer SETSMutex.RUnlock()
val, ok := SETs[key]
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: val}
}
func del(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'del' command"}
}
key := args[0].bulk
SETSMutex.Lock()
defer SETSMutex.Unlock()
_, ok := SETs[key]
if !ok {
return Value{typ: "integer", num: 0}
}
delete(SETs, key)
return Value{typ: "string", str: "OK"}
}
func hset(args []Value) Value {
if len(args) != 3 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'hset' command"}
}
key := args[0].bulk
field := args[1].bulk
val := args[2].bulk
defer HSETSMutex.Unlock()
HSETSMutex.Lock()
// Check if the key exists in the hash map,
// if not, create a new map for the key
if _, ok := HSETS[key]; !ok {
HSETS[key] = make(map[string]string)
}
HSETS[key][field] = val
return Value{typ: "string", str: "OK"}
}
func hget(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'hget' command"}
}
key := args[0].bulk
field := args[1].bulk
HSETSMutex.RLock()
defer HSETSMutex.RUnlock()
// Check if the key exists in the hash map,
// if not, return nil
if _, ok := HSETS[key]; !ok {
return Value{typ: "null"}
}
val, ok := HSETS[key][field]
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: val}
}
func hgetall(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'hgetall' command"}
}
key := args[0].bulk
HSETSMutex.RLock()
val, ok := HSETS[key]
HSETSMutex.RUnlock()
if !ok {
return Value{typ: "null"}
}
var values []Value
for k, v := range val {
values = append(values, Value{typ: "bulk", bulk: k}, Value{typ: "bulk", bulk: v})
}
return Value{typ: "array", array: values}
}
func mget(args []Value) Value {
if len(args) < 1 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'mget' command"}
}
var values []Value
SETSMutex.RLock()
defer SETSMutex.RUnlock()
for _, key := range args {
val, ok := SETs[key.bulk]
if !ok {
values = append(values, Value{typ: "null"})
} else {
values = append(values, Value{typ: "bulk", bulk: val})
}
}
return Value{typ: "array", array: values}
}
func mset(args []Value) Value {
if len(args)%2 != 0 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'mset' command"}
}
SETSMutex.Lock()
defer SETSMutex.Unlock()
for i := 0; i < len(args); i += 2 {
key := args[i].bulk
val := args[i+1].bulk
SETs[key] = val
}
return Value{typ: "string", str: "OK"}
}
func append_(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'append' command "}
}
key := args[0].bulk
val := args[1].bulk
SETSMutex.Lock()
defer SETSMutex.Unlock()
_, ok := SETs[key]
if !ok {
SETs[key] = val
return Value{typ: "bluk", bulk: strconv.Itoa(len(val))}
}
SETs[key] += val
return Value{typ: "bluk", bulk: strconv.Itoa(len(SETs[key]))}
}
func incr(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'incr' command "}
}
key := args[0].bulk
SETSMutex.Lock()
defer SETSMutex.Unlock()
val, ok := SETs[key]
if !ok {
SETs[key] = "1"
return Value{typ: "bulk", bulk: "1"}
}
valInt, err := strconv.Atoi(val)
if err == nil {
SETs[key] = strconv.Itoa(valInt + 1)
return Value{typ: "bulk", bulk: strconv.Itoa(valInt + 1)}
}
return Value{typ: "error", str: "value is not an integer or out of range"}
}
func decr(args []Value) Value {
if len(args) != 1 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'decr' command "}
}
key := args[0].bulk
val, ok := SETs[key]
SETSMutex.Lock()
defer SETSMutex.Unlock()
if !ok {
SETs[key] = "-1"
return Value{typ: "bulk", bulk: strconv.Itoa(-1)}
}
valInt, err := strconv.Atoi(val)
if err == nil {
SETs[key] = strconv.Itoa(valInt - 1)
return Value{typ: "bulk", bulk: strconv.Itoa(valInt - 1)}
}
return Value{typ: "error", str: "value is not an integer or out of range"}
}
func incrby(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'incrby' command "}
}
key := args[0].bulk
increment, err1 := strconv.Atoi(args[1].bulk)
SETSMutex.Lock()
defer SETSMutex.Unlock()
val, ok := SETs[key]
if !ok {
SETs[key] = strconv.Itoa(increment)
return Value{typ: "bulk", bulk: strconv.Itoa(increment)}
}
valInt, err2 := strconv.Atoi(val)
if err1 != nil || err2 != nil {
return Value{typ: "error", str: "value of the key or the increment is not an integer or maybe out of range"}
}
SETs[key] = strconv.Itoa(valInt + increment)
return Value{typ: "bulk", bulk: strconv.Itoa(valInt + increment)}
}
func decrby(args []Value) Value {
if len(args) != 2 {
return Value{typ: "error", str: "ERR: Wrong number of arguments for 'decrby' command "}
}
key := args[0].bulk
decrement, err1 := strconv.Atoi(args[1].bulk)
SETSMutex.Lock()
defer SETSMutex.Unlock()
val, ok := SETs[key]
if !ok {
SETs[key] = strconv.Itoa(-1 * decrement)
return Value{typ: "bulk", bulk: strconv.Itoa(-1 * decrement)}
}
valInt, err2 := strconv.Atoi(val)
if err1 != nil || err2 != nil {
return Value{typ: "error", str: "value of the key or the decrement is not an integer or out of range"}
}
SETs[key] = strconv.Itoa(valInt - decrement)
return Value{typ: "bulk", bulk: strconv.Itoa(valInt - decrement)}
}