Skip to content

Commit 61783ba

Browse files
committed
storage: remove Status::garbage_disk_size
1 parent 10ee125 commit 61783ba

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

src/bin/toysql.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,18 @@ Transactions: {active_txns} active, {versions} total
142142
nodes = status.raft.match_index.len(),
143143
committed = status.raft.commit_index,
144144
applied = status.raft.applied_index,
145-
raft_size = format_args!("{:.3}", status.raft.storage.size as f64 / 1000000.0),
146-
raft_garbage = format_args!("{:.0}", status.raft.storage.garbage_percent()),
145+
raft_size =
146+
format_args!("{:.3}", status.raft.storage.size as f64 / 1_000_000.0),
147+
raft_garbage =
148+
format_args!("{:.0}", status.raft.storage.garbage_disk_percent()),
147149
raft_storage = status.raft.storage.name,
148150
raft_match =
149151
status.raft.match_index.iter().map(|(n, m)| format!("n{n}:{m}")).join(" "),
150152
sql_keys = status.mvcc.storage.keys,
151-
sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1000000.0),
152-
sql_disk_size = format_args!(
153-
"{:.3}",
154-
status.mvcc.storage.total_disk_size as f64 / 1000000.0
155-
),
156-
sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_percent()),
153+
sql_size = format_args!("{:.3}", status.mvcc.storage.size as f64 / 1_000_000.0),
154+
sql_disk_size =
155+
format_args!("{:.3}", status.mvcc.storage.disk_size as f64 / 1_000_000.0),
156+
sql_garbage = format_args!("{:.0}", status.mvcc.storage.garbage_disk_percent()),
157157
sql_storage = status.mvcc.storage.name,
158158
active_txns = status.mvcc.active_txns,
159159
versions = status.mvcc.versions,

src/storage/bitcask.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ impl BitCask {
7777

7878
let status = s.status()?;
7979
if Self::should_compact(
80-
status.garbage_disk_size,
81-
status.total_disk_size,
80+
status.garbage_disk_size(),
81+
status.disk_size,
8282
garbage_min_fraction,
8383
garbage_min_bytes,
8484
) {
8585
info!(
8686
"Compacting {} to remove {:.0}% garbage ({} MB out of {} MB)",
8787
s.log.path.display(),
88-
status.garbage_percent(),
89-
status.garbage_disk_size / 1024 / 1024,
90-
status.total_disk_size / 1024 / 1024
88+
status.garbage_disk_percent(),
89+
status.garbage_disk_size() / 1024 / 1024,
90+
status.disk_size / 1024 / 1024
9191
);
9292
s.compact()?;
9393
info!(
9494
"Compacted {} to size {} MB",
9595
s.log.path.display(),
96-
(status.total_disk_size - status.garbage_disk_size) / 1024 / 1024
96+
(status.disk_size - status.garbage_disk_size()) / 1024 / 1024
9797
);
9898
}
9999

@@ -161,18 +161,11 @@ impl Engine for BitCask {
161161
let size = self
162162
.keydir
163163
.iter()
164-
.fold(0, |size, (key, (_, value_len))| size + key.len() as u64 + *value_len as u64);
165-
let total_disk_size = self.log.file.metadata()?.len();
164+
.map(|(key, (_, value_len))| key.len() as u64 + *value_len as u64)
165+
.sum();
166+
let disk_size = self.log.file.metadata()?.len();
166167
let live_disk_size = size + 8 * keys; // account for length prefixes
167-
let garbage_disk_size = total_disk_size - live_disk_size;
168-
Ok(Status {
169-
name: "bitcask".to_string(),
170-
keys,
171-
size,
172-
total_disk_size,
173-
live_disk_size,
174-
garbage_disk_size,
175-
})
168+
Ok(Status { name: "bitcask".to_string(), keys, size, disk_size, live_disk_size })
176169
}
177170
}
178171

src/storage/engine.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,23 @@ pub struct Status {
6969
/// The logical size of live key/value pairs.
7070
pub size: u64,
7171
/// The on-disk size of all data, live and garbage.
72-
pub total_disk_size: u64,
72+
pub disk_size: u64,
7373
/// The on-disk size of live data.
7474
pub live_disk_size: u64,
75-
/// The on-disk size of garbage data.
76-
pub garbage_disk_size: u64,
7775
}
7876

7977
impl Status {
80-
pub fn garbage_percent(&self) -> f64 {
81-
if self.total_disk_size == 0 {
78+
/// The on-disk size of garbage data.
79+
pub fn garbage_disk_size(&self) -> u64 {
80+
self.disk_size - self.live_disk_size
81+
}
82+
83+
/// The ratio of on-disk garbage to total size.
84+
pub fn garbage_disk_percent(&self) -> f64 {
85+
if self.disk_size == 0 {
8286
return 0.0;
8387
}
84-
self.garbage_disk_size as f64 / self.total_disk_size as f64 * 100.0
88+
self.garbage_disk_size() as f64 / self.disk_size as f64 * 100.0
8589
}
8690
}
8791

src/storage/memory.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ impl Engine for Memory {
5555
name: "memory".to_string(),
5656
keys: self.data.len() as u64,
5757
size: self.data.iter().fold(0, |size, (k, v)| size + k.len() as u64 + v.len() as u64),
58-
total_disk_size: 0,
58+
disk_size: 0,
5959
live_disk_size: 0,
60-
garbage_disk_size: 0,
6160
})
6261
}
6362
}

0 commit comments

Comments
 (0)