diff --git a/src/core/index.rs b/src/core/index.rs index e1ad16b1f..f697a0dc9 100644 --- a/src/core/index.rs +++ b/src/core/index.rs @@ -123,8 +123,8 @@ impl IndexBuilder { /// If a previous index was in this directory, it returns an `IndexAlreadyExists` error. #[cfg(feature = "mmap")] pub fn create_in_dir>(self, directory_path: P) -> crate::Result { - let mmap_directory = MmapDirectory::open(directory_path)?; - if Index::exists(&mmap_directory)? { + let mmap_directory: Box = Box::new(MmapDirectory::open(directory_path)?); + if Index::exists(&*mmap_directory)? { return Err(TantivyError::IndexAlreadyExists); } self.create(mmap_directory) @@ -139,7 +139,7 @@ impl IndexBuilder { /// For other unit tests, prefer the `RAMDirectory`, see: `create_in_ram`. #[cfg(feature = "mmap")] pub fn create_from_tempdir(self) -> crate::Result { - let mmap_directory = MmapDirectory::create_from_tempdir()?; + let mmap_directory: Box = Box::new(MmapDirectory::create_from_tempdir()?); self.create(mmap_directory) } fn get_expect_schema(&self) -> crate::Result { @@ -149,8 +149,9 @@ impl IndexBuilder { .ok_or(TantivyError::IndexBuilderMissingArgument("schema")) } /// Opens or creates a new index in the provided directory - pub fn open_or_create(self, dir: Dir) -> crate::Result { - if !Index::exists(&dir)? { + pub fn open_or_create>>(self, dir: T) -> crate::Result { + let dir = dir.into(); + if !Index::exists(&*dir)? { return self.create(dir); } let index = Index::open(dir)?; @@ -165,7 +166,8 @@ impl IndexBuilder { /// Creates a new index given an implementation of the trait `Directory`. /// /// If a directory previously existed, it will be erased. - fn create(self, dir: Dir) -> crate::Result { + fn create>>(self, dir: T) -> crate::Result { + let dir = dir.into(); let directory = ManagedDirectory::wrap(dir)?; save_new_metas( self.get_expect_schema()?, @@ -198,7 +200,7 @@ impl Index { /// Examines the directory to see if it contains an index. /// /// Effectively, it only checks for the presence of the `meta.json` file. - pub fn exists(dir: &Dir) -> Result { + pub fn exists(dir: &dyn Directory) -> Result { dir.exists(&META_FILEPATH) } @@ -250,7 +252,11 @@ impl Index { } /// Opens or creates a new index in the provided directory - pub fn open_or_create(dir: Dir, schema: Schema) -> crate::Result { + pub fn open_or_create>>( + dir: T, + schema: Schema, + ) -> crate::Result { + let dir = dir.into(); IndexBuilder::new().schema(schema).open_or_create(dir) } @@ -270,11 +276,12 @@ impl Index { /// Creates a new index given an implementation of the trait `Directory`. /// /// If a directory previously existed, it will be erased. - pub fn create( - dir: Dir, + pub fn create>>( + dir: T, schema: Schema, settings: IndexSettings, ) -> crate::Result { + let dir: Box = dir.into(); let mut builder = IndexBuilder::new().schema(schema); builder = builder.settings(settings); builder.create(dir) @@ -365,7 +372,8 @@ impl Index { } /// Open the index using the provided directory - pub fn open(directory: D) -> crate::Result { + pub fn open>>(directory: T) -> crate::Result { + let directory = directory.into(); let directory = ManagedDirectory::wrap(directory)?; let inventory = SegmentMetaInventory::default(); let metas = load_metas(&directory, &inventory)?; @@ -577,15 +585,15 @@ mod tests { #[test] fn test_index_exists() { - let directory = RamDirectory::create(); - assert!(!Index::exists(&directory).unwrap()); + let directory: Box = Box::new(RamDirectory::create()); + assert!(!Index::exists(directory.as_ref()).unwrap()); assert!(Index::create( directory.clone(), throw_away_schema(), IndexSettings::default() ) .is_ok()); - assert!(Index::exists(&directory).unwrap()); + assert!(Index::exists(directory.as_ref()).unwrap()); } #[test] @@ -598,27 +606,27 @@ mod tests { #[test] fn open_or_create_should_open() { - let directory = RamDirectory::create(); + let directory: Box = Box::new(RamDirectory::create()); assert!(Index::create( directory.clone(), throw_away_schema(), IndexSettings::default() ) .is_ok()); - assert!(Index::exists(&directory).unwrap()); + assert!(Index::exists(directory.as_ref()).unwrap()); assert!(Index::open_or_create(directory, throw_away_schema()).is_ok()); } #[test] fn create_should_wipeoff_existing() { - let directory = RamDirectory::create(); + let directory: Box = Box::new(RamDirectory::create()); assert!(Index::create( directory.clone(), throw_away_schema(), IndexSettings::default() ) .is_ok()); - assert!(Index::exists(&directory).unwrap()); + assert!(Index::exists(directory.as_ref()).unwrap()); assert!(Index::create( directory, Schema::builder().build(), diff --git a/src/directory/directory.rs b/src/directory/directory.rs index fbbbbdd3f..36c0172c5 100644 --- a/src/directory/directory.rs +++ b/src/directory/directory.rs @@ -230,3 +230,15 @@ where Box::new(self.clone()) } } + +impl Clone for Box { + fn clone(&self) -> Self { + self.box_clone() + } +} + +impl From for Box { + fn from(t: T) -> Self { + Box::new(t) + } +} diff --git a/src/directory/managed_directory.rs b/src/directory/managed_directory.rs index 5a358552b..3034558ef 100644 --- a/src/directory/managed_directory.rs +++ b/src/directory/managed_directory.rs @@ -64,7 +64,7 @@ fn save_managed_paths( impl ManagedDirectory { /// Wraps a directory as managed directory. - pub fn wrap(directory: Dir) -> crate::Result { + pub fn wrap(directory: Box) -> crate::Result { match directory.atomic_read(&MANAGED_FILEPATH) { Ok(data) => { let managed_files_json = String::from_utf8_lossy(&data); @@ -76,14 +76,14 @@ impl ManagedDirectory { ) })?; Ok(ManagedDirectory { - directory: Box::new(directory), + directory, meta_informations: Arc::new(RwLock::new(MetaInformation { managed_paths: managed_files, })), }) } Err(OpenReadError::FileDoesNotExist(_)) => Ok(ManagedDirectory { - directory: Box::new(directory), + directory, meta_informations: Arc::default(), }), io_err @ Err(OpenReadError::IoError { .. }) => Err(io_err.err().unwrap().into()), @@ -340,7 +340,7 @@ mod tests_mmap_specific { let test_path2: &'static Path = Path::new("some_path_for_test_2"); { let mmap_directory = MmapDirectory::open(&tempdir_path).unwrap(); - let mut managed_directory = ManagedDirectory::wrap(mmap_directory).unwrap(); + let mut managed_directory = ManagedDirectory::wrap(Box::new(mmap_directory)).unwrap(); let write_file = managed_directory.open_write(test_path1).unwrap(); write_file.terminate().unwrap(); managed_directory @@ -355,7 +355,7 @@ mod tests_mmap_specific { } { let mmap_directory = MmapDirectory::open(&tempdir_path).unwrap(); - let mut managed_directory = ManagedDirectory::wrap(mmap_directory).unwrap(); + let mut managed_directory = ManagedDirectory::wrap(Box::new(mmap_directory)).unwrap(); assert!(managed_directory.exists(test_path1).unwrap()); assert!(!managed_directory.exists(test_path2).unwrap()); let living_files: HashSet = HashSet::new(); @@ -374,7 +374,7 @@ mod tests_mmap_specific { let living_files = HashSet::new(); let mmap_directory = MmapDirectory::open(&tempdir_path).unwrap(); - let mut managed_directory = ManagedDirectory::wrap(mmap_directory).unwrap(); + let mut managed_directory = ManagedDirectory::wrap(Box::new(mmap_directory)).unwrap(); let mut write = managed_directory.open_write(test_path1).unwrap(); write.write_all(&[0u8, 1u8]).unwrap(); write.terminate().unwrap(); diff --git a/src/indexer/demuxer.rs b/src/indexer/demuxer.rs index 9d9ad56ec..2ac7dafd8 100644 --- a/src/indexer/demuxer.rs +++ b/src/indexer/demuxer.rs @@ -107,11 +107,11 @@ fn get_alive_bitsets( /// The number of output_directories need to match max new segment ordinal from `demux_mapping`. /// /// The ordinal of `segments` need to match the ordinals provided in `demux_mapping`. -pub fn demux( +pub fn demux( segments: &[Segment], demux_mapping: &DemuxMapping, target_settings: IndexSettings, - output_directories: Vec, + output_directories: Vec>, ) -> crate::Result> { let mut indices = vec![]; for (target_segment_ord, output_directory) in output_directories.into_iter().enumerate() { @@ -253,7 +253,10 @@ mod tests { &segments, &demux_mapping, target_settings, - vec![RamDirectory::default(), RamDirectory::default()], + vec![ + Box::new(RamDirectory::default()), + Box::new(RamDirectory::default()), + ], )?; { diff --git a/src/indexer/segment_updater.rs b/src/indexer/segment_updater.rs index e31e88eb1..cc2064526 100644 --- a/src/indexer/segment_updater.rs +++ b/src/indexer/segment_updater.rs @@ -160,9 +160,9 @@ fn merge( /// meant to work if you have an IndexWriter running for the origin indices, or /// the destination Index. #[doc(hidden)] -pub fn merge_indices( +pub fn merge_indices>>( indices: &[Index], - output_directory: Dir, + output_directory: T, ) -> crate::Result { if indices.is_empty() { // If there are no indices to merge, there is no need to do anything. @@ -206,11 +206,11 @@ pub fn merge_indices( /// meant to work if you have an IndexWriter running for the origin indices, or /// the destination Index. #[doc(hidden)] -pub fn merge_filtered_segments( +pub fn merge_filtered_segments>>( segments: &[Segment], target_settings: IndexSettings, filter_doc_ids: Vec>, - output_directory: Dir, + output_directory: T, ) -> crate::Result { if segments.is_empty() { // If there are no indices to merge, there is no need to do anything. @@ -690,6 +690,7 @@ mod tests { use crate::indexer::segment_updater::merge_filtered_segments; use crate::query::QueryParser; use crate::schema::*; + use crate::Directory; use crate::DocAddress; use crate::Index; use crate::Segment; @@ -882,7 +883,7 @@ mod tests { } assert_eq!(indices.len(), 3); - let output_directory = RamDirectory::default(); + let output_directory: Box = Box::new(RamDirectory::default()); let index = merge_indices(&indices, output_directory)?; assert_eq!(index.schema(), schema); diff --git a/tests/failpoints/mod.rs b/tests/failpoints/mod.rs index 238881074..45c72a92b 100644 --- a/tests/failpoints/mod.rs +++ b/tests/failpoints/mod.rs @@ -10,7 +10,7 @@ fn test_failpoints_managed_directory_gc_if_delete_fails() { let test_path: &'static Path = Path::new("some_path_for_test"); - let ram_directory = RamDirectory::create(); + let ram_directory = Box::new(RamDirectory::create()); let mut managed_directory = ManagedDirectory::wrap(ram_directory).unwrap(); managed_directory .open_write(test_path)