aboutsummaryrefslogtreecommitdiffstats
path: root/src/state/mod.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 03:21:43 +0000
committertv <tv@krebsco.de>2026-11-29 03:21:43 +0000
commite63d30c2581033a85489b10c3f5b252fdbb5049e (patch)
tree5f4e6f65f1cecfb32f841e7ad7d9d106aeb69b20 /src/state/mod.rs
parent9188fa6e13ea7facfab5fb6f55bf26444ec2c01e (diff)
state: plain writes take a view to write, Automerge's are asked for as such
AppState::mutate_doc, mutate_doc_registering and mutate_doc_by hand what they are given a document::view::Write: the member registry, the users, sessions, forgetting and bootstrap write through them, and take whatever kind of document gives it. AppState::mutate_doc_for is for what asks more of an Automerge document than values at paths (the store's API, a remake's host, a store's events), and gives the heads after the write. What reads a view takes one of any size (dyn Write among them).
Diffstat (limited to 'src/state/mod.rs')
-rw-r--r--src/state/mod.rs52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/state/mod.rs b/src/state/mod.rs
index ccd8a93..604f6ae 100644
--- a/src/state/mod.rs
+++ b/src/state/mod.rs
@@ -41,6 +41,7 @@ use crate::document::lock::DocumentLock;
use crate::document::replica::{Epoch, Stamp};
use crate::document::signatures::{Sign, SignEvery};
use crate::document::store::{ChangeSet, StoreError};
+use crate::document::view::Write;
use crate::document::{Document, Transaction};
use crate::locks::{MutexExt, RwLockExt};
use crate::remake::{Host, RemakeState};
@@ -573,44 +574,61 @@ impl AppState {
.expect("document read task panicked")
}
- /// Mutates the document on the blocking pool as one change, then
- /// publishes it. Write and notification run in the same blocking task, so a
- /// cancelled request can't leave a committed write unannounced. While a rejoin runs,
- /// the write goes into the document before it (`StaleView`), replayed when the
- /// rejoin ends; on a node that keeps none, it is refused (`StoreError::Rejoining`,
- /// `f` not called). Protocol data: the change is signed (`document::signatures`).
+ /// Writes to the document on the blocking pool, all of it or none, as one unit of
+ /// its kind (one change), then publishes it. Write and notification run in the same
+ /// blocking task, so a cancelled request can't leave a committed write unannounced.
+ /// While a rejoin runs, the write goes into the document before it (`StaleView`),
+ /// replayed when the rejoin ends; on a node that keeps none, it is refused
+ /// (`StoreError::Rejoining`, `f` not called). Protocol data: what it makes is signed
+ /// (`document::signatures`).
pub(crate) async fn mutate_doc<T, E>(
&self,
- f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E> + Send + 'static,
+ f: impl FnOnce(&mut dyn Write) -> Result<T, E> + Send + 'static,
) -> Result<T, E>
where
T: Send + 'static,
E: From<StoreError> + Send + 'static,
{
- self.mutate_doc_for(None, Sign::Protocol, f)
- .await
- .map(|(out, _)| out)
+ self.mutate_doc_by(None, Sign::Protocol, f).await
}
/// [`mutate_doc`](Self::mutate_doc) of this node's registration in the member list
/// (`swarm::members::ensure_registered`): the one write not preceded by it.
pub(crate) async fn mutate_doc_registering<T, E>(
&self,
- f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E> + Send + 'static,
+ f: impl FnOnce(&mut dyn Write) -> Result<T, E> + Send + 'static,
+ ) -> Result<T, E>
+ where
+ T: Send + 'static,
+ E: From<StoreError> + Send + 'static,
+ {
+ self.mutate_doc_as(None, Sign::Protocol, false, move |write| f(write))
+ .await
+ .map(|(out, _)| out)
+ }
+
+ /// [`mutate_doc`](Self::mutate_doc) for `user`, who wrote through the API: what it
+ /// makes names them (`replica::Writer`), and is signed as `sign` says, with this
+ /// node's key.
+ pub(crate) async fn mutate_doc_by<T, E>(
+ &self,
+ user: Option<String>,
+ sign: Sign,
+ f: impl FnOnce(&mut dyn Write) -> Result<T, E> + Send + 'static,
) -> Result<T, E>
where
T: Send + 'static,
E: From<StoreError> + Send + 'static,
{
- self.mutate_doc_as(None, Sign::Protocol, false, f)
+ self.mutate_doc_as(user, sign, true, move |write| f(write))
.await
.map(|(out, _)| out)
}
- /// [`mutate_doc`](Self::mutate_doc) for `user`, who wrote through the API: the
- /// change names them (`replica::Writer`), and is signed as `sign` says, with this
- /// node's key. With the heads of the document written to after the
- /// write (sorted): what a read must have to include it.
+ /// [`mutate_doc_by`](Self::mutate_doc_by) of an Automerge document as such, for what
+ /// asks more of it than values at paths (`document::store`): the write is one of
+ /// Automerge's. With the heads of the document written to after the write (sorted):
+ /// what a read must have to include it.
pub(crate) async fn mutate_doc_for<T, E>(
&self,
user: Option<String>,
@@ -693,7 +711,7 @@ impl AppState {
let before = replica.doc.get_heads();
replica
.transact(state.clock.now_millis(), &writer, |doc| {
- crate::document::store::write_path(doc, &path, &entry)
+ crate::document::view::write_path(doc, &path, &entry)
})
.map_err(E::from)?;
state.sign_made(&mut replica, &before, &Sign::Protocol);