Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Feb 14, 2024
1 parent 8695f2e commit 4963415
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 38 deletions.
5 changes: 4 additions & 1 deletion pkg/btree/bptree.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (t *BPTree) traverse(key ReferencedValue, node *BPTreeNode, ptr MemoryPoint
}

func (t *BPTree) Insert(key ReferencedValue, value MemoryPointer) error {
fmt.Printf("\ninsert parameters look like \nkey: %v\nvalue: %v\n\n", key, value)
root, rootOffset, err := t.root()
if err != nil {
return fmt.Errorf("read root node: %w", err)
Expand All @@ -123,11 +124,13 @@ func (t *BPTree) Insert(key ReferencedValue, value MemoryPointer) error {
return err
}

log.Printf("path: %v", path)
log.Printf("path: %v\n", path)

// insert the key into the leaf
n := path[0].node
fmt.Printf("keys %v and key %v", n.Keys, key)
j, found := slices.BinarySearchFunc(n.Keys, key, CompareReferencedValues)
fmt.Printf("binary search results: j: %v and found %v", j, found)
if found {
return fmt.Errorf("key already exists")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/btree/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func CompareReferencedValues(a, b ReferencedValue) int {
if cmp != 0 {
return cmp
}

fmt.Printf("the offsets to compare are: %v %v\n", a.DataPointer.Offset, b.DataPointer.Offset)
if a.DataPointer.Offset != b.DataPointer.Offset {
return int(a.DataPointer.Offset - b.DataPointer.Offset)
}
Expand Down
36 changes: 28 additions & 8 deletions pkg/handlers/csv_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"encoding/binary"
"log/slog"
"math"
Expand Down Expand Up @@ -99,25 +100,35 @@ func TestCSV(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {

if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(test1) = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != uint64(len("test\n")) || mp1.Length != uint32(len("test1")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {%d, %d}", mp1, len("test\n"), len("test1"))
}

mp2, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test2")})
rv2, mp2, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test2")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test2\") = nil, want non-nil")
}

if !bytes.Equal(rv2.Value, []byte("test2")) {
t.Errorf("incorrect values, got %v, want %v", rv2.Value, []byte("test2"))
}

if mp2.Offset != uint64(len("test\ntest1\n")) || mp2.Length != uint32(len("test2")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test2\") = %+v, want {%d, %d}", mp2, len("test\ntest1\n"), len("test2"))
}
Expand Down Expand Up @@ -158,13 +169,18 @@ func TestCSV(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != uint64(len("test\n")) || mp1.Length != uint32(len("test1")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {%d, %d}", mp1, len("test\n"), len("test1"))
}
Expand All @@ -183,13 +199,17 @@ func TestCSV(t *testing.T) {

v2 := make([]byte, 8)
binary.BigEndian.PutUint64(v2, math.Float64bits(123))
mp2, found, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: v2})
rv2, mp2, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: v2})
if err != nil {
t.Fatal(err)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = nil, want non-nil")
}
if !bytes.Equal(rv2.Value, v2) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, v2)
}

if mp2.Offset != uint64(len("test\ntest1\n")) || mp2.Length != uint32(len("123")) {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = %+v, want {%d, %d}", mp2, len("test\ntest1\n"), len("123"))
}
Expand Down
32 changes: 22 additions & 10 deletions pkg/handlers/equality_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"encoding/binary"
"fmt"
"log/slog"
Expand Down Expand Up @@ -186,47 +187,58 @@ func compareMetaPages(i1, i2 []*btree.LinkedMetaPage, jr, cr []byte) (bool, stri
if i == 0 {

for _, val := range h1 {
_, found, err := collected1.BPTree(jr).Find(btree.ReferencedValue{Value: []byte(val)})
rv1, mp1, err := collected1.BPTree(jr).Find(btree.ReferencedValue{Value: []byte(val)})

if err != nil {
return false, fmt.Sprintf("failed to find btree for jsonl reader %v", val)
}
if !found {
return false, fmt.Sprintf("failed to find %v for josnl reader", val)
if mp1 == (btree.MemoryPointer{}) {
return false, fmt.Sprintf("failed to find %v for reader", val)
}

_, found, err = collected2.BPTree(cr).Find(btree.ReferencedValue{Value: []byte(val)})
rv2, mp2, err := collected2.BPTree(cr).Find(btree.ReferencedValue{Value: []byte(val)})

if err != nil {
return false, fmt.Sprintf("failed to find btree for jsonl reader %v", val)
}
if !found {
return false, fmt.Sprintf("failed to find %v for josnl reader", val)
if mp2 == (btree.MemoryPointer{}) {
return false, fmt.Sprintf("failed to find %v for reader", val)
}

if !bytes.Equal(rv1.Value, rv2.Value) {
return false, fmt.Sprintf("mismatched keys: %v, %v", rv1.Value, rv2.Value)
}

}

} else if i == 1 {
for _, val := range h2 {

v2 := make([]byte, 8)
binary.BigEndian.PutUint64(v2, math.Float64bits(val))
_, found, err := collected1.BPTree(jr).Find(btree.ReferencedValue{Value: v2})
rv1, mp1, err := collected1.BPTree(jr).Find(btree.ReferencedValue{Value: v2})

if err != nil {
return false, fmt.Sprintf("failed to find btree for jsonl reader %v", val)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
return false, fmt.Sprintf("failed to find %v for josnl reader", val)
}

_, found, err = collected2.BPTree(cr).Find(btree.ReferencedValue{Value: v2})
fmt.Printf("rv1: %v", rv1)

rv2, mp2, err := collected2.BPTree(cr).Find(btree.ReferencedValue{Value: v2})

if err != nil {
return false, fmt.Sprintf("failed to find btree for jsonl reader %v", val)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
return false, fmt.Sprintf("failed to find %v for josnl reader", val)
}

if !bytes.Equal(rv1.Value, rv2.Value) {
return false, fmt.Sprintf("mismatched keys: %v, %v", rv1.Value, rv2.Value)
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/handlers/jsonl.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,16 @@ func handleJSONLObject(f *appendable.IndexFile, r []byte, dec *json.Decoder, pat
return fmt.Errorf("unexpected token '%v'", value)
}
case nil:
fmt.Printf("\n\ninserting data with offset: %v\n", data.Offset)
// nil values are a bit of a degenerate case, we are essentially using the btree
// as a set. we store the value as an empty byte slice.
if err := page.BPTree(r).Insert(btree.ReferencedValue{Value: []byte{}}, data); err != nil {
return fmt.Errorf("failed to insert into b+tree: %w", err)
if err := page.BPTree(r).Insert(btree.ReferencedValue{
Value: []byte{},
DataPointer: data,
}, data); err != nil {
return fmt.Errorf("failed to insert into b+tree: %w\nmp: %v\n", err, data.Offset)
} else {
fmt.Printf("mp: %v\n", data.Offset)
}
default:
return fmt.Errorf("unexpected type '%T'", value)
Expand Down
75 changes: 58 additions & 17 deletions pkg/handlers/jsonl_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"bytes"
"encoding/binary"
"math"
"testing"
Expand Down Expand Up @@ -86,24 +87,34 @@ func TestJSONL(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != 0 || mp1.Length != uint32(len("{\"test\":\"test1\"}")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {0, %d}", mp1, len("{\"test\":\"test1\"}"))
}

mp2, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test3")})
rv2, mp2, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test3")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test3\") = nil, want non-nil")
}

if !bytes.Equal(rv2.Value, []byte("test3")) {
t.Errorf("incorrect values, got %v, want %v", rv2.Value, []byte("test3"))
}

if mp2.Offset != uint64(len("{\"test\":\"test1\"}\n")) || mp2.Length != uint32(len("{\"test\":\"test3\"}")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test3\") = %+v, want {%d, %d}", mp2, len("{\"test\":\"test1\"}\n"), len("{\"test\":\"test3\"}"))
}
Expand Down Expand Up @@ -141,13 +152,18 @@ func TestJSONL(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != 0 || mp1.Length != uint32(len("{\"test\":\"test1\"}")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {0, %d}", mp1, len("{\"test\":\"test1\"}"))
}
Expand All @@ -164,13 +180,18 @@ func TestJSONL(t *testing.T) {
t.Errorf("got i.Indexes[0].FieldType = %#v, want FieldTypeString", md1.FieldType)
}

mp2, found, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test3")})
rv2, mp2, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test3")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = nil, want non-nil")
}

if !bytes.Equal(rv2.Value, []byte("test3")) {
t.Errorf("incorrect values, got %v, want %v", rv2.Value, []byte("test3"))
}

if mp2.Offset != uint64(len("{\"test\":\"test1\"}\n")) || mp2.Length != uint32(len("{\"test2\":\"test3\"}")) {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = %+v, want {%d, %d}", mp2, len("{\"test\":\"test1\"}\n"), len("{\"test2\":\"test3\"}"))
}
Expand Down Expand Up @@ -216,13 +237,18 @@ func TestJSONL(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != 0 || mp1.Length != uint32(len("{\"test\":\"test1\"}")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {0, %d}", mp1, len("{\"test\":\"test1\"}"))
}
Expand All @@ -241,13 +267,18 @@ func TestJSONL(t *testing.T) {

v2 := make([]byte, 8)
binary.BigEndian.PutUint64(v2, math.Float64bits(123))
mp2, found, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: v2})
rv2, mp2, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{Value: v2})
if err != nil {
t.Fatal(err)
}
if !found {
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[1].BPTree().Find(123) = nil, want non-nil")
}

if !bytes.Equal(rv2.Value, v2) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, v2)
}

if mp2.Offset != uint64(len("{\"test\":\"test1\"}\n")) || mp2.Length != uint32(len("{\"test\":123}")) {
t.Errorf("got i.Indexes[1].BPTree().Find(123)= %+v, want {%d, %d}", mp2, len("{\"test\":\"test1\"}\n"), len("{\"test\":123}"))
}
Expand Down Expand Up @@ -507,13 +538,18 @@ func TestJSONL(t *testing.T) {
t.Errorf("got len(i.Indexes) = %d, want 1", len(collected))
}

mp1, found, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
rv1, mp1, err := collected[0].BPTree(r2).Find(btree.ReferencedValue{Value: []byte("test1")})
if err != nil {
t.Fatal(err)
}
if !found {
if mp1 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = nil, want non-nil")
}

if !bytes.Equal(rv1.Value, []byte("test1")) {
t.Errorf("incorrect values, got %v, want %v", rv1.Value, []byte("test1"))
}

if mp1.Offset != 0 || mp1.Length != uint32(len("{\"test\":\"test1\"}")) {
t.Errorf("got i.Indexes[0].BPTree().Find(\"test1\") = %+v, want {0, %d}", mp1, len("{\"test\":\"test1\"}"))
}
Expand All @@ -530,13 +566,18 @@ func TestJSONL(t *testing.T) {
t.Errorf("got i.Indexes[0].FieldType = %#v, want FieldTypeString", md1.FieldType)
}

mp2, found, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{})
rv2, mp2, err := collected[1].BPTree(r2).Find(btree.ReferencedValue{})
if err != nil {
t.Fatal(err)
}
if !found {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = nil, want non-nil")
if mp2 == (btree.MemoryPointer{}) {
t.Errorf("got i.Indexes[1].BPTree().Find(null) = nil, want non-nil")
}

if len(rv2.Value) != 0 {
t.Errorf("incorrect values, got %v, want %v", rv2.Value, "null")
}

if mp2.Offset != uint64(len("{\"test\":\"test1\"}\n")) || mp2.Length != uint32(len("{\"test\":null}")) {
t.Errorf("got i.Indexes[1].BPTree().Find(\"test3\") = %+v, want {%d, %d}", mp2, len("{\"test\":\"test1\"}\n"), len("{\"test\":null}"))
}
Expand Down

0 comments on commit 4963415

Please sign in to comment.