MCPcopy Create free account
hub / github.com/HDT3213/rdb / readLength

Method readLength

core/string.go:41–79  ·  view source on GitHub ↗

readLength parse Length Encoding see: https://github.com/sripathikrishnan/redis-rdb-tools/wiki/Redis-RDB-Dump-File-Format#length-encoding

()

Source from the content-addressed store, hash-verified

39// readLength parse Length Encoding
40// see: https://github.com/sripathikrishnan/redis-rdb-tools/wiki/Redis-RDB-Dump-File-Format#length-encoding
41func (dec *Decoder) readLength() (uint64, bool, error) {
42 firstByte, err := dec.readByte()
43 if err != nil {
44 return 0, false, fmt.Errorf("read length failed: %v", err)
45 }
46 lenType := (firstByte & 0xc0) >> 6 // get first 2 bits
47 var length uint64
48 special := false
49 switch lenType {
50 case len6Bit:
51 length = uint64(firstByte) & 0x3f
52 case len14Bit:
53 nextByte, err := dec.readByte()
54 if err != nil {
55 return 0, false, fmt.Errorf("read len14Bit failed: %v", err)
56 }
57 length = (uint64(firstByte)&0x3f)<<8 | uint64(nextByte)
58 case len32or64Bit:
59 if firstByte == len32Bit {
60 err = dec.readFull(dec.buffer[0:4])
61 if err != nil {
62 return 0, false, fmt.Errorf("read len32Bit failed: %v", err)
63 }
64 length = uint64(binary.BigEndian.Uint32(dec.buffer))
65 } else if firstByte == len64Bit {
66 err = dec.readFull(dec.buffer)
67 if err != nil {
68 return 0, false, fmt.Errorf("read len64Bit failed: %v", err)
69 }
70 length = binary.BigEndian.Uint64(dec.buffer)
71 } else {
72 return 0, false, fmt.Errorf("illegal length encoding: %x", firstByte)
73 }
74 case lenSpecial:
75 special = true
76 length = uint64(firstByte) & 0x3f
77 }
78 return length, special, nil
79}
80
81func (dec *Decoder) readString() ([]byte, error) {
82 length, special, err := dec.readLength()

Callers 15

parseMethod · 0.95
readSetMethod · 0.95
readHashMapMethod · 0.95
readHashMapExMethod · 0.95
readHashMapExValkeyMethod · 0.95
TestLengthEncodingFunction · 0.95
readStringMethod · 0.95
readLZFMethod · 0.95
readZSetMethod · 0.95
readStreamListPacksMethod · 0.95
readStreamIdMethod · 0.95
readStreamEntriesMethod · 0.95

Calls 2

readByteMethod · 0.95
readFullMethod · 0.95

Tested by 1

TestLengthEncodingFunction · 0.76