-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
152 lines (117 loc) · 2.77 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
package main
import (
"fmt"
"sync"
)
var Handlers = map[string]func([]Value) Value{
"PING": ping,
"GET": get,
"SET": set,
"DEL": rm,
"HSET": hset,
"HGET": hget,
"HGETALL": hgetall,
}
// GET & SET command maps
var SETs = map[string]string{}
var SETsMu = &sync.RWMutex{}
// HGET & HSET commands map
var HSETs = map[string]map[string]string{}
var HSETsMux = &sync.RWMutex{}
// PING --> PONG
func ping(args []Value) Value {
return Value{typ: "string", str: "PONG"}
}
// SET command to add a key value pair to the KV DB
func set(args []Value) Value {
if len(args) != 2 {
return Value{typ: "ERROR", str: "Expected only 2 args for GET command"}
}
key, value := args[0].bulk, args[1].bulk
SETsMu.Lock()
SETs[key] = value
SETsMu.Unlock()
return Value{typ: "string", str: "OK"}
}
// GET command to get the value assigned to the key passed as argument
func get(args []Value) Value {
if len(args) != 1 {
return Value{typ: "ERROR", str: "Expected only 1 argument 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}
}
func rm(args []Value) Value {
keys_len := len(args)
if keys_len == 0 {
return Value{typ: "ERROR", str: "Expected at least one argument for DEL command"}
}
count := 0
// Lock
SETsMu.Lock()
for k:=0; k<keys_len; k++ {
if _, ok := SETs[args[0].bulk]; ok {
delete(SETs, args[k].bulk)
count++
}
}
SETsMu.Unlock()
return Value{typ: "string", str: fmt.Sprint(count)}
}
func hset(args []Value) Value {
if len(args) != 3 {
return Value{typ: "ERROR", str: "Expected only 3 arguments for HSET command"}
}
hash := args[0].bulk
key := args[1].bulk
value := args[2].bulk
// Lock
HSETsMux.Lock()
// Check if hash exist in the map
// hash -> users / key -> user1 / value -> yahiaID
if _, ok := HSETs[hash]; !ok {
HSETs[hash] = map[string]string{}
}
HSETs[hash][key] = value
HSETsMux.Unlock()
return Value{typ: "string", str: "OK"}
}
func hget(args []Value) Value {
if len(args) != 2 {
return Value{typ: "ERROR", str: "Expected only 2 arguments for HGET command"}
}
hash := args[0].bulk
key := args[1].bulk
HSETsMux.RLock()
value, ok := HSETs[hash][key]
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
func hgetall(args []Value) Value {
if len(args) != 1 {
return Value{typ: "ERROR", str: "Expected only 1 argument for HGETALL command"}
}
hash := args[0].bulk
HSETsMux.RLock()
_, ok := HSETs[hash]
if !ok {
return Value{typ: "null"}
}
// res := Value{}
// res.typ = "array"
var arr []Value
for key := range(HSETs[hash]) {
v := Value{typ: "bulk", bulk: HSETs[hash][key]}
arr = append(arr, v)
}
HSETsMux.RUnlock()
return Value{typ:"array", arr: arr}
}