forked from osmosis-labs/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_node.go
67 lines (57 loc) · 1.65 KB
/
fast_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package iavl
import (
"github.com/pkg/errors"
"io"
)
// NOTE: This file favors int64 as opposed to int for size/counts.
// The Tree on the other hand favors int. This is intentional.
type FastNode struct {
key []byte
versionLastUpdatedAt int64
value []byte
}
// NewFastNode returns a new fast node from a value and version.
func NewFastNode(key []byte, value []byte, version int64) *FastNode {
return &FastNode{
key: key,
versionLastUpdatedAt: version,
value: value,
}
}
// DeserializeFastNode constructs an *FastNode from an encoded byte slice.
func DeserializeFastNode(key []byte, buf []byte) (*FastNode, error) {
ver, n, cause := decodeVarint(buf)
if cause != nil {
return nil, errors.Wrap(cause, "decoding fastnode.version")
}
buf = buf[n:]
val, _, cause := decodeBytes(buf)
if cause != nil {
return nil, errors.Wrap(cause, "decoding fastnode.value")
}
fastNode := &FastNode{
key: key,
versionLastUpdatedAt: ver,
value: val,
}
return fastNode, nil
}
func (node *FastNode) encodedSize() int {
n := encodeVarintSize(node.versionLastUpdatedAt) + encodeBytesSize(node.value)
return n
}
// writeBytes writes the FastNode as a serialized byte slice to the supplied io.Writer.
func (node *FastNode) writeBytes(w io.Writer) error {
if node == nil {
return errors.New("cannot write nil node")
}
cause := encodeVarint(w, node.versionLastUpdatedAt)
if cause != nil {
return errors.Wrap(cause, "writing version last updated at")
}
cause = encodeBytes(w, node.value)
if cause != nil {
return errors.Wrap(cause, "writing value")
}
return nil
}