-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
2959 lines (2833 loc) · 68.1 KB
/
handler.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nodis
import (
"fmt"
"log"
"os"
"runtime"
"strconv"
"time"
"github.com/diiyw/nodis/ds"
"github.com/diiyw/nodis/ds/zset"
"github.com/diiyw/nodis/internal/geohash"
"github.com/diiyw/nodis/internal/strings"
"github.com/diiyw/nodis/redis"
)
func execCommand(conn *redis.Conn, fn func()) {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered error: ", r)
conn.WriteError("WRONGTYPE " + r.(error).Error())
return
}
}()
if conn.State == redis.MultiNone || conn.State == redis.MultiCommit {
fn()
return
}
if conn.State&redis.MultiPrepare == redis.MultiPrepare {
conn.Commands = append(conn.Commands, fn)
}
conn.WriteString("QUEUED")
}
func getCommand(name string) func(n *Nodis, conn *redis.Conn, cmd redis.Command) {
switch name {
case "CLIENT":
return client
case "CONFIG":
return config
case "DBSIZE":
return dbSize
case "PING":
return ping
case "ECHO":
return echo
case "QUIT":
return quit
case "FLUSHDB":
return flushDB
case "WATCH":
return watchKey
case "UNWATCH":
return unwatchKey
case "MULTI":
return multi
case "DISCARD":
return discard
case "EXEC":
return exec
case "FLUSHALL":
return flushDB
case "SAVE":
return save
case "INFO":
return info
case "DEL":
return del
case "UNLINK":
return unlink
case "EXISTS":
return exists
case "EXPIRE":
return expire
case "EXPIREAT":
return expireAt
case "KEYS":
return keys
case "RANDOMKEY":
return randomKey
case "TTL":
return ttl
case "PTTL":
return pTtl
case "PERSIST":
return Persist
case "RENAME":
return rename
case "RENAMENX":
return renameNx
case "TYPE":
return typ
case "SCAN":
return scan
case "SET":
return setString
case "MSET":
return mSet
case "APPEND":
return appendString
case "SETEX":
return setex
case "SETNX":
return setnx
case "GET":
return getString
case "GETSET":
return getSet
case "MGET":
return mGet
case "SETRANGE":
return setRange
case "GETRANGE":
return getRange
case "STRLEN":
return strLen
case "INCR":
return incr
case "INCRBY":
return incrBy
case "DECR":
return decr
case "DECRBY":
return decrBy
case "INCRBYFLOAT":
return incrByFloat
case "SETBIT":
return setBit
case "GETBIT":
return getBit
case "BITCOUNT":
return bitCount
case "SADD":
return sAdd
case "SMOVE":
return sMove
case "SSCAN":
return sScan
case "SCARD":
return scard
case "SPOP":
return sPop
case "SDIFF":
return sDiff
case "SDIFFSTORE":
return sDiffStore
case "SINTER":
return sInter
case "SINTERSTORE":
return sInterStore
case "SUNION":
return sUnion
case "SUNIONSTORE":
return sUnionStore
case "SISMEMBER":
return sIsMember
case "SMEMBERS":
return sMembers
case "SRANDMEMBER":
return sRandMember
case "SREM":
return sRem
case "HSET":
return hSet
case "HGET":
return hGet
case "HDEL":
return hDel
case "HLEN":
return hLen
case "HKEYS":
return hKeys
case "HEXISTS":
return hExists
case "HGETALL":
return hGetAll
case "HINCRBY":
return hIncrBy
case "HINCRBYFLOAT":
return hIncrByFloat
case "HSETNX":
return hSetNX
case "HMGET":
return hMGet
case "HMSET":
return hMSet
case "HCLEAR":
return hClear
case "HSTRLEN":
return hStrLen
case "HSCAN":
return hScan
case "HVALS":
return hVals
case "LPUSH":
return lPush
case "RPUSH":
return rPush
case "LPOP":
return lPop
case "RPOP":
return rPop
case "LLEN":
return llen
case "LINDEX":
return lIndex
case "LINSERT":
return lInsert
case "LPUSHX":
return lPushx
case "RPUSHX":
return rPushx
case "LREM":
return lRem
case "LTRIM":
return lTrim
case "LSET":
return lSet
case "LRANGE":
return lRange
case "LPOPRPUSH":
return lPopRPush
case "RPOPLPUSH":
return rPopLPush
case "BLPOP":
return bLPop
case "BRPOP":
return bRPop
case "ZADD":
return zAdd
case "ZCARD":
return zCard
case "ZRANK":
return zRank
case "ZREVRANK":
return zRevRank
case "ZSCORE":
return zScore
case "ZINCRBY":
return zIncrBy
case "ZRANGE":
return zRange
case "ZREVRANGE":
return zRevRange
case "ZRANGEBYSCORE":
return zRangeByScore
case "ZREVRANGEBYSCORE":
return zRevRangeByScore
case "ZREM":
return zRem
case "ZCOUNT":
return zCount
case "ZREMRANGEBYRANK":
return zRemRangeByRank
case "ZREMRANGEBYSCORE":
return zRemRangeByScore
case "ZCLEAR":
return zClear
case "ZUNIONSTORE":
return zUnionStore
case "ZINTERSTORE":
return zInterStore
case "ZEXISTS":
return zExists
case "ZSCAN":
return zScan
case "GEOADD":
return geoAdd
case "GEODIST":
return geoDist
case "GEOHASH":
return geoHash
case "GEOPOS":
return geoPos
case "GEORADIUS":
return geoRadius
case "GEORADIUSBYMEMBER":
return geoRadiusByMember
}
return cmdNotFound
}
func cmdNotFound(n *Nodis, conn *redis.Conn, cmd redis.Command) {
conn.WriteError("ERR unknown command '" + cmd.Name + "'")
}
func client(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("CLIENT subcommand must be provided")
return
}
execCommand(conn, func() {
switch strings.ToUpper(cmd.Args[0]) {
case "LIST":
conn.WriteString("id=1 addr=" + conn.Network.RemoteAddr().String() + " fd=5 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=client")
case "SETNAME":
conn.WriteString("OK")
default:
conn.WriteError("CLIENT subcommand must be provided")
}
})
}
func config(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("CONFIG GET requires at least two argument")
return
}
execCommand(conn, func() {
if cmd.Args[0] == "GET" {
if len(cmd.Args) < 2 {
conn.WriteError("CONFIG GET requires at least one argument")
return
}
if strings.ToUpper(cmd.Args[1]) == "DATABASES" {
conn.WriteArray(2)
conn.WriteBulk("databases")
conn.WriteBulk("0")
return
}
}
conn.WriteBulkNull()
})
}
func dbSize(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
conn.WriteInt64(int64(n.store.metadata.Len()))
})
}
func info(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
memStats := runtime.MemStats{}
runtime.ReadMemStats(&memStats)
usedMemory := strconv.FormatUint(memStats.HeapInuse+memStats.StackInuse, 10)
pid := strconv.Itoa(os.Getpid())
keys, expires, avgTTL := n.Keyspace()
var keyspace = ""
if keys > 0 {
keyspace = "db0:keys=" + strconv.FormatInt(keys, 10) + ",expires=" + strconv.FormatInt(expires, 10) + ",avg_ttl=" + strconv.FormatInt(avgTTL, 10) + "\r\n"
}
conn.WriteBulk(`# Server` + "\r\n" +
`redis_version:6.0.0` + "\r\n" +
`os:` + runtime.GOOS + "\r\n" +
`process_id:` + pid + "\r\n" +
`# Memory` + "\r\n" +
`used_memory:` + usedMemory + "\r\n" +
`used_memory_human:` + strconv.FormatUint(memStats.HeapInuse+memStats.StackInuse/1024, 10) + "KB" + "\r\n" +
`maxmemory:0` + "\r\n" +
`maxmemory_human:0B` + "\r\n" +
`maxmemory_policy:noeviction` + "\r\n" +
`# Client` + "\r\n" +
`maxclients:10000` + "\r\n" +
`connected_clients:` + strconv.FormatInt(redis.ClientNum.Load(), 10) + "\r\n" +
`# Keyspace` + "\r\n" + keyspace +
"\r\n")
})
}
func ping(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
if len(cmd.Args) == 0 {
conn.WriteBulk("PONG")
return
}
conn.WriteBulk(cmd.Args[0])
})
}
func echo(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
if len(cmd.Args) == 0 {
conn.WriteBulkNull()
return
}
conn.WriteBulk(cmd.Args[0])
})
}
func quit(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
conn.WriteOK()
conn.Network.Close()
})
}
func flushDB(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
n.Clear()
conn.WriteOK()
})
}
func watchKey(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if conn.State&redis.MultiPrepare == redis.MultiPrepare {
conn.WriteError("ERR WATCH inside MULTI is not allowed")
return
}
if len(cmd.Args) == 0 {
conn.WriteError("WATCH requires at least one argument")
return
}
n.Watch(conn, cmd.Args...)
conn.WriteOK()
}
func multi(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if conn.State&redis.MultiPrepare == redis.MultiPrepare {
conn.WriteError("ERR MULTI calls can not be nested")
return
}
conn.State |= redis.MultiPrepare
conn.WriteOK()
}
func discard(n *Nodis, conn *redis.Conn, cmd redis.Command) {
conn.State = redis.MultiNone
conn.Commands = nil
conn.WatchKeys.Clear()
conn.WriteOK()
}
func exec(n *Nodis, conn *redis.Conn, cmd redis.Command) {
defer func() {
conn.State = redis.MultiNone
conn.Commands = nil
conn.WatchKeys.Clear()
}()
if conn.State&redis.MultiPrepare != redis.MultiPrepare {
conn.WriteError("ERR EXEC without MULTI")
return
}
if conn.State&redis.MultiError == redis.MultiError {
conn.WriteError("EXECABORT Transaction discarded because of previous errors.")
return
}
if len(conn.Commands) == 0 {
conn.WriteArray(0)
return
}
var watchKeysNoChanged = true
tx := newTx(n.store)
defer tx.commit()
conn.WatchKeys.Scan(func(key string, modified bool) bool {
if modified {
watchKeysNoChanged = false
return false
}
return true
})
if !watchKeysNoChanged {
conn.WriteBulkNull()
return
}
conn.State |= redis.MultiCommit
conn.WriteArray(len(conn.Commands))
for _, command := range conn.Commands {
func() {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered error: ", r)
conn.WriteError("WRONGTYPE " + r.(error).Error())
return
}
}()
command()
}()
}
}
func unwatchKey(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
n.UnWatch(conn)
conn.WriteOK()
})
}
func del(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("DEL requires at least one argument")
return
}
execCommand(conn, func() {
conn.WriteInt64(n.Del(cmd.Args...))
})
}
func unlink(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("UNLINK requires at least one argument")
return
}
execCommand(conn, func() {
conn.WriteInt64(n.Unlink(cmd.Args...))
})
}
func exists(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("EXISTS requires at least one argument")
return
}
execCommand(conn, func() {
conn.WriteInt64(n.Exists(cmd.Args...))
})
}
// EXPIRE key seconds [NX | XX | GT | LT]
func expire(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("EXPIRE requires at least two arguments")
return
}
execCommand(conn, func() {
seconds, _ := strconv.ParseInt(cmd.Args[1], 10, 64)
if cmd.Options.NX > 1 {
conn.WriteInt64(n.ExpireNX(cmd.Args[0], seconds))
return
}
if cmd.Options.XX > 1 {
conn.WriteInt64(n.ExpireXX(cmd.Args[0], seconds))
return
}
if cmd.Options.LT > 1 {
conn.WriteInt64(n.ExpireLT(cmd.Args[0], seconds))
return
}
if cmd.Options.GT > 1 {
conn.WriteInt64(n.ExpireGT(cmd.Args[0], seconds))
return
}
conn.WriteInt64(n.Expire(cmd.Args[0], seconds))
})
}
// EXPIREAT key unix-time-seconds [NX | XX | GT | LT]
func expireAt(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("EXPIREAT requires at least two arguments")
return
}
timestamp, err := strconv.ParseInt(cmd.Args[1], 10, 64)
if err != nil {
conn.WriteError("ERR value is not an integer or out of range")
return
}
execCommand(conn, func() {
e := time.Unix(timestamp, 0)
if cmd.Options.NX > 1 {
conn.WriteInt64(n.ExpireAtNX(cmd.Args[0], e))
return
}
if cmd.Options.XX > 1 {
conn.WriteInt64(n.ExpireAtXX(cmd.Args[0], e))
return
}
if cmd.Options.LT > 1 {
conn.WriteInt64(n.ExpireAtLT(cmd.Args[0], e))
return
}
if cmd.Options.GT > 1 {
conn.WriteInt64(n.ExpireAtGT(cmd.Args[0], e))
return
}
conn.WriteInt64(n.ExpireAt(cmd.Args[0], e))
})
}
func keys(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("KEYS requires at least one argument")
return
}
execCommand(conn, func() {
keys := n.Keys(cmd.Args[0])
conn.WriteArray(len(keys))
for _, v := range keys {
conn.WriteBulk(v)
}
})
}
func ttl(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("TTL requires at least one argument")
return
}
execCommand(conn, func() {
v := n.TTL(cmd.Args[0])
if v == -1 {
conn.WriteInt64(-1)
return
}
if v == -2 {
conn.WriteInt64(-2)
return
}
conn.WriteInt64(int64(v.Seconds()))
})
}
func pTtl(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("PTTL requires at least one argument")
return
}
execCommand(conn, func() {
v := n.PTTL(cmd.Args[0])
if v == -1 {
conn.WriteInt64(-1)
return
}
if v == -2 {
conn.WriteInt64(-2)
return
}
conn.WriteInt64(v)
})
}
func Persist(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("PERSIST requires at least one argument")
return
}
execCommand(conn, func() {
conn.WriteInt64(n.Persist(cmd.Args[0]))
})
}
func randomKey(n *Nodis, conn *redis.Conn, cmd redis.Command) {
execCommand(conn, func() {
key := n.RandomKey()
if key == "" {
conn.WriteBulkNull()
return
}
conn.WriteBulk(key)
})
}
func rename(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("RENAME requires at least two arguments")
return
}
execCommand(conn, func() {
oldKey := cmd.Args[0]
newKey := cmd.Args[1]
_ = n.Rename(oldKey, newKey)
conn.WriteOK()
})
}
func renameNx(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("RENAMENX requires at least two arguments")
return
}
execCommand(conn, func() {
oldKey := cmd.Args[0]
newKey := cmd.Args[1]
v := n.RenameNX(oldKey, newKey)
if v == nil {
conn.WriteInt64(1)
return
}
conn.WriteInt64(0)
})
}
func typ(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("TYPE requires at least one argument")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
conn.WriteString(n.Type(key))
})
}
// SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
func scan(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("SCAN requires at least one argument")
return
}
cursor, err := strconv.ParseInt(cmd.Args[0], 10, 64)
if err != nil {
conn.WriteError("ERR cursor value is not an integer or out of range")
return
}
var match = "*"
var count int64 = 10
var typ ds.ValueType
if cmd.Options.MATCH > 0 {
match = cmd.Args[cmd.Options.MATCH]
}
if cmd.Options.COUNT > 0 {
count, err = strconv.ParseInt(cmd.Args[cmd.Options.COUNT], 10, 64)
if err != nil {
conn.WriteError("ERR count value is not an integer or out of range")
return
}
if count == 0 {
conn.WriteError("ERR syntax error")
return
}
}
if cmd.Options.TYPE > 0 {
typ = ds.StringToDataType(strings.ToUpper(cmd.Args[cmd.Options.TYPE]))
}
execCommand(conn, func() {
nextCursor, keys := n.Scan(cursor, match, count, typ)
conn.WriteArray(2)
conn.WriteBulk(strconv.FormatInt(nextCursor, 10))
conn.WriteArray(len(keys))
for _, v := range keys {
conn.WriteBulk(v)
}
})
}
// SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
func setString(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("SET requires at least two arguments")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
value := []byte(cmd.Args[1])
var get []byte
if cmd.Options.GET > 1 {
get = n.Get(key)
}
if cmd.Options.NX > 1 {
if !n.SetNX(key, value, cmd.Options.KEEPTTL > 1) {
conn.WriteBulkNull()
return
}
} else if cmd.Options.XX > 1 {
if !n.SetXX(key, value, cmd.Options.KEEPTTL > 1) {
conn.WriteBulkNull()
return
}
} else {
n.Set(key, value, cmd.Options.KEEPTTL > 1)
}
if cmd.Options.EX > 1 {
seconds, _ := strconv.ParseInt(cmd.Args[cmd.Options.EX], 10, 64)
n.Expire(key, seconds)
conn.WriteOK()
return
}
if cmd.Options.PX > 1 {
milliseconds, _ := strconv.ParseInt(cmd.Args[cmd.Options.PX], 10, 64)
if n.ExpirePX(key, milliseconds) == 0 {
conn.WriteBulkNull()
return
}
conn.WriteOK()
return
}
if cmd.Options.EXAT > 1 {
seconds, _ := strconv.ParseInt(cmd.Args[cmd.Options.EXAT], 10, 64)
if n.ExpireAt(key, time.Unix(seconds, 0)) == 0 {
conn.WriteBulkNull()
return
}
conn.WriteOK()
return
}
if cmd.Options.PXAT > 1 {
milliseconds, _ := strconv.ParseInt(cmd.Args[cmd.Options.PXAT], 10, 64)
seconds := milliseconds / 1000
ns := (milliseconds - seconds*1000) * 1000 * 1000
n.ExpireAt(key, time.Unix(seconds, ns))
conn.WriteOK()
return
}
if get != nil {
conn.WriteBulk(string(get))
return
}
conn.WriteOK()
})
}
func mSet(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 || len(cmd.Args)%2 != 0 {
conn.WriteError("MSET requires at least two arguments")
return
}
execCommand(conn, func() {
for i := 0; i < len(cmd.Args); i += 2 {
n.Set(cmd.Args[i], []byte(cmd.Args[i+1]), false)
}
conn.WriteOK()
})
}
func appendString(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("APPEND requires at least two arguments")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
value := []byte(cmd.Args[1])
conn.WriteInt64(n.Append(key, value))
})
}
func setex(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 3 {
conn.WriteError("SETEX requires at least three arguments")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
seconds, _ := strconv.ParseInt(cmd.Args[1], 10, 64)
value := []byte(cmd.Args[2])
n.SetEX(key, value, seconds)
conn.WriteOK()
})
}
func setnx(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("SETNX requires at least two arguments")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
value := []byte(cmd.Args[1])
if n.SetNX(key, value, false) {
conn.WriteInt64(1)
return
}
conn.WriteInt64(0)
})
}
func incr(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("INCR requires at least one argument")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
v, err := n.Incr(key)
if err != nil {
conn.WriteBulkNull()
return
}
conn.WriteInt64(v)
})
}
func incrBy(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("INCRBY requires at least two arguments")
return
}
key := cmd.Args[0]
value, err := strconv.ParseInt(cmd.Args[1], 10, 64)
if err != nil {
conn.WriteError("ERR value is not an integer or out of range")
return
}
execCommand(conn, func() {
v, err := n.IncrBy(key, value)
if err != nil {
conn.WriteBulkNull()
return
}
conn.WriteInt64(v)
})
}
func decr(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("DECR requires at least one argument")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
v, err := n.Decr(key)
if err != nil {
conn.WriteBulkNull()
return
}
conn.WriteInt64(v)
})
}
func decrBy(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("INCRBY requires at least two arguments")
return
}
key := cmd.Args[0]
value, err := strconv.ParseInt(cmd.Args[1], 10, 64)
if err != nil {
conn.WriteError("ERR value is not an integer or out of range")
return
}
execCommand(conn, func() {
v, err := n.DecrBy(key, value)
if err != nil {
conn.WriteBulkNull()
return
}
conn.WriteInt64(v)
})
}
func incrByFloat(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("INCRBYFLOAT requires at least two arguments")
return
}
key := cmd.Args[0]
value, err := redis.FormatFloat64(cmd.Args[1])
if err != nil {
conn.WriteError("ERR value is not a valid float")
return
}
execCommand(conn, func() {
v, err := n.IncrByFloat(key, value)
if err != nil {
conn.WriteBulkNull()
return
}
conn.WriteBulk(strconv.FormatFloat(v, 'f', -1, 64))
})
}
func getString(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("GET requires at least one argument")
return
}
execCommand(conn, func() {
v := n.Get(cmd.Args[0])
if v == nil {
conn.WriteBulkNull()
return
}
conn.WriteBulk(string(v))
})
}
func getSet(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 2 {
conn.WriteError("GETSET requires at least two arguments")
return
}
execCommand(conn, func() {
key := cmd.Args[0]
value := []byte(cmd.Args[1])
v := n.GetSet(key, value)
if v == nil {
conn.WriteBulkNull()
return
}
conn.WriteBulk(string(v))
})
}
func mGet(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) == 0 {
conn.WriteError("MGET requires at least one argument")
return
}
execCommand(conn, func() {
conn.WriteArray(len(cmd.Args))
for _, v := range cmd.Args {
value := n.Get(v)
if value == nil {
conn.WriteBulkNull()
continue
}
conn.WriteBulk(string(value))
}
})
}
func setRange(n *Nodis, conn *redis.Conn, cmd redis.Command) {
if len(cmd.Args) < 3 {
conn.WriteError("SETRANGE requires at least three arguments")
return
}
key := cmd.Args[0]
offset, err := strconv.ParseInt(cmd.Args[1], 10, 64)