-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
273 lines (226 loc) · 6.92 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
package main
import (
"bytes"
"encoding/binary"
)
type Item struct {
key []byte
value []byte
}
//will store dal to interact , list of items in current node , Node's self pageNum, and PageNumbers of child nodes
type Node struct {
*dal
items []*Item
pageNum pgnum
childNodes []pgnum
}
func newEmptyNode() *Node {
return &Node{}
}
func newItem(key []byte, value []byte) *Item {
return &Item{
key: key,
value: value,
}
}
func (n *Node) isLeaf() bool {
return len(n.childNodes) == 0
}
func (n *Node) serialize(buf []byte) []byte {
leftPos := 0
rightPos := len(buf) - 1
// Add page header: isLeaf, key-value pairs count, node num
isLeaf := n.isLeaf()
var bitSetVar uint64
if isLeaf {
bitSetVar = 1
}
buf[leftPos] = byte(bitSetVar)
leftPos += 1
// key-value pairs count
binary.LittleEndian.PutUint16(buf[leftPos:], uint16(len(n.items)))
leftPos += 2
// We use slotted pages for storing data in the page. It means the actual keys and values (the cells) are appended
// to right of the page whereas offsets have a fixed size and are appended from the left.
for i := 0; i < len(n.items); i++ {
item := n.items[i]
if !isLeaf {
childNode := n.childNodes[i]
// Write the child page as a fixed size of 8 bytes
binary.LittleEndian.PutUint64(buf[leftPos:], uint64(childNode))
leftPos += pageNumSize
}
klen := len(item.key)
vlen := len(item.value)
//offset is position of data in Node's 2nd partition i.e. from end.
offset := rightPos - (klen + vlen) - 2
//offset address of each key-value pair is stored on left side
binary.LittleEndian.PutUint16(buf[leftPos:], uint16(offset))
leftPos += 2
rightPos -= vlen
//whereas key-value data is appended from the right side
copy(buf[rightPos:], item.value)
rightPos -= 1
buf[rightPos] = byte(vlen)
rightPos -= klen
copy(buf[rightPos:], item.key)
rightPos -= 1
buf[rightPos] = byte(klen)
}
if !isLeaf {
// Write the last child node
lastChildNode := n.childNodes[len(n.childNodes)-1]
// Write the child page as a fixed size of 8 bytes
binary.LittleEndian.PutUint64(buf[leftPos:], uint64(lastChildNode))
}
return buf
}
//to deserialize data that is written to a page format into Node format.
func (n *Node) deserialize(buf []byte) {
leftPos := 0
isLeaf := uint(buf[0])
itemsCount := int(binary.LittleEndian.Uint16(buf[1:3]))
leftPos += 3
for i := 0; i < itemsCount; i++ {
//since we stored isLeaf in int format
if isLeaf == 0 { //false
pageNum := binary.LittleEndian.Uint64(buf[leftPos:])
leftPos += pageNumSize
n.childNodes = append(n.childNodes, pgnum(pageNum))
}
//read offset
offset := binary.LittleEndian.Uint16(buf[leftPos:])
leftPos += 2
klen := uint16(buf[int(offset)])
offset += 1
key := buf[offset : offset+klen]
offset += klen
vlen := uint16(buf[int(offset)])
offset += 1
value := buf[offset : offset+vlen]
offset += vlen
n.items = append(n.items, newItem(key, value))
}
if isLeaf == 0 { // False
// Read the last child node
pageNum := pgnum(binary.LittleEndian.Uint64(buf[leftPos:]))
n.childNodes = append(n.childNodes, pageNum)
}
}
func (n *Node) writeNode(node *Node) *Node {
node, _ = n.dal.writeNode(node)
return node
}
func (n *Node) writeNodes(nodes ...*Node) {
for _, node := range nodes {
n.writeNode(node)
}
}
func (n *Node) getNode(pageNum pgnum) (*Node, error) {
return n.dal.getNode(pageNum)
}
// findKeyInNode iterates all the items and finds the key. If the key is found, then the item is returned. If the key
// isn't found then return the index where it should have been (the first index that key is greater than it's previous)
func (n *Node) findKeyInNode(key []byte) (bool, int) {
for i, exisistingItem := range n.items {
res := bytes.Compare(exisistingItem.key, key)
if res == 0 {
//key matches
return true, i
}
if res == 1 {
//key is bigger than last key
return false, i
}
}
return false, len(n.items)
}
func (n *Node) findKey(key []byte, exact bool) (int, *Node, []int, error) {
ancestorsIndexes := []int{0} // index of root
index, node, err := findKeyHelper(n, key, exact, &ancestorsIndexes)
if err != nil {
return -1, nil, nil, err
}
return index, node, ancestorsIndexes, nil
}
func findKeyHelper(node *Node, key []byte, exact bool, ancestorsIndexes *[]int) (int, *Node, error) {
wasFound, index := node.findKeyInNode(key)
if wasFound {
return index, node, nil
}
if node.isLeaf() {
if exact {
return -1, nil, nil
}
return index, node, nil
}
*ancestorsIndexes = append(*ancestorsIndexes, index)
nextChild, err := node.getNode(node.childNodes[index])
if err != nil {
return -1, nil, err
}
return findKeyHelper(nextChild, key, exact, ancestorsIndexes)
}
func (n *Node) nodeSize() int {
size := 0
size += nodeHeaderSize
for i := range n.items {
size += n.elementSize(i)
}
//add last page
size += pageNumSize
return size
}
// elementSize returns the size of a key-value-childNode triplet at a given index.
// If the node is a leaf, then the size of a key-value pair is returned.
// It's assumed i <= len(n.items)
func (n *Node) elementSize(i int) int {
size := 0
size += len(n.items[i].key)
size += len(n.items[i].value)
size += pageNumSize
return size
}
func (n *Node) addItem(item *Item, insertionIndex int) int {
if len(n.items) == insertionIndex { // nil or empty slice or after last element
n.items = append(n.items, item)
return insertionIndex
}
n.items = append(n.items[:insertionIndex+1], n.items[insertionIndex:]...)
n.items[insertionIndex] = item
return insertionIndex
}
// isOverPopulated checks if the node size is bigger than the size of a page.
func (n *Node) isOverPopulated() bool {
return n.dal.isOverPopulated(n)
}
// isUnderPopulated checks if the node size is smaller than the size of a page.
func (n *Node) isUnderPopulated() bool {
return n.dal.isUnderPopulated(n)
}
func (n *Node) split(nodeToSplit *Node, nodeToSplitIndex int) {
// The first index where min amount of bytes to populate a page is achieved. Then add 1 so it will be split one
// index after.
splitIndex := nodeToSplit.dal.getSplitIndex(nodeToSplit)
middleItem := nodeToSplit.items[splitIndex]
var newNode *Node
if nodeToSplit.isLeaf() {
newNode = n.writeNode(n.dal.newNode(nodeToSplit.items[splitIndex+1:], []pgnum{}))
nodeToSplit.items = nodeToSplit.items[:splitIndex]
} else {
newNode = n.writeNode(n.dal.newNode(nodeToSplit.items[splitIndex+1:], nodeToSplit.childNodes[splitIndex+1:]))
nodeToSplit.items = nodeToSplit.items[:splitIndex]
nodeToSplit.childNodes = nodeToSplit.childNodes[:splitIndex+1]
}
n.addItem(middleItem, nodeToSplitIndex)
if len(n.childNodes) == nodeToSplitIndex+1 { // If middle of list, then move items forward
n.childNodes = append(n.childNodes, newNode.pageNum)
} else {
n.childNodes = append(n.childNodes[:nodeToSplitIndex+1], n.childNodes[nodeToSplitIndex:]...)
n.childNodes[nodeToSplitIndex+1] = newNode.pageNum
}
n.writeNodes(n, nodeToSplit)
}
func NewEmptyNode() *Node {
return &Node{}
}