-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
318 lines (294 loc) · 7.13 KB
/
page.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bytes"
"encoding/binary"
"unsafe"
)
type PageType int
const (
PageInternal PageType = iota
PageLeft
PageOverflow
PageFree
)
const (
PageSize = 1 << 12
RowSize = int32(unsafe.Sizeof(Row{}))
NodeTypeSize = int32(unsafe.Sizeof(Internal))
CommonNodeHeaderSize = int32(unsafe.Sizeof(CommonNodeHeader{}))
LeafNodeHeaderSize = int32(unsafe.Sizeof(LeafNodeHeader{}))
InternalNodeHeaderSize = int32(unsafe.Sizeof(InternalNodeHeader{}))
RowsPerPage = (PageSize-CommonNodeHeaderSize-LeafNodeHeaderSize) / RowSize
ChildSize = int32(unsafe.Sizeof(Child{}))
ChildrenPerPage = (PageSize-CommonNodeHeaderSize-InternalNodeHeaderSize) / ChildSize
)
type Page struct {
CommonNodeHeader
LeafNode
InternalNode
// Rows [RowsPerPage]Row
}
type NodeType uint8
const (
Internal NodeType = iota
Leaf
)
type CommonNodeHeader struct {
NodeType NodeType
RootNode bool
_ int16
ParentNode int32
PageNum int32
}
type LeafNodeHeader struct {
NumCells int32
Sibling int32
}
type InternalNodeHeader struct {
ChildrenNum int32
RightmostChild int32
}
type Child struct {
// max child key
Key int32
PageNum int32
}
type LeafNode struct {
LeafNodeHeader
Rows [RowsPerPage]Row
}
type InternalNode struct {
InternalNodeHeader
Children [ChildrenPerPage]Child
}
func (page *Page) ToBytes() ([]byte, error) {
headerBuf := &bytes.Buffer{}
err := binary.Write(headerBuf, binary.BigEndian, page.CommonNodeHeader)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
switch page.NodeType {
case Internal:
err = binary.Write(buf, binary.BigEndian, page.InternalNode)
case Leaf:
err = binary.Write(buf, binary.BigEndian, page.LeafNode)
}
if err != nil {
return nil, err
}
pageBytes := headerBuf.Bytes()
pageBytes = append(pageBytes, buf.Bytes()...)
if len(pageBytes) > PageSize {
panic("page size > PageSize, seems like a bug")
}
return pageBytes, nil
}
func FromBytes(bs [PageSize]byte) Page {
nodeType := NodeType(bs[0]) // TODO: fix
var page Page
buf := bytes.NewBuffer(bs[:CommonNodeHeaderSize])
err := binary.Read(buf, binary.BigEndian, &page.CommonNodeHeader)
if err != nil {
panic(err)
}
buf = bytes.NewBuffer(bs[CommonNodeHeaderSize:])
switch nodeType {
case Internal:
err = binary.Read(buf, binary.BigEndian, &page.InternalNode)
case Leaf:
err = binary.Read(buf, binary.BigEndian, &page.LeafNode)
}
if err != nil {
panic(err)
}
return page
}
func (page *Page) Insert(row Row, cursor *Cursor) error {
if page.NodeType != Leaf {
panic("page should be leaf node")
}
if page.NumCells >= RowsPerPage {
return page.SplitAndInsert(row, cursor)
}
for i:=page.NumCells; i>cursor.CellNum; i-- {
page.Rows[i] = page.Rows[i-1]
}
page.Rows[cursor.CellNum] = row
if cursor.CellNum == page.NumCells && !page.RootNode {
var oldMax int32
if page.NumCells > 0 {
oldMax = page.Rows[page.NumCells-1].ID
}
parent, err := cursor.Table.Pager.GetPage(page.ParentNode, false)
if err != nil {
return err
}
idx := parent.InternalNodeFindChild(oldMax)
parent.Children[idx].Key = row.ID
}
page.NumCells++
return nil
}
func (page *Page) SplitAndInsert(row Row, cursor *Cursor) error {
table := cursor.Table
newPageIdx := table.Pager.GetNewPageNum()
newPage, err := table.Pager.GetPage(newPageIdx, true)
if err != nil {
return err
}
// move right half rows from old page to new page
halfPageCount := (RowsPerPage+1)/2
oldLeftMax := page.Rows[page.NumCells-1].ID
for i:=RowsPerPage; i>=0; i-- {
var target *Page
if i >= halfPageCount {
target = newPage
} else {
target = page
}
if i == cursor.CellNum {
target.Rows[i%halfPageCount] = row
} else if i > cursor.CellNum {
target.Rows[i%halfPageCount] = page.Rows[i-1]
} else {
target.Rows[i%halfPageCount] = page.Rows[i]
}
}
newPage.NumCells = halfPageCount
page.NumCells = halfPageCount
if page.RootNode {
err = CreateNewRoot(table, newPageIdx)
if err != nil {
return err
}
newPage.ParentNode = table.RootPageNum
newPage.Sibling = 0 // End
} else {
parent, err := table.Pager.GetPage(page.ParentNode, false)
if err != nil {
return err
}
newLeftMax := page.Rows[page.NumCells-1].ID
if parent.ChildrenNum >= ChildrenPerPage {
panic("need to implement internal node split")
}
if page.Sibling == 0 { // page is the rightmost page
parent.Children[parent.ChildrenNum] = Child{
Key: newLeftMax,
PageNum: page.PageNum,
}
parent.ChildrenNum++
parent.RightmostChild = newPageIdx
} else {
childIdx := parent.InternalNodeFindChild(oldLeftMax)
parent.Children[childIdx].Key = newLeftMax
// insert new page
for i:=parent.ChildrenNum; i>childIdx+1; i-- {
parent.Children[i] = parent.Children[i-1]
}
parent.Children[childIdx+1].Key = newPage.Rows[newPage.NumCells-1].ID
parent.Children[childIdx+1].PageNum = newPageIdx
parent.ChildrenNum++
}
page.Sibling = newPageIdx
}
return nil
}
func CreateNewRoot(table *Table, rightChildIdx int32) error {
leftChildIdx := table.Pager.GetNewPageNum()
leftChild, err := table.Pager.GetPage(leftChildIdx, true)
if err != nil {
return err
}
// copy root to left child
rootPage, err := table.Pager.GetPage(table.RootPageNum, false)
if err != nil {
return err
}
leftChild.LeafNode = rootPage.LeafNode
leftChild.RootNode = false
leftChild.NodeType = Leaf
leftChild.ParentNode = table.RootPageNum
rootPage.LeafNode = LeafNode{}
rootPage.ChildrenNum = 1
rootPage.NodeType = Internal
rootPage.Children[0].PageNum = leftChildIdx
rootPage.Children[0].Key = leftChild.Rows[leftChild.NumCells-1].ID
rootPage.RightmostChild = rightChildIdx
leftChild.Sibling = rightChildIdx
return nil
}
func (page *Page) LeafNodeSearch(table *Table, key int32) (Cursor, error) {
cursor := Cursor{
Table: table,
PageNum: page.PageNum,
CellNum: 0,
EndOfTable: false,
}
switch page.NodeType {
case Leaf:
left := int32(0)
right := page.NumCells
mid := left + (right-left)/2
for left < right {
midRow := page.Rows[mid]
if midRow.ID == key {
break
} else if midRow.ID < key {
left = mid+1
} else {
right = mid
}
mid = left + (right-left)/2
}
cursor.CellNum = left
case Internal:
if page.ChildrenNum == 0 {
panic("empty children cannot be internal")
}
var left int32
if key > page.Children[page.ChildrenNum-1].Key {
left = page.RightmostChild
} else {
left = int32(0)
right := page.ChildrenNum
mid := left + (right-left)/2
for left < right {
midKey := page.Children[mid]
if midKey.Key == key {
break
} else if midKey.Key < key {
left = mid + 1
} else {
right = mid
}
mid = left + (right-left)/2
}
}
cursor.PageNum = page.Children[left].PageNum
newPage, err := table.Pager.GetPage(left, false)
if err != nil {
return cursor, err
}
return newPage.LeafNodeSearch(table, key)
}
return cursor, nil
}
func (page *Page) InternalNodeFindChild(key int32) int32 {
left := int32(0)
right := page.ChildrenNum
mid := left + (right-left)/2
for left < right {
midKey := page.Children[mid]
if midKey.Key == key {
break
} else if midKey.Key < key {
left = mid+1
} else {
right = mid
}
mid = left + (right-left)/2
}
return mid
}