|
| 1 | +package leveldb |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "io" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/pkg/errors" |
| 10 | + |
| 11 | + "github.com/Mrs4s/go-cqhttp/global" |
| 12 | +) |
| 13 | + |
| 14 | +type intReader struct { |
| 15 | + data string |
| 16 | + *strings.Reader |
| 17 | +} |
| 18 | + |
| 19 | +func newIntReader(s string) intReader { |
| 20 | + return intReader{ |
| 21 | + data: s, |
| 22 | + Reader: strings.NewReader(s), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (r *intReader) varint() int64 { |
| 27 | + i, _ := binary.ReadVarint(r) |
| 28 | + return i |
| 29 | +} |
| 30 | + |
| 31 | +func (r *intReader) uvarint() uint64 { |
| 32 | + i, _ := binary.ReadUvarint(r) |
| 33 | + return i |
| 34 | +} |
| 35 | + |
| 36 | +// reader implements the index read. |
| 37 | +// data format is the same as the writer's |
| 38 | +type reader struct { |
| 39 | + data intReader |
| 40 | + strings intReader |
| 41 | + stringIndex map[uint64]string |
| 42 | +} |
| 43 | + |
| 44 | +func (r *reader) coder() coder { o, _ := r.data.ReadByte(); return coder(o) } |
| 45 | +func (r *reader) varint() int64 { return r.data.varint() } |
| 46 | +func (r *reader) uvarint() uint64 { return r.data.uvarint() } |
| 47 | +func (r *reader) int32() int32 { return int32(r.varint()) } |
| 48 | +func (r *reader) int64() int64 { return r.varint() } |
| 49 | +func (r *reader) uint64() uint64 { return r.uvarint() } |
| 50 | + |
| 51 | +// func (r *reader) uint32() uint32 { return uint32(r.uvarint()) } |
| 52 | +// func (r *reader) int() int { return int(r.varint()) } |
| 53 | +// func (r *reader) uint() uint { return uint(r.uvarint()) } |
| 54 | + |
| 55 | +func (r *reader) string() string { |
| 56 | + off := r.data.uvarint() |
| 57 | + if s, ok := r.stringIndex[off]; ok { |
| 58 | + return s |
| 59 | + } |
| 60 | + _, _ = r.strings.Seek(int64(off), io.SeekStart) |
| 61 | + l := int64(r.strings.uvarint()) |
| 62 | + whence, _ := r.strings.Seek(0, io.SeekCurrent) |
| 63 | + s := r.strings.data[whence : whence+l] |
| 64 | + r.stringIndex[off] = s |
| 65 | + return s |
| 66 | +} |
| 67 | + |
| 68 | +func (r *reader) msg() global.MSG { |
| 69 | + length := r.uvarint() |
| 70 | + msg := make(global.MSG, length) |
| 71 | + for i := uint64(0); i < length; i++ { |
| 72 | + s := r.string() |
| 73 | + msg[s] = r.obj() |
| 74 | + } |
| 75 | + return msg |
| 76 | +} |
| 77 | + |
| 78 | +func (r *reader) arrayMsg() []global.MSG { |
| 79 | + length := r.uvarint() |
| 80 | + msgs := make([]global.MSG, length) |
| 81 | + for i := range msgs { |
| 82 | + msgs[i] = r.msg() |
| 83 | + } |
| 84 | + return msgs |
| 85 | +} |
| 86 | + |
| 87 | +func (r *reader) obj() interface{} { |
| 88 | + switch coder := r.coder(); coder { |
| 89 | + case coderNil: |
| 90 | + return nil |
| 91 | + case coderInt: |
| 92 | + return int(r.varint()) |
| 93 | + case coderUint: |
| 94 | + return uint(r.uvarint()) |
| 95 | + case coderInt32: |
| 96 | + return int32(r.varint()) |
| 97 | + case coderUint32: |
| 98 | + return uint32(r.uvarint()) |
| 99 | + case coderInt64: |
| 100 | + return r.varint() |
| 101 | + case coderUint64: |
| 102 | + return r.uvarint() |
| 103 | + case coderString: |
| 104 | + return r.string() |
| 105 | + case coderMSG: |
| 106 | + return r.msg() |
| 107 | + case coderArrayMSG: |
| 108 | + return r.arrayMsg() |
| 109 | + default: |
| 110 | + panic("db/leveldb: invalid coder " + strconv.Itoa(int(coder))) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func newReader(data string) (*reader, error) { |
| 115 | + in := newIntReader(data) |
| 116 | + v := in.uvarint() |
| 117 | + if v != dataVersion { |
| 118 | + return nil, errors.Errorf("db/leveldb: invalid data version %d", v) |
| 119 | + } |
| 120 | + sl := int64(in.uvarint()) |
| 121 | + dl := int64(in.uvarint()) |
| 122 | + whence, _ := in.Seek(0, io.SeekCurrent) |
| 123 | + sData := data[whence : whence+sl] |
| 124 | + dData := data[whence+sl : whence+sl+dl] |
| 125 | + r := reader{ |
| 126 | + data: newIntReader(dData), |
| 127 | + strings: newIntReader(sData), |
| 128 | + stringIndex: make(map[uint64]string), |
| 129 | + } |
| 130 | + return &r, nil |
| 131 | +} |
0 commit comments