diff --git a/src/bin/toysql.rs b/src/bin/toysql.rs index 90863b78..83ab8c40 100644 --- a/src/bin/toysql.rs +++ b/src/bin/toysql.rs @@ -142,18 +142,18 @@ Transactions: {active_txns} active, {versions} total nodes = status.raft.match_index.len(), committed = status.raft.commit_index, applied = status.raft.applied_index, - raft_size = format_args!("{:.3}", status.raft.storage.size as f64 / 1000000.0), - raft_garbage = format_args!("{:.0}", status.raft.storage.garbage_percent()), + raft_size = + format_args!("{:.3}", status.raft.storage.size as f64 / 1_000_000.0), + raft_garbage = + format_args!("{:.0}", status.raft.storage.garbage_disk_percent()), raft_storage = status.raft.storage.name, raft_match = status.raft.match_index.iter().map(|(n, m)| format!("n{n}:{m}")).join(" "), sql_keys = status.mvcc.storage.keys, - sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1000000.0), - sql_disk_size = format_args!( - "{:.3}", - status.mvcc.storage.total_disk_size as f64 / 1000000.0 - ), - sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_percent()), + sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1_000_000.0), + sql_disk_size = + format_args!("{:.3}", status.mvcc.storage.disk_size as f64 / 1_000_000.0), + sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_disk_percent()), sql_storage = status.mvcc.storage.name, active_txns = status.mvcc.active_txns, versions = status.mvcc.versions, diff --git a/src/storage/bitcask.rs b/src/storage/bitcask.rs index 6cc6eee5..3f44c358 100644 --- a/src/storage/bitcask.rs +++ b/src/storage/bitcask.rs @@ -77,23 +77,23 @@ impl BitCask { let status = s.status()?; if Self::should_compact( - status.garbage_disk_size, - status.total_disk_size, + status.garbage_disk_size(), + status.disk_size, garbage_min_fraction, garbage_min_bytes, ) { info!( "Compacting {} to remove {:.0}% garbage ({} MB out of {} MB)", s.log.path.display(), - status.garbage_percent(), - status.garbage_disk_size / 1024 / 1024, - status.total_disk_size / 1024 / 1024 + status.garbage_disk_percent(), + status.garbage_disk_size() / 1024 / 1024, + status.disk_size / 1024 / 1024 ); s.compact()?; info!( "Compacted {} to size {} MB", s.log.path.display(), - (status.total_disk_size - status.garbage_disk_size) / 1024 / 1024 + (status.disk_size - status.garbage_disk_size()) / 1024 / 1024 ); } @@ -161,18 +161,11 @@ impl Engine for BitCask { let size = self .keydir .iter() - .fold(0, |size, (key, (_, value_len))| size + key.len() as u64 + *value_len as u64); - let total_disk_size = self.log.file.metadata()?.len(); + .map(|(key, (_, value_len))| key.len() as u64 + *value_len as u64) + .sum(); + let disk_size = self.log.file.metadata()?.len(); let live_disk_size = size + 8 * keys; // account for length prefixes - let garbage_disk_size = total_disk_size - live_disk_size; - Ok(Status { - name: "bitcask".to_string(), - keys, - size, - total_disk_size, - live_disk_size, - garbage_disk_size, - }) + Ok(Status { name: "bitcask".to_string(), keys, size, disk_size, live_disk_size }) } } diff --git a/src/storage/engine.rs b/src/storage/engine.rs index c40ec1ea..4e8c0123 100644 --- a/src/storage/engine.rs +++ b/src/storage/engine.rs @@ -69,19 +69,23 @@ pub struct Status { /// The logical size of live key/value pairs. pub size: u64, /// The on-disk size of all data, live and garbage. - pub total_disk_size: u64, + pub disk_size: u64, /// The on-disk size of live data. pub live_disk_size: u64, - /// The on-disk size of garbage data. - pub garbage_disk_size: u64, } impl Status { - pub fn garbage_percent(&self) -> f64 { - if self.total_disk_size == 0 { + /// The on-disk size of garbage data. + pub fn garbage_disk_size(&self) -> u64 { + self.disk_size - self.live_disk_size + } + + /// The ratio of on-disk garbage to total size. + pub fn garbage_disk_percent(&self) -> f64 { + if self.disk_size == 0 { return 0.0; } - self.garbage_disk_size as f64 / self.total_disk_size as f64 * 100.0 + self.garbage_disk_size() as f64 / self.disk_size as f64 * 100.0 } } diff --git a/src/storage/memory.rs b/src/storage/memory.rs index 347c34dc..066e2633 100644 --- a/src/storage/memory.rs +++ b/src/storage/memory.rs @@ -55,9 +55,8 @@ impl Engine for Memory { name: "memory".to_string(), keys: self.data.len() as u64, size: self.data.iter().fold(0, |size, (k, v)| size + k.len() as u64 + v.len() as u64), - total_disk_size: 0, + disk_size: 0, live_disk_size: 0, - garbage_disk_size: 0, }) } }