wasmer_journal/concrete/
counting.rs

1use std::sync::{
2    atomic::{AtomicU64, AtomicUsize, Ordering},
3    Arc,
4};
5
6use super::*;
7
8/// Journal that counts the size of the entries that are written to it
9#[derive(Debug, Clone, Default)]
10pub struct CountingJournal {
11    n_cnt: Arc<AtomicUsize>,
12    n_size: Arc<AtomicU64>,
13}
14
15impl CountingJournal {
16    pub fn cnt(&self) -> usize {
17        self.n_cnt.load(Ordering::SeqCst)
18    }
19
20    pub fn size(&self) -> u64 {
21        self.n_size.load(Ordering::SeqCst)
22    }
23}
24
25impl ReadableJournal for CountingJournal {
26    fn read(&self) -> anyhow::Result<Option<LogReadResult<'_>>> {
27        Ok(None)
28    }
29
30    fn as_restarted(&self) -> anyhow::Result<Box<DynReadableJournal>> {
31        Ok(Box::<CountingJournal>::default())
32    }
33}
34
35impl WritableJournal for CountingJournal {
36    fn write<'a>(&'a self, entry: JournalEntry<'a>) -> anyhow::Result<LogWriteResult> {
37        let size = entry.estimate_size() as u64;
38        let offset = self.n_cnt.fetch_add(1, Ordering::SeqCst);
39        self.n_size.fetch_add(size, Ordering::SeqCst);
40        Ok(LogWriteResult {
41            record_start: offset as u64,
42            record_end: offset as u64 + size,
43        })
44    }
45
46    fn flush(&self) -> anyhow::Result<()> {
47        Ok(())
48    }
49}
50
51impl Journal for CountingJournal {
52    fn split(self) -> (Box<DynWritableJournal>, Box<DynReadableJournal>) {
53        (Box::new(self.clone()), Box::new(self))
54    }
55}