33
33
trueBytes = []byte {trueByte }
34
34
falseBytes = []byte {falseByte }
35
35
36
- errEncodeNil = errors .New ("can't encode nil pointer or interface" )
37
36
errDecodeNil = errors .New ("can't decode nil" )
38
37
errNegativeNumChildren = errors .New ("number of children is negative" )
39
38
errTooManyChildren = fmt .Errorf ("length of children list is larger than branching factor of %d" , NodeBranchFactor )
@@ -55,8 +54,10 @@ type encoderDecoder interface {
55
54
}
56
55
57
56
type encoder interface {
58
- encodeDBNode (n * dbNode ) ([]byte , error )
59
- encodeHashValues (hv * hashValues ) ([]byte , error )
57
+ // Assumes [n] is non-nil.
58
+ encodeDBNode (n * dbNode ) []byte
59
+ // Assumes [hv] is non-nil.
60
+ encodeHashValues (hv * hashValues ) []byte
60
61
}
61
62
62
63
type decoder interface {
@@ -73,71 +74,45 @@ func newCodec() encoderDecoder {
73
74
}
74
75
}
75
76
77
+ // Note that bytes.Buffer.Write always returns nil so we
78
+ // can ignore its return values in [codecImpl] methods.
76
79
type codecImpl struct {
77
80
varIntPool sync.Pool
78
81
}
79
82
80
- func (c * codecImpl ) encodeDBNode (n * dbNode ) ([]byte , error ) {
81
- if n == nil {
82
- return nil , errEncodeNil
83
- }
84
-
83
+ func (c * codecImpl ) encodeDBNode (n * dbNode ) []byte {
85
84
buf := & bytes.Buffer {}
86
- if err := c .encodeMaybeByteSlice (buf , n .value ); err != nil {
87
- return nil , err
88
- }
85
+ c .encodeMaybeByteSlice (buf , n .value )
89
86
childrenLength := len (n .children )
90
- if err := c .encodeInt (buf , childrenLength ); err != nil {
91
- return nil , err
92
- }
87
+ c .encodeInt (buf , childrenLength )
93
88
for index := byte (0 ); index < NodeBranchFactor ; index ++ {
94
89
if entry , ok := n .children [index ]; ok {
95
- if err := c .encodeInt (buf , int (index )); err != nil {
96
- return nil , err
97
- }
90
+ c .encodeInt (buf , int (index ))
98
91
path := entry .compressedPath .Serialize ()
99
- if err := c .encodeSerializedPath (path , buf ); err != nil {
100
- return nil , err
101
- }
102
- if _ , err := buf .Write (entry .id [:]); err != nil {
103
- return nil , err
104
- }
92
+ c .encodeSerializedPath (path , buf )
93
+ _ , _ = buf .Write (entry .id [:])
105
94
}
106
95
}
107
- return buf .Bytes (), nil
96
+ return buf .Bytes ()
108
97
}
109
98
110
- func (c * codecImpl ) encodeHashValues (hv * hashValues ) ([]byte , error ) {
111
- if hv == nil {
112
- return nil , errEncodeNil
113
- }
114
-
99
+ func (c * codecImpl ) encodeHashValues (hv * hashValues ) []byte {
115
100
buf := & bytes.Buffer {}
116
101
117
102
length := len (hv .Children )
118
- if err := c .encodeInt (buf , length ); err != nil {
119
- return nil , err
120
- }
103
+ c .encodeInt (buf , length )
121
104
122
105
// ensure that the order of entries is consistent
123
106
for index := byte (0 ); index < NodeBranchFactor ; index ++ {
124
107
if entry , ok := hv .Children [index ]; ok {
125
- if err := c .encodeInt (buf , int (index )); err != nil {
126
- return nil , err
127
- }
128
- if _ , err := buf .Write (entry .id [:]); err != nil {
129
- return nil , err
130
- }
108
+ c .encodeInt (buf , int (index ))
109
+ _ , _ = buf .Write (entry .id [:])
131
110
}
132
111
}
133
- if err := c .encodeMaybeByteSlice (buf , hv .Value ); err != nil {
134
- return nil , err
135
- }
136
- if err := c .encodeSerializedPath (hv .Key , buf ); err != nil {
137
- return nil , err
138
- }
112
+ c .encodeMaybeByteSlice (buf , hv .Value )
113
+ c .encodeSerializedPath (hv .Key , buf )
139
114
140
- return buf .Bytes (), nil
115
+ return buf .Bytes ()
141
116
}
142
117
143
118
func (c * codecImpl ) decodeDBNode (b []byte , n * dbNode ) error {
@@ -201,13 +176,12 @@ func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) error {
201
176
return err
202
177
}
203
178
204
- func (* codecImpl ) encodeBool (dst io. Writer , value bool ) error {
179
+ func (* codecImpl ) encodeBool (dst * bytes. Buffer , value bool ) {
205
180
bytesValue := falseBytes
206
181
if value {
207
182
bytesValue = trueBytes
208
183
}
209
- _ , err := dst .Write (bytesValue )
210
- return err
184
+ _ , _ = dst .Write (bytesValue )
211
185
}
212
186
213
187
func (* codecImpl ) decodeBool (src * bytes.Reader ) (bool , error ) {
@@ -228,8 +202,8 @@ func (*codecImpl) decodeBool(src *bytes.Reader) (bool, error) {
228
202
}
229
203
}
230
204
231
- func (c * codecImpl ) encodeInt (dst io. Writer , value int ) error {
232
- return c .encodeInt64 (dst , int64 (value ))
205
+ func (c * codecImpl ) encodeInt (dst * bytes. Buffer , value int ) {
206
+ c .encodeInt64 (dst , int64 (value ))
233
207
}
234
208
235
209
func (* codecImpl ) decodeInt (src * bytes.Reader ) (int , error ) {
@@ -267,22 +241,18 @@ func (*codecImpl) decodeInt(src *bytes.Reader) (int, error) {
267
241
return int (val64 ), nil
268
242
}
269
243
270
- func (c * codecImpl ) encodeInt64 (dst io. Writer , value int64 ) error {
244
+ func (c * codecImpl ) encodeInt64 (dst * bytes. Buffer , value int64 ) {
271
245
buf := c .varIntPool .Get ().([]byte )
272
246
size := binary .PutVarint (buf , value )
273
- _ , err : = dst .Write (buf [:size ])
247
+ _ , _ = dst .Write (buf [:size ])
274
248
c .varIntPool .Put (buf )
275
- return err
276
249
}
277
250
278
- func (c * codecImpl ) encodeMaybeByteSlice (dst io.Writer , maybeValue maybe.Maybe [[]byte ]) error {
279
- if err := c .encodeBool (dst , ! maybeValue .IsNothing ()); err != nil {
280
- return err
281
- }
282
- if maybeValue .IsNothing () {
283
- return nil
251
+ func (c * codecImpl ) encodeMaybeByteSlice (dst * bytes.Buffer , maybeValue maybe.Maybe [[]byte ]) {
252
+ c .encodeBool (dst , ! maybeValue .IsNothing ())
253
+ if maybeValue .HasValue () {
254
+ c .encodeByteSlice (dst , maybeValue .Value ())
284
255
}
285
- return c .encodeByteSlice (dst , maybeValue .Value ())
286
256
}
287
257
288
258
func (c * codecImpl ) decodeMaybeByteSlice (src * bytes.Reader ) (maybe.Maybe [[]byte ], error ) {
@@ -338,16 +308,11 @@ func (c *codecImpl) decodeByteSlice(src *bytes.Reader) ([]byte, error) {
338
308
return result , nil
339
309
}
340
310
341
- func (c * codecImpl ) encodeByteSlice (dst io.Writer , value []byte ) error {
342
- if err := c .encodeInt (dst , len (value )); err != nil {
343
- return err
344
- }
311
+ func (c * codecImpl ) encodeByteSlice (dst * bytes.Buffer , value []byte ) {
312
+ c .encodeInt (dst , len (value ))
345
313
if value != nil {
346
- if _ , err := dst .Write (value ); err != nil {
347
- return err
348
- }
314
+ _ , _ = dst .Write (value )
349
315
}
350
- return nil
351
316
}
352
317
353
318
func (* codecImpl ) decodeID (src * bytes.Reader ) (ids.ID , error ) {
@@ -365,12 +330,9 @@ func (*codecImpl) decodeID(src *bytes.Reader) (ids.ID, error) {
365
330
return id , nil
366
331
}
367
332
368
- func (c * codecImpl ) encodeSerializedPath (s SerializedPath , dst io.Writer ) error {
369
- if err := c .encodeInt (dst , s .NibbleLength ); err != nil {
370
- return err
371
- }
372
- _ , err := dst .Write (s .Value )
373
- return err
333
+ func (c * codecImpl ) encodeSerializedPath (s SerializedPath , dst * bytes.Buffer ) {
334
+ c .encodeInt (dst , s .NibbleLength )
335
+ _ , _ = dst .Write (s .Value )
374
336
}
375
337
376
338
func (c * codecImpl ) decodeSerializedPath (src * bytes.Reader ) (SerializedPath , error ) {
0 commit comments