Skip to content

Commit 2128749

Browse files
committed
fix a panic at search time when the index is empty
1 parent 9a6559f commit 2128749

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/reader.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ impl<'t, D: Distance> Reader<'t, D> {
203203
search_k: Option<NonZeroUsize>,
204204
candidates: Option<&RoaringBitmap>,
205205
) -> Result<Vec<(ItemId, f32)>> {
206+
if self.items.is_empty() {
207+
return Ok(Vec::new());
208+
}
206209
// Since the datastructure describes a kind of btree, the capacity is something in the order of:
207210
// The number of root nodes + log2 of the total number of vectors.
208211
let mut queue =

src/tests/reader.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,19 @@ fn filtering() {
203203
id(99): distance(99)
204204
"###);
205205
}
206+
207+
#[test]
208+
fn search_in_empty_database() {
209+
// See https://github.com/meilisearch/arroy/issues/74
210+
let handle = create_database::<Euclidean>();
211+
212+
let mut wtxn = handle.env.write_txn().unwrap();
213+
let writer = Writer::new(handle.database, 0, 2);
214+
writer.build(&mut wtxn, &mut rng(), None).unwrap();
215+
wtxn.commit().unwrap();
216+
217+
let rtxn = handle.env.read_txn().unwrap();
218+
let reader = Reader::open(&rtxn, 0, handle.database).unwrap();
219+
let ret = reader.nns_by_vector(&rtxn, &[0., 0.], 10, None, None).unwrap();
220+
insta::assert_debug_snapshot!(ret, @"[]");
221+
}

0 commit comments

Comments
 (0)