Skip to content

Commit 2fc15a9

Browse files
committed
wip
1 parent 03656d1 commit 2fc15a9

File tree

6 files changed

+193
-68
lines changed

6 files changed

+193
-68
lines changed

src/index.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ pub trait Writer {
2525
/// # Errors
2626
///
2727
/// Will return `Err` if an IO error occurs.
28-
fn insert_indirection(&mut self, key: &[u8], value: ValueHandle) -> std::io::Result<()>;
28+
fn insert_indirection(
29+
&mut self,
30+
key: &[u8],
31+
handle: ValueHandle,
32+
size: u32,
33+
) -> std::io::Result<()>;
2934

3035
/// Finishes the write batch.
3136
///

src/manifest.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,20 @@ impl SegmentManifest {
164164
id: segment_id,
165165
path: segment_folder,
166166
stats: Stats {
167-
item_count: writer.item_count.into(),
168-
total_bytes: writer.written_blob_bytes.into(),
169-
total_uncompressed_bytes: writer.uncompressed_bytes.into(),
167+
item_count: writer.item_count,
168+
total_bytes: writer.written_blob_bytes,
169+
total_uncompressed_bytes: writer.uncompressed_bytes,
170170
stale_items: AtomicU64::default(),
171171
stale_bytes: AtomicU64::default(),
172172
},
173173
}),
174174
);
175+
176+
log::debug!(
177+
"Created segment #{segment_id:?} ({} items, {} userdata bytes)",
178+
writer.item_count,
179+
writer.uncompressed_bytes,
180+
);
175181
}
176182

177183
Self::write_to_disk(&self.path, &lock.keys().copied().collect::<Vec<_>>())
@@ -234,35 +240,17 @@ impl SegmentManifest {
234240
}
235241

236242
/// Returns the amount of bytes on disk that are occupied by blobs.
237-
///
238-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
239243
#[must_use]
240244
pub fn disk_space_used(&self) -> u64 {
241245
self.segments
242246
.read()
243247
.expect("lock is poisoned")
244248
.values()
245-
.map(|x| x.stats.total_bytes())
249+
.map(|x| x.stats.total_bytes)
246250
.sum::<u64>()
247251
}
248252

249-
/// Returns the amount of bytes that can be freed on disk
250-
/// if all segments were to be defragmented
251-
///
252-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
253-
#[must_use]
254-
pub fn reclaimable_bytes(&self) -> u64 {
255-
self.segments
256-
.read()
257-
.expect("lock is poisoned")
258-
.values()
259-
.map(|x| x.stats.stale_bytes())
260-
.sum::<u64>()
261-
}
262-
263-
/// Returns the amount of stale items
264-
///
265-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
253+
/* /// Returns the amount of stale items
266254
#[must_use]
267255
pub fn stale_items_count(&self) -> u64 {
268256
self.segments
@@ -271,19 +259,17 @@ impl SegmentManifest {
271259
.values()
272260
.map(|x| x.stats.stale_items())
273261
.sum::<u64>()
274-
}
262+
} */
275263

276-
/// Returns the percent of dead bytes in the value log
277-
///
278-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
264+
/// Returns the percent of dead bytes (uncompressed) in the value log
279265
#[must_use]
280266
pub fn stale_ratio(&self) -> f32 {
281267
let used_bytes = self
282268
.segments
283269
.read()
284270
.expect("lock is poisoned")
285271
.values()
286-
.map(|x| x.stats.total_bytes())
272+
.map(|x| x.stats.total_uncompressed_bytes)
287273
.sum::<u64>();
288274
if used_bytes == 0 {
289275
return 0.0;
@@ -296,6 +282,7 @@ impl SegmentManifest {
296282
.values()
297283
.map(|x| x.stats.stale_bytes())
298284
.sum::<u64>();
285+
299286
if stale_bytes == 0 {
300287
return 0.0;
301288
}
@@ -306,17 +293,16 @@ impl SegmentManifest {
306293
/// Returns the approximate space amplification
307294
///
308295
/// Returns 0.0 if there are no items.
309-
///
310-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
311296
#[must_use]
312297
pub fn space_amp(&self) -> f32 {
313298
let used_bytes = self
314299
.segments
315300
.read()
316301
.expect("lock is poisoned")
317302
.values()
318-
.map(|x| x.stats.total_bytes())
303+
.map(|x| x.stats.total_uncompressed_bytes)
319304
.sum::<u64>();
305+
320306
if used_bytes == 0 {
321307
return 0.0;
322308
}

src/mock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ impl From<MockIndex> for MockIndexWriter {
4747
}
4848

4949
impl IndexWriter for MockIndexWriter {
50-
fn insert_indirection(&mut self, key: &[u8], value: ValueHandle) -> std::io::Result<()> {
50+
fn insert_indirection(
51+
&mut self,
52+
key: &[u8],
53+
value: ValueHandle,
54+
_size: u32,
55+
) -> std::io::Result<()> {
5156
self.0.insert_indirection(key, value)
5257
}
5358

src/segment/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ impl Segment {
4040

4141
/// Returns the amount of items in the segment
4242
pub fn len(&self) -> u64 {
43-
self.stats
44-
.item_count
45-
.load(std::sync::atomic::Ordering::Acquire)
43+
self.stats.item_count
4644
}
4745
}

src/segment/stats.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,28 @@ use std::sync::atomic::AtomicU64;
33
#[derive(Debug, Default)]
44
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
55
pub struct Stats {
6-
pub(crate) item_count: AtomicU64, // TODO: u64?
6+
pub(crate) item_count: u64,
77
pub(crate) stale_items: AtomicU64,
88

9-
pub total_uncompressed_bytes: AtomicU64, // TODO: u64?
10-
pub total_bytes: AtomicU64, // TODO: u64?
9+
pub total_uncompressed_bytes: u64,
10+
pub total_bytes: u64,
1111
pub(crate) stale_bytes: AtomicU64,
1212
// TODO: key range
1313
}
1414

1515
impl Stats {
1616
pub(crate) fn mark_as_stale(&self) {
1717
self.stale_items
18-
.store(self.item_count(), std::sync::atomic::Ordering::Release);
18+
.store(self.item_count, std::sync::atomic::Ordering::Release);
1919

20-
self.stale_bytes
21-
.store(self.total_bytes(), std::sync::atomic::Ordering::Release);
22-
}
23-
24-
pub fn item_count(&self) -> u64 {
25-
self.item_count.load(std::sync::atomic::Ordering::Acquire)
26-
}
27-
28-
pub fn total_uncompressed_bytes(&self) -> u64 {
29-
self.total_uncompressed_bytes
30-
.load(std::sync::atomic::Ordering::Acquire)
31-
}
32-
33-
pub fn total_bytes(&self) -> u64 {
34-
self.total_bytes.load(std::sync::atomic::Ordering::Acquire)
20+
self.stale_bytes.store(
21+
self.total_uncompressed_bytes,
22+
std::sync::atomic::Ordering::Release,
23+
);
3524
}
3625

3726
pub fn is_stale(&self) -> bool {
38-
self.stale_items() == self.item_count()
27+
self.stale_items() == self.item_count
3928
}
4029

4130
/// Returns the percent of dead items in the segment
@@ -45,19 +34,15 @@ impl Stats {
4534
return 0.0;
4635
}
4736

48-
dead / self.item_count() as f32
37+
dead / self.item_count as f32
4938
}
5039

5140
/// Returns the amount of dead items in the segment
52-
///
53-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
5441
pub fn stale_items(&self) -> u64 {
5542
self.stale_items.load(std::sync::atomic::Ordering::Acquire)
5643
}
5744

5845
/// Returns the amount of dead bytes in the segment
59-
///
60-
/// This value may not be fresh, as it is only set after running [`ValueLog::refresh_stats`].
6146
pub fn stale_bytes(&self) -> u64 {
6247
self.stale_bytes.load(std::sync::atomic::Ordering::Acquire)
6348
}

0 commit comments

Comments
 (0)