Skip to content

Commit

Permalink
fix: Don't crash on reaching hnsw capacity (#2046)
Browse files Browse the repository at this point in the history
* fix: Don't crash on reaching hnsw capacity

Signed-off-by: Vladislav Oleshko <[email protected]>

---------

Signed-off-by: Vladislav Oleshko <[email protected]>
  • Loading branch information
dranikpg authored Oct 20, 2023
1 parent 1d02e12 commit 4a2dd30
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/search/indices.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ struct HnswlibAdapter {
}

void Add(float* data, DocId id) {
if (world_.cur_element_count + 1 >= world_.max_elements_)
world_.resizeIndex(world_.cur_element_count * 2);
world_.addPoint(data, id);
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/search/search_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ TEST_P(KnnTest, Simple2D) {
}
}

TEST_P(KnnTest, AutoResize) {
// Make sure index resizes automatically even with a small initial capacity
const size_t kInitialCapacity = 5;

auto schema = MakeSimpleSchema({{"pos", SchemaField::VECTOR}});
schema.fields["pos"].special_params =
SchemaField::VectorParams{GetParam(), 1, VectorSimilarity::L2, kInitialCapacity};
FieldIndices indices{schema, PMR_NS::get_default_resource()};

for (size_t i = 0; i < 100; i++) {
MockedDocument doc{Map{{"pos", ToBytes({float(i)})}}};
indices.Add(i, &doc);
}

EXPECT_EQ(indices.GetAllDocs().size(), 100);
}

INSTANTIATE_TEST_SUITE_P(KnnFlat, KnnTest, testing::Values(false));
INSTANTIATE_TEST_SUITE_P(KnnHnsw, KnnTest, testing::Values(true));

Expand Down

0 comments on commit 4a2dd30

Please sign in to comment.