Skip to content

Commit

Permalink
Merge pull request #79 from meilisearch/fix-lints
Browse files Browse the repository at this point in the history
Fix lints and update dependencies
  • Loading branch information
Kerollmops authored Jun 20, 2024
2 parents 67e74b8 + b21f732 commit 973f093
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ license = "MIT"
edition = "2021"

[dependencies]
bytemuck = { version = "1.14.0", features = ["derive", "extern_crate_alloc"] }
bytemuck = { version = "1.16.1", features = ["derive", "extern_crate_alloc"] }
byteorder = "1.5.0"
heed = { version = "0.20.1", default-features = false }
log = "0.4.20"
memmap2 = "0.9.0"
ordered-float = "4.1.1"
heed = { version = "0.20.2", default-features = false }
log = "0.4.21"
memmap2 = "0.9.4"
ordered-float = "4.2.0"
rand = { version = "0.8.5", features = ["alloc"] }
rayon = "1.8.0"
roaring = "0.10.2"
tempfile = "3.8.1"
thiserror = "1.0.50"
rayon = "1.10.0"
roaring = "0.10.5"
tempfile = "3.10.1"
thiserror = "1.0.61"

[dev-dependencies]
anyhow = "1.0.75"
anyhow = "1.0.86"
arbitrary = { version = "1.3.2", features = ["derive"] }
clap = { version = "4.4.10", features = ["derive"] }
env_logger = "0.10.1"
insta = "1.34.0"
clap = { version = "4.5.7", features = ["derive"] }
env_logger = "0.11.3"
insta = "1.39.0"
instant-distance = "0.6.1"
rand = { version = "0.8.5", features = ["std_rng"] }
tempfile = "3.8.1"
tempfile = "3.10.1"

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl UnalignedF32Slice {
/// Creates an unaligned slice of f32 wrapper from a slice of bytes.
pub fn from_bytes(bytes: &[u8]) -> Result<&Self, SizeMismatch> {
if bytes.len() % size_of::<f32>() == 0 {
// safety: `UnalignedF32Slice` is transparent
Ok(unsafe { transmute(bytes) })

Check warning on line 61 in src/node.rs

View workflow job for this annotation

GitHub Actions / lint

transmute used without annotations
} else {
Err(SizeMismatch)
Expand Down
8 changes: 8 additions & 0 deletions src/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl<'a, DE: BytesEncode<'a>> TmpNodes<DE> {
/// Converts it into a readers to read the nodes.
pub fn into_bytes_reader(self) -> Result<TmpNodesReader> {
let file = self.file.into_inner().map_err(|iie| iie.into_error())?;
// safety: No one should move our files around
let mmap = unsafe { Mmap::map(&file)? };
#[cfg(unix)]
mmap.advise(memmap2::Advice::Sequential)?;
Expand Down Expand Up @@ -224,6 +225,10 @@ impl<'t, D: Distance> ImmutableLeafs<'t, D> {
Some(ptr) => *ptr,
None => return Ok(None),
};

// safety:
// - ptr: The pointer comes from LMDB. Since the database cannot be written to, it is still valid.
// - len: All the items share the same dimensions and are the same size
let bytes = unsafe { slice::from_raw_parts(ptr, len) };
NodeCodec::bytes_decode(bytes).map_err(heed::Error::Decoding).map(|node| node.leaf())
}
Expand Down Expand Up @@ -326,6 +331,9 @@ impl<'t, D: Distance> ImmutableTrees<'t, D> {
None => return Ok(None),
};

// safety:
// - ptr: The pointer comes from LMDB. Since the database cannot be written to, it is still valid.
// - len: The len cannot change either
let bytes = unsafe { slice::from_raw_parts(ptr, len) };
NodeCodec::bytes_decode(bytes).map_err(heed::Error::Decoding).map(Some)
}
Expand Down
18 changes: 9 additions & 9 deletions src/tests/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn open_unfinished_db() {

let rtxn = handle.env.read_txn().unwrap();
let ret = Reader::<Euclidean>::open(&rtxn, 0, handle.database).map(|_| ()).unwrap_err();
insta::assert_display_snapshot!(ret, @"Metadata are missing on index 0, You must build your database before attempting to read it");
insta::assert_snapshot!(ret, @"Metadata are missing on index 0, You must build your database before attempting to read it");
}

#[test]
Expand All @@ -50,7 +50,7 @@ fn open_db_with_wrong_dimension() {
let rtxn = handle.env.read_txn().unwrap();
let reader = Reader::<Euclidean>::open(&rtxn, 0, handle.database).unwrap();
let ret = reader.nns_by_vector(&rtxn, &[1.0, 2.0, 3.0], 5, None, None).unwrap_err();
insta::assert_display_snapshot!(ret, @"Invalid vector dimensions. Got 3 but expected 2");
insta::assert_snapshot!(ret, @"Invalid vector dimensions. Got 3 but expected 2");
}

#[test]
Expand Down Expand Up @@ -89,7 +89,7 @@ fn search_in_db_with_a_single_vector() {
let reader = Reader::<Angular>::open(&rtxn, 0, handle.database).unwrap();

let ret = reader.nns_by_item(&rtxn, 0, 1, None, None).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(0): distance(0)
"###);
}
Expand All @@ -113,14 +113,14 @@ fn two_dimension_on_a_line() {

// if we can't look into enough nodes we find some random points
let ret = reader.nns_by_item(&rtxn, 0, 5, NonZeroUsize::new(1), None).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(48): distance(48)
id(92): distance(92)
"###);

// if we can look into all the node there is no inifinite loop and it works
let ret = reader.nns_by_item(&rtxn, 0, 5, NonZeroUsize::new(usize::MAX), None).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(0): distance(0)
id(1): distance(1)
id(2): distance(2)
Expand All @@ -129,7 +129,7 @@ fn two_dimension_on_a_line() {
"###);

let ret = reader.nns_by_item(&rtxn, 0, 5, None, None).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(1): distance(1)
id(2): distance(2)
id(3): distance(3)
Expand Down Expand Up @@ -160,7 +160,7 @@ fn two_dimension_on_a_column() {
let reader = Reader::<Euclidean>::open(&rtxn, 0, handle.database).unwrap();
let ret = reader.nns_by_item(&rtxn, 0, 5, None, None).unwrap();

insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(1): distance(1)
id(2): distance(2)
id(3): distance(3)
Expand Down Expand Up @@ -208,14 +208,14 @@ fn filtering() {
let reader = Reader::<Euclidean>::open(&rtxn, 0, handle.database).unwrap();

let ret = reader.nns_by_item(&rtxn, 0, 5, None, Some(&RoaringBitmap::from_iter(0..2))).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(0): distance(0)
id(1): distance(1)
"###);

let ret =
reader.nns_by_item(&rtxn, 0, 5, None, Some(&RoaringBitmap::from_iter(98..1000))).unwrap();
insta::assert_display_snapshot!(NnsRes(ret), @r###"
insta::assert_snapshot!(NnsRes(ret), @r###"
id(98): distance(98)
id(99): distance(99)
"###);
Expand Down
Loading

0 comments on commit 973f093

Please sign in to comment.