Skip to content

Commit b01d98d

Browse files
Remove merkledb codec struct (#2883)
1 parent 10b881f commit b01d98d

File tree

4 files changed

+86
-126
lines changed

4 files changed

+86
-126
lines changed

x/merkledb/codec.go

Lines changed: 59 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
)
3737

3838
var (
39-
_ encoderDecoder = (*codecImpl)(nil)
40-
4139
trueBytes = []byte{trueByte}
4240
falseBytes = []byte{falseByte}
4341

@@ -49,131 +47,107 @@ var (
4947
errIntOverflow = errors.New("value overflows int")
5048
)
5149

52-
// encoderDecoder defines the interface needed by merkleDB to marshal
53-
// and unmarshal relevant types.
54-
type encoderDecoder interface {
55-
encoder
56-
decoder
57-
}
58-
59-
type encoder interface {
60-
// Assumes [n] is non-nil.
61-
encodeDBNode(n *dbNode) []byte
62-
encodedDBNodeSize(n *dbNode) int
63-
64-
// Returns the bytes that will be hashed to generate [n]'s ID.
65-
// Assumes [n] is non-nil.
66-
encodeHashValues(n *node) []byte
67-
encodeKey(key Key) []byte
68-
}
69-
70-
type decoder interface {
71-
// Assumes [n] is non-nil.
72-
decodeDBNode(bytes []byte, n *dbNode) error
73-
decodeKey(bytes []byte) (Key, error)
74-
}
75-
76-
func newCodec() encoderDecoder {
77-
return &codecImpl{}
78-
}
79-
80-
// Note that bytes.Buffer.Write always returns nil, so we
81-
// can ignore its return values in [codecImpl] methods.
82-
type codecImpl struct{}
50+
// Note that bytes.Buffer.Write always returns nil, so we ignore its return
51+
// values in all encode methods.
8352

84-
func (c *codecImpl) childSize(index byte, childEntry *child) int {
53+
func childSize(index byte, childEntry *child) int {
8554
// * index
8655
// * child ID
8756
// * child key
8857
// * bool indicating whether the child has a value
89-
return c.uintSize(uint64(index)) + ids.IDLen + c.keySize(childEntry.compressedKey) + boolLen
58+
return uintSize(uint64(index)) + ids.IDLen + keySize(childEntry.compressedKey) + boolLen
9059
}
9160

92-
// based on the current implementation of codecImpl.encodeUint which uses binary.PutUvarint
93-
func (*codecImpl) uintSize(value uint64) int {
61+
// based on the implementation of encodeUint which uses binary.PutUvarint
62+
func uintSize(value uint64) int {
9463
if value == 0 {
9564
return 1
9665
}
9766
return (bits.Len64(value) + 6) / 7
9867
}
9968

100-
func (c *codecImpl) keySize(p Key) int {
101-
return c.uintSize(uint64(p.length)) + bytesNeeded(p.length)
69+
func keySize(p Key) int {
70+
return uintSize(uint64(p.length)) + bytesNeeded(p.length)
10271
}
10372

104-
func (c *codecImpl) encodedDBNodeSize(n *dbNode) int {
73+
// Assumes [n] is non-nil.
74+
func encodedDBNodeSize(n *dbNode) int {
10575
// * number of children
10676
// * bool indicating whether [n] has a value
10777
// * the value (optional)
10878
// * children
109-
size := c.uintSize(uint64(len(n.children))) + boolLen
79+
size := uintSize(uint64(len(n.children))) + boolLen
11080
if n.value.HasValue() {
11181
valueLen := len(n.value.Value())
112-
size += c.uintSize(uint64(valueLen)) + valueLen
82+
size += uintSize(uint64(valueLen)) + valueLen
11383
}
11484
// for each non-nil entry, we add the additional size of the child entry
11585
for index, entry := range n.children {
116-
size += c.childSize(index, entry)
86+
size += childSize(index, entry)
11787
}
11888
return size
11989
}
12090

121-
func (c *codecImpl) encodeDBNode(n *dbNode) []byte {
122-
buf := bytes.NewBuffer(make([]byte, 0, c.encodedDBNodeSize(n)))
123-
c.encodeMaybeByteSlice(buf, n.value)
124-
c.encodeUint(buf, uint64(len(n.children)))
91+
// Assumes [n] is non-nil.
92+
func encodeDBNode(n *dbNode) []byte {
93+
buf := bytes.NewBuffer(make([]byte, 0, encodedDBNodeSize(n)))
94+
encodeMaybeByteSlice(buf, n.value)
95+
encodeUint(buf, uint64(len(n.children)))
12596
// Note we insert children in order of increasing index
12697
// for determinism.
12798
keys := maps.Keys(n.children)
12899
slices.Sort(keys)
129100
for _, index := range keys {
130101
entry := n.children[index]
131-
c.encodeUint(buf, uint64(index))
132-
c.encodeKeyToBuffer(buf, entry.compressedKey)
102+
encodeUint(buf, uint64(index))
103+
encodeKeyToBuffer(buf, entry.compressedKey)
133104
_, _ = buf.Write(entry.id[:])
134-
c.encodeBool(buf, entry.hasValue)
105+
encodeBool(buf, entry.hasValue)
135106
}
136107
return buf.Bytes()
137108
}
138109

139-
func (c *codecImpl) encodeHashValues(n *node) []byte {
110+
// Returns the bytes that will be hashed to generate [n]'s ID.
111+
// Assumes [n] is non-nil.
112+
func encodeHashValues(n *node) []byte {
140113
var (
141114
numChildren = len(n.children)
142115
// Estimate size [hv] to prevent memory allocations
143116
estimatedLen = minVarIntLen + numChildren*hashValuesChildLen + estimatedValueLen + estimatedKeyLen
144117
buf = bytes.NewBuffer(make([]byte, 0, estimatedLen))
145118
)
146119

147-
c.encodeUint(buf, uint64(numChildren))
120+
encodeUint(buf, uint64(numChildren))
148121

149122
// ensure that the order of entries is consistent
150123
keys := maps.Keys(n.children)
151124
slices.Sort(keys)
152125
for _, index := range keys {
153126
entry := n.children[index]
154-
c.encodeUint(buf, uint64(index))
127+
encodeUint(buf, uint64(index))
155128
_, _ = buf.Write(entry.id[:])
156129
}
157-
c.encodeMaybeByteSlice(buf, n.valueDigest)
158-
c.encodeKeyToBuffer(buf, n.key)
130+
encodeMaybeByteSlice(buf, n.valueDigest)
131+
encodeKeyToBuffer(buf, n.key)
159132

160133
return buf.Bytes()
161134
}
162135

163-
func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
136+
// Assumes [n] is non-nil.
137+
func decodeDBNode(b []byte, n *dbNode) error {
164138
if minDBNodeLen > len(b) {
165139
return io.ErrUnexpectedEOF
166140
}
167141

168142
src := bytes.NewReader(b)
169143

170-
value, err := c.decodeMaybeByteSlice(src)
144+
value, err := decodeMaybeByteSlice(src)
171145
if err != nil {
172146
return err
173147
}
174148
n.value = value
175149

176-
numChildren, err := c.decodeUint(src)
150+
numChildren, err := decodeUint(src)
177151
switch {
178152
case err != nil:
179153
return err
@@ -184,7 +158,7 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
184158
n.children = make(map[byte]*child, numChildren)
185159
var previousChild uint64
186160
for i := uint64(0); i < numChildren; i++ {
187-
index, err := c.decodeUint(src)
161+
index, err := decodeUint(src)
188162
if err != nil {
189163
return err
190164
}
@@ -193,15 +167,15 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
193167
}
194168
previousChild = index
195169

196-
compressedKey, err := c.decodeKeyFromReader(src)
170+
compressedKey, err := decodeKeyFromReader(src)
197171
if err != nil {
198172
return err
199173
}
200-
childID, err := c.decodeID(src)
174+
childID, err := decodeID(src)
201175
if err != nil {
202176
return err
203177
}
204-
hasValue, err := c.decodeBool(src)
178+
hasValue, err := decodeBool(src)
205179
if err != nil {
206180
return err
207181
}
@@ -217,15 +191,15 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
217191
return nil
218192
}
219193

220-
func (*codecImpl) encodeBool(dst *bytes.Buffer, value bool) {
194+
func encodeBool(dst *bytes.Buffer, value bool) {
221195
bytesValue := falseBytes
222196
if value {
223197
bytesValue = trueBytes
224198
}
225199
_, _ = dst.Write(bytesValue)
226200
}
227201

228-
func (*codecImpl) decodeBool(src *bytes.Reader) (bool, error) {
202+
func decodeBool(src *bytes.Reader) (bool, error) {
229203
boolByte, err := src.ReadByte()
230204
switch {
231205
case err == io.EOF:
@@ -241,7 +215,7 @@ func (*codecImpl) decodeBool(src *bytes.Reader) (bool, error) {
241215
}
242216
}
243217

244-
func (*codecImpl) decodeUint(src *bytes.Reader) (uint64, error) {
218+
func decodeUint(src *bytes.Reader) (uint64, error) {
245219
// To ensure encoding/decoding is canonical, we need to check for leading
246220
// zeroes in the varint.
247221
// The last byte of the varint we read is the most significant byte.
@@ -274,43 +248,43 @@ func (*codecImpl) decodeUint(src *bytes.Reader) (uint64, error) {
274248
return val64, nil
275249
}
276250

277-
func (*codecImpl) encodeUint(dst *bytes.Buffer, value uint64) {
251+
func encodeUint(dst *bytes.Buffer, value uint64) {
278252
var buf [binary.MaxVarintLen64]byte
279253
size := binary.PutUvarint(buf[:], value)
280254
_, _ = dst.Write(buf[:size])
281255
}
282256

283-
func (c *codecImpl) encodeMaybeByteSlice(dst *bytes.Buffer, maybeValue maybe.Maybe[[]byte]) {
257+
func encodeMaybeByteSlice(dst *bytes.Buffer, maybeValue maybe.Maybe[[]byte]) {
284258
hasValue := maybeValue.HasValue()
285-
c.encodeBool(dst, hasValue)
259+
encodeBool(dst, hasValue)
286260
if hasValue {
287-
c.encodeByteSlice(dst, maybeValue.Value())
261+
encodeByteSlice(dst, maybeValue.Value())
288262
}
289263
}
290264

291-
func (c *codecImpl) decodeMaybeByteSlice(src *bytes.Reader) (maybe.Maybe[[]byte], error) {
265+
func decodeMaybeByteSlice(src *bytes.Reader) (maybe.Maybe[[]byte], error) {
292266
if minMaybeByteSliceLen > src.Len() {
293267
return maybe.Nothing[[]byte](), io.ErrUnexpectedEOF
294268
}
295269

296-
if hasValue, err := c.decodeBool(src); err != nil || !hasValue {
270+
if hasValue, err := decodeBool(src); err != nil || !hasValue {
297271
return maybe.Nothing[[]byte](), err
298272
}
299273

300-
rawBytes, err := c.decodeByteSlice(src)
274+
rawBytes, err := decodeByteSlice(src)
301275
if err != nil {
302276
return maybe.Nothing[[]byte](), err
303277
}
304278

305279
return maybe.Some(rawBytes), nil
306280
}
307281

308-
func (c *codecImpl) decodeByteSlice(src *bytes.Reader) ([]byte, error) {
282+
func decodeByteSlice(src *bytes.Reader) ([]byte, error) {
309283
if minByteSliceLen > src.Len() {
310284
return nil, io.ErrUnexpectedEOF
311285
}
312286

313-
length, err := c.decodeUint(src)
287+
length, err := decodeUint(src)
314288
switch {
315289
case err == io.EOF:
316290
return nil, io.ErrUnexpectedEOF
@@ -330,14 +304,14 @@ func (c *codecImpl) decodeByteSlice(src *bytes.Reader) ([]byte, error) {
330304
return result, err
331305
}
332306

333-
func (c *codecImpl) encodeByteSlice(dst *bytes.Buffer, value []byte) {
334-
c.encodeUint(dst, uint64(len(value)))
307+
func encodeByteSlice(dst *bytes.Buffer, value []byte) {
308+
encodeUint(dst, uint64(len(value)))
335309
if value != nil {
336310
_, _ = dst.Write(value)
337311
}
338312
}
339313

340-
func (*codecImpl) decodeID(src *bytes.Reader) (ids.ID, error) {
314+
func decodeID(src *bytes.Reader) (ids.ID, error) {
341315
if ids.IDLen > src.Len() {
342316
return ids.ID{}, io.ErrUnexpectedEOF
343317
}
@@ -350,21 +324,21 @@ func (*codecImpl) decodeID(src *bytes.Reader) (ids.ID, error) {
350324
return id, err
351325
}
352326

353-
func (c *codecImpl) encodeKey(key Key) []byte {
327+
func encodeKey(key Key) []byte {
354328
estimatedLen := binary.MaxVarintLen64 + len(key.Bytes())
355329
dst := bytes.NewBuffer(make([]byte, 0, estimatedLen))
356-
c.encodeKeyToBuffer(dst, key)
330+
encodeKeyToBuffer(dst, key)
357331
return dst.Bytes()
358332
}
359333

360-
func (c *codecImpl) encodeKeyToBuffer(dst *bytes.Buffer, key Key) {
361-
c.encodeUint(dst, uint64(key.length))
334+
func encodeKeyToBuffer(dst *bytes.Buffer, key Key) {
335+
encodeUint(dst, uint64(key.length))
362336
_, _ = dst.Write(key.Bytes())
363337
}
364338

365-
func (c *codecImpl) decodeKey(b []byte) (Key, error) {
339+
func decodeKey(b []byte) (Key, error) {
366340
src := bytes.NewReader(b)
367-
key, err := c.decodeKeyFromReader(src)
341+
key, err := decodeKeyFromReader(src)
368342
if err != nil {
369343
return Key{}, err
370344
}
@@ -374,12 +348,12 @@ func (c *codecImpl) decodeKey(b []byte) (Key, error) {
374348
return key, err
375349
}
376350

377-
func (c *codecImpl) decodeKeyFromReader(src *bytes.Reader) (Key, error) {
351+
func decodeKeyFromReader(src *bytes.Reader) (Key, error) {
378352
if minKeyLen > src.Len() {
379353
return Key{}, io.ErrUnexpectedEOF
380354
}
381355

382-
length, err := c.decodeUint(src)
356+
length, err := decodeUint(src)
383357
if err != nil {
384358
return Key{}, err
385359
}

0 commit comments

Comments
 (0)