forked from BrotherGao/RDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
1079 lines (1007 loc) · 24.9 KB
/
decoder.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 decode implements parsing of the Redis RDB file format.
/*
1.modified Decoder interface func StartHash StartSet StartList StartZSet(add paras encodetype and sizezip)
2.const Version = 6 from cupcake rdb/encoder.go
3.ZSET version 2 with doubles stored in binary.(not used)
4.modified read func readZipmap readZiplist readIntset readZiplistZset readZiplistHash(add paras encodetype)
5.skip module and stream parse
*/
package rdb
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"strconv"
"github.com/yarthur1/rdb-tool/crc64"
)
//from cupcake rdb/encoder.go
const Version = 6
// 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()
// 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 uint64)
// Set is called once for each string key.
Set(key, value []byte, expiry int64)
// StartHash is called at the beginning of a hash.
// Hset will be called exactly length times before EndHash.
StartHash(key []byte, length, expiry int64, encodeType ValueType, sizeZip uint64) //modified
// 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, encodeType ValueType, sizeZip uint64) //modified
// 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)
// 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, encodeType ValueType, sizeZip uint64) //modified
// 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, encodeType ValueType, sizeZip uint64) //modified
// 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 { //decode entrance
decoder := &decode{d, make([]byte, 8), bufio.NewReader(r), 0}
return decoder.decode()
}
// Decode 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 err
}
decoder := &decode{d, make([]byte, 8), bytes.NewReader(dump[1:]), 0}
decoder.event.StartRDB()
decoder.event.StartDatabase(db)
err = decoder.readObject(key, ValueType(dump[0]), expiry)
decoder.event.EndDatabase(db)
decoder.event.EndRDB()
return err
}
type byteReader interface {
io.Reader
io.ByteReader
}
type decode struct {
event Decoder
intBuf []byte
r byteReader
rdbVersion int
}
type ValueType byte
const (
TypeString ValueType = 0
TypeList ValueType = 1
TypeSet ValueType = 2
TypeZSet ValueType = 3
TypeHash ValueType = 4
TypeZSet2 ValueType = 5 // ZSET version 2 with doubles stored in binary.
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 (
rdb6bitLen = 0
rdb14bitLen = 1
rdb32bitLen = 0x80
rdb64bitLen = 0x81
rdbEncVal = 3
rdbFlagModuleAux= 0xf7
rdbFlagIdle = 0xf8
rdbFlagFreq = 0xf9
rdbFlagAux = 0xfa
rdbFlagResizeDB = 0xfb
rdbFlagExpiryMS = 0xfc
rdbFlagExpiry = 0xfd
rdbFlagSelectDB = 0xfe
rdbFlagEOF = 0xff
rdbEncInt8 = 0
rdbEncInt16 = 1
rdbEncInt32 = 2
rdbEncLZF = 3
// reference https://github.com/sripathikrishnan/redis-rdb-tools/blob/548b11ec3c81a603f5b321228d07a61a0b940159/rdbtools/parser.py#L61
rdbModuleEOF = 0 // End of module value.
rdbModuleSInt = 1
rdbModuleUInt = 2
rdbModuleFloat = 3
rdbModuleDouble = 4
rdbModuleString = 5
rdbZiplist6bitlenString = 0
rdbZiplist14bitlenString = 1
rdbZiplist32bitlenString = 2
rdbZiplistInt16 = 0xc0
rdbZiplistInt32 = 0xd0
rdbZiplistInt64 = 0xe0
rdbZiplistInt24 = 0xf0
rdbZiplistInt8 = 0xfe
rdbZiplistInt4 = 15
)
func (d *decode) decode() error {
err := d.checkHeader()
if err != nil {
return err
}
d.event.StartRDB()
var db uint64
var expiry int64
firstDB := true
for {
objType, err := d.r.ReadByte()
if err != nil {
return err
}
switch objType {
case rdbFlagAux:
auxKey, err := d.readString()
if err != nil {
return err
}
auxVal, err := d.readString()
if err != nil {
return err
}
d.event.Aux(auxKey, auxVal)
case rdbFlagResizeDB:
dbSize, _, err := d.readLength()
if err != nil {
return err
}
expiresSize, _, err := d.readLength()
if err != nil {
return err
}
d.event.ResizeDatabase(dbSize, expiresSize)
case rdbFlagExpiryMS:
_, err := io.ReadFull(d.r, d.intBuf)
if err != nil {
return err
}
expiry = int64(binary.LittleEndian.Uint64(d.intBuf))
case rdbFlagExpiry:
_, err := io.ReadFull(d.r, d.intBuf[:4])
if err != nil {
return err
}
expiry = int64(binary.LittleEndian.Uint32(d.intBuf)) * 1000
case rdbFlagIdle:
_, _, err := d.readLength()
if err != nil {
return err
}
case rdbFlagFreq:
_, err := d.r.ReadByte()
if err != nil {
return err
}
case rdbFlagSelectDB:
if !firstDB {
d.event.EndDatabase(int(db))
}
db, _, err = d.readLength()
if err != nil {
return err
}
d.event.StartDatabase(int(db))
firstDB = false
case rdbFlagModuleAux:
d.skipModule()
case rdbFlagEOF:
d.event.EndDatabase(int(db))
d.event.EndRDB()
if d.rdbVersion >= 5{
d.skipNBytes(8)
}
return nil
default:
key, err := d.readString()
if err != nil {
return err
}
err = d.readObject(key, ValueType(objType), expiry)
if err != nil {
return err
}
expiry = 0 // no expiry
}
}
panic("not reached")
}
// ValueType https://github.com/redis/redis/blob/265341b577dadcbdd40343bdf32d7dcb0a4bc1d1/src/rdb.c#L625
func (d *decode) readObject(key []byte, typ ValueType, expiry int64) error {
switch typ {
case TypeString:
value, err := d.readString()
if err != nil {
return err
}
d.event.Set(key, value, expiry)
case TypeList: // encoding linkedlist
length, _, err := d.readLength()
if err != nil {
return err
}
d.event.StartList(key, int64(length), expiry, typ, 0)
for i := uint64(0); i < length; i++ {
value, err := d.readString()
if err != nil {
return err
}
d.event.Rpush(key, value)
}
d.event.EndList(key)
case TypeListQuicklist:
length, _, err := d.readLength()
if err != nil {
return err
}
d.event.StartList(key, int64(-1), expiry, typ, 0)
for i := uint64(0); i < length; i++ {
d.readZiplist(key, 0, false, typ)
}
d.event.EndList(key)
case TypeSet:
cardinality, _, err := d.readLength()
if err != nil {
return err
}
d.event.StartSet(key, int64(cardinality), expiry, typ, 0)
for i := uint64(0); i < cardinality; i++ {
member, err := d.readString()
if err != nil {
return err
}
d.event.Sadd(key, member)
}
d.event.EndSet(key)
case TypeZSet, TypeZSet2: // encoding skiplist
cardinality, _, err := d.readLength()
if err != nil {
return err
}
d.event.StartZSet(key, int64(cardinality), expiry, typ, 0)
for i := uint64(0); i < cardinality; i++ {
member, err := d.readString()
if err != nil {
return err
}
var score float64
if typ == TypeZSet2 {
if score, err = d.readBinaryFloat64(); err != nil {
return err
}
} else {
if score, err = d.readFloat64(); err != nil {
return err
}
}
d.event.Zadd(key, score, member)
}
d.event.EndZSet(key)
case TypeHash: // encoding hashtable
length, _, err := d.readLength()
if err != nil {
return err
}
d.event.StartHash(key, int64(length), expiry, typ, 0)
for i := uint64(0); i < length; i++ {
field, err := d.readString()
if err != nil {
return err
}
value, err := d.readString()
if err != nil {
return err
}
d.event.Hset(key, field, value)
}
d.event.EndHash(key)
case TypeHashZipmap:
return d.readZipmap(key, expiry, typ)
case TypeListZiplist:
return d.readZiplist(key, expiry, true, typ)
case TypeSetIntset:
return d.readIntset(key, expiry, typ)
case TypeZSetZiplist:
return d.readZiplistZset(key, expiry, typ)
case TypeHashZiplist:
return d.readZiplistHash(key, expiry, typ)
case TypeModule2:
return d.skipModule()
case TypeStreamListPacks:
return d.skipStream()
case TypeModule:
return fmt.Errorf("rdb: Unable to read Redis Modules RDB objects (key %s)", key)
default:
return fmt.Errorf("rdb: unknown object type %d for key %s", typ, key)
}
return nil
}
func (d *decode) readZipmap(key []byte, expiry int64, typ ValueType) error {
var length int
zipmap, err := d.readString()
if err != nil {
return err
}
buf := newSliceBuffer(zipmap)
lenByte, err := buf.ReadByte()
if err != nil {
return err
}
if lenByte >= 254 { // we need to count the items manually
length, err = countZipmapItems(buf)
length /= 2
if err != nil {
return err
}
} else {
length = int(lenByte)
}
d.event.StartHash(key, int64(length), expiry, typ, uint64(len(zipmap)))
for i := 0; i < length; i++ {
field, err := readZipmapItem(buf, false)
if err != nil {
return err
}
value, err := readZipmapItem(buf, true)
if err != nil {
return 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) readZiplist(key []byte, expiry int64, addListEvents bool, typ ValueType) error {
ziplist, err := d.readString()
if err != nil {
return err
}
buf := newSliceBuffer(ziplist)
length, err := readZiplistLength(buf)
if err != nil {
return err
}
if addListEvents {
d.event.StartList(key, length, expiry, typ, uint64(len(ziplist)))
}
for i := int64(0); i < length; i++ {
entry, err := readZiplistEntry(buf)
if err != nil {
return err
}
d.event.Rpush(key, entry)
}
if addListEvents {
d.event.EndList(key)
}
return nil
}
func (d *decode) readZiplistZset(key []byte, expiry int64, typ ValueType) error {
ziplist, err := d.readString()
if err != nil {
return err
}
buf := newSliceBuffer(ziplist)
cardinality, err := readZiplistLength(buf)
if err != nil {
return err
}
cardinality /= 2
d.event.StartZSet(key, cardinality, expiry, typ, uint64(len(ziplist)))
for i := int64(0); i < cardinality; i++ {
member, err := readZiplistEntry(buf)
if err != nil {
return err
}
scoreBytes, err := readZiplistEntry(buf)
if err != nil {
return err
}
score, err := strconv.ParseFloat(string(scoreBytes), 64)
if err != nil {
return err
}
d.event.Zadd(key, score, member)
}
d.event.EndZSet(key)
return nil
}
func (d *decode) readZiplistHash(key []byte, expiry int64, typ ValueType) error {
ziplist, err := d.readString()
if err != nil {
return err
}
buf := newSliceBuffer(ziplist)
length, err := readZiplistLength(buf)
if err != nil {
return err
}
length /= 2
d.event.StartHash(key, length, expiry, typ, uint64(len(ziplist)))
for i := int64(0); i < length; i++ {
field, err := readZiplistEntry(buf)
if err != nil {
return err
}
value, err := readZiplistEntry(buf)
if err != nil {
return 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
}
return int64(binary.LittleEndian.Uint16(lenBytes)), nil
}
func readZiplistEntry(buf *sliceBuffer) ([]byte, error) {
prevLen, err := buf.ReadByte()
if err != nil {
return nil, err
}
if prevLen == 254 {
buf.Seek(4, 1) // skip the 4-byte prevlen
}
header, err := buf.ReadByte()
if err != nil {
return nil, err
}
switch {
case header>>6 == rdbZiplist6bitlenString:
return buf.Slice(int(header & 0x3f))
case header>>6 == rdbZiplist14bitlenString:
b, err := buf.ReadByte()
if err != nil {
return nil, err
}
return buf.Slice((int(header&0x3f) << 8) | int(b))
case header>>6 == rdbZiplist32bitlenString:
lenBytes, err := buf.Slice(4)
if err != nil {
return nil, err
}
return buf.Slice(int(binary.BigEndian.Uint32(lenBytes)))
case header == rdbZiplistInt16:
intBytes, err := buf.Slice(2)
if err != nil {
return nil, err
}
return []byte(strconv.FormatInt(int64(int16(binary.LittleEndian.Uint16(intBytes))), 10)), nil
case header == rdbZiplistInt32:
intBytes, err := buf.Slice(4)
if err != nil {
return nil, err
}
return []byte(strconv.FormatInt(int64(int32(binary.LittleEndian.Uint32(intBytes))), 10)), nil
case header == rdbZiplistInt64:
intBytes, err := buf.Slice(8)
if err != nil {
return nil, err
}
return []byte(strconv.FormatInt(int64(binary.LittleEndian.Uint64(intBytes)), 10)), nil
case header == rdbZiplistInt24:
intBytes := make([]byte, 4)
_, err := buf.Read(intBytes[1:])
if err != nil {
return nil, err
}
return []byte(strconv.FormatInt(int64(int32(binary.LittleEndian.Uint32(intBytes))>>8), 10)), nil
case header == rdbZiplistInt8:
b, err := buf.ReadByte()
return []byte(strconv.FormatInt(int64(int8(b)), 10)), err
case header>>4 == rdbZiplistInt4:
return []byte(strconv.FormatInt(int64(header&0x0f)-1, 10)), nil
}
return nil, fmt.Errorf("rdb: unknown ziplist header byte: %d", header)
}
func (d *decode) readIntset(key []byte, expiry int64, typ ValueType) error {
intset, err := d.readString()
if err != nil {
return err
}
buf := newSliceBuffer(intset)
intSizeBytes, err := buf.Slice(4)
if err != nil {
return err
}
intSize := binary.LittleEndian.Uint32(intSizeBytes)
if intSize != 2 && intSize != 4 && intSize != 8 {
return fmt.Errorf("rdb: unknown intset encoding: %d", intSize)
}
lenBytes, err := buf.Slice(4)
if err != nil {
return err
}
cardinality := binary.LittleEndian.Uint32(lenBytes)
d.event.StartSet(key, int64(cardinality), expiry, typ, uint64(len(intset)))
for i := uint32(0); i < cardinality; i++ {
intBytes, err := buf.Slice(int(intSize))
if err != nil {
return err
}
var intString string
switch intSize {
case 2:
intString = strconv.FormatInt(int64(int16(binary.LittleEndian.Uint16(intBytes))), 10)
case 4:
intString = strconv.FormatInt(int64(int32(binary.LittleEndian.Uint32(intBytes))), 10)
case 8:
intString = strconv.FormatInt(int64(int64(binary.LittleEndian.Uint64(intBytes))), 10)
}
d.event.Sadd(key, []byte(intString))
}
d.event.EndSet(key)
return nil
}
func (d *decode) skipModule() error {
var err error
var lcode uint64
_, _, err = d.readLength()
if err != nil {
return err
}
lcode, _, err = d.readLength()
if err != nil {
return err
}
for lcode != rdbModuleEOF {
switch lcode {
case rdbModuleSInt,rdbModuleUInt :
_, _, err = d.readLength()
if err != nil {
return err
}
case rdbModuleFloat:
err = d.skipBinaryFloat32()
if err != nil {
return err
}
case rdbModuleDouble:
err = d.skipBinaryDouble()
if err != nil {
return err
}
case rdbModuleString:
err = d.skipString()
if err != nil {
return err
}
default:
return fmt.Errorf("rdb: Unknown module:%d", lcode)
}
lcode, _, err = d.readLength()
if err != nil {
return err
}
}
return nil
}
func (d *decode) skipStream() error {
var err error
var l uint64
var lc uint64
var pending uint64
var consumers uint64
l, _, err = d.readLength()
if err != nil {
return err
}
for i:=uint64(0); i<l; i++ {
if err = d.skipString();err != nil { // err is function scope
return err
}
if err = d.skipString();err != nil {
return err
}
}
for i:=0; i<3; i++ {
if _, _, err = d.readLength(); err != nil {
return err
}
}
l, _, err = d.readLength()
if err != nil {
return err
}
for i:=uint64(0); i<l; i++ {
if err = d.skipString();err != nil {
return err
}
for j:=0; j<2; j++ {
if _, _, err = d.readLength(); err != nil {
return err
}
}
pending, _, err = d.readLength()
if err != nil {
return err
}
for k:=uint64(0); k< pending; k++ {
if err=d.skipNBytes(24); err != nil {
return err
}
if _, _, err=d.readLength(); err != nil {
return err
}
}
consumers, _, err = d.readLength()
if err != nil {
return err
}
for k:=uint64(0); k< consumers; k++ {
if err=d.skipString(); err != nil {
return err
}
if err=d.skipNBytes(8); err != nil {
return err
}
lc, _, err=d.readLength()
if err != nil {
return err
}
if err=d.skipNBytes(lc*16); err != nil {
return err
}
}
}
return nil
}
func (d *decode) checkHeader() error {
header := make([]byte, 9)
_, err := io.ReadFull(d.r, header)
if err != nil {
return err
}
if !bytes.Equal(header[:5], []byte("REDIS")) {
return fmt.Errorf("rdb: invalid file format")
}
version, _ := strconv.ParseInt(string(header[5:]), 10, 64)
if version < 1 || version > 9 {
return fmt.Errorf("rdb: invalid RDB version number %d", version)
}
d.rdbVersion = int(version)
return nil
}
func (d *decode) readString() ([]byte, error) {
length, encoded, err := d.readLength()
if err != nil {
return nil, err
}
if encoded {
switch length {
case rdbEncInt8:
i, err := d.readUint8()
return []byte(strconv.FormatInt(int64(int8(i)), 10)), err
case rdbEncInt16:
i, err := d.readUint16()
return []byte(strconv.FormatInt(int64(int16(i)), 10)), err
case rdbEncInt32:
i, err := d.readUint32()
return []byte(strconv.FormatInt(int64(int32(i)), 10)), err
case rdbEncLZF:
clen, _, err := d.readLength()
if err != nil {
return nil, err
}
ulen, _, err := d.readLength()
if err != nil {
return nil, err
}
compressed := make([]byte, clen)
_, err = io.ReadFull(d.r, compressed)
if err != nil {
return nil, err
}
decompressed := lzfDecompress(compressed, int(ulen))
if len(decompressed) != int(ulen) {
return nil, fmt.Errorf("decompressed string length %d didn't match expected length %d", len(decompressed), ulen)
}
return decompressed, nil
}
}
str := make([]byte, length)
_, err = io.ReadFull(d.r, str)
return str, err
}
func (d *decode) skipString() error {
length, encoded, err := d.readLength()
if err != nil {
return err
}
var skip uint64
if encoded {
switch length {
case rdbEncInt8:
skip = 1
case rdbEncInt16:
skip = 2
case rdbEncInt32:
skip = 4
case rdbEncLZF:
clen, _, err := d.readLength()
if err != nil {
return err
}
_, _, err = d.readLength()
if err != nil {
return err
}
skip = clen
}
} else {
skip = length
}
str := make([]byte, skip)
_, err = io.ReadFull(d.r, str)
return err
}
func (d *decode) skipNBytes(n uint64) error {
str := make([]byte, n)
_, err := io.ReadFull(d.r, str)
return err
}
func (d *decode) readUint8() (uint8, error) {
b, err := d.r.ReadByte()
return uint8(b), err
}
func (d *decode) readUint16() (uint16, error) {
_, err := io.ReadFull(d.r, d.intBuf[:2])
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint16(d.intBuf), nil
}
func (d *decode) readUint32() (uint32, error) {
_, err := io.ReadFull(d.r, d.intBuf[:4])
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(d.intBuf), nil
}
func (d *decode) readUint64() (uint64, error) {
_, err := io.ReadFull(d.r, d.intBuf)
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint64(d.intBuf), nil
}
func (d *decode) readUint32Big() (uint32, error) {
_, err := io.ReadFull(d.r, d.intBuf[:4])
if err != nil {
return 0, err
}
return binary.BigEndian.Uint32(d.intBuf), nil
}
func (d *decode) readUint64Big() (uint64, error) {
_, err := io.ReadFull(d.r, d.intBuf)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint64(d.intBuf), nil
}
// Doubles are saved as strings prefixed by an unsigned
// 8 bit integer specifying the length of the representation.
// This 8 bit integer has special values in order to specify the following
// conditions:
// 253: not a number
// 254: + inf
// 255: - inf
func (d *decode) readFloat64() (float64, error) {
length, err := d.readUint8()
if err != nil {
return 0, err
}
switch length {
case 253:
return math.NaN(), nil
case 254:
return math.Inf(0), nil
case 255:
return math.Inf(-1), nil
default:
floatBytes := make([]byte, length)
_, err := io.ReadFull(d.r, floatBytes)
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(string(floatBytes), 64)
return f, err
}
panic("not reached")
}
//add zset2
func (d *decode) readBinaryFloat64() (float64, error) {
if _, err := io.ReadFull(d.r, d.intBuf); err != nil {
return 0, err
}
bits := binary.LittleEndian.Uint64(d.intBuf)
return math.Float64frombits(bits), nil
}
func (d *decode) skipBinaryFloat32() error {
_, err := io.ReadFull(d.r, d.intBuf[:4])
if err != nil {
return err
}
return nil
}
func (d *decode) skipBinaryDouble() error {
_, err := io.ReadFull(d.r, d.intBuf[:8])
if err != nil {
return err
}
return nil
}
func (d *decode) readLength() (uint64, bool, error) {
b, err := d.r.ReadByte()
if err != nil {