-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
142 lines (112 loc) · 3.07 KB
/
handlers.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
package main
import (
"sync"
)
// handler function that maps the uppercase command
// to appropriate function
var Handlers = map[string]func([]Value) Value{
"PING": ping,
"SET": set,
"GET": get,
"HSET": hset,
"HGET": hget,
"HGETALL": hgetall,
}
// ping command, returns pong.
// used to check if the service is up or not
func ping(args []Value) Value {
return Value{typ: "string", str: "PONG"}
}
// the main storage for set, get methods
var SETs = map[string]string{}
var SETsMu = sync.RWMutex{}
func set(args []Value) Value {
// filtering args, we need key and value for set
if len(args) != 2 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'set' command"}
}
// getting key and value
key := args[0].bulk
value := args[1].bulk
// locking the variable, writing to map and then unlocking
SETsMu.Lock()
SETs[key] = value
SETsMu.Unlock()
return Value{typ: "string", str: "OK"}
}
func get(args []Value) Value {
// filtering args, we need key for set
if len(args) != 1 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'get' command"}
}
// getting the key
key := args[0].bulk
// setting read lock reading value and unlocking
SETsMu.RLock()
value, ok := SETs[key]
SETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
var HSETs = map[string]map[string]string{}
var HSETsMu = sync.RWMutex{}
func hset(args []Value) Value {
// filtering args, we need key, value and hash for hset
if len(args) != 3 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hset' command"}
}
// getting required params
hash := args[0].bulk
key := args[1].bulk
value := args[2].bulk
// locking, creating a new map, storing key, value in map and unlocking
HSETsMu.Lock()
if _, ok := HSETs[hash]; !ok {
HSETs[hash] = map[string]string{}
}
HSETs[hash][key] = value
HSETsMu.Unlock()
return Value{typ: "string", str: "OK"}
}
func hget(args []Value) Value {
// filtering args, we need key and hash for hget
if len(args) != 2 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hget' command"}
}
// getting hash and key
hash := args[0].bulk
key := args[1].bulk
// read locking, getting hash set, getting
// value from hash using key, unlocking
HSETsMu.RLock()
value, ok := HSETs[hash][key]
HSETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
func hgetall(args []Value) Value {
// filtering args, we need hash for hgetall
if len(args) != 1 {
return Value{typ: "error", str: "ERR wrong number of arguments for 'hgetall' command"}
}
// getting required hash
hash := args[0].bulk
// read locking, getting value for hash, unlocking
HSETsMu.RLock()
value, ok := HSETs[hash]
HSETsMu.RUnlock()
if !ok {
return Value{typ: "null"}
}
values := []Value{}
// iterating over results, appending for returning
for key, value := range value {
values = append(values, Value{typ: "bulk", bulk: key})
values = append(values, Value{typ: "bulk", bulk: value})
}
return Value{typ: "array", array: values}
}