@@ -36,8 +36,6 @@ const (
36
36
)
37
37
38
38
var (
39
- _ encoderDecoder = (* codecImpl )(nil )
40
-
41
39
trueBytes = []byte {trueByte }
42
40
falseBytes = []byte {falseByte }
43
41
@@ -49,131 +47,107 @@ var (
49
47
errIntOverflow = errors .New ("value overflows int" )
50
48
)
51
49
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.
83
52
84
- func ( c * codecImpl ) childSize (index byte , childEntry * child ) int {
53
+ func childSize (index byte , childEntry * child ) int {
85
54
// * index
86
55
// * child ID
87
56
// * child key
88
57
// * 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
90
59
}
91
60
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 {
94
63
if value == 0 {
95
64
return 1
96
65
}
97
66
return (bits .Len64 (value ) + 6 ) / 7
98
67
}
99
68
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 )
102
71
}
103
72
104
- func (c * codecImpl ) encodedDBNodeSize (n * dbNode ) int {
73
+ // Assumes [n] is non-nil.
74
+ func encodedDBNodeSize (n * dbNode ) int {
105
75
// * number of children
106
76
// * bool indicating whether [n] has a value
107
77
// * the value (optional)
108
78
// * children
109
- size := c . uintSize (uint64 (len (n .children ))) + boolLen
79
+ size := uintSize (uint64 (len (n .children ))) + boolLen
110
80
if n .value .HasValue () {
111
81
valueLen := len (n .value .Value ())
112
- size += c . uintSize (uint64 (valueLen )) + valueLen
82
+ size += uintSize (uint64 (valueLen )) + valueLen
113
83
}
114
84
// for each non-nil entry, we add the additional size of the child entry
115
85
for index , entry := range n .children {
116
- size += c . childSize (index , entry )
86
+ size += childSize (index , entry )
117
87
}
118
88
return size
119
89
}
120
90
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 )))
125
96
// Note we insert children in order of increasing index
126
97
// for determinism.
127
98
keys := maps .Keys (n .children )
128
99
slices .Sort (keys )
129
100
for _ , index := range keys {
130
101
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 )
133
104
_ , _ = buf .Write (entry .id [:])
134
- c . encodeBool (buf , entry .hasValue )
105
+ encodeBool (buf , entry .hasValue )
135
106
}
136
107
return buf .Bytes ()
137
108
}
138
109
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 {
140
113
var (
141
114
numChildren = len (n .children )
142
115
// Estimate size [hv] to prevent memory allocations
143
116
estimatedLen = minVarIntLen + numChildren * hashValuesChildLen + estimatedValueLen + estimatedKeyLen
144
117
buf = bytes .NewBuffer (make ([]byte , 0 , estimatedLen ))
145
118
)
146
119
147
- c . encodeUint (buf , uint64 (numChildren ))
120
+ encodeUint (buf , uint64 (numChildren ))
148
121
149
122
// ensure that the order of entries is consistent
150
123
keys := maps .Keys (n .children )
151
124
slices .Sort (keys )
152
125
for _ , index := range keys {
153
126
entry := n .children [index ]
154
- c . encodeUint (buf , uint64 (index ))
127
+ encodeUint (buf , uint64 (index ))
155
128
_ , _ = buf .Write (entry .id [:])
156
129
}
157
- c . encodeMaybeByteSlice (buf , n .valueDigest )
158
- c . encodeKeyToBuffer (buf , n .key )
130
+ encodeMaybeByteSlice (buf , n .valueDigest )
131
+ encodeKeyToBuffer (buf , n .key )
159
132
160
133
return buf .Bytes ()
161
134
}
162
135
163
- func (c * codecImpl ) decodeDBNode (b []byte , n * dbNode ) error {
136
+ // Assumes [n] is non-nil.
137
+ func decodeDBNode (b []byte , n * dbNode ) error {
164
138
if minDBNodeLen > len (b ) {
165
139
return io .ErrUnexpectedEOF
166
140
}
167
141
168
142
src := bytes .NewReader (b )
169
143
170
- value , err := c . decodeMaybeByteSlice (src )
144
+ value , err := decodeMaybeByteSlice (src )
171
145
if err != nil {
172
146
return err
173
147
}
174
148
n .value = value
175
149
176
- numChildren , err := c . decodeUint (src )
150
+ numChildren , err := decodeUint (src )
177
151
switch {
178
152
case err != nil :
179
153
return err
@@ -184,7 +158,7 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
184
158
n .children = make (map [byte ]* child , numChildren )
185
159
var previousChild uint64
186
160
for i := uint64 (0 ); i < numChildren ; i ++ {
187
- index , err := c . decodeUint (src )
161
+ index , err := decodeUint (src )
188
162
if err != nil {
189
163
return err
190
164
}
@@ -193,15 +167,15 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
193
167
}
194
168
previousChild = index
195
169
196
- compressedKey , err := c . decodeKeyFromReader (src )
170
+ compressedKey , err := decodeKeyFromReader (src )
197
171
if err != nil {
198
172
return err
199
173
}
200
- childID , err := c . decodeID (src )
174
+ childID , err := decodeID (src )
201
175
if err != nil {
202
176
return err
203
177
}
204
- hasValue , err := c . decodeBool (src )
178
+ hasValue , err := decodeBool (src )
205
179
if err != nil {
206
180
return err
207
181
}
@@ -217,15 +191,15 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
217
191
return nil
218
192
}
219
193
220
- func ( * codecImpl ) encodeBool (dst * bytes.Buffer , value bool ) {
194
+ func encodeBool (dst * bytes.Buffer , value bool ) {
221
195
bytesValue := falseBytes
222
196
if value {
223
197
bytesValue = trueBytes
224
198
}
225
199
_ , _ = dst .Write (bytesValue )
226
200
}
227
201
228
- func ( * codecImpl ) decodeBool (src * bytes.Reader ) (bool , error ) {
202
+ func decodeBool (src * bytes.Reader ) (bool , error ) {
229
203
boolByte , err := src .ReadByte ()
230
204
switch {
231
205
case err == io .EOF :
@@ -241,7 +215,7 @@ func (*codecImpl) decodeBool(src *bytes.Reader) (bool, error) {
241
215
}
242
216
}
243
217
244
- func ( * codecImpl ) decodeUint (src * bytes.Reader ) (uint64 , error ) {
218
+ func decodeUint (src * bytes.Reader ) (uint64 , error ) {
245
219
// To ensure encoding/decoding is canonical, we need to check for leading
246
220
// zeroes in the varint.
247
221
// 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) {
274
248
return val64 , nil
275
249
}
276
250
277
- func ( * codecImpl ) encodeUint (dst * bytes.Buffer , value uint64 ) {
251
+ func encodeUint (dst * bytes.Buffer , value uint64 ) {
278
252
var buf [binary .MaxVarintLen64 ]byte
279
253
size := binary .PutUvarint (buf [:], value )
280
254
_ , _ = dst .Write (buf [:size ])
281
255
}
282
256
283
- func ( c * codecImpl ) encodeMaybeByteSlice (dst * bytes.Buffer , maybeValue maybe.Maybe [[]byte ]) {
257
+ func encodeMaybeByteSlice (dst * bytes.Buffer , maybeValue maybe.Maybe [[]byte ]) {
284
258
hasValue := maybeValue .HasValue ()
285
- c . encodeBool (dst , hasValue )
259
+ encodeBool (dst , hasValue )
286
260
if hasValue {
287
- c . encodeByteSlice (dst , maybeValue .Value ())
261
+ encodeByteSlice (dst , maybeValue .Value ())
288
262
}
289
263
}
290
264
291
- func ( c * codecImpl ) decodeMaybeByteSlice (src * bytes.Reader ) (maybe.Maybe [[]byte ], error ) {
265
+ func decodeMaybeByteSlice (src * bytes.Reader ) (maybe.Maybe [[]byte ], error ) {
292
266
if minMaybeByteSliceLen > src .Len () {
293
267
return maybe .Nothing [[]byte ](), io .ErrUnexpectedEOF
294
268
}
295
269
296
- if hasValue , err := c . decodeBool (src ); err != nil || ! hasValue {
270
+ if hasValue , err := decodeBool (src ); err != nil || ! hasValue {
297
271
return maybe .Nothing [[]byte ](), err
298
272
}
299
273
300
- rawBytes , err := c . decodeByteSlice (src )
274
+ rawBytes , err := decodeByteSlice (src )
301
275
if err != nil {
302
276
return maybe .Nothing [[]byte ](), err
303
277
}
304
278
305
279
return maybe .Some (rawBytes ), nil
306
280
}
307
281
308
- func ( c * codecImpl ) decodeByteSlice (src * bytes.Reader ) ([]byte , error ) {
282
+ func decodeByteSlice (src * bytes.Reader ) ([]byte , error ) {
309
283
if minByteSliceLen > src .Len () {
310
284
return nil , io .ErrUnexpectedEOF
311
285
}
312
286
313
- length , err := c . decodeUint (src )
287
+ length , err := decodeUint (src )
314
288
switch {
315
289
case err == io .EOF :
316
290
return nil , io .ErrUnexpectedEOF
@@ -330,14 +304,14 @@ func (c *codecImpl) decodeByteSlice(src *bytes.Reader) ([]byte, error) {
330
304
return result , err
331
305
}
332
306
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 )))
335
309
if value != nil {
336
310
_ , _ = dst .Write (value )
337
311
}
338
312
}
339
313
340
- func ( * codecImpl ) decodeID (src * bytes.Reader ) (ids.ID , error ) {
314
+ func decodeID (src * bytes.Reader ) (ids.ID , error ) {
341
315
if ids .IDLen > src .Len () {
342
316
return ids.ID {}, io .ErrUnexpectedEOF
343
317
}
@@ -350,21 +324,21 @@ func (*codecImpl) decodeID(src *bytes.Reader) (ids.ID, error) {
350
324
return id , err
351
325
}
352
326
353
- func ( c * codecImpl ) encodeKey (key Key ) []byte {
327
+ func encodeKey (key Key ) []byte {
354
328
estimatedLen := binary .MaxVarintLen64 + len (key .Bytes ())
355
329
dst := bytes .NewBuffer (make ([]byte , 0 , estimatedLen ))
356
- c . encodeKeyToBuffer (dst , key )
330
+ encodeKeyToBuffer (dst , key )
357
331
return dst .Bytes ()
358
332
}
359
333
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 ))
362
336
_ , _ = dst .Write (key .Bytes ())
363
337
}
364
338
365
- func ( c * codecImpl ) decodeKey (b []byte ) (Key , error ) {
339
+ func decodeKey (b []byte ) (Key , error ) {
366
340
src := bytes .NewReader (b )
367
- key , err := c . decodeKeyFromReader (src )
341
+ key , err := decodeKeyFromReader (src )
368
342
if err != nil {
369
343
return Key {}, err
370
344
}
@@ -374,12 +348,12 @@ func (c *codecImpl) decodeKey(b []byte) (Key, error) {
374
348
return key , err
375
349
}
376
350
377
- func ( c * codecImpl ) decodeKeyFromReader (src * bytes.Reader ) (Key , error ) {
351
+ func decodeKeyFromReader (src * bytes.Reader ) (Key , error ) {
378
352
if minKeyLen > src .Len () {
379
353
return Key {}, io .ErrUnexpectedEOF
380
354
}
381
355
382
- length , err := c . decodeUint (src )
356
+ length , err := decodeUint (src )
383
357
if err != nil {
384
358
return Key {}, err
385
359
}
0 commit comments