Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chewxy/update btree insertion #228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 48 additions & 28 deletions z/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (t *Tree) initRootNode() {
// This is the root node.
t.newNode(0)
// This acts as the rightmost pointer (all the keys are <= this key).
t.Set(absoluteMax, 0)
t.setRightmost(absoluteMax, 0)
}

// NewTree returns a memory mapped B+ tree with given filename.
Expand Down Expand Up @@ -200,7 +200,7 @@ func (t *Tree) Set(k, v uint64) {
if k == math.MaxUint64 || k == 0 {
panic("Error setting zero or MaxUint64")
}
root := t.set(1, k, v)
root := t.set(1, k, v, false)
if root.isFull() {
right := t.split(1)
left := t.newNode(root.bits())
Expand All @@ -221,10 +221,13 @@ func (t *Tree) Set(k, v uint64) {

// For internal nodes, they contain <key, ptr>.
// where all entries <= key are stored in the corresponding ptr.
func (t *Tree) set(pid, k, v uint64) node {
func (t *Tree) set(pid, k, v uint64, isRightmost bool) node {
n := t.node(pid)
if n.isLeaf() {
return n.set(k, v)
if !isRightmost {
return n.setAsLeaf(k, v)
}
return n.setRightmostLeaf(k, v)
}

// This is an internal node.
Expand All @@ -243,7 +246,7 @@ func (t *Tree) set(pid, k, v uint64) node {
n = t.node(pid)
n.setAt(valOffset(idx), child.pageID())
}
child = t.set(child.pageID(), k, v)
child = t.set(child.pageID(), k, v, isRightmost)
// Re-read n as the underlying buffer for tree might have changed during set.
n = t.node(pid)
if child.isFull() {
Expand All @@ -265,6 +268,30 @@ func (t *Tree) set(pid, k, v uint64) node {
return n
}

// setRightmost sets the key-value pair in the tree.
func (t *Tree) setRightmost(k, v uint64) {
if k == math.MaxUint64 || k == 0 {
panic("Error setting zero or MaxUint64")
}
root := t.set(1, k, v, true)
if root.isFull() {
right := t.split(1)
left := t.newNode(root.bits())
// Re-read the root as the underlying buffer for tree might have changed during split.
root = t.node(1)
copy(left[:keyOffset(maxKeys)], root)
left.setNumKeys(root.numKeys())

// reset the root node.
zeroOut(root[:keyOffset(maxKeys)])
root.setNumKeys(0)

// set the pointers for left and right child in the root node.
root.set(left.maxKey(), left.pageID())
root.set(right.maxKey(), right.pageID())
}
}

// Get looks for key and returns the corresponding value.
// If key is not found, 0 is returned.
func (t *Tree) Get(k uint64) uint64 {
Expand Down Expand Up @@ -377,7 +404,7 @@ func (t *Tree) split(pid uint64) node {
nn := t.newNode(n.bits())
// Re-read n as the underlying buffer for tree might have changed during newNode.
n = t.node(pid)
rightHalf := n[keyOffset(maxKeys/2):keyOffset(maxKeys)]
rightHalf := n[keyOffset(maxKeys/2):keyOffset(maxKeys-1)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is where I start getting uncomfortable with this PR. I will have to better study this btree impl.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can help with the logic of this custom implementation.

copy(nn, rightHalf)
nn.setNumKeys(maxKeys - maxKeys/2)

Expand Down Expand Up @@ -494,28 +521,6 @@ func (n node) search(k uint64) int {
return N
}
return int(simd.Search(n[:2*N], k))
// lo, hi := 0, N
// // Reduce the search space using binary seach and then do linear search.
// for hi-lo > 32 {
// mid := (hi + lo) / 2
// km := n.key(mid)
// if k == km {
// return mid
// }
// if k > km {
// // key is greater than the key at mid, so move right.
// lo = mid + 1
// } else {
// // else move left.
// hi = mid
// }
// }
// for i := lo; i <= hi; i++ {
// if ki := n.key(i); ki >= k {
// return i
// }
// }
// return N
}
func (n node) maxKey() uint64 {
idx := n.numKeys()
Expand Down Expand Up @@ -593,6 +598,21 @@ func (n node) set(k, v uint64) node {
panic("shouldn't reach here")
}

func (n node) setAsLeaf(k, v uint64) node {
idx := n.numKeys()
n.setAt(keyOffset(idx), k)
n.setAt(valOffset(idx), v)
n.setNumKeys(n.numKeys() + 1)
return n
}
func (n node) setRightmostLeaf(k, v uint64) node {
idx := maxKeys - 1
n.setAt(keyOffset(idx), k)
n.setAt(valOffset(idx), v)
n.setNumKeys(n.numKeys() + 1)
return n
}

func (n node) iterate(fn func(node, int)) {
for i := 0; i < maxKeys; i++ {
if k := n.key(i); k > 0 {
Expand Down