Skip to content

Commit 73846ed

Browse files
committed
complete
1 parent 474bdbe commit 73846ed

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

db/btree.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ func (p *LeafPage) leafNodeValue(cellNum uint32_t) *Row {
8989
}
9090

9191
func (p *LeafPage) initializeLeafNode() {
92-
p.setPageType(PageLeaf) // actually, it's not necessary since default PageType is PageLeaf
93-
p.setNodeRoot(false)
92+
//p.setPageType(PageLeaf) // actually, it's not necessary since default PageType is PageLeaf
93+
//p.setNodeRoot(false)
9494
*p.leafNodeNumCells() = 0
9595
*p.leafNodeNextLeaf() = 0
9696
}
@@ -143,8 +143,6 @@ func (c *Cursor) leafNodeSplitAndInsert(key uint32_t, value *Row) {
143143
if oldPage.header.isRoot != 0 {
144144
c.table.createNewRoot(newPageNum)
145145
} else {
146-
//fmt.Println("Need to implement updating parent after split.")
147-
//os.Exit(ExitFailure)
148146
parentPageNum := oldPage.header.parentPointer
149147
newMax := oldPage.getMaxKey()
150148
parentHeader, parentBody := c.table.pager.getPage(parentPageNum)
@@ -161,8 +159,6 @@ func (p *Pager) getUnusedPageNum() uint32_t { return p.numPages }
161159

162160
func (t *Table) createNewRoot(rightChildPageNum uint32_t) {
163161
rootHeader, rootBody := t.pager.getPage(t.rootPageNum)
164-
//root := LeafPage{header: rootHeader, body: (*LeafPageBody)(rootBody)}
165-
//rightChild := t.pager.getPage(rightChildPageNum)
166162
leftChildPageNum := t.pager.getUnusedPageNum()
167163
leftChildHeader, leftChildBody := t.pager.getPage(leftChildPageNum)
168164
leftChild := LeafPage{header: leftChildHeader, body: (*LeafPageBody)(leftChildBody)}
@@ -172,7 +168,6 @@ func (t *Table) createNewRoot(rightChildPageNum uint32_t) {
172168
copy((*[PageBodySize]byte)(leftChildBody)[:], (*[PageBodySize]byte)(rootBody)[:])
173169
leftChild.setNodeRoot(false)
174170

175-
//internalNode := InternalPage{header: new(PageHeader), body: new(InternalPageBody)}
176171
newRootHeader := new(PageHeader)
177172
newRootBody := new(InternalPageBody)
178173
newRoot := InternalPage{header: newRootHeader, body: newRootBody}
@@ -352,19 +347,20 @@ func (t *Table) internalNodeInsert(parentPageNum, childPageNum uint32_t) {
352347
}
353348

354349
rightChildPageNum := *parentPage.internalNodeRightChild()
355-
rightChildHeader, _ := t.pager.getPage(rightChildPageNum)
350+
rightChildHeader, rightChildBody := t.pager.getPage(rightChildPageNum)
356351

357352
var rightChildMaxKey uint32_t
358353
if rightChildHeader.pageType == PageLeaf {
359-
rightChildPage := LeafPage{header: childHeader, body: (*LeafPageBody)(childBody)}
354+
rightChildPage := &LeafPage{header: rightChildHeader, body: (*LeafPageBody)(rightChildBody)}
360355
rightChildMaxKey = rightChildPage.getMaxKey()
361356
} else {
362-
rightChildPage := InternalPage{header: childHeader, body: (*InternalPageBody)(childBody)}
357+
rightChildPage := &InternalPage{header: rightChildHeader, body: (*InternalPageBody)(rightChildBody)}
363358
rightChildMaxKey = rightChildPage.getMaxKey()
364359
}
365360
if childMaxKey > rightChildMaxKey {
366361
*parentPage.internalNodeChild(originalNumKeys) = rightChildPageNum
367362
*parentPage.internalNodeKey(originalNumKeys) = rightChildMaxKey
363+
*parentPage.internalNodeRightChild() = childPageNum
368364
} else {
369365
for i := originalNumKeys; i > index; i-- {
370366
destination := parentPage.internalNodeCell(i)

db/db_tutorial.go

+50-28
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ const (
9898
PageBodySize = PageSize - PageHeaderSize
9999
)
100100

101-
//type Page struct { // to create a pagesize memory without malloc()
102-
// header PageHeader
103-
// body [PageBodySize]byte
104-
//}
105-
106101
type LeafPage struct {
107102
header *PageHeader
108103
body *LeafPageBody
@@ -135,7 +130,7 @@ type LeafPageCell struct {
135130
}
136131

137132
type InternalPageCell struct {
138-
value uint32_t //pageNum
133+
value uint32_t //pageNum
139134
key uint32_t
140135
}
141136

@@ -378,9 +373,6 @@ func doMetaCommand(inputBuffer *InputBuffer, table *Table) MetaCommandResult {
378373
return MetaCommandSuccess
379374
} else if strings.TrimSpace(string(inputBuffer.buffer)) == ".btree" {
380375
fmt.Println("Tree:")
381-
//header, body := table.pager.getPage(0)
382-
//leafPage := LeafPage{header: header, body: (*LeafPageBody)(body)}
383-
//leafPage.printLeafNode()
384376
table.pager.printTree(0, 0)
385377
return MetaCommandSuccess
386378
} else if strings.TrimSpace(string(inputBuffer.buffer)) == ".pages" {
@@ -390,29 +382,59 @@ func doMetaCommand(inputBuffer *InputBuffer, table *Table) MetaCommandResult {
390382
}
391383
return MetaCommandSuccess
392384
} else if strings.TrimSpace(string(inputBuffer.buffer)) == ".keys" {
393-
fmt.Println("keys:")
394-
for i := uint32_t(0); i < table.pager.numPages; i++ {
395-
header, body := table.pager.getPage(i)
396-
if header.pageType == PageLeaf {
397-
cells := (*LeafPageBody)(body).cells
398-
fmt.Println("leaf page ", i)
399-
for _, c := range cells {
400-
fmt.Println(c.key)
401-
}
402-
} else if header.pageType == PageInternal {
403-
fmt.Println("internal page ", i)
404-
cells := (*InternalPageBody)(body).cells
405-
for _, c := range cells {
406-
fmt.Println(c.key)
407-
}
408-
}
409-
}
410-
return MetaCommandSuccess
411-
}
385+
printKeys(table)
386+
return MetaCommandSuccess
387+
} else if strings.TrimSpace(string(inputBuffer.buffer)) == ".kvs" {
388+
printKvs(table)
389+
return MetaCommandSuccess
390+
}
412391

413392
return MetaCommandUnrecognizedCommand
414393
}
415394

395+
func printKeys(table *Table) {
396+
fmt.Println("keys:")
397+
for i := uint32_t(0); i < table.pager.numPages; i++ {
398+
header, body := table.pager.getPage(i)
399+
if header.pageType == PageLeaf {
400+
cells := (*LeafPageBody)(body).cells
401+
fmt.Println("leaf page ", i)
402+
for _, c := range cells {
403+
fmt.Println(c.key)
404+
}
405+
} else if header.pageType == PageInternal {
406+
fmt.Println("internal page ", i)
407+
cells := (*InternalPageBody)(body).cells
408+
for _, c := range cells {
409+
fmt.Println(c.key)
410+
}
411+
}
412+
}
413+
}
414+
415+
func printKvs(table *Table) {
416+
fmt.Println("keys&values:")
417+
for i := uint32_t(0); i < table.pager.numPages; i++ {
418+
header, body := table.pager.getPage(i)
419+
if header.pageType == PageLeaf {
420+
cells := (*LeafPageBody)(body).cells
421+
fmt.Println("leaf page ", i)
422+
fmt.Println("num rows: ", header.numCells)
423+
for _, c := range cells {
424+
fmt.Println(c.key)
425+
}
426+
} else if header.pageType == PageInternal {
427+
fmt.Println("internal page ", i)
428+
fmt.Println("num rows: ", header.numCells)
429+
fmt.Println("right child: ", (*InternalPageBody)(body).rightChild)
430+
cells := (*InternalPageBody)(body).cells
431+
for _, c := range cells {
432+
fmt.Println(c.key, c.value)
433+
}
434+
}
435+
}
436+
}
437+
416438
func prepareStatement(inputBuffer *InputBuffer, statement *Statement) PrepareResult {
417439
bufferContent := strings.TrimSpace(string(inputBuffer.buffer))
418440
if len(bufferContent) >= 6 {

0 commit comments

Comments
 (0)