Skip to content

Commit

Permalink
make DeleteBelow delete values that are less than lo (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
NamanJain8 authored Oct 28, 2020
1 parent e2057c1 commit 0eff948
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions z/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,20 @@ func (n node) maxKey() uint64 {
return n.key(idx)
}

// compacts the node i.e., remove all the kvs with value <= lo. It returns the remaining number of
// compacts the node i.e., remove all the kvs with value < lo. It returns the remaining number of
// keys.
func (n node) compact(lo uint64) int {
// compact should be called only on leaf nodes
assert(n.isLeaf())
N := n.numKeys()
mk := n.maxKey()
// Just zero-out the value of maxKey if value <= lo. Don't remove the key.
if N > 0 && n.val(N-1) <= lo {
if N > 0 && n.val(N-1) < lo {
n.setAt(valOffset(N-1), 0)
}
var left, right int
for right = 0; right < N; right++ {
if n.val(right) <= lo && n.key(right) < mk {
if n.val(right) < lo && n.key(right) < mk {
// Skip over this key. Don't copy it.
continue
}
Expand Down
6 changes: 3 additions & 3 deletions z/btree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func TestTree(t *testing.T) {
}

bt.DeleteBelow(100)
for i := uint64(1); i <= 100; i++ {
for i := uint64(1); i < 100; i++ {
require.Equal(t, uint64(0), bt.Get(i))
}
for i := uint64(101); i < N; i++ {
for i := uint64(100); i < N; i++ {
require.Equal(t, i, bt.Get(i))
}
// bt.Print()
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestNodeCompact(t *testing.T) {
n.set(key, val)
}

require.Equal(t, int(N/2), n.compact(10))
require.Equal(t, int(N/2), n.compact(11))
for k, v := range mp {
require.Equal(t, v, n.get(k))
}
Expand Down

0 comments on commit 0eff948

Please sign in to comment.