Skip to content

Commit

Permalink
add size-variant
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew authored and Matthew committed Mar 13, 2024
1 parent 07076dd commit d9f18e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/btree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math/bits"
)

// MemoryPointer is a uint64 offset and uint32 length
Expand Down Expand Up @@ -81,13 +82,18 @@ func (n *BPTreeNode) NumPointers() int {
return len(n.InternalPointers) + len(n.LeafPointers)
}

func SizeVariant(v uint64) int {
return int(9*uint32(bits.Len64(v))+64) / 64
}

func (n *BPTreeNode) Size() int64 {

size := 4 // number of keys
for _, k := range n.Keys {
size += 8

lengthBytes := len(binary.AppendUvarint([]byte{}, uint64(k.DataPointer.Length)))
size += lengthBytes
lb := SizeVariant(uint64(k.DataPointer.Length))
size += lb

if n.Width != uint16(0) {
size += len(k.Value)
Expand All @@ -96,8 +102,8 @@ func (n *BPTreeNode) Size() int64 {
for _, n := range n.LeafPointers {
size += 8

lengthBytes := len(binary.AppendUvarint([]byte{}, uint64(n.Length)))
size += lengthBytes
lb := SizeVariant(uint64(n.Length))
size += lb
}
for range n.InternalPointers {
size += 8
Expand Down
11 changes: 11 additions & 0 deletions pkg/btree/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btree

import (
"bytes"
"encoding/binary"
"reflect"
"testing"
)
Expand Down Expand Up @@ -103,3 +104,13 @@ func TestBPTreeNode_CompareReferencedValues(t *testing.T) {
}
}
}

func TestSizeVariant(t *testing.T) {

x := len(binary.AppendUvarint([]byte{}, uint64(123)))
y := SizeVariant(uint64(123))

if x != y {
t.Fatalf("expected x == y, got %v == %v", x, y)
}
}

0 comments on commit d9f18e9

Please sign in to comment.