diff --git a/src/lib.rs b/src/lib.rs index 9f2ff57..7821b2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ //! value_log.register_writer(writer)?; //! //! // Get some stats -//! assert_eq!(1.0, value_log.manifest.space_amp()); +//! assert_eq!(1.0, value_log.space_amp()); //! # //! # Ok(()) //! # } diff --git a/src/value_log.rs b/src/value_log.rs index 7ff2bdd..ec49589 100644 --- a/src/value_log.rs +++ b/src/value_log.rs @@ -248,7 +248,7 @@ impl ValueLog { /// merge to reach a certain space amplification. #[must_use] pub fn select_segments_for_space_amp_reduction(&self, space_amp_target: f32) -> Vec { - let current_space_amp = self.manifest.space_amp(); + let current_space_amp = self.space_amp(); if current_space_amp < space_amp_target { log::trace!("Space amp is <= target {space_amp_target}, nothing to do"); @@ -348,6 +348,14 @@ impl ValueLog { } } + /// Returns the approximate space amplification + /// + /// Returns 0.0 if there are no items. + #[must_use] + pub fn space_amp(&self) -> f32 { + self.manifest.space_amp() + } + /// Scans the given index and collecting GC statistics. /// /// # Errors @@ -441,7 +449,7 @@ impl ValueLog { log::info!("Total bytes: {}", self.manifest.total_bytes()); log::info!("Stale bytes: {}", self.manifest.stale_bytes()); - log::info!("Space amp: {}", self.manifest.space_amp()); + log::info!("Space amp: {}", self.space_amp()); log::info!("--- GC report done ---"); Ok(()) diff --git a/tests/gc_space_amp.rs b/tests/gc_space_amp.rs index ba0e2e4..2d2caef 100644 --- a/tests/gc_space_amp.rs +++ b/tests/gc_space_amp.rs @@ -10,7 +10,7 @@ fn gc_space_amp_target_1() -> value_log::Result<()> { let value_log = ValueLog::open(vl_path, Config::default())?; - assert_eq!(0.0, value_log.manifest.space_amp()); + assert_eq!(0.0, value_log.space_amp()); assert_eq!(0.0, value_log.manifest.stale_ratio()); let key = "key"; @@ -36,7 +36,7 @@ fn gc_space_amp_target_1() -> value_log::Result<()> { value_log.scan_for_stats(index.read().unwrap().values().cloned().map(Ok))?; - assert!(value_log.manifest.space_amp() > 2.0); + assert!(value_log.space_amp() > 2.0); { let target_space_amp = 8.0; @@ -46,7 +46,7 @@ fn gc_space_amp_target_1() -> value_log::Result<()> { value_log.drop_stale_segments()?; value_log.scan_for_stats(index.read().unwrap().values().cloned().map(Ok))?; - assert!(value_log.manifest.space_amp() <= target_space_amp); + assert!(value_log.space_amp() <= target_space_amp); } { @@ -57,7 +57,7 @@ fn gc_space_amp_target_1() -> value_log::Result<()> { value_log.drop_stale_segments()?; value_log.scan_for_stats(index.read().unwrap().values().cloned().map(Ok))?; - assert!(value_log.manifest.space_amp() <= target_space_amp); + assert!(value_log.space_amp() <= target_space_amp); } Ok(()) diff --git a/tests/space_amp.rs b/tests/space_amp.rs index 976f106..d3c346d 100644 --- a/tests/space_amp.rs +++ b/tests/space_amp.rs @@ -10,7 +10,7 @@ fn worst_case_space_amp() -> value_log::Result<()> { let value_log = ValueLog::open(vl_path, Config::default())?; - assert_eq!(0.0, value_log.manifest.space_amp()); + assert_eq!(0.0, value_log.space_amp()); assert_eq!(0.0, value_log.manifest.stale_ratio()); let key = "key"; @@ -28,7 +28,7 @@ fn worst_case_space_amp() -> value_log::Result<()> { value_log.scan_for_stats(index.read().unwrap().values().cloned().map(Ok))?; - assert_eq!(x as f32, value_log.manifest.space_amp()); + assert_eq!(x as f32, value_log.space_amp()); if x > 1 { assert!((1.0 - (1.0 / x as f32) - value_log.manifest.stale_ratio()) < 0.00001); @@ -48,7 +48,7 @@ fn no_overlap_space_amp() -> value_log::Result<()> { let value_log = ValueLog::open(vl_path, Config::default())?; assert_eq!(0.0, value_log.manifest.stale_ratio()); - assert_eq!(0.0, value_log.manifest.space_amp()); + assert_eq!(0.0, value_log.space_amp()); // NOTE: No blobs overlap, so there are no dead blobs => space amp = 1.0 => perfect space amp for i in 0..100 { @@ -63,7 +63,7 @@ fn no_overlap_space_amp() -> value_log::Result<()> { value_log.scan_for_stats(index.read().unwrap().values().cloned().map(Ok))?; - assert_eq!(1.0, value_log.manifest.space_amp()); + assert_eq!(1.0, value_log.space_amp()); assert_eq!(0.0, value_log.manifest.stale_ratio()); }