-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
500 lines (387 loc) · 9.71 KB
/
command.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package rpc
import (
"strconv"
"strings"
"time"
)
// get fetches Item pointer from the data store.
func (dataStore *DataStore) get(key string) (*Item, bool) {
value, ok := dataStore.values[key]
if ok {
return value, true
}
return nil, false
}
func (dataStore *DataStore) set(key string, value *Item) {
dataStore.values[key] = value
}
// remove item from the data store by the given key.
// the item is also removed from cache
func (dataStore *DataStore) remove(key string) error {
item, ok := dataStore.get(key)
if !ok {
return errNoItem
}
cacheEl := item.el
// break the link from the Item for element in the cache for safe removal
item.el = nil
dataStore.cache.Remove(cacheEl)
delete(dataStore.values, key)
return nil
}
// Get command retrieves string value by given key from the data store.
// If item was successfully read, it will be updated as the most recently used in cache.
// Arguments are read from Args field of client object.
func Get(client *Client) {
dataStore := client.ds
if len(client.args) != 1 {
client.err = errArgumentNumber
return
}
key := client.args[0]
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
if !ok {
client.err = errNoItem
return
}
// convert item's value to the string
result, ok := item.Value.(string)
if !ok {
client.err = errNotString
return
}
client.reply = result
// updating cache to set the current item as the most recently used
dataStore.cache.MoveToFront(item.el)
}
// Set command will set string value by given key and value in the data store.
// If item was successfully set, it will be updated as the most recently used in cache.
// Arguments are read from Args field of client object.
func Set(client *Client) {
dataStore := client.ds
if len(client.args) < 2 || len(client.args) > 3 {
client.err = errArgumentNumber
return
}
key := client.args[0]
value := client.args[1]
// expire is the time to live in seconds
var expire int64
var err error
// if ttl is set by user
if len(client.args) == 3 {
// parse ttl and check it for correctness
expire, err = strconv.ParseInt(client.args[2], 10, 64)
if err != nil {
client.err = errTTLFormat
return
}
if expire < 0 {
client.err = errTTLValue
return
}
} else {
// use default expiration time
expire = defaultExpiration
}
if expire != 0 {
expire += time.Now().Unix()
}
item := &Item{
Value: value,
el: nil,
}
dataStore.Lock()
defer dataStore.Unlock()
dataStore.set(key, item)
dataStore.ttlCommands <- expiration{"SET", key, expire}
// update the cache
el := dataStore.cache.PushFront(key)
item.el = el
client.reply = "OK"
}
// Size command return number of all keys in the data store.
func Size(client *Client) {
if len(client.args) != 0 {
client.err = errArgumentNumber
return
}
dataStore := client.ds
dataStore.RLock()
defer dataStore.RUnlock()
// convert int number of values to the string
client.reply = strconv.Itoa(len(dataStore.values))
}
// Remove element from the data store by given key.
func Remove(client *Client) {
if len(client.args) != 1 {
client.err = errArgumentNumber
return
}
dataStore := client.ds
key := client.args[0]
dataStore.Lock()
defer dataStore.Unlock()
dataStore.ttlCommands <- expiration{"DELETE", key, 0}
err := dataStore.remove(key)
if err == nil {
client.reply = "OK"
} else {
client.err = err
}
}
// RemoveBatch of keys from the datastore.
func RemoveBatch(client *Client) {
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
for _, key := range client.args {
dataStore.ttlCommands <- expiration{"DELETE", key, 0}
dataStore.remove(key)
}
client.reply = "OK"
}
// Keys which are currently in the data store.
// Result of the command is the string, containing all the keys.
func Keys(client *Client) {
if len(client.args) != 0 {
client.err = errArgumentNumber
return
}
dataStore := client.ds
dataStore.RLock()
defer dataStore.RUnlock()
data := &dataStore.values
res := make([]string, len(*data))
// fill list of strings with current keys
i := 0
for k := range *data {
res[i] = k
i++
}
// convert list of strings to the one string
client.reply = strings.Join(res, " ")
}
// TTL updates ttl value of the item.
func TTL(client *Client) {
if len(client.args) != 2 {
client.err = errArgumentNumber
return
}
key := client.args[0]
// parse expiration time and check it for correctness
expire, err := strconv.ParseInt(client.args[1], 10, 64)
if err != nil {
client.err = errTTLFormat
return
}
if expire < 0 {
client.err = errTTLValue
return
}
// set absolute time for the expiration
if expire != 0 {
expire += time.Now().Unix()
}
dataStore := client.ds
dataStore.ttlCommands <- expiration{"SET", key, expire}
// set the expiration time
//item.Expiration = time.Now().Unix() + expire
client.reply = "OK"
}
// LSet updates item in the list object.
// This command is checking the index being in the range,
// so you cannot insert new value in the list, only update existing ones.
// Arguments are read from Args field of client object.
// List item will be updated as the most recently used in the cache.
func LSet(client *Client) {
if len(client.args) != 3 {
client.err = errArgumentNumber
return
}
key := client.args[0]
// get the index of the list and check it for correctness
index, err := strconv.Atoi(client.args[1])
if err != nil {
client.err = errIndexFormat
return
}
value := client.args[2]
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
if !ok {
client.err = errNoItem
return
}
// convert the item to the list type
list, ok := item.Value.([]string)
if !ok {
client.err = errNotList
return
}
if index >= len(list) || index < 0 {
client.err = errIndexRange
return
}
list[index] = value
// update the cache
dataStore.cache.MoveToFront(item.el)
client.reply = "OK"
}
// LPush is used to push value in the list.
// If there is no list, the command will create new one.
// Arguments are read from Args field of client object.
// List item will be updated as the most recently used in the cache.
func LPush(client *Client) {
if len(client.args) != 2 {
client.err = errArgumentNumber
return
}
key := client.args[0]
value := client.args[1]
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
// create new list, if there is none
if !ok {
newItem := &Item{
Value: []string{value},
//Expiration: time.Now().Unix() + defaultExpiration,
el: nil,
}
dataStore.set(key, newItem)
el := dataStore.cache.PushFront(key)
newItem.el = el
client.reply = "OK"
return
}
// convert existing item to the list type
list, ok := item.Value.([]string)
if !ok {
client.err = errNotList
return
}
item.Value = append(list, value)
// update the cache
dataStore.cache.MoveToFront(item.el)
client.reply = "OK"
}
// LGet returns value from the list item by given key.
// Arguments are read from Args field of client object.
// Successful run will put the value in the client's Reply field.
// Otherwise client's Reply field is empty string, and error in the client.Err field.
func LGet(client *Client) {
if len(client.args) != 2 {
client.err = errArgumentNumber
return
}
key := client.args[0]
index, err := strconv.Atoi(client.args[1])
if err != nil {
client.err = errIndexFormat
return
}
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
if !ok {
client.err = errNoItem
return
}
// convert existing item to the list type
list, ok := item.Value.([]string)
if !ok {
client.err = errNotList
return
}
if index >= len(list) || index < 0 {
client.err = errIndexRange
return
}
// update the cache
dataStore.cache.MoveToFront(item.el)
client.reply = list[index]
}
// HSet updates or creates the value in the hash item in the data store.
// If there is no hash item, it will be created.
// Arguments are read from Args field of client object.
// Hash item will be updated as the most recently used in the cache.
func HSet(client *Client) {
if len(client.args) != 3 {
client.err = errArgumentNumber
return
}
key := client.args[0]
hashKey := client.args[1]
value := client.args[2]
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
// create new hash if it doesn't exist
if !ok {
newItem := &Item{
Value: map[string]string{
hashKey: value,
},
//Expiration: time.Now().Unix() + defaultExpiration,
el: nil,
}
// set the value to new hash
dataStore.set(key, newItem)
// add new hash to the cache
el := dataStore.cache.PushFront(key)
newItem.el = el
client.reply = "OK"
return
}
// convert existing item to the map type
hash, ok := item.Value.(map[string]string)
if !ok {
client.err = errNotHash
return
}
hash[hashKey] = value
dataStore.cache.MoveToFront(item.el)
client.reply = "OK"
}
// HGet retrieves value from hash by given key.
// If there is no such item, or hash doesn't exist - error will be thrown.
// Arguments are read from Args field of client object.
// Hash item will be updated as the most recently used in the cache.
func HGet(client *Client) {
if len(client.args) != 2 {
client.err = errArgumentNumber
return
}
key := client.args[0]
hashKey := client.args[1]
dataStore := client.ds
dataStore.Lock()
defer dataStore.Unlock()
item, ok := dataStore.get(key)
if !ok {
client.err = errNoItem
return
}
// convert existing item to the hash type
hash, ok := item.Value.(map[string]string)
if !ok {
client.err = errNotHash
return
}
result, ok := hash[hashKey]
if !ok {
client.err = errNoKeyHash
return
}
// update the cache
dataStore.cache.MoveToFront(item.el)
client.reply = result
}