forked from sei-protocol/sei-iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_node.go
76 lines (63 loc) · 1.88 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
68
69
70
71
72
73
74
75
76
package iavl
import (
"io"
"github.com/cosmos/iavl/cache"
"github.com/cosmos/iavl/internal/encoding"
"github.com/pkg/errors"
)
// 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
}
var _ cache.Node = (*FastNode)(nil)
// 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 := encoding.DecodeVarint(buf)
if cause != nil {
return nil, errors.Wrap(cause, "decoding fastnode.version")
}
buf = buf[n:]
val, _, cause := encoding.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 (fn *FastNode) GetCacheKey() []byte {
return fn.key
}
func (node *FastNode) encodedSize() int {
n := encoding.EncodeVarintSize(node.versionLastUpdatedAt) + encoding.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 := encoding.EncodeVarint(w, node.versionLastUpdatedAt)
if cause != nil {
return errors.Wrap(cause, "writing version last updated at")
}
cause = encoding.EncodeBytes(w, node.value)
if cause != nil {
return errors.Wrap(cause, "writing value")
}
return nil
}