-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
72 lines (66 loc) · 1.48 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
package byor
import (
"strings"
)
const (
RES_OK = 0 // Status OK
RES_ERR = 1 // Status Error
RES_NX = 2 // Status Not Found
)
var safeMap = NewHashMap(128)
func requestHandler(req []byte) (int, interface{}) {
cmds, parseErr := parseReq(req)
if parseErr != nil {
return RES_ERR, parseErr.Error()
}
if len(cmds) < 1 {
return RES_ERR, "invalid number of commands"
}
switch strings.ToLower(cmds[0]) {
case "ping":
return RES_OK, "pong"
case "get":
if len(cmds) == 2 {
value := safeMap.Get(cmds[1])
if value == "" {
return RES_NX, "key doesn't exists"
} else {
return RES_OK, value
}
} else {
return RES_ERR, "invalid parameters for command: get"
}
case "set":
if len(cmds) == 3 {
safeMap.Put(cmds[1], cmds[2])
return RES_OK, "key, value saved"
} else {
return RES_ERR, "invalid parameters for command: set"
}
case "delete":
if len(cmds) == 2 {
safeMap.Delete(cmds[1])
return RES_OK, "key deleted"
} else {
return RES_ERR, "invalid parameters for command: del"
}
case "size":
return RES_OK, safeMap.Size()
case "keys":
return RES_OK, safeMap.Keys()
case "purge":
safeMap.Clear()
return RES_OK, "hashmap purged"
default:
return RES_ERR, "invalid command"
}
}
func responseHandler(res []byte) (int, []string) {
return parseRes(res)
}
func validResponseOnComposeError() []byte {
res := make([]byte, 0)
res = appendVarint(res, int32(RES_ERR))
res = append(res, []byte("Server response length invalid")...)
return res
}