1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use super::*;

#[derive(Debug)]
pub struct RecombinedJournal<W: WritableJournal, R: ReadableJournal> {
    tx: W,
    rx: R,
}

impl<W: WritableJournal, R: ReadableJournal> RecombinedJournal<W, R> {
    pub fn new(tx: W, rx: R) -> Self {
        Self { tx, rx }
    }
}

impl<W: WritableJournal, R: ReadableJournal> WritableJournal for RecombinedJournal<W, R> {
    fn write<'a>(&'a self, entry: JournalEntry<'a>) -> anyhow::Result<LogWriteResult> {
        self.tx.write(entry)
    }

    fn flush(&self) -> anyhow::Result<()> {
        self.tx.flush()
    }

    fn commit(&self) -> anyhow::Result<usize> {
        self.tx.commit()
    }

    fn rollback(&self) -> anyhow::Result<usize> {
        self.tx.rollback()
    }
}

impl<W: WritableJournal, R: ReadableJournal> ReadableJournal for RecombinedJournal<W, R> {
    fn read(&self) -> anyhow::Result<Option<LogReadResult<'_>>> {
        self.rx.read()
    }

    fn as_restarted(&self) -> anyhow::Result<Box<DynReadableJournal>> {
        self.rx.as_restarted()
    }
}

impl<W, R> Journal for RecombinedJournal<W, R>
where
    W: WritableJournal + Send + Sync + 'static,
    R: ReadableJournal + Send + Sync + 'static,
{
    fn split(self) -> (Box<DynWritableJournal>, Box<DynReadableJournal>) {
        (Box::new(self.tx), Box::new(self.rx))
    }
}