-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandleCommands.go
148 lines (118 loc) · 3.06 KB
/
handleCommands.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
package main
import (
"sync"
)
// Handler to handle commands based on type(array,bulk etc..)
var Handlers = map[string]func([]Value) Value{
"PING": ping,
"SET": set,
"GET": get,
"HSET": hset,
}
//COMMANDS FOR REDIS CLIENT
// PING- returns "PONG" just to check everything is working fine
// BELOW IS HOW THE COMMANDS LOOK LIKE WITH ARGS : SET NAME ABHISHEK
//
//Value{
// typ: "array",
// array: []Value{
// Value{typ: "bulk", bulk: "SET"},
// Value{typ: "bulk", bulk: "name"},
// Value{typ: "bulk", bulk: "Abhishek"},
// },
// }
// Above example looks like this in code
// command := Value{typ: "bulk", bulk: "SET"}.bulk // "SET"
// args := []Value{
// Value{typ: "bulk", bulk: "name"},
// Value{typ: "bulk", bulk: "Abhishek"},
// }
// PING - RETURNS "PONG" IF NO ARGS IS PASSED
// ELSE RETURNS THE ARGS PASSED
func ping(args []Value) Value {
if len(args) == 0 {
return Value{typ: "string", str: "PONG"}
}
return Value{typ: "string", str: args[0].bulk}
}
// SET - set name(key) abhishek(value)
/*
The SET command in Redis is a key-value pair.
You can set a key to a specific value at any time
and retrieve it later using the GET command.
USES HASHMAP OF `map[string]string`
*/
var SETs = map[string]string{}
var setMutx = sync.RWMutex{} // read-write mutex- resouces can be read by many , written by one at a time
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
setMutx.Lock()
SETs[key] = value
return Value{typ: "string", str: "OK"}
}
// GET - gets the value of (key) , otherwise returns nil
// `get name: return Abhishek`
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
setMutx.RLock()
value, ok := SETs[key]
setMutx.RUnlock()
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}
//HSET - set of key-value pairs
// this is basically hashmap of hashmap
// `map[string]map[string]string`
/* The skeleton of hset behind the scenes
{
"users": {
"u1": "Abhishek",
"u2": "Arko",
},
"languages": {
"p1": "Golang",
"p2": "Javascript",
},
}
*/
var HSETs = map[string]map[string]string{}
var hsetMutx = sync.RWMutex{}
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
hsetMutx.RLock()
if _, ok := HSETs[hash]; !ok {
HSETs[hash] = map[string]string{}
}
HSETs[hash][key] = value
hsetMutx.RUnlock()
return Value{typ: "string", str: "OK"}
}
//HGET - gets the value of (key) set (hset)
// hget users u1
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
hsetMutx.RLock()
value, ok := HSETs[hash][key]
if !ok {
return Value{typ: "null"}
}
return Value{typ: "bulk", bulk: value}
}