automerge_persistent/
persister.rs

1use std::error::Error;
2
3use automerge::ActorId;
4
5use crate::StoredSizes;
6
7/// A Persister persists both changes and documents to durable storage.
8///
9/// In the event of a power loss changes should still be around for loading after. It is up to the
10/// implementation to decide on trade-offs regarding how often to fsync for example.
11///
12/// Changes are identified by a pair of `actor_id` and `sequence_number`. This uniquely identifies a
13/// change and so is suitable for use as a key in the implementation.
14///
15/// Documents are saved automerge Backends so are more compact than the raw changes they represent.
16pub trait Persister {
17    /// The error type that the operations can produce
18    type Error: Error + 'static;
19
20    /// Returns all of the changes that have been persisted through this persister.
21    /// Ordering is not specified as the automerge Backend should handle that.
22    fn get_changes(&self) -> Result<Vec<Vec<u8>>, Self::Error>;
23
24    /// Inserts the given change at the unique address specified by the `actor_id` and `sequence_number`.
25    fn insert_changes(&mut self, changes: Vec<(ActorId, u64, Vec<u8>)>) -> Result<(), Self::Error>;
26
27    /// Removes the change at the unique address specified by the `actor_id` and `sequence_number`.
28    ///
29    /// If the change does not exist this should not return an error.
30    fn remove_changes(&mut self, changes: Vec<(&ActorId, u64)>) -> Result<(), Self::Error>;
31
32    /// Returns the document, if one has been persisted previously.
33    fn get_document(&self) -> Result<Option<Vec<u8>>, Self::Error>;
34
35    /// Sets the document to the given data.
36    fn set_document(&mut self, data: Vec<u8>) -> Result<(), Self::Error>;
37
38    /// Returns the sync state for the given peer if one exists.
39    ///
40    /// A peer id corresponds to an instance of a backend and may be serving multiple frontends so
41    /// we cannot have it work on `ActorIds`.
42    fn get_sync_state(&self, peer_id: &[u8]) -> Result<Option<Vec<u8>>, Self::Error>;
43
44    /// Sets the sync state for the given peer.
45    ///
46    /// A peer id corresponds to an instance of a backend and may be serving multiple frontends so
47    /// we cannot have it work on `ActorIds`.
48    fn set_sync_state(&mut self, peer_id: Vec<u8>, sync_state: Vec<u8>) -> Result<(), Self::Error>;
49
50    /// Removes the sync states associated with the given `peer_ids`.
51    fn remove_sync_states(&mut self, peer_ids: &[&[u8]]) -> Result<(), Self::Error>;
52
53    /// Returns the list of peer ids with stored `SyncStates`.
54    ///
55    /// This is intended for use by users to see what `peer_ids` are taking space so that they can be
56    /// removed during a compaction.
57    fn get_peer_ids(&self) -> Result<Vec<Vec<u8>>, Self::Error>;
58
59    /// Returns the sizes components being stored consume.
60    ///
61    /// This can be used as an indicator of when to compact the storage.
62    fn sizes(&self) -> StoredSizes;
63
64    /// Flush the data out to disk.
65    fn flush(&mut self) -> Result<usize, Self::Error>;
66}