From 694347ad456766004e2792c4b4cda865a8a40386 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 13:25:11 +0100 Subject: rename AutoMerge to Document --- src/swarm/blob.rs | 18 +++++++++--------- src/swarm/bootstrap.rs | 12 ++++++------ src/swarm/members.rs | 34 +++++++++++++++++----------------- src/swarm/peer/forget.rs | 4 ++-- src/swarm/relay.rs | 2 +- src/swarm/state_file.rs | 2 +- 6 files changed, 36 insertions(+), 36 deletions(-) (limited to 'src/swarm') diff --git a/src/swarm/blob.rs b/src/swarm/blob.rs index ec99c93..57b02b2 100644 --- a/src/swarm/blob.rs +++ b/src/swarm/blob.rs @@ -28,11 +28,11 @@ use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; use anyhow::{Result, anyhow, bail}; -use automerge::{AutoCommit, ChangeHash}; use tokio::sync::{Semaphore, mpsc}; use tokio::time::Instant; use tracing::{debug, info, warn}; +use crate::document::{ChangeHash, Document}; use crate::locks::MutexExt; use crate::state::AppState; use crate::transport::{BlobBody, BlobId, BlobMessage, DeltaSpec, ParsedMessage}; @@ -586,9 +586,9 @@ pub(crate) async fn fetch_change( state: &AppState, hash: ChangeHash, peers: Vec, -) -> Result { +) -> Result { let bytes = fetch(state, BlobId::Change(hash), peers, None, None).await?; - let change = automerge::Change::from_bytes(bytes) + let change = crate::document::Change::from_bytes(bytes) .map_err(|error| anyhow!("downloaded change {hash} doesn't load: {error}"))?; if change.hash() != hash { bail!("downloaded change has hash {}, not {hash}", change.hash()); @@ -608,9 +608,9 @@ pub(crate) async fn fetch_delta( /// The bytes of delta `spec` from `doc`: the changes in the history of `spec.to` but /// not of `spec.from`, each as Automerge encodes it, sorted by hash and concatenated -/// (`AutoCommit::load_incremental` reads that). The same on every node holding `to`, +/// (`Document::load_incremental` reads that). The same on every node holding `to`, /// whatever else it has. None if `doc` lacks part of `to`. -pub(crate) fn delta_bytes(doc: &mut AutoCommit, spec: &DeltaSpec) -> Option> { +pub(crate) fn delta_bytes(doc: &mut Document, spec: &DeltaSpec) -> Option> { if spec .to .iter() @@ -659,7 +659,7 @@ mod tests { use super::*; use automerge::{ActorId, ROOT, transaction::Transactable}; - fn random_write(rng: &mut crate::testrand::Rng, doc: &mut AutoCommit) { + fn random_write(rng: &mut crate::testrand::Rng, doc: &mut Document) { let key = format!("k{}", rng.below(6)); doc.put(ROOT, key, rng.below(100) as i64).unwrap(); doc.commit(); @@ -677,12 +677,12 @@ mod tests { fn delta_bytes_are_the_same_everywhere_and_complete() { crate::testrand::for_seeds(200, |rng| { let actor = |i: u8| ActorId::from(&[i; 16][..]); - let mut base = AutoCommit::new().with_actor(actor(0)); + let mut base = Document::new().with_actor(actor(0)); for _ in 0..rng.between(0, 5) { random_write(rng, &mut base); } let from = base.get_heads(); - let mut writers: Vec = (1..=rng.between(1, 3)) + let mut writers: Vec = (1..=rng.between(1, 3)) .map(|i| base.fork().with_actor(actor(i as u8))) .collect(); for _ in 0..rng.between(1, 15) { @@ -714,7 +714,7 @@ mod tests { #[test] fn no_delta_without_its_heads() { - let mut doc = AutoCommit::new(); + let mut doc = Document::new(); doc.put(ROOT, "k", 1_i64).unwrap(); doc.commit(); let spec = DeltaSpec::new(Vec::new(), vec![ChangeHash([7; 32])]); diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs index a9ab1b2..ec9a355 100644 --- a/src/swarm/bootstrap.rs +++ b/src/swarm/bootstrap.rs @@ -18,7 +18,6 @@ use std::collections::HashSet; use std::path::Path; use anyhow::{Context, Result, bail}; -use automerge::{ActorId, AutoCommit}; use serde_json::Value as JsonValue; use tokio::time::Instant; use tracing::{debug, info, warn}; @@ -28,6 +27,7 @@ use crate::document; use crate::document::meta::{self, SWARM}; use crate::document::replica::Epoch; use crate::document::store; +use crate::document::{ActorId, Document}; use crate::files::{FileKind, Files}; use crate::force::Force; use crate::remake::snapshot; @@ -156,7 +156,7 @@ fn swarm_id_path() -> Vec { } /// The swarm ID the document names, if any. -fn read_swarm_id(doc: &AutoCommit) -> Option { +fn read_swarm_id(doc: &Document) -> Option { match store::read_path(doc, &swarm_id_path()) { Ok(Some(JsonValue::String(id))) if !id.is_empty() => Some(id), _ => None, @@ -184,7 +184,7 @@ impl Origin { /// A document the node starts from, ready at once: on the swarm and epoch it names. pub(crate) struct StartingDocument { /// Under a fresh actor. - pub(crate) doc: AutoCommit, + pub(crate) doc: Document, pub(crate) epoch: Epoch, pub(crate) swarm_id: String, pub(crate) origin: Origin, @@ -281,7 +281,7 @@ pub(crate) fn starting_document( /// The swarm and epoch `doc` is on. fn identify( - mut doc: AutoCommit, + mut doc: Document, origin: Origin, earlier_actors: Vec, ) -> Result { @@ -301,12 +301,12 @@ fn identify( } /// A backup to recover from, as `GET /admin/document` saves it, under a fresh actor. -fn load_backup(path: &Path, files: &Files) -> Result { +fn load_backup(path: &Path, files: &Files) -> Result { let bytes = files .read(FileKind::Backup, path) .with_context(|| format!("Reading the backup {}", path.display()))? .with_context(|| format!("The backup {} doesn't exist", path.display()))?; - Ok(AutoCommit::load(&bytes) + Ok(Document::load(&bytes) .with_context(|| format!("Loading the backup {}", path.display()))? .with_actor(ActorId::random())) } diff --git a/src/swarm/members.rs b/src/swarm/members.rs index 94db4c9..0e27681 100644 --- a/src/swarm/members.rs +++ b/src/swarm/members.rs @@ -18,10 +18,10 @@ use std::collections::HashMap; use std::time::Duration; -use automerge::AutoCommit; use serde_json::{Value as JsonValue, json}; use tracing::{debug, info, warn}; +use crate::document::Document; use crate::document::meta; use crate::document::store::{self, StoreError}; use crate::state::AppState; @@ -37,7 +37,7 @@ pub(crate) fn member_path(pk: &str) -> Vec { } /// Public keys of all registered members, sorted. -pub(crate) fn members(doc: &AutoCommit) -> Vec { +pub(crate) fn members(doc: &Document) -> Vec { let mut members = store::keys_at(doc, &members_dir()).unwrap_or_default(); members.sort(); members @@ -67,14 +67,14 @@ fn tier_path(pk: &str) -> Vec { } /// `pk`'s tier; transient unless its entry says stable. -pub(crate) fn tier(doc: &AutoCommit, pk: &str) -> Tier { +pub(crate) fn tier(doc: &Document, pk: &str) -> Tier { match store::read_path(doc, &tier_path(pk)) { Ok(Some(JsonValue::String(tier))) if tier == "stable" => Tier::Stable, _ => Tier::Transient, } } -pub(crate) fn set_tier(doc: &mut AutoCommit, pk: &str, tier: Tier) -> Result<(), StoreError> { +pub(crate) fn set_tier(doc: &mut Document, pk: &str, tier: Tier) -> Result<(), StoreError> { store::write_path(doc, &tier_path(pk), &json!(tier.as_str())) } @@ -85,7 +85,7 @@ fn alias_path(pk: &str) -> Vec { } /// `pk`'s alias, if it gave itself one. -pub(crate) fn alias(doc: &AutoCommit, pk: &str) -> Option { +pub(crate) fn alias(doc: &Document, pk: &str) -> Option { match store::read_path(doc, &alias_path(pk)) { Ok(Some(JsonValue::String(alias))) => Some(alias), _ => None, @@ -93,14 +93,14 @@ pub(crate) fn alias(doc: &AutoCommit, pk: &str) -> Option { } /// The aliases of all members that have one. -pub(crate) fn aliases(doc: &AutoCommit) -> HashMap { +pub(crate) fn aliases(doc: &Document) -> HashMap { members(doc) .into_iter() .filter_map(|pk| alias(doc, &pk).map(|alias| (pk, alias))) .collect() } -fn set_alias(doc: &mut AutoCommit, pk: &str, alias: Option<&str>) -> Result<(), StoreError> { +fn set_alias(doc: &mut Document, pk: &str, alias: Option<&str>) -> Result<(), StoreError> { match alias { Some(alias) => store::write_path(doc, &alias_path(pk), &json!(alias)), None => match store::delete_path(doc, &alias_path(pk), false) { @@ -121,7 +121,7 @@ const MAX_ADDRESSES: usize = 8; /// Where `pk` says it can be reached (at most `MAX_ADDRESSES`); none if its entry /// doesn't say. -pub(crate) fn addresses(doc: &AutoCommit, pk: &str) -> Vec { +pub(crate) fn addresses(doc: &Document, pk: &str) -> Vec { match store::read_path(doc, &addresses_path(pk)) { Ok(Some(JsonValue::Array(items))) => items .into_iter() @@ -138,7 +138,7 @@ pub(crate) fn addresses(doc: &AutoCommit, pk: &str) -> Vec { /// What the member list tells this node besides who its neighbours are: where members /// are (`learn_addresses`), what they call themselves (`set_aliases`), and the relays /// listed (`relays`). Done whenever the neighbours are recomputed (`topology::refresh`). -pub(crate) fn apply(state: &AppState, doc: &AutoCommit) { +pub(crate) fn apply(state: &AppState, doc: &Document) { learn_addresses(state, doc); set_aliases(state, aliases(doc)); state.transport.learn_relays(&relays(doc)); @@ -159,7 +159,7 @@ fn set_aliases(state: &AppState, aliases: HashMap) { /// Hands every other member's addresses to the transport (`Transport::learn_addresses`): /// the member list is how a node finds where members it never met are. -pub(crate) fn learn_addresses(state: &AppState, doc: &AutoCommit) { +pub(crate) fn learn_addresses(state: &AppState, doc: &Document) { let me = state.local_id(); for pk in members(doc) { if pk != me { @@ -175,7 +175,7 @@ const MAX_RELAYS: usize = 32; /// addresses (`libp2p:`, with or without the relay's peer ID) of relays that /// aren't members, e.g. nodes that only relay. Every member's transport may /// pick them (`Transport::learn_relays`), after the relays it is configured with. -pub(crate) fn relays(doc: &AutoCommit) -> Vec { +pub(crate) fn relays(doc: &Document) -> Vec { match store::read_path(doc, &meta::path(&[meta::RELAYS])) { Ok(Some(JsonValue::Array(items))) => items .into_iter() @@ -195,7 +195,7 @@ fn alive_path(pk: &str) -> Vec { path } -fn millis_at(doc: &AutoCommit, path: &[String]) -> Option { +fn millis_at(doc: &Document, path: &[String]) -> Option { match store::read_path(doc, path) { Ok(Some(JsonValue::Number(number))) => number.as_u64(), _ => None, @@ -203,7 +203,7 @@ fn millis_at(doc: &AutoCommit, path: &[String]) -> Option { } /// When `pk` last said it is alive (Unix ms): its last renewal, or its registration. -pub(crate) fn alive_at(doc: &AutoCommit, pk: &str) -> Option { +pub(crate) fn alive_at(doc: &Document, pk: &str) -> Option { let mut since = member_path(pk); since.push("since".to_owned()); millis_at(doc, &alive_path(pk)).max(millis_at(doc, &since)) @@ -211,7 +211,7 @@ pub(crate) fn alive_at(doc: &AutoCommit, pk: &str) -> Option { /// The members that said they are alive within `window` before `now_ms` (sorted): the /// electorate of a remake (`remake::coordinator`). -pub(crate) fn electorate(doc: &AutoCommit, now_ms: u64, window: Duration) -> Vec { +pub(crate) fn electorate(doc: &Document, now_ms: u64, window: Duration) -> Vec { let oldest = now_ms.saturating_sub(window.as_millis() as u64); members(doc) .into_iter() @@ -314,7 +314,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { } /// Removes `pk`'s entry (an operator forgot it). Absent is fine. -pub(crate) fn unregister(doc: &mut AutoCommit, pk: &str) -> Result<(), StoreError> { +pub(crate) fn unregister(doc: &mut Document, pk: &str) -> Result<(), StoreError> { match store::delete_path(doc, &member_path(pk), false) { Err(StoreError::NotFound) => Ok(()), result => result, @@ -329,7 +329,7 @@ mod tests { fn electorate_by_alive_stamp() { let day = 24 * 3600 * 1000; let now = 100 * day; - let mut doc = AutoCommit::new(); + let mut doc = Document::new(); let entries = [ ("fresh", json!({ "since": 1, "alive": now - day })), ("stale", json!({ "since": 1, "alive": now - 8 * day })), @@ -347,7 +347,7 @@ mod tests { #[test] fn listed_relays() { - let mut doc = AutoCommit::new(); + let mut doc = Document::new(); assert!(relays(&doc).is_empty()); let relay = "libp2p:/ip6/fd00::1/tcp/9661"; store::write_path(&mut doc, &meta::path(&[meta::RELAYS]), &json!([relay, 7])).unwrap(); diff --git a/src/swarm/peer/forget.rs b/src/swarm/peer/forget.rs index 6386e3e..3a97677 100644 --- a/src/swarm/peer/forget.rs +++ b/src/swarm/peer/forget.rs @@ -5,11 +5,11 @@ use std::collections::HashMap; use std::time::Duration; -use automerge::AutoCommit; use serde_json::Value as JsonValue; use tokio::time::Instant; use tracing::{info, warn}; +use crate::document::Document; use crate::document::meta; use crate::document::store::{self, StoreError}; use crate::state::{AppState, Standing}; @@ -24,7 +24,7 @@ pub(crate) fn forgotten_path(pk: &str) -> Vec { path } -fn forgotten_peers(doc: &AutoCommit) -> Vec { +fn forgotten_peers(doc: &Document) -> Vec { store::keys_at(doc, &forgotten_dir()).unwrap_or_default() } diff --git a/src/swarm/relay.rs b/src/swarm/relay.rs index 00043f2..c28c9f1 100644 --- a/src/swarm/relay.rs +++ b/src/swarm/relay.rs @@ -131,7 +131,7 @@ pub(crate) async fn pull_if_behind( // Those of the listed heads we lack. With a digest only a sample is listed; if we // have all of it, a later keepalive lists others. let missing: Vec<_> = { - // AutoCommit's lookups take &mut self. + // Document's lookups take &mut self. let mut replica = state.doc.write().await; remote .heads diff --git a/src/swarm/state_file.rs b/src/swarm/state_file.rs index a65fa5b..415fb1a 100644 --- a/src/swarm/state_file.rs +++ b/src/swarm/state_file.rs @@ -185,7 +185,7 @@ pub(crate) async fn update(state: &AppState, now: bool) { /// Where to rejoin through: the addresses of members alive within the electorate /// window, stable ones first, of at most `MAX_SEEDS` members, sorted. Changes only when /// members join, leave, move or change tier, so the file rarely does. -fn seeds(doc: &automerge::AutoCommit, me: &str, window: Duration, now: u64) -> Vec { +fn seeds(doc: &crate::document::Document, me: &str, window: Duration, now: u64) -> Vec { let (mut stable, transient): (Vec, Vec) = members::electorate(doc, now, window) .into_iter() .filter(|pk| pk != me) -- cgit v1.3.1