Skip to content

Commit

Permalink
refactor: expose space_amp directly
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Jun 11, 2024
1 parent 27326c5 commit dd3265a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
//! # }
Expand Down
12 changes: 10 additions & 2 deletions src/value_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SegmentId> {
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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions tests/gc_space_amp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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);
}

{
Expand All @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions tests/space_amp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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());
}

Expand Down

0 comments on commit dd3265a

Please sign in to comment.