Repository navigation
Expand file tree
/
Copy patht_string.go
More file actions
138 lines (125 loc) · 2.63 KB
/
Copy patht_string.go
File metadata and controls
138 lines (125 loc) · 2.63 KB
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
package main
import (
"fmt"
"strconv"
)
// ---- helpers ----
func expireIfNeeded(key *Gobj) {
entry := server.db.expire.Find(key)
if entry == nil {
return
}
when := entry.Val.IntVal()
if when > GetMsTime() {
return
}
server.db.expire.Delete(key)
server.db.data.Delete(key)
}
func findKeyRead(key *Gobj) *Gobj {
expireIfNeeded(key)
return server.db.data.Get(key)
}
func replyWrongType(c *GodisClient) {
c.AddReplyStr("-ERR: wrong type\r\n")
}
// ---- string commands ----
func getCommand(c *GodisClient) {
key := c.args[1]
val := findKeyRead(key)
if val == nil {
c.AddReplyStr("$-1\r\n")
} else if val.Type_ != GSTR {
replyWrongType(c)
} else {
str := val.StrVal()
c.AddReplyStr(fmt.Sprintf("$%d\r\n%v\r\n", len(str), str))
}
}
func setCommand(c *GodisClient) {
key := c.args[1]
val := c.args[2]
if val.Type_ != GSTR {
replyWrongType(c)
return
}
server.db.data.Set(key, val)
server.db.expire.Delete(key)
c.AddReplyStr("+OK\r\n")
}
func expireCommand(c *GodisClient) {
key := c.args[1]
val := c.args[2]
if val.Type_ != GSTR {
replyWrongType(c)
return
}
expire := GetMsTime() + (val.IntVal() * 1000)
expObj := CreateFromInt(expire)
server.db.expire.Set(key, expObj)
expObj.DecrRefCount()
c.AddReplyStr("+OK\r\n")
}
func delCommand(c *GodisClient) {
deleted := 0
for i := 1; i < len(c.args); i++ {
if server.db.data.Delete(c.args[i]) == nil {
deleted++
}
server.db.expire.Delete(c.args[i])
}
c.AddReplyStr(fmt.Sprintf(":%d\r\n", deleted))
}
func existsCommand(c *GodisClient) {
count := 0
for i := 1; i < len(c.args); i++ {
expireIfNeeded(c.args[i])
if server.db.data.Find(c.args[i]) != nil {
count++
}
}
c.AddReplyStr(fmt.Sprintf(":%d\r\n", count))
}
func incrCommand(c *GodisClient) {
key := c.args[1]
val := findKeyRead(key)
if val == nil {
// key doesn't exist, set to 1
server.db.data.Set(key, CreateFromInt(1))
c.AddReplyStr(":1\r\n")
return
}
if val.Type_ != GSTR {
replyWrongType(c)
return
}
i, err := strconv.ParseInt(val.StrVal(), 10, 64)
if err != nil {
c.AddReplyStr("-ERR: value is not an integer\r\n")
return
}
i++
server.db.data.Set(key, CreateFromInt(i))
c.AddReplyStr(fmt.Sprintf(":%d\r\n", i))
}
func decrCommand(c *GodisClient) {
key := c.args[1]
val := findKeyRead(key)
if val == nil {
server.db.data.Set(key, CreateFromInt(-1))
c.AddReplyStr(":-1\r\n")
return
}
if val.Type_ != GSTR {
replyWrongType(c)
return
}
i, err := strconv.ParseInt(val.StrVal(), 10, 64)
if err != nil {
c.AddReplyStr("-ERR: value is not an integer\r\n")
return
}
i--
server.db.data.Set(key, CreateFromInt(i))
c.AddReplyStr(fmt.Sprintf(":%d\r\n", i))
}