From cc137a78a10d8a5c61c5863efaef0842eee4c578 Mon Sep 17 00:00:00 2001 From: Matthew Kim <38759997+friendlymatthew@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:48:04 -0400 Subject: [PATCH 1/2] sequential search with upper bound param --- pkg/hnsw/hnsw_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/pkg/hnsw/hnsw_test.go b/pkg/hnsw/hnsw_test.go index 99736bb..336d7e5 100644 --- a/pkg/hnsw/hnsw_test.go +++ b/pkg/hnsw/hnsw_test.go @@ -476,6 +476,20 @@ func TestHnsw_InsertVector(t *testing.T) { } } }) + + t.Run("524,288 points inserted", func(t *testing.T) { + numPoints := 524288 + + h := NewHnsw(2, 14, 14, Point{0, 0}) + numPoints -= 1 + + for i := 0; i < numPoints; i++ { + if err := h.InsertVector(Point{float32(i + 1), float32(i + 1)}); err != nil { + t.Fatalf("failed to insert vector: %v", err) + } + } + }) + } func TestHnsw_KnnSearch(t *testing.T) { @@ -598,4 +612,37 @@ func TestHnsw_KnnSearch(t *testing.T) { t.Fatalf("expected closest points to be %v, got %v", expected, got) } }) + + t.Run("sequential search with upper bound params", func(t *testing.T) { + h := NewHnsw(2, 12, 12, Point{0, 0}) + for i := 1; i <= 8; i++ { + if err := h.InsertVector(Point{float32(i), float32(i + 1)}); err != nil { + t.Fatalf("failed to insert point: %v, err: %v", Point{float32(i), float32(i + 1)}, err) + } + } + + found, err := h.KnnSearch(Point{float32(0), float32(0)}, 10) + + if err != nil { + t.Fatalf("failed to find closest neighbors: %v", err) + } + + if found.Len() != 9 { + t.Fatalf("expected to find 9 closest neighbors, got %v", found.Len()) + } + + expectedId := Id(0) + + for found.IsEmpty() { + nnItem, err := found.PopItem() + if err != nil { + t.Fatalf("failed to pop item: %v, err: %v", found, err) + } + if expectedId != nnItem.id { + t.Fatalf("expected to find %v, got %v", expectedId, nnItem.id) + } + + expectedId += 1 + } + }) } From 912ed63681be3b0d1e5eeb6b461495d12083fc7a Mon Sep 17 00:00:00 2001 From: Matthew Kim <38759997+friendlymatthew@users.noreply.github.com> Date: Tue, 11 Jun 2024 10:49:31 -0400 Subject: [PATCH 2/2] remove additional test --- pkg/hnsw/hnsw_test.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/pkg/hnsw/hnsw_test.go b/pkg/hnsw/hnsw_test.go index 336d7e5..ca36756 100644 --- a/pkg/hnsw/hnsw_test.go +++ b/pkg/hnsw/hnsw_test.go @@ -477,19 +477,6 @@ func TestHnsw_InsertVector(t *testing.T) { } }) - t.Run("524,288 points inserted", func(t *testing.T) { - numPoints := 524288 - - h := NewHnsw(2, 14, 14, Point{0, 0}) - numPoints -= 1 - - for i := 0; i < numPoints; i++ { - if err := h.InsertVector(Point{float32(i + 1), float32(i + 1)}); err != nil { - t.Fatalf("failed to insert vector: %v", err) - } - } - }) - } func TestHnsw_KnnSearch(t *testing.T) {