Skip to content

Commit

Permalink
updated save signature
Browse files Browse the repository at this point in the history
  • Loading branch information
bwsw committed Jun 4, 2024
1 parent 054c1af commit b454f31
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! hdf5 utilities for examples
pub mod annhdf5;
pub mod annhdf5;
8 changes: 3 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait AnnT {
/// **We do not overwrite old files if they are currently in use by memory map**
/// If these files already exist , they are not overwritten and a unique filename is generated by concatenating a random number to filename.
/// The function returns the basename used for the dump
fn file_dump(&self, filename: &String) -> anyhow::Result<String>;
fn file_dump(&self, path: &PathBuf, file_basename: &str) -> anyhow::Result<String>;
}

impl<'b, T, D> AnnT for Hnsw<'b, T, D>
Expand Down Expand Up @@ -68,14 +68,12 @@ where
///
///
fn file_dump(&self, filename: &String) -> anyhow::Result<String> {
fn file_dump(&self, path: &PathBuf, file_basename: &str) -> anyhow::Result<String> {
log::info!("in Hnsw::file_dump");
//
let mut dir = PathBuf::new();
dir.push(".");
// do not overwrite if mmap is active
let overwrite = !self.get_datamap_opt();
let mut dumpinit = DumpInit::new(dir, filename.clone(), overwrite);
let mut dumpinit = DumpInit::new(path, file_basename, overwrite);
let dumpname = dumpinit.get_basename().clone();
//
let res = self.dump(DumpMode::Full, &mut dumpinit);
Expand Down
9 changes: 5 additions & 4 deletions src/datamap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ mod tests {
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("mmap_test");
let _res = hnsw.file_dump(&fname);
let _res = hnsw.file_dump(&PathBuf::from("."), &fname);

let check_reload = false;
if check_reload {
Expand Down Expand Up @@ -435,10 +435,11 @@ mod tests {
// some loggin info
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("mmap_order_test");
let _res = hnsw.file_dump(&fname);
let fname = "mmap_order_test";
let directory = PathBuf::from(".");
let _res = hnsw.file_dump(&directory, &fname);
// now we have check that datamap seems ok, test reload of hnsw with mmap
let datamap: DataMap = DataMap::from_hnswdump::<u32>(".", &fname).unwrap();
let datamap: DataMap = DataMap::from_hnswdump::<u32>(".", &fname.to_string()).unwrap();
// testing type check
assert!(datamap.check_data_type::<u32>());
assert!(!datamap.check_data_type::<f32>());
Expand Down
16 changes: 9 additions & 7 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
//! defines a trait for filtering requests.
//! See examples in tests/filtertest.rs
//! See examples in tests/filtertest.rs
use crate::prelude::DataId;

/// Only queries returning true are taken into account along the search
pub trait FilterT {
fn hnsw_filter(&self, id:&DataId) -> bool;
fn hnsw_filter(&self, id: &DataId) -> bool;
}

impl FilterT for Vec<usize> {
fn hnsw_filter(&self, id:&DataId) -> bool {
fn hnsw_filter(&self, id: &DataId) -> bool {
return match &self.binary_search(id) {
Ok(_) => true,
_ => false }
_ => false,
};
}
}

impl<F> FilterT for F
where F: Fn(&DataId) -> bool,
where
F: Fn(&DataId) -> bool,
{
fn hnsw_filter(&self, id:&DataId) -> bool {
return self(id)
fn hnsw_filter(&self, id: &DataId) -> bool {
return self(id);
}
}
4 changes: 2 additions & 2 deletions src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ mod tests {
println!("voisins du point 2 {:?}", nbg_2_before);
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("dumpreloadtestflat");
let _res = hnsw.file_dump(&fname);
let directory = PathBuf::from(".");
let _res = hnsw.file_dump(&directory, &fname);
// This will dump in 2 files named dumpreloadtest.hnsw.graph and dumpreloadtest.hnsw.data
//
// reload
log::debug!("\n\n hnsw reload");
// we will need a procedural macro to get from distance name to its instanciation.
// from now on we test with DistL1
let directory = PathBuf::from(".");
let mut reloader = HnswIo::new(directory, String::from("dumpreloadtestflat"));
let hnsw_loaded: Hnsw<NoData, NoDist> = reloader.load_hnsw().unwrap();
let neighborhood_after_dump = FlatNeighborhood::from(&hnsw_loaded);
Expand Down
31 changes: 15 additions & 16 deletions src/hnswio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,14 @@ pub struct DumpInit {

impl DumpInit {
// This structure will check existence of dumps of same name and generate a unique filename if necessary according to overwrite flag
pub fn new(dir: PathBuf, basename_default: String, overwrite: bool) -> Self {
pub fn new(dir: &PathBuf, basename_default: &str, overwrite: bool) -> Self {
// if we cannot overwrite data files (in case of mmap in particular)
// we will ensure we have a unique basename

let basename = match overwrite {
true => basename_default,
true => basename_default.to_string(),
false => {
// we check
let mut dataname = basename_default.clone();
let mut dataname = basename_default.to_string();
dataname.push_str(".hnsw.data");
let mut datapath = dir.clone();
datapath.push(dataname);
Expand All @@ -167,7 +166,7 @@ impl DumpInit {
let mut dataname: String;
let id: usize = rand::thread_rng().gen_range(0..10000);
let strid: String = id.to_string();
unique_basename = basename_default.clone();
unique_basename = basename_default.to_string();
unique_basename.push('-');
unique_basename.push_str(&strid);
dataname = unique_basename.clone();
Expand All @@ -181,7 +180,7 @@ impl DumpInit {
};
unique_basename
} else {
basename_default
basename_default.to_string()
}
}
};
Expand Down Expand Up @@ -1457,13 +1456,13 @@ mod tests {
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("dumpreloadtest1");
let _res = hnsw.file_dump(&fname);
let directory = PathBuf::from(".");
let _res = hnsw.file_dump(&directory, &fname);
//
// reload
log::debug!("\n\n test_dump_reload_1 hnsw reload");
// we will need a procedural macro to get from distance name to its instanciation.
// from now on we test with DistL1
let directory = PathBuf::from(".");
let mut reloader = HnswIo::new(directory, String::from("dumpreloadtest1"));
let hnsw_loaded: Hnsw<f32, DistL1> = reloader.load_hnsw::<f32, DistL1>().unwrap();
// test equality
Expand Down Expand Up @@ -1510,12 +1509,12 @@ mod tests {
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("dumpreloadtest_myfn");
let _res = hnsw.file_dump(&fname);
let directory = PathBuf::from(".");
let _res = hnsw.file_dump(&directory, &fname);
// This will dump in 2 files named dumpreloadtest.hnsw.graph and dumpreloadtest.hnsw.data
//
// reload
log::debug!("\n\n hnsw reload");
let directory = PathBuf::from(".");
let reloader = HnswIo::new(directory, String::from("dumpreloadtest_myfn"));
let mydist = dist::DistPtr::<f32, f32>::new(my_fn);
let _hnsw_loaded: Hnsw<f32, DistPtr<f32, f32>> =
Expand Down Expand Up @@ -1561,12 +1560,12 @@ mod tests {
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("dumpreloadtestgraph");
let _res = hnsw.file_dump(&fname);
let directory = PathBuf::from(".");
let _res = hnsw.file_dump(&directory, &fname);
// This will dump in 2 files named dumpreloadtest.hnsw.graph and dumpreloadtest.hnsw.data
//
// reload
log::debug!("\n\n hnsw reload");
let directory = PathBuf::from(".");
let mut reloader = HnswIo::new(directory, String::from("dumpreloadtestgraph"));
let hnsw_loaded: Hnsw<NoData, NoDist> = reloader.load_hnsw().unwrap();
// test equality
Expand Down Expand Up @@ -1616,12 +1615,12 @@ mod tests {
hnsw.dump_layer_info();
// dump in a file. Must take care of name as tests runs in // !!!
let fname = String::from("mmapreloadtest");
let dumpname = hnsw.file_dump(&fname).unwrap();
let directory = PathBuf::from(".");
let dumpname = hnsw.file_dump(&directory, &fname).unwrap();
log::debug!("dump succeeded in file basename : {}", dumpname);
//
// reload reload_with_mmap
log::debug!("\n\n hnsw reload");
let directory = PathBuf::from(".");
let mut reloader = HnswIo::new(directory.clone(), dumpname);
// use mmap for points after half number of points
let options = ReloadOptions::default().set_mmap_threshold(nbcolumn / 2);
Expand Down Expand Up @@ -1680,9 +1679,9 @@ mod tests {
//
// TODO: redump and care about mmapped file, so we do not overwrite
//
let dump_init = DumpInit::new(directory, fname, false);
let dump_init = DumpInit::new(&directory, &fname, false);
log::info!("will use basename : {}", dump_init.get_basename());
let res = hnsw.file_dump(dump_init.get_basename());
let res = hnsw.file_dump(&directory, dump_init.get_basename());
if res.is_err() {
log::error!("hnsw.file_dump failed");
std::panic!("hnsw.file_dump failed");
Expand Down
2 changes: 1 addition & 1 deletion src/libext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ macro_rules! generate_file_dump(
log::info!("receiving request for file dump");
let slice = unsafe { std::slice::from_raw_parts(filename, namelen) } ;
let fstring = String::from_utf8_lossy(slice).into_owned();
let res = unsafe { (*hnsw_api).opaque.file_dump(&fstring) } ;
let res = unsafe { (*hnsw_api).opaque.file_dump(&PathBuf::from("."), &fstring) } ;
if res.is_ok() {
return 1;
}
Expand Down

0 comments on commit b454f31

Please sign in to comment.