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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// #![warn(missing_docs)]
#![warn(missing_crate_level_docs)]
#![warn(missing_doc_code_examples)]
// #![warn(clippy::pedantic)]
#![warn(clippy::nursery)]

//! A library for constructing efficient persistent automerge documents.
//!
//! A [`PersistentAutomerge`] wraps an [`automerge::Automerge`] and handles making the changes applied
//! to it durable. This works by persisting every change before it is applied to the document. Then
//! occasionally the user should call `compact` to save the document in a more compact format and
//! cleanup the included changes. This strategy aims to be fast while also being space efficient
//! (up to the user's requirements).
//!
//! ```rust
//! # use automerge_persistent::MemoryPersister;
//! # use automerge_persistent::PersistentAutomerge;
//! let persister = MemoryPersister::default();
//! let doc = PersistentAutomerge::load(persister).unwrap();
//! ```

mod autocommit;
mod mem;
mod persister;

use std::{collections::HashMap, fmt::Debug};

pub use autocommit::PersistentAutoCommit;
use automerge::{
    sync::{self, DecodeStateError, SyncDoc},
    transaction::{CommitOptions, Failure, Observed, Success, Transaction, UnObserved},
    Automerge, AutomergeError, Change, LoadChangeError, OpObserver, op_observer::BranchableObserver,
};
pub use mem::MemoryPersister;
pub use persister::Persister;

/// Bytes stored for each of the stored types.
#[derive(Debug, Default, Clone)]
pub struct StoredSizes {
    /// Total bytes stored for all changes.
    pub changes: u64,
    /// Total bytes stored in the document.
    pub document: u64,
    /// Total bytes stored for all sync states.
    pub sync_states: u64,
}

/// Errors that persistent documents can return.
#[derive(Debug, thiserror::Error)]
pub enum Error<E> {
    /// An automerge error.
    #[error(transparent)]
    AutomergeError(#[from] AutomergeError),
    #[error(transparent)]
    AutomergeDecodeError(#[from] DecodeStateError),
    #[error(transparent)]
    AutomergeLoadChangeError(#[from] LoadChangeError),
    /// A persister error.
    #[error(transparent)]
    PersisterError(E),
}

/// Errors that persistent documents can return after a transaction.
#[derive(Debug, thiserror::Error)]
pub enum TransactionError<PE, E> {
    /// A persister error.
    #[error(transparent)]
    PersisterError(PE),
    /// A transaction error
    #[error(transparent)]
    TransactionError(#[from] Failure<E>),
}

pub type TransactionResult<O, Obs, E, PE> = Result<Success<O, Obs>, TransactionError<PE, E>>;

type PeerId = Vec<u8>;

/// A wrapper for a persister and an automerge document.
#[derive(Debug)]
pub struct PersistentAutomerge<P> {
    document: Automerge,
    sync_states: HashMap<PeerId, sync::State>,
    persister: P,
}

impl<P> PersistentAutomerge<P>
where
    P: Persister + 'static,
{
    pub const fn document(&self) -> &Automerge {
        &self.document
    }

    pub fn document_mut(&mut self) -> &mut Automerge {
        &mut self.document
    }

    pub fn transact<F, O, E>(&mut self, f: F) -> TransactionResult<O, (), E, P::Error>
    where
        F: FnOnce(&mut Transaction<UnObserved>) -> Result<O, E>,
    {
        let result = self.document.transact(f)?;
        if let Err(e) = self.after_transaction() {
            return Err(TransactionError::PersisterError(e));
        }
        Ok(result)
    }

    fn after_transaction(&mut self) -> Result<(), P::Error> {
        if let Some(change) = self.document.get_last_local_change() {
            self.persister.insert_changes(vec![(
                change.actor_id().clone(),
                change.seq(),
                change.raw_bytes().to_vec(),
            )])?;
        }
        Ok(())
    }

    pub fn transact_with<F, O, E, C, Obs>(
        &mut self,
        c: C,
        f: F,
    ) -> TransactionResult<O, Obs, E, P::Error>
    where
        F: FnOnce(&mut Transaction<'_, Observed<Obs>>) -> Result<O, E>,
        C: FnOnce(&O) -> CommitOptions,
        Obs: OpObserver + BranchableObserver + Default,
    {
        let result = self.document.transact_observed_with(c, f)?;
        if let Err(e) = self.after_transaction() {
            return Err(TransactionError::PersisterError(e));
        }
        Ok(result)
    }

    /// Apply changes to this document.
    pub fn apply_changes(
        &mut self,
        changes: impl IntoIterator<Item = Change>,
    ) -> Result<(), Error<P::Error>> {
        self.apply_changes_with::<_, ()>(changes, None)
    }

    pub fn apply_changes_with<I: IntoIterator<Item = Change>, Obs: OpObserver>(
        &mut self,
        changes: I,
        op_observer: Option<&mut Obs>,
    ) -> Result<(), Error<P::Error>> {
        let mut to_persist = vec![];
        self.document.apply_changes_with(
            changes.into_iter().map(|change| {
                to_persist.push((
                    change.actor_id().clone(),
                    change.seq(),
                    change.raw_bytes().to_vec(),
                ));
                change
            }),
            op_observer,
        )?;
        self.persister
            .insert_changes(to_persist)
            .map_err(Error::PersisterError)?;
        Ok(())
    }

    /// Load the persisted changes (both individual changes and a document) from storage and
    /// rebuild the Document.
    ///
    /// ```rust
    /// # use automerge_persistent::MemoryPersister;
    /// # use automerge_persistent::PersistentAutomerge;
    /// let persister = MemoryPersister::default();
    /// let doc = PersistentAutomerge::load(persister).unwrap();
    /// ```
    pub fn load(persister: P) -> Result<Self, Error<P::Error>> {
        let document = persister.get_document().map_err(Error::PersisterError)?;
        let mut doc = if let Some(document) = document {
            Automerge::load(&document).map_err(Error::AutomergeError)?
        } else {
            Automerge::default()
        };

        let change_bytes = persister.get_changes().map_err(Error::PersisterError)?;

        let mut changes = Vec::new();
        for change_bytes in change_bytes {
            changes.push(Change::from_bytes(change_bytes).map_err(Error::AutomergeLoadChangeError)?)
        }

        doc
            .apply_changes(changes)
            .map_err(Error::AutomergeError)?;
        Ok(Self {
            document: doc,
            sync_states: HashMap::new(),
            persister,
        })
    }

    /// Compact the storage.
    ///
    /// This first obtains the changes currently in the document, saves the document and persists the
    /// saved document. We then can remove the previously obtained changes one by one.
    ///
    /// It also clears out the storage used up by old sync states for peers by removing those given
    /// in `old_peers`.
    ///
    /// ```rust
    /// # use automerge_persistent::MemoryPersister;
    /// # use automerge_persistent::PersistentAutomerge;
    /// # let persister = MemoryPersister::default();
    /// # let mut document = PersistentAutomerge::load(persister).unwrap();
    /// document.compact(&[]).unwrap();
    /// ```
    pub fn compact(&mut self, old_peer_ids: &[&[u8]]) -> Result<(), Error<P::Error>> {
        let saved_document = self.document.save();
        let changes = self.document.get_changes(&[])?;
        self.persister
            .set_document(saved_document)
            .map_err(Error::PersisterError)?;
        self.persister
            .remove_changes(
                changes
                    .into_iter()
                    .map(|c| (c.actor_id(), c.seq()))
                    .collect(),
            )
            .map_err(Error::PersisterError)?;
        self.persister
            .remove_sync_states(old_peer_ids)
            .map_err(Error::PersisterError)?;
        Ok(())
    }

    /// Generate a sync message to be sent to a peer document.
    ///
    /// Peer id is intentionally low level and up to the user as it can be a DNS name, IP address or
    /// something else.
    ///
    /// This internally retrieves the previous sync state from storage and saves the new one
    /// afterwards.
    ///
    /// ```rust
    /// # use automerge_persistent::MemoryPersister;
    /// # use automerge_persistent::PersistentAutomerge;
    /// # let persister = MemoryPersister::default();
    /// # let mut document = PersistentAutomerge::load(persister).unwrap();
    /// let message = document.generate_sync_message(vec![]).unwrap();
    /// ```
    pub fn generate_sync_message(
        &mut self,
        peer_id: PeerId,
    ) -> Result<Option<sync::Message>, Error<P::Error>> {
        if !self.sync_states.contains_key(&peer_id) {
            if let Some(sync_state) = self
                .persister
                .get_sync_state(&peer_id)
                .map_err(Error::PersisterError)?
            {
                let s = sync::State::decode(&sync_state).map_err(Error::AutomergeDecodeError)?;
                self.sync_states.insert(peer_id.clone(), s);
            }
        }
        let sync_state = self.sync_states.entry(peer_id.clone()).or_default();
        let message = self.document.generate_sync_message(sync_state);
        self.persister
            .set_sync_state(peer_id, sync_state.encode())
            .map_err(Error::PersisterError)?;
        Ok(message)
    }

    /// Receive a sync message from a peer document.
    ///
    /// Peer id is intentionally low level and up to the user as it can be a DNS name, IP address or
    /// something else.
    ///
    /// This internally retrieves the previous sync state from storage and saves the new one
    /// afterwards.
    pub fn receive_sync_message(
        &mut self,
        peer_id: PeerId,
        message: sync::Message,
    ) -> Result<(), Error<P::Error>> {
        self.receive_sync_message_with(peer_id, message, &mut ())
    }

    /// Receive a sync message from a peer document.
    ///
    /// Peer id is intentionally low level and up to the user as it can be a DNS name, IP address or
    /// something else.
    ///
    /// This internally retrieves the previous sync state from storage and saves the new one
    /// afterwards.
    pub fn receive_sync_message_with<Obs: OpObserver>(
        &mut self,
        peer_id: PeerId,
        message: sync::Message,
        op_observer: &mut Obs,
    ) -> Result<(), Error<P::Error>> {
        if !self.sync_states.contains_key(&peer_id) {
            if let Some(sync_state) = self
                .persister
                .get_sync_state(&peer_id)
                .map_err(Error::PersisterError)?
            {
                let s = sync::State::decode(&sync_state).map_err(Error::AutomergeDecodeError)?;
                self.sync_states.insert(peer_id.clone(), s);
            }
        }
        let sync_state = self.sync_states.entry(peer_id.clone()).or_default();

        let heads = self.document.get_heads();
        self.document
            .receive_sync_message_with(sync_state, message, op_observer)
            .map_err(Error::AutomergeError)?;
        let changes = self.document.get_changes(&heads)?;
        self.persister
            .insert_changes(
                changes
                    .into_iter()
                    .map(|c| (c.actor_id().clone(), c.seq(), c.raw_bytes().to_vec()))
                    .collect(),
            )
            .map_err(Error::PersisterError)?;

        self.persister
            .set_sync_state(peer_id, sync_state.encode())
            .map_err(Error::PersisterError)?;
        Ok(())
    }

    /// Flush any data out to storage returning the number of bytes flushed.
    ///
    /// # Errors
    ///
    /// Returns the error returned by the persister during flushing.
    pub fn flush(&mut self) -> Result<usize, P::Error> {
        self.persister.flush()
    }

    /// Close the document.
    ///
    /// This calls flush on the persister and returns it for potential use in other documents.
    ///
    /// # Errors
    ///
    /// Returns the error from flushing.
    pub fn close(mut self) -> Result<P, P::Error> {
        self.flush()?;
        Ok(self.persister)
    }

    /// Obtain a reference to the persister.
    pub const fn persister(&self) -> &P {
        &self.persister
    }

    /// Obtain a mut reference to the persister.
    pub fn persister_mut(&mut self) -> &mut P {
        &mut self.persister
    }

    /// Reset the sync state for a peer.
    ///
    /// This is typically used when a peer disconnects, we need to reset the sync state for them as
    /// they may come back up with different state.
    pub fn reset_sync_state(&mut self, peer_id: &[u8]) -> Result<(), P::Error> {
        self.sync_states.remove(peer_id);
        self.persister.remove_sync_states(&[peer_id])
    }
}