-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefaultHandler.go
204 lines (191 loc) · 4.9 KB
/
defaultHandler.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package memcached
import (
//"fmt"
//"os"
"github.com/luxuan/go-memcached-server/protocol"
"strconv"
)
var dict = make(map[string][]byte)
//implement: set/get incr (delete) (flush_all)| stats version
func DefaultGet(req *protocol.McRequest, res *protocol.McResponse) error {
for _, key := range req.Keys {
value := dict[key]
// TODO missed
res.Values = append(res.Values, protocol.McValue{key, "0", value})
}
res.Response = "END"
return nil
}
func DefaultSet(req *protocol.McRequest, res *protocol.McResponse) error {
key := req.Key
value := req.Value
dict[key] = value
res.Response = "STORED"
return nil
}
func DefaultDelete(req *protocol.McRequest, res *protocol.McResponse) error {
count := 0
for _, key := range req.Keys {
if _, exists := dict[key]; exists {
delete(dict, key)
count++
}
}
if count > 0 {
res.Response = "DELETED"
} else {
res.Response = "NOT_FOUND"
}
return nil
}
func DefaultIncr(req *protocol.McRequest, res *protocol.McResponse) error {
key := req.Key
increment := req.Increment
var base int64
if value, exists := dict[key]; exists {
var err error
base, err = strconv.ParseInt(string(value), 10, 64)
if err != nil {
return err
}
}
value := strconv.FormatInt(base+increment, 10)
dict[key] = []byte(value)
res.Response = value
return nil
}
func DefaultFlushAll(req *protocol.McRequest, res *protocol.McResponse) error {
// TODO clear map
res.Response = "OK"
return nil
}
func DefaultVersion(req *protocol.McRequest, res *protocol.McResponse) error {
res.Response = "VERSION simple-memcached-0.1"
return nil
}
////implement: set/get incr (delete) (flush_all)| stats version
//type DefaultHandler struct {
// // TODO lock when goroutine
// values map[string][]byte
// /*
// TODO do stats in framework, especially for cmd stats
// stats map[string]int
// stats: make(map[string]int),
// h.stats["cmd_get"]++
// h.stats["get_hits"] += len(res.Values)
// h.stats["get_misses"] += len(req.Keys) - len(res.Values)
// h.stats["cmd_set"]++
// */
//}
//
//func NewDefaultHandler() *DefaultHandler {
// return &DefaultHandler{
// values: make(map[string][]byte),
// }
//}
//
//
//func (h *DefaultHandler) Get(req *protocol.McRequest, res *protocol.McResponse) error {
// for _, key := range req.Keys {
// value := h.values[key]
// // TODO missed
// res.Values = append(res.Values, protocol.McValue{key, "0", value})
// }
// return nil
//}
//
//func (h *DefaultHandler) Set(req *protocol.McRequest, res *protocol.McResponse) error {
// key := req.Key
// value := req.Value
// h.values[key] = value
//
// res.Response = "STORED"
// return nil
//}
//
//func (h *DefaultHandler) Delete(req *protocol.McRequest, res *protocol.McResponse) error {
// count := 0
// for _, key := range req.Keys {
// if _, exists := h.values[key]; exists {
// delete(h.values, key)
// count++
// }
// }
// if count > 0 {
// res.Response = "DELETED"
// } else {
// res.Response = "NOT_FOUND"
// }
// return nil
//}
//
//func (h *DefaultHandler) Incr(req *protocol.McRequest, res *protocol.McResponse) error {
// key := req.Key
// increment := req.Increment
// var base int64
// if value, exists := h.values[key]; exists {
// var err error
// base, err = strconv.ParseInt(string(value), 10, 64)
// if err != nil {
// return err
// }
// }
//
// value := strconv.FormatInt(base+increment, 10)
// h.values[key] = []byte(value)
//
// res.Response = value
// return nil
//}
//
//func (h *DefaultHandler) FlushAll(req *protocol.McRequest, res *protocol.McResponse) error {
// // TODO clear map
// res.Response = "OK"
// return nil
//}
//
//func (h *DefaultHandler) Version(req *protocol.McRequest, res *protocol.McResponse) error {
// res.Response = "VERSION simple-memcached-0.1"
// return nil
//}
//
//
//func (h *DefaultHandler) Stats(req *protocol.McRequest) (*protocol.McResponse, error) {
// var b bytes.Buffer
// b.WriteString("STAT pid ")
// b.WriteString(strconv.Itoa(os.Getpid()))
// b.WriteString("\r\n")
//
// b.WriteString("STAT uptime ")
// b.WriteString(strconv.Itoa(int(time.Now().Sub(startTime).Seconds())))
// b.WriteString("\r\n")
//
// b.WriteString("STAT cmd_get ")
// b.WriteString(strconv.Itoa(stats.cmd_get))
// b.WriteString("\r\n")
//
// b.WriteString("STAT cmd_set ")
// b.WriteString(strconv.Itoa(stats.cmd_set))
// b.WriteString("\r\n")
//
// b.WriteString("STAT curr_connections ")
// b.WriteString(strconv.Itoa(stats.curr_connections))
// b.WriteString("\r\n")
//
// b.WriteString("STAT total_connections ")
// b.WriteString(strconv.Itoa(stats.total_connections))
// b.WriteString("\r\n")
//
// b.WriteString("STAT get_hits ")
// b.WriteString(strconv.Itoa(stats.get_hits))
// b.WriteString("\r\n")
//
// b.WriteString("STAT get_misses ")
// b.WriteString(strconv.Itoa(stats.get_misses))
// b.WriteString("\r\n")
//
// b.WriteString("END")
//
// return protocol.McResponse{Response: b.String()}, nil
//
//}