-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
282 lines (225 loc) · 7.47 KB
/
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package radixdb
import (
"bytes"
"encoding/binary"
"hash/crc32"
"sort"
)
// node represents an in-memory node of a Radix tree. This implementation
// is designed to be memory-efficient by using a minimal set of fields to
// represent each node. In a Radix tree, the node's key inherently carries
// significant information, hence reducing the need to maintain metadata.
// Adding fields to this struct can significantly increase memory overhead.
// Think carefully before adding anything to the struct.
type node struct {
key []byte // Path segment of the node.
isRecord bool // True if node is a record; false if path component.
isBlob bool // True if the value is stored in the blob store.
children []*node // Pointers to child nodes.
checksum uint32 // CRC32 checksum of the node content.
// Holds the content of the node. Values less than or equal to 32-bytes
// are stored directly in this byte slice. Otherwise, it holds the blobID
// that points to the content in the blobStore.
data []byte
}
// hasChidren returns true if the receiver node has children.
func (n node) hasChildren() bool {
return len(n.children) > 0
}
// isLeaf returns true if the receiver node is a leaf node.
func (n node) isLeaf() bool {
return len(n.children) == 0
}
// value retrieves the record value of the node. If the value is stored in the
// blobStore, it fetches the value using the blobID stored in the data field.
func (n node) value(blobs blobStore) []byte {
ret := n.data
if n.isBlob {
blobID, err := buildBlobID(n.data)
if err != nil {
return nil
}
ret = blobs.getValue(blobID)
}
return ret
}
// findCompatibleChild searches through the child nodes of the receiver node.
// It returns the first child node that shares a common prefix. If no child is
// found, the function returns nil.
func (n node) findCompatibleChild(key []byte) *node {
for _, child := range n.children {
prefix := longestCommonPrefix(child.key, key)
if len(prefix) > 0 {
return child
}
}
return nil
}
// findChild returns the node's child that matches the given key.
// If not found, an ErrKeyNotFound error is returned.
func (n node) findChild(key []byte) (*node, int, error) {
index := sort.Search(len(n.children), func(i int) bool {
return bytes.Compare(n.children[i].key, key) >= 0
})
if index >= len(n.children) || longestCommonPrefix(n.children[index].key, key) == nil {
return nil, -1, ErrKeyNotFound
}
return n.children[index], index, nil
}
// addChild efficiently adds the given child to the node's children slice
// while preserving lexicographic order based on the child's key.
func (n *node) addChild(child *node) {
// Binary search for the correct position to insert the new child.
// This is faster than appending the child and then calling sort.Slice().
index := sort.Search(len(n.children), func(i int) bool {
return bytes.Compare(n.children[i].key, child.key) >= 0
})
// Expand the slice by one element, making room for the new child.
n.children = append(n.children, nil)
// Shift elements to the right to make space at the index.
copy(n.children[index+1:], n.children[index:])
// Insert the child in its correct position.
n.children[index] = child
}
// removeChild removes a child from the node's (sorted) children slice. It does
// so by identifying the index of the child using binary search.
func (n *node) removeChild(child *node) error {
index := sort.Search(len(n.children), func(i int) bool {
return bytes.Compare(n.children[i].key, child.key) >= 0
})
if index >= len(n.children) || longestCommonPrefix(n.children[index].key, child.key) == nil {
return ErrKeyNotFound
}
// Remove the child node at the index by shifting the elements after the
// index to the left. In other words, the shift overwrites the child node.
// We then truncate the slice by one element to remove the empty space.
copy(n.children[index:], n.children[index+1:])
n.children = n.children[:len(n.children)-1]
return nil
}
// calculateChecksum calculates the CRC32 checksum of the receiver node.
func (n node) calculateChecksum() (uint32, error) {
h := crc32.NewIEEE()
if _, err := h.Write(n.key); err != nil {
return 0, err
}
if _, err := h.Write(n.data); err != nil {
return 0, err
}
// Include the isRecord field.
if n.isRecord {
h.Write([]byte{1})
} else {
h.Write([]byte{0})
}
// Include the isBlob field.
if n.isBlob {
h.Write([]byte{1})
} else {
h.Write([]byte{0})
}
return h.Sum32(), nil
}
// updateChecksum computes and updates the checksum for the node using CRC32.
func (n *node) updateChecksum() error {
checksum, err := n.calculateChecksum()
if err != nil {
return err
}
n.checksum = checksum
return nil
}
// verifyChecksum verifies the integrity of the node's data by comparing the
// stored checksum based on the current node state.
func (n node) verifyChecksum() bool {
if n.checksum == 0 {
return false
}
checksum, err := n.calculateChecksum()
if err != nil {
return false
}
return n.checksum == checksum
}
// shallowCopyFrom copies the properties from the src node to the receiver node.
// This function performs a shallow copy, meaning that the copied fields share
// memory references with the original and are not actual copies. The function
// is intended for cases where sustaining the receiver's address is necessary.
func (n *node) shallowCopyFrom(src *node) {
n.key = src.key
n.data = src.data
n.isBlob = src.isBlob
n.isRecord = src.isRecord
n.children = src.children
n.updateChecksum()
}
// setKey updates the node's key with the provided value and recalculates
// the checksum to reflect the update.
func (n *node) setKey(key []byte) {
n.key = key
n.updateChecksum()
}
// setValue sets the given value to the node.
func (n *node) setValue(blobs blobStore, value []byte) {
if len(value) <= inlineValueThreshold {
n.data = value
n.isBlob = false
} else {
id := blobs.put(value)
n.data = id.toSlice()
n.isBlob = true
}
}
// prependKey prepends the given prefix to the node's existing key.
func (n *node) prependKey(prefix []byte) {
if len(prefix) == 0 {
return
}
newKey := make([]byte, len(prefix)+len(n.key))
copy(newKey, prefix)
copy(newKey[len(prefix):], n.key)
n.key = newKey
n.updateChecksum()
}
// serializeWithoutKey converts the receiver node into a platform-agonostic
// binary representation, and returns it as a byte slice. The returned byte
// slice does not contain the node key.
func (n node) serializeWithoutKey() ([]byte, error) {
var buf bytes.Buffer
if !n.verifyChecksum() {
return nil, ErrInvalidChecksum
}
// Step 1: Serialize the node checksum.
if err := binary.Write(&buf, binary.LittleEndian, n.checksum); err != nil {
return nil, err
}
// Step 2: Serialize the value and its length, if the node holds a record.
if n.isRecord {
valLen := uint64(len(n.data))
if err := binary.Write(&buf, binary.LittleEndian, valLen); err != nil {
return nil, err
}
if valLen > 0 {
if _, err := buf.Write(n.data); err != nil {
return nil, err
}
}
} else {
if err := binary.Write(&buf, binary.LittleEndian, uint64(0)); err != nil {
return nil, err
}
}
// Step 3: Serialize the number of children.
numChildren := uint64(len(n.children))
if err := binary.Write(&buf, binary.LittleEndian, numChildren); err != nil {
return nil, err
}
// Step 4: Reserve the space to hold the child node offsets.
tmpOffset := uint64(0)
for i := 0; i < int(numChildren); i++ {
if err := binary.Write(&buf, binary.LittleEndian, tmpOffset); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}