-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisakaDataBase.go
905 lines (874 loc) · 26.4 KB
/
misakaDataBase.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
package main
import (
"MisakaDB/index"
"MisakaDB/logger"
"MisakaDB/storage"
"MisakaDB/util"
"errors"
"fmt"
"github.com/tidwall/redcon"
"runtime/debug"
"strconv"
"strings"
"time"
)
// 以下为可选配置项
const (
MisakaDataBaseFolderPath = "D:\\MisakaDBTest" // 数据库进行数据持久化时 文件的保存位置 注意该路径下不可以有其他人任何文件
RecordFileMaxSize = 65536 // 文件的最大存储字节数
RecordFileIOMode = storage.TraditionalIOFile // 对文件的读写方式 可以是传统的IO 也可以是Mmap
MisakaServerAddr = ":23456" // 数据库的端口
LoggerPath = "D:\\MisakaDBLog" // 数据库的Log保存位置 该位置下有无其它文件都可以
SyncDuration = 1000 // 持久化文件定时同步的时间间隔 单位为毫秒
)
// 下面这是Linux版的路径 方便我切换
//const (
// MisakaDataBaseFolderPath = "/home/MisakaDB"
// LoggerPath = "/home/MisakaDBLog"
//)
type MisakaDataBase struct {
server *redcon.Server
logger *logger.Logger
hashIndex *index.HashIndex
stringIndex *index.StringIndex
listIndex *index.ListIndex
zsetIndex *index.ZSetIndex
}
func Init() (*MisakaDataBase, error) {
database := &MisakaDataBase{}
var e error
// 初始化logger
database.logger, e = logger.NewLogger(LoggerPath)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("Logger is Ready!")
// 读取文件
activeFiles, archiveFiles, e := storage.RecordFilesInit(MisakaDataBaseFolderPath, RecordFileMaxSize, RecordFileIOMode)
if e != nil {
return nil, e
}
// 开始构建索引
for key, value := range activeFiles {
if key == storage.Hash {
database.hashIndex, e = index.BuildHashIndex(value, archiveFiles[storage.Hash], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("Hash Index is Ready!")
}
if key == storage.String {
database.stringIndex, e = index.BuildStringIndex(value, archiveFiles[storage.String], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("String Index is Ready!")
}
if key == storage.List {
database.listIndex, e = index.BuildListIndex(value, archiveFiles[storage.List], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("List Index is Ready! ")
}
if key == storage.ZSet {
database.zsetIndex, e = index.BuildZSetIndex(value, archiveFiles[storage.ZSet], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("ZSet Index is Ready! ")
}
}
// 开始检查索引是否构建 如果否 构建一个空的索引
// 这是防activeFiles本身不存在
if database.hashIndex == nil {
database.hashIndex, e = index.BuildHashIndex(nil, nil, RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
logger.GenerateErrorLog(false, false, e.Error(), "Build Empty Hash Index Failed!")
return nil, e
}
logger.GenerateInfoLog("Hash Index is Ready!")
}
if database.stringIndex == nil {
database.stringIndex, e = index.BuildStringIndex(nil, nil, RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
logger.GenerateErrorLog(false, false, e.Error(), "Build Empty String Index Failed!")
return nil, e
}
logger.GenerateInfoLog("String Index is Ready!")
}
if database.listIndex == nil {
database.listIndex, e = index.BuildListIndex(nil, archiveFiles[storage.List], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("List Index is Ready! ")
}
if database.zsetIndex == nil {
database.zsetIndex, e = index.BuildZSetIndex(nil, archiveFiles[storage.ZSet], RecordFileIOMode, MisakaDataBaseFolderPath, RecordFileMaxSize, time.Millisecond*SyncDuration)
if e != nil {
return nil, e
}
logger.GenerateInfoLog("ZSet Index is Ready! ")
}
// 初始化服务器
e = database.ServerInit()
if e != nil {
logger.GenerateErrorLog(false, false, e.Error(), "Server Init Failed!")
return nil, e
}
logger.GenerateInfoLog("Server is Ready!")
return database, nil
}
func (db *MisakaDataBase) Destroy() error {
// 关闭服务器
e := db.server.Close()
if e != nil {
logger.GenerateErrorLog(false, false, e.Error())
return e
}
// 关闭索引 索引里会挨个关闭文件的
e = db.hashIndex.CloseIndex()
if e != nil {
return e
}
e = db.stringIndex.CloseIndex()
if e != nil {
return e
}
e = db.listIndex.CloseIndex()
if e != nil {
return e
}
// 关闭logger
db.logger.StopLogger()
return nil
}
func (db *MisakaDataBase) ServerInit() error {
// redcon是多线程的 而且应该是线程安全的
// 创建一个Server需要三个回调函数:
// 1 通过连接接收请求时调用的函数
// 2 接受连接时调用的函数
// 3 断开连接时调用的函数
var (
e error
expired int
)
db.server = redcon.NewServer(MisakaServerAddr,
func(conn redcon.Conn, cmd redcon.Command) {
// 捕捉panic
defer func() {
p := recover()
if p != nil {
stackTrace := debug.Stack() // 获取引发panic位置的堆栈信息
logger.GenerateErrorLog(true, false, string(stackTrace), fmt.Sprintf("%v", p))
}
return
}()
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query Command: " + util.TurnByteArray2ToString(cmd.Args))
switch strings.ToLower(string(cmd.Args[0])) {
default:
// 命令不能识别
conn.WriteError("ERR unknown command '" + util.TurnByteArray2ToString(cmd.Args) + "'")
logger.GenerateInfoLog(conn.RemoteAddr() + ": Unknown Query: " + util.TurnByteArray2ToString(cmd.Args))
return
case "ping":
conn.WriteString("PONG")
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: ping")
return
case "quit":
conn.WriteString("OK")
e = conn.Close()
if e != nil {
logger.GenerateErrorLog(false, false, e.Error())
}
return
// string部分的命令解析
case "set":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: set")
if len(cmd.Args) == 3 {
// set key value
e = db.stringIndex.Set(cmd.Args[1], cmd.Args[2], -1)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
} else if len(cmd.Args) == 5 {
// set key value ex/px time
expired, e = strconv.Atoi(string(cmd.Args[4]))
if e != nil {
conn.WriteError("Cannot Read Expired As Number: " + e.Error())
return
}
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[3]), expired)
if e != nil {
conn.WriteError(e.Error() + string(cmd.Args[3]))
return
}
e = db.stringIndex.Set(cmd.Args[1], cmd.Args[2], expiredAt)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "setnx":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: setnx")
if len(cmd.Args) == 3 {
// setnx key value
e = db.stringIndex.SetNX(cmd.Args[1], cmd.Args[2], -1)
if e != nil {
if errors.Is(logger.KeyIsExisted, e) {
conn.WriteInt(0)
return
} else {
conn.WriteError(e.Error())
return
}
}
conn.WriteInt(1)
} else if len(cmd.Args) == 5 {
// setnx key value ex/px time
expired, e = strconv.Atoi(string(cmd.Args[4]))
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[3]), expired)
if e != nil {
conn.WriteError(e.Error() + string(cmd.Args[3]))
return
}
if e != nil {
conn.WriteError("Cannot Read Expired As Number: " + e.Error())
return
}
e = db.stringIndex.SetNX(cmd.Args[1], cmd.Args[2], expiredAt)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "get":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: get")
if len(cmd.Args) == 2 {
// get key
var result string
result, e = db.stringIndex.Get(cmd.Args[1])
if errors.Is(logger.KeyIsNotExisted, e) {
conn.WriteString("nil")
return
} else if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "getrange":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: getrange")
if len(cmd.Args) == 4 {
// getrange key start end
var (
result string
start int
end int
)
start, e = strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Start As Number: " + e.Error())
return
}
end, e = strconv.Atoi(string(cmd.Args[3]))
if e != nil {
conn.WriteError("Cannot Read End As Number: " + e.Error())
return
}
result, e = db.stringIndex.GetRange(cmd.Args[1], start, end)
if errors.Is(logger.KeyIsNotExisted, e) {
conn.WriteString("nil")
} else if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "getset":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: getset")
if len(cmd.Args) == 3 {
// getset key value
var result string
result, e = db.stringIndex.GetSet(cmd.Args[1], cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "append":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: append")
if len(cmd.Args) == 3 {
// append key appendValue
e = db.stringIndex.Append(cmd.Args[1], cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(len(cmd.Args[2]))
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "del":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: del")
if len(cmd.Args) == 2 {
// del key
e = db.stringIndex.Del(cmd.Args[1])
if e != nil {
if errors.Is(logger.KeyIsNotExisted, e) {
conn.WriteInt(0)
} else {
conn.WriteError(e.Error())
}
return
}
conn.WriteInt(1)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
// hash部分的命令解析
case "hset":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hset")
if len(cmd.Args) == 4 {
// hset key field value
e = db.hashIndex.HSet(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]), -1)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else if len(cmd.Args) == 6 {
// hset key field value ex/px time
// 设置过期时间
expired, e = strconv.Atoi(string(cmd.Args[5]))
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[4]), expired)
if e != nil {
conn.WriteError(e.Error() + string(cmd.Args[4]))
return
}
e = db.hashIndex.HSet(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]), expiredAt)
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hsetnx":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hsetnx")
if len(cmd.Args) == 4 {
// hset key field value
e = db.hashIndex.HSetNX(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]), -1)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else if len(cmd.Args) == 6 {
// hsetnx key field value ex/px time
// 设置过期时间
expired, e = strconv.Atoi(string(cmd.Args[5]))
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[4]), expired)
if e != nil {
conn.WriteError(e.Error() + string(cmd.Args[4]))
return
}
e = db.hashIndex.HSetNX(string(cmd.Args[1]), string(cmd.Args[2]), string(cmd.Args[3]), expiredAt)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hget":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hget")
if len(cmd.Args) == 3 {
// hget key field
var result string
result, e = db.hashIndex.HGet(string(cmd.Args[1]), string(cmd.Args[2]))
if errors.Is(logger.KeyIsNotExisted, e) || errors.Is(logger.FieldIsNotExisted, e) {
conn.WriteString("nil")
} else if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hdel":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hdel")
if len(cmd.Args) == 3 {
// hdel key field
e = db.hashIndex.HDel(string(cmd.Args[1]), string(cmd.Args[2]), true)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(1)
return
} else if len(cmd.Args) == 2 {
// hdel key
e = db.hashIndex.HDel(string(cmd.Args[1]), "", false)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(1)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hlen":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hlen")
if len(cmd.Args) == 2 {
// hlen key
var result int
result, e = db.hashIndex.HLen(string(cmd.Args[1]))
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hexists":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hexists")
if len(cmd.Args) == 3 {
// hexists key field
var result bool
result, e = db.hashIndex.HExist(string(cmd.Args[1]), string(cmd.Args[2]))
if e != nil {
conn.WriteError(e.Error())
return
}
if result {
conn.WriteInt(1)
} else {
conn.WriteInt(0)
}
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "hstrlen":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: hstrlen")
if len(cmd.Args) == 3 {
// hstrlen key field
var result int
result, e = db.hashIndex.HStrLen(string(cmd.Args[1]), string(cmd.Args[2]))
if errors.Is(logger.FieldIsNotExisted, e) {
conn.WriteInt(0)
return
} else if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
// 参数数量错误
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
// list 部分的命令解析
case "linsert":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: linsert")
if len(cmd.Args) == 4 {
// linsert key index value
i, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Index As Number: " + e.Error())
return
}
e = db.listIndex.LInsert(cmd.Args[1], i, cmd.Args[3], -1)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else if len(cmd.Args) == 6 {
// linsert key index value ex/px time
i, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Index As Number: " + e.Error())
return
}
expired, e = strconv.Atoi(string(cmd.Args[5]))
if e != nil {
conn.WriteError("Cannot Read Expired As Number: " + e.Error())
return
}
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[4]), expired)
e = db.listIndex.LInsert(cmd.Args[1], i, cmd.Args[3], expiredAt)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lpop":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: lpop")
if len(cmd.Args) == 2 {
// lpop key
v, e := db.listIndex.LPop(cmd.Args[1])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(string(v))
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lpush":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: lpush")
if len(cmd.Args) == 3 {
// lpush key value
e = db.listIndex.LPush(cmd.Args[1], -1, cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else if len(cmd.Args) == 5 {
// lpush key value ex/px time
expired, e = strconv.Atoi(string(cmd.Args[4]))
if e != nil {
conn.WriteError("Cannot Read Expired As Number: " + e.Error())
return
}
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[3]), expired)
e = db.listIndex.LPush(cmd.Args[1], expiredAt, cmd.Args[3])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lset":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: lset")
if len(cmd.Args) == 4 {
// lset key index value
i, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Index As Number: " + e.Error())
return
}
e = db.listIndex.LSet(cmd.Args[1], i, cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lrem":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: lrem")
if len(cmd.Args) == 4 {
// lrem key index value
i, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Index As Number: " + e.Error())
return
}
e = db.listIndex.LRem(cmd.Args[1], i, cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "llen":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 2 {
// llen key
result, e := db.listIndex.LLen(cmd.Args[1])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lindex":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 3 {
// lindex key index
i, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Index As Number: " + e.Error())
return
}
result, e := db.listIndex.LIndex(cmd.Args[1], i)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(string(result))
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "lrange":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 4 {
// lrange key start end
start, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Start As Number: " + e.Error())
return
}
end, e := strconv.Atoi(string(cmd.Args[3]))
if e != nil {
conn.WriteError("Cannot Read End As Number: " + e.Error())
return
}
result, e := db.listIndex.LRange(cmd.Args[1], start, end)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(util.TurnByteArray2ToString(result))
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
// zset 部分命令解析
case "zadd":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 4 {
// zadd key score member
s, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Score As Number: " + e.Error())
return
}
e = db.zsetIndex.ZAdd(cmd.Args[1], s, cmd.Args[3], -1)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else if len(cmd.Args) == 6 {
// zadd key score member ex/px time
s, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Score As Number: " + e.Error())
return
}
expired, e = strconv.Atoi(string(cmd.Args[5]))
if e != nil {
conn.WriteError("Cannot Read Expired As Number: " + e.Error())
return
}
var expiredAt int64
expiredAt, e = util.CalcTimeUnix(string(cmd.Args[4]), expired)
e = db.zsetIndex.ZAdd(cmd.Args[1], s, cmd.Args[3], expiredAt)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "zrem":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 3 {
// zrem key member
e = db.zsetIndex.ZRem(cmd.Args[1], cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString("OK")
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "zscore":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 3 {
// zscore key member
result, e := db.zsetIndex.ZScore(cmd.Args[1], cmd.Args[2])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "zcard":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 2 {
// zcard key
result, e := db.zsetIndex.ZCard(cmd.Args[1])
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "zcount":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 4 {
// zcount key min max
minIndex, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Min As Number: " + e.Error())
return
}
maxIndex, e := strconv.Atoi(string(cmd.Args[3]))
if e != nil {
conn.WriteError("Cannot Read Max As Number: " + e.Error())
return
}
result, e := db.zsetIndex.ZCount(cmd.Args[1], minIndex, maxIndex)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteInt(result)
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
case "zrange":
logger.GenerateInfoLog(conn.RemoteAddr() + ": Query: " + string(cmd.Args[0]))
if len(cmd.Args) == 4 {
// zrange key min max
minIndex, e := strconv.Atoi(string(cmd.Args[2]))
if e != nil {
conn.WriteError("Cannot Read Min As Number: " + e.Error())
return
}
maxIndex, e := strconv.Atoi(string(cmd.Args[3]))
if e != nil {
conn.WriteError("Cannot Read Max As Number: " + e.Error())
return
}
result, e := db.zsetIndex.ZRange(cmd.Args[1], minIndex, maxIndex)
if e != nil {
conn.WriteError(e.Error())
return
}
conn.WriteString(util.TurnByteArray2ToString(result))
return
} else {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
}
},
func(conn redcon.Conn) bool {
logger.GenerateInfoLog("DataBase Connection Accept: " + conn.RemoteAddr())
return true
},
func(conn redcon.Conn, err error) {
logger.GenerateInfoLog("DataBase Connection Closed: " + conn.RemoteAddr())
return
},
)
return nil
}
func (db *MisakaDataBase) StartServe() error {
logger.GenerateInfoLog("Server start Listen And Serve!")
return db.server.ListenAndServe()
// 翻源码可知:
// ListenAndServe -> ListenServeAndSignal -> serve -> 如果有tcp连接 -> go handle
// 所以ListenServeAndSignal是阻塞线程监听的
}