Skip to content

Commit

Permalink
link pages
Browse files Browse the repository at this point in the history
  • Loading branch information
chirst committed Jun 26, 2024
1 parent bcfb5cc commit 17c3e55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 22 additions & 1 deletion kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,36 @@ func (kv *KV) getLeafPage(nextPageNumber uint16, key []byte) *pager.Page {
}

func (kv *KV) splitPage(page *pager.Page) (left, right *pager.Page) {
hasParent, _ := page.GetParentPageNumber()
_, parentLeftPageNumber := page.GetLeftPageNumber()
_, parentRightPageNumber := page.GetRightPageNumber()
entries := page.GetEntries()
leftPage := kv.pager.NewPage()
// If it is splitting the root page should make two new nodes so the
// root can keep the same page number. Otherwise will only need to split
// into one new node and also use the existing node.
leftPage := page
if !hasParent {
leftPage = kv.pager.NewPage()
}
leftEntries := entries[:len(entries)/2]
leftPage.SetEntries(leftEntries)
rightPage := kv.pager.NewPage()
rightEntries := entries[len(entries)/2:]
rightPage.SetEntries(rightEntries)
// Set relative left page's right page
if parentLeftPageNumber != 0 {
kv.pager.GetPage(parentLeftPageNumber).SetRightPageNumber(leftPage.GetNumber())
}
// Set split left's left and right
leftPage.SetLeftPageNumber(parentLeftPageNumber)
leftPage.SetRightPageNumber(rightPage.GetNumber())
// Set split right's left and right
rightPage.SetLeftPageNumber(leftPage.GetNumber())
rightPage.SetRightPageNumber(parentRightPageNumber)
// Set relative right page's left page
if parentRightPageNumber != 0 {
kv.pager.GetPage(parentRightPageNumber).SetLeftPageNumber(rightPage.GetNumber())
}
return leftPage, rightPage
}

Expand Down
2 changes: 2 additions & 0 deletions pager/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (p *Page) SetParentPageNumber(pageNumber uint16) {
copy(p.content[PARENT_POINTER_OFFSET:PARENT_POINTER_OFFSET+PAGE_POINTER_SIZE], bpn)
}

// TODO consider just removing the boolean return from all these page pointers
// since 0 means the same thing
func (p *Page) GetLeftPageNumber() (hasLeft bool, pageNumber uint16) {
pn := binary.LittleEndian.Uint16(p.content[LEFT_POINTER_OFFSET : LEFT_POINTER_OFFSET+PAGE_POINTER_SIZE])
if pn == EMPTY_PARENT_PAGE_NUMBER {
Expand Down

0 comments on commit 17c3e55

Please sign in to comment.