summaryrefslogtreecommitdiffstats
path: root/src/compaction/snapshot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/compaction/snapshot.rs')
-rw-r--r--src/compaction/snapshot.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/compaction/snapshot.rs b/src/compaction/snapshot.rs
index 83ff28a..1f57352 100644
--- a/src/compaction/snapshot.rs
+++ b/src/compaction/snapshot.rs
@@ -18,7 +18,7 @@
//! at the snapshot change itself, so edits made afterwards through the store API can't
//! change what the protocol sees.
-// `Snapshot::doc` and `Snapshot::clock` are consumed by the epoch switch (step 5).
+// `Snapshot::clock` is used by returning nodes (step 7).
#![cfg_attr(not(test), allow(dead_code))]
use std::collections::BTreeMap;
@@ -66,6 +66,43 @@ pub(crate) enum SnapshotError {
Doc(#[from] automerge::AutomergeError),
#[error(transparent)]
Store(#[from] StoreError),
+ #[error("snapshot task failed: {0}")]
+ Task(String),
+}
+
+/// Owned version of [`SnapshotParams`], for moving into a blocking task.
+#[derive(Clone, Debug)]
+pub(crate) struct SnapshotRequest {
+ pub(crate) compaction_id: String,
+ pub(crate) previous: Epoch,
+ pub(crate) base_heads: Vec<ChangeHash>,
+ pub(crate) policy: JsonValue,
+}
+
+impl SnapshotRequest {
+ pub(crate) fn params(&self) -> SnapshotParams<'_> {
+ SnapshotParams {
+ compaction_id: &self.compaction_id,
+ previous: self.previous,
+ base_heads: &self.base_heads,
+ policy: &self.policy,
+ }
+ }
+}
+
+/// Builds the snapshot from `copy`, a clone of the live document, on the blocking pool.
+/// No lock is held while building; the caller only needs a lock for the clone.
+/// Returns the copy as well, for callers that continue working with it.
+pub(crate) async fn build_detached(
+ mut copy: AutoCommit,
+ request: SnapshotRequest,
+) -> Result<(Snapshot, AutoCommit), SnapshotError> {
+ tokio::task::spawn_blocking(move || {
+ let snapshot = build(&mut copy, &request.params())?;
+ Ok((snapshot, copy))
+ })
+ .await
+ .map_err(|error| SnapshotError::Task(error.to_string()))?
}
/// Path of the metadata record for an epoch generation.