automerge_persistent/
mem.rs

1use std::collections::HashMap;
2
3use automerge::ActorId;
4
5use crate::{Persister, StoredSizes};
6
7/// **For Testing** An in-memory persister.
8///
9/// As this provides no actual persistence it should not be used for any real application, it
10/// actually reduces performance of the plain backend slightly due to tracking the changes itself.
11#[derive(Debug, Default)]
12pub struct MemoryPersister {
13    changes: HashMap<(ActorId, u64), Vec<u8>>,
14    document: Option<Vec<u8>>,
15    sync_states: HashMap<Vec<u8>, Vec<u8>>,
16    sizes: StoredSizes,
17}
18
19impl Persister for MemoryPersister {
20    type Error = std::convert::Infallible;
21
22    /// Get the changes out of the map.
23    fn get_changes(&self) -> Result<Vec<Vec<u8>>, Self::Error> {
24        Ok(self.changes.values().cloned().collect())
25    }
26
27    /// Insert changes into the map.
28    fn insert_changes(&mut self, changes: Vec<(ActorId, u64, Vec<u8>)>) -> Result<(), Self::Error> {
29        for (a, u, c) in changes {
30            self.sizes.changes += c.len() as u64;
31            if let Some(old) = self.changes.insert((a, u), c) {
32                self.sizes.changes -= old.len() as u64;
33            }
34        }
35        Ok(())
36    }
37
38    /// Remove changes from the map.
39    fn remove_changes(&mut self, changes: Vec<(&ActorId, u64)>) -> Result<(), Self::Error> {
40        for (a, u) in changes {
41            if let Some(old) = self.changes.remove(&(a.clone(), u)) {
42                self.sizes.changes -= old.len() as u64;
43            }
44        }
45        Ok(())
46    }
47
48    /// Get the document.
49    fn get_document(&self) -> Result<Option<Vec<u8>>, Self::Error> {
50        Ok(self.document.clone())
51    }
52
53    /// Set the document.
54    fn set_document(&mut self, data: Vec<u8>) -> Result<(), Self::Error> {
55        self.sizes.document = data.len() as u64;
56        self.document = Some(data);
57        Ok(())
58    }
59
60    fn get_sync_state(&self, peer_id: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
61        Ok(self.sync_states.get(peer_id).cloned())
62    }
63
64    fn set_sync_state(&mut self, peer_id: Vec<u8>, sync_state: Vec<u8>) -> Result<(), Self::Error> {
65        self.sizes.sync_states += sync_state.len() as u64;
66        if let Some(old) = self.sync_states.insert(peer_id, sync_state) {
67            self.sizes.sync_states -= old.len() as u64;
68        }
69        Ok(())
70    }
71
72    fn remove_sync_states(&mut self, peer_ids: &[&[u8]]) -> Result<(), Self::Error> {
73        for id in peer_ids {
74            if let Some(old) = self.sync_states.remove(*id) {
75                self.sizes.sync_states -= old.len() as u64;
76            }
77        }
78        Ok(())
79    }
80
81    fn get_peer_ids(&self) -> Result<Vec<Vec<u8>>, Self::Error> {
82        Ok(self.sync_states.keys().cloned().collect())
83    }
84
85    fn sizes(&self) -> StoredSizes {
86        self.sizes.clone()
87    }
88
89    fn flush(&mut self) -> Result<usize, Self::Error> {
90        Ok(0)
91    }
92}