-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoding.go
181 lines (146 loc) · 3.92 KB
/
encoding.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package fbptree
import (
"encoding/binary"
)
func decodeUint16(data []byte) uint16 {
return binary.BigEndian.Uint16(data)
}
func encodeUint16(v uint16) []byte {
var data [2]byte
binary.BigEndian.PutUint16(data[:], v)
return data[:]
}
func decodeUint32(data []byte) uint32 {
return binary.BigEndian.Uint32(data)
}
func encodeUint32(v uint32) []byte {
var data [4]byte
binary.BigEndian.PutUint32(data[:], v)
return data[:]
}
func encodeBool(v bool) []byte {
var data [1]byte
if v {
data[0] = 1
}
return data[:]
}
func decodeBool(data []byte) bool {
return data[0] == 1
}
func encodeNode(node *node) []byte {
data := make([]byte, 0)
data = append(data, encodeUint32(node.id)...)
data = append(data, encodeUint32(node.parentID)...)
data = append(data, encodeBool(node.leaf)...)
data = append(data, encodeUint16(uint16(node.keyNum))...)
data = append(data, encodeUint16(uint16(len(node.keys)))...)
for _, key := range node.keys {
if key == nil {
break
}
data = append(data, encodeUint16(uint16(len(key)))...)
data = append(data, key...)
}
pointerNum := node.keyNum
if !node.leaf {
pointerNum += 1
}
data = append(data, encodeUint16(uint16(pointerNum))...)
data = append(data, encodeUint16(uint16(len(node.pointers)))...)
for i := 0; i < pointerNum; i++ {
pointer := node.pointers[i]
if pointer.isNodeID() {
data = append(data, 0)
data = append(data, encodeUint32(pointer.asNodeID())...)
} else if pointer.isValue() {
data = append(data, 1)
data = append(data, encodeUint16(uint16(len(pointer.asValue())))...)
data = append(data, pointer.asValue()...)
}
}
var nextID uint32
if node.next() != nil {
nextID = node.next().asNodeID()
data = append(data, encodeBool(true)...)
data = append(data, encodeUint32(nextID)...)
} else {
data = append(data, encodeBool(false)...)
data = append(data, 0)
}
return data
}
func decodeNode(data []byte) (*node, error) {
position := 0
nodeID := decodeUint32(data[position : position+4])
position += 4
parentID := decodeUint32(data[position : position+4])
position += 4
leaf := decodeBool(data[position : position+1])
position += 1
keyNum := decodeUint16(data[position : position+2])
position += 2
keyLen := int(decodeUint16(data[position : position+2]))
position += 2
keys := make([][]byte, keyLen)
for k := 0; k < int(keyNum); k++ {
keySize := int(decodeUint16(data[position : position+2]))
position += 2
key := data[position : position+keySize]
keys[k] = key
position += keySize
}
pointerNum := decodeUint16(data[position : position+2])
position += 2
pointerLen := int(decodeUint16(data[position : position+2]))
position += 2
pointers := make([]*pointer, pointerLen)
for p := 0; p < int(pointerNum); p++ {
if data[position] == 0 {
position += 1
// nodeID
nodeID := decodeUint32(data[position : position+4])
position += 4
pointers[p] = &pointer{nodeID}
} else if data[position] == 1 {
position += 1
// value
valueSize := int(decodeUint16(data[position : position+2]))
position += 2
value := data[position : position+valueSize]
position += valueSize
pointers[p] = &pointer{value}
}
}
n := &node{
nodeID,
leaf,
parentID,
keys,
int(keyNum),
pointers,
}
hasNextID := decodeBool(data[position : position+1])
position += 1
if hasNextID {
nextID := decodeUint32(data[position : position+4])
n.setNext(&pointer{nextID})
}
return n, nil
}
func encodeTreeMetadata(metadata *treeMetadata) []byte {
var data [14]byte
copy(data[0:2], encodeUint16(metadata.order))
copy(data[2:6], encodeUint32(metadata.rootID))
copy(data[6:10], encodeUint32(metadata.leftmostID))
copy(data[10:14], encodeUint32(metadata.size))
return data[:]
}
func decodeTreeMetadata(data []byte) (*treeMetadata, error) {
return &treeMetadata{
order: decodeUint16(data[0:2]),
rootID: decodeUint32(data[2:6]),
leftmostID: decodeUint32(data[6:10]),
size: decodeUint32(data[10:14]),
}, nil
}