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
use async_trait::async_trait;
use futures::io::AsyncRead;

use crate::Identifier;

/// Appended to the shared log and applied to the shared `State`.
#[async_trait(?Send)]
pub trait LogEntry: 'static + Clone + std::fmt::Debug + Send + Sync + Unpin {
    type Id: Identifier;
    type Reader: std::io::Read;

    /// Deserializes log entry that was previously serialized using
    /// `to_reader()`.
    ///
    /// While implementations need not detect arbitrary data corruption, they
    /// must not panic.
    async fn from_reader<R: AsyncRead + Unpin>(read: R) -> Result<Self, crate::error::BoxError>;

    /// Number of bytes the result of `to_reader()` will emit.
    fn size(&self) -> usize;

    /// Serializes the log entry to enable snapshots.
    ///
    /// `LogEntry::from_reader(e.to_reader())` must yield an equivalent log
    /// entry.
    fn to_reader(&self) -> Self::Reader;

    /// Returns a unique identifier for the log entry.
    ///
    /// Identifiers need only be unique within the rough timeframe of them being
    /// appended to the distributed log. That notwithstanding it is recommended
    /// that UUIDs or some other "universally unique" identifier are used.
    fn id(&self) -> Self::Id;
}

#[derive(Debug)]
pub struct LogKeeping {
    pub(crate) logs_kept: usize,
    pub(crate) entry_limit: usize,
}

impl Default for LogKeeping {
    fn default() -> Self {
        Self {
            logs_kept: 5,
            entry_limit: 1024,
        }
    }
}