Skip to content

Commit

Permalink
Merge pull request #76 from meilisearch/panic-at-search
Browse files Browse the repository at this point in the history
Fix panic at search when the index is empty
  • Loading branch information
Kerollmops committed Jun 19, 2024
2 parents 7d65b82 + 2128749 commit 6470bb7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ impl<'t, D: Distance> Reader<'t, D> {
search_k: Option<NonZeroUsize>,
candidates: Option<&RoaringBitmap>,
) -> Result<Vec<(ItemId, f32)>> {
if self.items.is_empty() {
return Ok(Vec::new());
}
// Since the datastructure describes a kind of btree, the capacity is something in the order of:
// The number of root nodes + log2 of the total number of vectors.
let mut queue =
Expand Down
16 changes: 16 additions & 0 deletions src/tests/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,19 @@ fn filtering() {
id(99): distance(99)
"###);
}

#[test]
fn search_in_empty_database() {
// See https://github.com/meilisearch/arroy/issues/74
let handle = create_database::<Euclidean>();

let mut wtxn = handle.env.write_txn().unwrap();
let writer = Writer::new(handle.database, 0, 2);
writer.build(&mut wtxn, &mut rng(), None).unwrap();
wtxn.commit().unwrap();

let rtxn = handle.env.read_txn().unwrap();
let reader = Reader::open(&rtxn, 0, handle.database).unwrap();
let ret = reader.nns_by_vector(&rtxn, &[0., 0.], 10, None, None).unwrap();
insta::assert_debug_snapshot!(ret, @"[]");
}
39 changes: 38 additions & 1 deletion src/tests/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rand::seq::SliceRandom;
use rand::Rng;

use super::{create_database, rng};
use crate::distance::{DotProduct, Euclidean};
use crate::distance::{Angular, DotProduct, Euclidean};
use crate::{Database, Reader, Writer};

#[test]
Expand Down Expand Up @@ -467,6 +467,43 @@ fn delete_one_leaf_in_a_split() {
"###);
}

#[test]
fn delete_one_item_in_a_single_document_database() {
let handle = create_database::<Angular>();
let mut rng = rng();
let mut wtxn = handle.env.write_txn().unwrap();
let writer = Writer::new(handle.database, 0, 2);

// first, insert a bunch of elements
writer.add_item(&mut wtxn, 0, &[0., 0.]).unwrap();
writer.build(&mut wtxn, &mut rng, None).unwrap();
wtxn.commit().unwrap();

insta::assert_display_snapshot!(handle, @r###"
==================
Dumping index 0
Item 0: Leaf(Leaf { header: NodeHeaderAngular { norm: 0.0 }, vector: [0.0000, 0.0000] })
Tree 0: Descendants(Descendants { descendants: [0] })
Root: Metadata { dimensions: 2, items: RoaringBitmap<[0]>, roots: [0], distance: "angular" }
updated_item_ids: RoaringBitmap<[]>
"###);

let mut wtxn = handle.env.write_txn().unwrap();
let writer = Writer::new(handle.database, 0, 2);

writer.del_item(&mut wtxn, 0).unwrap();

writer.build(&mut wtxn, &mut rng, None).unwrap();
wtxn.commit().unwrap();

insta::assert_display_snapshot!(handle, @r###"
==================
Dumping index 0
Root: Metadata { dimensions: 2, items: RoaringBitmap<[]>, roots: [], distance: "angular" }
updated_item_ids: RoaringBitmap<[]>
"###);
}

#[test]
fn delete_one_item() {
let handle = create_database::<Euclidean>();
Expand Down

0 comments on commit 6470bb7

Please sign in to comment.