-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdecoder.go
More file actions
1363 lines (1258 loc) · 33.3 KB
/
decoder.go
File metadata and controls
1363 lines (1258 loc) · 33.3 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
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 rdb implements parsing and encoding of the Redis RDB file format.
package rdb
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"strconv"
"github.com/dongmx/rdb/crc64"
"github.com/juju/errors"
)
type Info struct {
Encoding string
Idle uint64
Freq int
SizeOfValue int
Zips uint64
}
type StreamPendingEntry struct {
ID []byte
DeliveryTime uint64
DeliveryCount uint64
}
type StreamConsumerPendingEntry struct {
ID []byte
}
type StreamConsumerData struct {
Name []byte
SeenTime uint64
Pending []*StreamConsumerPendingEntry
}
type StreamGroup struct {
Name []byte
LastEntryId string
Pending []*StreamPendingEntry
Consumers []*StreamConsumerData
}
type StreamGroups []*StreamGroup
// A Decoder must be implemented to parse a RDB file.
type Decoder interface {
// StartRDB is called when parsing of a valid RDB file starts.
StartRDB(ver int)
// StartDatabase is called when database n starts.
// Once a database starts, another database will not start until EndDatabase is called.
StartDatabase(n int)
// AUX field
Aux(key, value []byte)
// ResizeDB hint
ResizeDatabase(dbSize, expiresSize uint32)
// Set is called once for each string key.
Set(key, value []byte, expiry int64, info *Info)
// StartHash is called at the beginning of a hash.
// Hset will be called exactly length times before EndHash.
StartHash(key []byte, length, expiry int64, info *Info)
// Hset is called once for each field=value pair in a hash.
Hset(key, field, value []byte)
// EndHash is called when there are no more fields in a hash.
EndHash(key []byte)
// StartSet is called at the beginning of a set.
// Sadd will be called exactly cardinality times before EndSet.
StartSet(key []byte, cardinality, expiry int64, info *Info)
// Sadd is called once for each member of a set.
Sadd(key, member []byte)
// EndSet is called when there are no more fields in a set.
EndSet(key []byte)
// StartStream is called at the beginning of a stream.
// Xadd will be called exactly length times before EndStream.
StartStream(key []byte, cardinality, expiry int64, info *Info)
// Xadd is called once for each id in a stream.
Xadd(key, id, listpack []byte)
// EndHash is called when there are no more fields in a hash.
EndStream(key []byte, items uint64, lastEntryID string, cgroupsData StreamGroups)
// StartList is called at the beginning of a list.
// Rpush will be called exactly length times before EndList.
// If length of the list is not known, then length is -1
StartList(key []byte, length, expiry int64, info *Info)
// Rpush is called once for each value in a list.
Rpush(key, value []byte)
// EndList is called when there are no more values in a list.
EndList(key []byte)
// StartZSet is called at the beginning of a sorted set.
// Zadd will be called exactly cardinality times before EndZSet.
StartZSet(key []byte, cardinality, expiry int64, info *Info)
// Zadd is called once for each member of a sorted set.
Zadd(key []byte, score float64, member []byte)
// EndZSet is called when there are no more members in a sorted set.
EndZSet(key []byte)
// EndDatabase is called at the end of a database.
EndDatabase(n int)
// EndRDB is called when parsing of the RDB file is complete.
EndRDB()
}
// Decode parses a RDB file from r and calls the decode hooks on d.
func Decode(r io.Reader, d Decoder) error {
decoder := &decode{d, make([]byte, 8), bufio.NewReader(r), 0, 0, nil, 0}
return decoder.decode()
}
// DecodeDump a byte slice from the Redis DUMP command. The dump does not contain the
// database, key or expiry, so they must be included in the function call (but
// can be zero values).
func DecodeDump(dump []byte, db int, key []byte, expiry int64, d Decoder) error {
err := verifyDump(dump)
if err != nil {
return errors.Trace(err)
}
decoder := &decode{d, make([]byte, 8), bytes.NewReader(dump[1:]), 0, 0, nil, 0}
decoder.event.StartRDB(0)
decoder.event.StartDatabase(db)
err = decoder.readObject(key, ValueType(dump[0]), expiry)
decoder.event.EndDatabase(db)
decoder.event.EndRDB()
return errors.Trace(err)
}
type byteReader interface {
io.Reader
io.ByteReader
}
type decode struct {
event Decoder
intBuf []byte
r byteReader
lruIdle uint64
lfuFreq int
info *Info
rdbVersion int
}
// ValueType of redis type
type ValueType byte
// type value
const (
TypeString ValueType = 0
TypeList ValueType = 1
TypeSet ValueType = 2
TypeZSet ValueType = 3
TypeHash ValueType = 4
TypeZSet2 ValueType = 5
TypeModule ValueType = 6
TypeModule2 ValueType = 7
TypeHashZipmap ValueType = 9
TypeListZiplist ValueType = 10
TypeSetIntset ValueType = 11
TypeZSetZiplist ValueType = 12
TypeHashZiplist ValueType = 13
TypeListQuicklist ValueType = 14
TypeStreamListPacks ValueType = 15
)
const (
rdbVersion = 9
rdb6bitLen = 0
rdb14bitLen = 1
rdb32bitLen = 0x80
rdb64bitLen = 0x81
rdbEncVal = 3
rdbLenErr = math.MaxUint64
rdbOpCodeModuleAux = 247
rdbOpCodeIdle = 248
rdbOpCodeFreq = 249
rdbOpCodeAux = 250
rdbOpCodeResizeDB = 251
rdbOpCodeExpiryMS = 252
rdbOpCodeExpiry = 253
rdbOpCodeSelectDB = 254
rdbOpCodeEOF = 255
rdbModuleOpCodeEOF = 0
rdbModuleOpCodeSint = 1
rdbModuleOpCodeUint = 2
rdbModuleOpCodeFloat = 3
rdbModuleOpCodeDouble = 4
rdbModuleOpCodeString = 5
rdbLoadNone = 0
rdbLoadEnc = (1 << 0)
rdbLoadPlain = (1 << 1)
rdbLoadSds = (1 << 2)
rdbSaveNode = 0
rdbSaveAofPreamble = (1 << 0)
rdbEncInt8 = 0
rdbEncInt16 = 1
rdbEncInt32 = 2
rdbEncLZF = 3
rdbZiplist6bitlenString = 0
rdbZiplist14bitlenString = 1
rdbZiplist32bitlenString = 2
rdbZiplistInt16 = 0xc0
rdbZiplistInt32 = 0xd0
rdbZiplistInt64 = 0xe0
rdbZiplistInt24 = 0xf0
rdbZiplistInt8 = 0xfe
rdbZiplistInt4 = 15
rdbLpHdrSize = 6
rdbLpHdrNumeleUnknown = math.MaxUint16
rdbLpMaxIntEncodingLen = 0
rdbLpMaxBacklenSize = 5
rdbLpMaxEntryBacklen = 34359738367
rdbLpEncodingInt = 0
rdbLpEncodingString = 1
rdbLpEncoding7BitUint = 0
rdbLpEncoding7BitUintMask = 0x80
rdbLpEncoding6BitStr = 0x80
rdbLpEncoding6BitStrMask = 0xC0
rdbLpEncoding13BitInt = 0xC0
rdbLpEncoding13BitIntMask = 0xE0
rdbLpEncoding12BitStr = 0xE0
rdbLpEncoding12BitStrMask = 0xF0
rdbLpEncoding16BitInt = 0xF1
rdbLpEncoding16BitIntMask = 0xFF
rdbLpEncoding24BitInt = 0xF2
rdbLpEncoding24BitIntMask = 0xFF
rdbLpEncoding32BitInt = 0xF3
rdbLpEncoding32BitIntMask = 0xFF
rdbLpEncoding64BitInt = 0xF4
rdbLpEncoding64BitIntMask = 0xFF
rdbLpEncoding32BitStr = 0xF0
rdbLpEncoding32BitStrMask = 0xFF
rdbLpEOF = 0xFF
)
func (d *decode) decode() error {
err := d.checkHeader()
if err != nil {
return errors.Trace(err)
}
d.event.StartRDB(d.rdbVersion)
var db uint64
var expiry int64
//var lruClock int64
firstDB := true
for {
d.lruIdle = 0
d.lfuFreq = 0
objType, err := d.r.ReadByte()
if err != nil {
return errors.Wrap(err, errors.New("readfailed"))
}
switch objType {
case rdbOpCodeFreq:
b, err := d.r.ReadByte()
d.lfuFreq = int(b)
if err != nil {
return errors.Trace(err)
}
case rdbOpCodeIdle:
idle, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.lruIdle = uint64(idle)
case rdbOpCodeAux:
auxKey, err := d.readString()
if err != nil {
return errors.Trace(err)
}
auxVal, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.event.Aux(auxKey, auxVal)
case rdbOpCodeResizeDB:
dbSize, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
expiresSize, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.event.ResizeDatabase(uint32(dbSize), uint32(expiresSize))
case rdbOpCodeExpiryMS:
_, err := io.ReadFull(d.r, d.intBuf)
if err != nil {
return errors.Trace(err)
}
expiry = int64(binary.LittleEndian.Uint64(d.intBuf))
case rdbOpCodeExpiry:
_, err := io.ReadFull(d.r, d.intBuf[:4])
if err != nil {
return errors.Trace(err)
}
expiry = int64(binary.LittleEndian.Uint32(d.intBuf)) * 1000
case rdbOpCodeSelectDB:
if !firstDB {
d.event.EndDatabase(int(db))
}
db, _, err = d.readLength()
if err != nil {
return errors.Trace(err)
}
d.event.StartDatabase(int(db))
case rdbOpCodeEOF:
d.event.EndDatabase(int(db))
d.event.EndRDB()
return nil
case rdbOpCodeModuleAux:
return errors.Errorf("unsupport module")
default:
key, err := d.readString()
if err != nil {
return errors.Trace(err)
}
err = d.readObject(key, ValueType(objType), expiry)
if err != nil {
return errors.Trace(err)
}
expiry = 0
}
}
}
func (d *decode) readObject(key []byte, typ ValueType, expiry int64) error {
d.info = &Info{
Idle: d.lruIdle,
Freq: d.lfuFreq,
}
switch typ {
case TypeString:
value, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "string"
d.event.Set(key, value, expiry, d.info)
case TypeList:
length, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "linkedlist"
d.event.StartList(key, int64(length), expiry, d.info)
for length > 0 {
length--
value, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.event.Rpush(key, value)
}
d.event.EndList(key)
case TypeListQuicklist:
length, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "quicklist"
d.info.Zips = length
d.event.StartList(key, int64(-1), expiry, d.info)
for length > 0 {
length--
d.readZiplist(key, 0, false)
}
d.event.EndList(key)
case TypeSet:
cardinality, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "hashtable"
d.event.StartSet(key, int64(cardinality), expiry, d.info)
for cardinality > 0 {
cardinality--
member, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.event.Sadd(key, member)
}
d.event.EndSet(key)
case TypeZSet2:
fallthrough
case TypeZSet:
cardinality, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "skiplist"
d.event.StartZSet(key, int64(cardinality), expiry, d.info)
for cardinality > 0 {
cardinality--
member, err := d.readString()
if err != nil {
return errors.Trace(err)
}
var score float64
if typ == TypeZSet2 {
score, err = d.readBinaryFloat64()
if err != nil {
return errors.Trace(err)
}
} else {
score, err = d.readFloat64()
if err != nil {
return errors.Trace(err)
}
}
d.event.Zadd(key, score, member)
}
d.event.EndZSet(key)
case TypeHash:
length, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "hashtable"
d.event.StartHash(key, int64(length), expiry, d.info)
for length > 0 {
length--
field, err := d.readString()
if err != nil {
return errors.Trace(err)
}
value, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.event.Hset(key, field, value)
}
d.event.EndHash(key)
case TypeHashZipmap:
return errors.Trace(d.readZipmap(key, expiry))
case TypeListZiplist:
return errors.Trace(d.readZiplist(key, expiry, true))
case TypeSetIntset:
return errors.Trace(d.readIntset(key, expiry))
case TypeZSetZiplist:
return errors.Trace(d.readZiplistZset(key, expiry))
case TypeHashZiplist:
return errors.Trace(d.readZiplistHash(key, expiry))
case TypeStreamListPacks:
return errors.Trace(d.readStream(key, expiry))
case TypeModule:
fallthrough
case TypeModule2:
return d.readModule(key, expiry)
default:
return fmt.Errorf("rdb: unknown object type %d for key %s", typ, key)
}
return nil
}
func (d *decode) readModule(key []byte, expiry int64) error {
moduleid, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
return fmt.Errorf("Not supported load module %v", moduleid)
}
func (d *decode) readStream(key []byte, expiry int64) error {
cardinality, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
d.info.Encoding = "listpack"
d.event.StartStream(key, int64(cardinality), expiry, d.info)
for cardinality > 0 {
cardinality--
streamID, err := d.readString()
if err != nil {
return errors.Trace(err)
}
/*
IDms := strconv.FormatUint(binary.BigEndian.Uint64(streamID[:8]), 10)
IDseq := strconv.FormatUint(binary.BigEndian.Uint64(streamID[8:]), 10)
fmt.Println(string(key))
fmt.Println(IDms + "-" + IDseq)
*/
listPack, err := d.readString()
if err != nil {
return errors.Trace(err)
}
d.event.Xadd(key, streamID, listPack)
}
var items, lastIDms, lastIDseq uint64
items, _, err = d.readLength()
if err != nil {
return errors.Trace(err)
}
lastIDms, _, err = d.readLength()
if err != nil {
return errors.Trace(err)
}
lastIDseq, _, err = d.readLength()
if err != nil {
return errors.Trace(err)
}
lastEntryID := fmt.Sprintf("%d-%d", lastIDms, lastIDseq)
//TODO output consumer groups
var groupsCount uint64
groupsCount, _, err = d.readLength()
if err != nil {
return errors.Trace(err)
}
cgroupsData := make(StreamGroups, 0, groupsCount)
for groupsCount > 0 {
groupsCount--
cgname, err := d.readString()
if err != nil {
return errors.Trace(err)
}
gIDms, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
gIDseq, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
lastCgEntryID := fmt.Sprintf("%d-%d", gIDms, gIDseq)
pelSize, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
groupPendingEntries := make([]*StreamPendingEntry, 0, pelSize)
for pelSize > 0 {
pelSize--
// d.readUint64()
rawid := make([]byte, 16)
n, err := io.ReadFull(d.r, rawid)
if err != nil {
return errors.Trace(err)
}
if n != 16 {
return errors.Errorf("expected %d got %d", 16, n)
}
deliveryTime, err := d.readUint64()
if err != nil {
return errors.Trace(err)
}
deliveryCount, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
groupPendingEntries = append(groupPendingEntries, &StreamPendingEntry{
ID: rawid,
DeliveryTime: deliveryTime,
DeliveryCount: deliveryCount,
})
}
consumersNum, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
consumersData := make([]*StreamConsumerData, 0, consumersNum)
for consumersNum > 0 {
consumersNum--
cname, err := d.readString()
if err != nil {
return errors.Trace(err)
}
seenTime, err := d.readUint64()
if err != nil {
return errors.Trace(err)
}
pelSize, _, err := d.readLength()
if err != nil {
return errors.Trace(err)
}
consumerPendingEntries := make([]*StreamConsumerPendingEntry, 0, pelSize)
for pelSize > 0 {
pelSize--
rawid := make([]byte, 16)
n, err := io.ReadFull(d.r, rawid)
if err != nil {
return errors.Trace(err)
}
if n != 16 {
return errors.Errorf("expected %d got %d", 16, n)
}
consumerPendingEntries = append(consumerPendingEntries, &StreamConsumerPendingEntry{ID: rawid})
}
consumersData = append(consumersData, &StreamConsumerData{
Name: cname,
SeenTime: seenTime,
Pending: consumerPendingEntries,
})
}
cgroupsData = append(cgroupsData, &StreamGroup{
Name: cgname,
LastEntryId: lastCgEntryID,
Pending: groupPendingEntries,
Consumers: consumersData,
})
}
d.event.EndStream(key, items, lastEntryID, cgroupsData)
return nil
}
func (d *decode) readZipmap(key []byte, expiry int64) error {
var length int
zipmap, err := d.readString()
if err != nil {
return errors.Trace(err)
}
buf := newSliceBuffer(zipmap)
lenByte, err := buf.ReadByte()
if err != nil {
return errors.Trace(err)
}
if lenByte >= 254 { // we need to count the items manually
length, err = countZipmapItems(buf)
length /= 2
if err != nil {
return errors.Trace(err)
}
} else {
length = int(lenByte)
}
d.info.Encoding = "zipmap"
d.info.SizeOfValue = len(zipmap)
d.event.StartHash(key, int64(length), expiry, d.info)
for i := 0; i < length; i++ {
field, err := readZipmapItem(buf, false)
if err != nil {
return errors.Trace(err)
}
value, err := readZipmapItem(buf, true)
if err != nil {
return errors.Trace(err)
}
d.event.Hset(key, field, value)
}
d.event.EndHash(key)
return nil
}
func readZipmapItem(buf *sliceBuffer, readFree bool) ([]byte, error) {
length, free, err := readZipmapItemLength(buf, readFree)
if err != nil {
return nil, err
}
if length == -1 {
return nil, nil
}
value, err := buf.Slice(length)
if err != nil {
return nil, err
}
_, err = buf.Seek(int64(free), 1)
return value, err
}
func countZipmapItems(buf *sliceBuffer) (int, error) {
n := 0
for {
strLen, free, err := readZipmapItemLength(buf, n%2 != 0)
if err != nil {
return 0, err
}
if strLen == -1 {
break
}
_, err = buf.Seek(int64(strLen)+int64(free), 1)
if err != nil {
return 0, err
}
n++
}
_, err := buf.Seek(0, 0)
return n, err
}
func readZipmapItemLength(buf *sliceBuffer, readFree bool) (int, int, error) {
b, err := buf.ReadByte()
if err != nil {
return 0, 0, err
}
switch b {
case 253:
s, err := buf.Slice(5)
if err != nil {
return 0, 0, err
}
return int(binary.BigEndian.Uint32(s)), int(s[4]), nil
case 254:
return 0, 0, fmt.Errorf("rdb: invalid zipmap item length")
case 255:
return -1, 0, nil
}
var free byte
if readFree {
free, err = buf.ReadByte()
}
return int(b), int(free), err
}
func (d *decode) readListPack() error {
listpack, err := d.readString()
//fmt.Println(len(listpack))
//fmt.Println(listpack)
if err != nil {
return errors.Trace(err)
}
buf := newSliceBuffer(listpack)
buf.Slice(4) // total bytes
numElements, _ := buf.Slice(2)
num := int64(binary.LittleEndian.Uint16(numElements))
if err != nil {
return errors.Trace(err)
}
for {
num--
b, _ := buf.Slice(1)
if b[0] == byte(rdbLpEOF) {
fmt.Println("eof")
break
}
//lpGet(b, buf)
}
return nil
}
func lpGet(b []byte, buf *sliceBuffer) {
var val int64
var uval, negstart, negmax uint64
fmt.Println(b[0], lpEncodingIs7BitUint(b[0]))
if lpEncodingIs7BitUint(b[0]) {
fmt.Println("lpEncodingIs7BitUint")
negstart = math.MaxUint64
negmax = 0
uval = uint64(b[0] & 0x7F)
} else if lpEncodingIs6BitStr(b[0]) {
fmt.Println("lpEncodingIs6BitStr")
len := lpEncoding6BitStrLen(b)
str, _ := buf.Slice(int(len))
fmt.Print(string(str))
} else if lpEncodingIs13BitInt(b[0]) {
fmt.Println("lpEncodingIs13BitInt")
tmp, _ := buf.Slice(1)
b = append(b, tmp...)
uval = (uint64(b[0]&0x1f) << 8) | uint64(b[1])
negstart = uint64(1) << 12
negmax = 8191
} else if lpEncodingIs16BitInt(b[0]) {
fmt.Println("lpEncodingIs16BitInt")
tmp, _ := buf.Slice(2)
b = append(b, tmp...)
uval = uint64(b[1]) |
uint64(b[2])<<8
negstart = uint64(1) << 15
negmax = math.MaxUint16
} else if lpEncodingIs24BitInt(b[0]) {
fmt.Println("lpEncodingIs24BitInt")
tmp, _ := buf.Slice(3)
b = append(b, tmp...)
uval = uint64(b[1]) |
uint64(b[2])<<8 |
uint64(b[3])<<16
negstart = uint64(1) << 23
negmax = math.MaxUint32 >> 8
} else if lpEncodingIs32BitInt(b[0]) {
fmt.Println("lpEncodingIs32BitInt")
tmp, _ := buf.Slice(4)
b = append(b, tmp...)
uval = uint64(b[1]) |
uint64(b[2])<<8 |
uint64(b[3])<<16 |
uint64(b[4])<<24
negstart = uint64(1) << 31
negmax = math.MaxUint32
} else if lpEncodingIs64BitInt(b[0]) {
fmt.Println("lpEncodingIs64BitInt")
tmp, _ := buf.Slice(8)
b = append(b, tmp...)
uval = uint64(b[1]) |
uint64(b[2])<<8 |
uint64(b[3])<<16 |
uint64(b[4])<<24 |
uint64(b[5])<<32 |
uint64(b[6])<<40 |
uint64(b[7])<<48 |
uint64(b[8])<<56
negstart = uint64(1) << 63
negmax = math.MaxUint64
} else if lpEncodingIs12BitStr(b[0]) {
fmt.Println("lpEncodingIs12BitStr")
tmp, _ := buf.Slice(1)
b = append(b, tmp...)
len := lpEncoding12BitStrLen(b)
fmt.Println(len)
str, _ := buf.Slice(int(len))
fmt.Print(string(str))
} else if lpEncodingIs32BitStr(b[0]) {
fmt.Println("lpEncodingIs32BitStr")
tmp, _ := buf.Slice(4)
b = append(b, tmp...)
len := lpEncoding32BitStrLen(b)
str, _ := buf.Slice(int(len))
fmt.Print(string(str))
} else {
fmt.Println("else")
uval = uint64(12345678900000000) + uint64(b[0])
negstart = math.MaxUint64
negmax = 0
}
if uval >= negstart {
uval = negmax - uval
val = int64(uval)
val = -val - 1
} else {
val = int64(uval)
}
fmt.Printf("%d\n", val)
}
func lpEncodingIs7BitUint(b byte) bool {
return (((b) & rdbLpEncoding7BitUintMask) == rdbLpEncoding7BitUint)
}
func lpEncodingIs6BitStr(b byte) bool {
return (((b) & rdbLpEncoding6BitStrMask) == rdbLpEncoding6BitStr)
}
func lpEncodingIs13BitInt(b byte) bool {
return (((b) & rdbLpEncoding13BitIntMask) == rdbLpEncoding13BitInt)
}
func lpEncodingIs12BitStr(b byte) bool {
return (((b) & rdbLpEncoding12BitStrMask) == rdbLpEncoding12BitStr)
}
func lpEncodingIs16BitInt(b byte) bool {
return (((b) & rdbLpEncoding16BitIntMask) == rdbLpEncoding16BitInt)
}
func lpEncodingIs24BitInt(b byte) bool {
return (((b) & rdbLpEncoding24BitIntMask) == rdbLpEncoding24BitInt)
}
func lpEncodingIs32BitInt(b byte) bool {
return (((b) & rdbLpEncoding32BitIntMask) == rdbLpEncoding32BitInt)
}
func lpEncodingIs64BitInt(b byte) bool {
return (((b) & rdbLpEncoding64BitIntMask) == rdbLpEncoding64BitInt)
}
func lpEncodingIs32BitStr(b byte) bool {
return (((b) & rdbLpEncoding32BitStrMask) == rdbLpEncoding32BitStr)
}
func lpEncoding6BitStrLen(b []byte) uint32 {
return uint32(b[0] & 0x3F)
}
func lpEncoding12BitStrLen(b []byte) uint32 {
return (uint32((b)[0]&0xF) << 8) | uint32((b)[1])
}
func lpEncoding32BitStrLen(b []byte) uint32 {
return (uint32(b[1]) << 0) |
(uint32(b[2]) << 8) |
(uint32(b[3]) << 16) |
(uint32(b[4]) << 24)
}
func (d *decode) readZiplist(key []byte, expiry int64, addListEvents bool) error {
ziplist, err := d.readString()
if err != nil {
return errors.Trace(err)
}
buf := newSliceBuffer(ziplist)
length, err := readZiplistLength(buf)
if err != nil {
return errors.Trace(err)
}
if addListEvents {
d.info.Encoding = "ziplist"
d.info.SizeOfValue = len(ziplist)
d.event.StartList(key, length, expiry, d.info)
}
for i := int64(0); i < length; i++ {
entry, err := readZiplistEntry(buf)
if err != nil {
return errors.Trace(err)
}
d.event.Rpush(key, entry)
}
if addListEvents {
d.event.EndList(key)
}
return nil
}
func (d *decode) readZiplistZset(key []byte, expiry int64) error {
ziplist, err := d.readString()
if err != nil {
return errors.Trace(err)
}
buf := newSliceBuffer(ziplist)
cardinality, err := readZiplistLength(buf)
if err != nil {
return errors.Trace(err)
}
cardinality /= 2
d.info.Encoding = "ziplist"
d.info.SizeOfValue = len(ziplist)
d.event.StartZSet(key, cardinality, expiry, d.info)
for i := int64(0); i < cardinality; i++ {
member, err := readZiplistEntry(buf)
if err != nil {
return errors.Trace(err)
}
scoreBytes, err := readZiplistEntry(buf)
if err != nil {
return errors.Trace(err)
}
score, err := strconv.ParseFloat(string(scoreBytes), 64)
if err != nil {
return errors.Trace(err)
}
d.event.Zadd(key, score, member)
}
d.event.EndZSet(key)
return nil
}
func (d *decode) readZiplistHash(key []byte, expiry int64) error {
ziplist, err := d.readString()
if err != nil {
return errors.Trace(err)
}
buf := newSliceBuffer(ziplist)
length, err := readZiplistLength(buf)
if err != nil {
return errors.Trace(err)
}
length /= 2
d.info.Encoding = "ziplist"
d.info.SizeOfValue = len(ziplist)
d.event.StartHash(key, length, expiry, d.info)
for i := int64(0); i < length; i++ {
field, err := readZiplistEntry(buf)
if err != nil {
return errors.Trace(err)
}
value, err := readZiplistEntry(buf)
if err != nil {
return errors.Trace(err)
}
d.event.Hset(key, field, value)
}
d.event.EndHash(key)
return nil
}
func readZiplistLength(buf *sliceBuffer) (int64, error) {
buf.Seek(8, 0) // skip the zlbytes and zltail
lenBytes, err := buf.Slice(2)
if err != nil {
return 0, err
}