Add Rust one-call snapshot document sync

This commit is contained in:
Meta Vens 2026-06-28 00:17:24 +01:00
commit bbde981082
3 changed files with 86 additions and 7 deletions

View file

@ -113,8 +113,8 @@ document without re-encoding the payload first.
The receiving Rust type does not have to be the sender's concrete Rust type. If
the local registry binds a different `DatabaseEntry` to the same schema id,
`apply_raw_snapshot_response::<LocalAlias>(...)` hydrates the local alias type
directly:
`sync_raw_document_from_snapshot_response::<LocalAlias>(...)` hydrates one local
alias type directly:
```rust
let mut target_registry = CultNetDocumentRegistry::new();
@ -123,9 +123,10 @@ target_registry.register(CultNetDocumentBinding::for_entry_with_schema_id::<UiNo
"aetheria.note.v1".to_string(),
));
let applied = target_registry.apply_raw_snapshot_response::<UiNote>(
let note = target_registry.sync_raw_document_from_snapshot_response::<UiNote>(
&mut target_cache,
&snapshot_response,
"note:bridge",
)?;
```

View file

@ -352,6 +352,40 @@ impl CultNetDocumentRegistry {
Ok(applied)
}
pub fn sync_document_from_snapshot_response<T>(
&self,
cache: &mut CultCache,
response: &CultNetMessage,
record_key: &str,
) -> Result<T>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::SnapshotResponse { documents, .. } = response else {
return Err(anyhow!("expected cultnet.snapshot_response.v0"));
};
let binding = self.require_binding(T::TYPE)?;
let document = documents
.iter()
.find(|document| {
document.schema_id == binding.schema_id && document.record_key == record_key
})
.ok_or_else(|| {
anyhow!(
"No CultNet snapshot document for schema {:?} and record key {:?}",
binding.schema_id,
record_key
)
})?;
self.apply_document_put_message::<T>(
cache,
&CultNetMessage::DocumentPut {
message_id: "snapshot-sync".to_string(),
document: document.clone(),
},
)
}
pub fn apply_raw_snapshot_response<T>(
&self,
cache: &mut CultCache,
@ -380,6 +414,40 @@ impl CultNetDocumentRegistry {
Ok(applied)
}
pub fn sync_raw_document_from_snapshot_response<T>(
&self,
cache: &mut CultCache,
response: &CultNetMessage,
record_key: &str,
) -> Result<T>
where
T: DatabaseEntry + Serialize + DeserializeOwned,
{
let CultNetMessage::SnapshotResponseRaw { documents, .. } = response else {
return Err(anyhow!("expected cultnet.snapshot_response_raw.v0"));
};
let binding = self.require_binding(T::TYPE)?;
let document = documents
.iter()
.find(|document| {
document.schema_id == binding.schema_id && document.record_key == record_key
})
.ok_or_else(|| {
anyhow!(
"No raw CultNet snapshot document for schema {:?} and record key {:?}",
binding.schema_id,
record_key
)
})?;
self.apply_raw_document_put_message::<T>(
cache,
&CultNetMessage::DocumentPutRaw {
message_id: "snapshot-sync-raw".to_string(),
document: document.clone(),
},
)
}
fn document_record_from_envelope(
&self,
envelope: &CultCacheEnvelope,

View file

@ -1630,7 +1630,13 @@ fn document_registry_replicates_typed_cultcache_state() -> Result<()> {
target.pull_all_backing_stores()?;
let applied =
registry.apply_snapshot_response::<GhostlightAgentStateFixture>(&mut target, &snapshot)?;
let synced = registry.sync_document_from_snapshot_response::<GhostlightAgentStateFixture>(
&mut target,
&snapshot,
"epiphany.persona",
)?;
assert_eq!(applied, vec![payload.clone()]);
assert_eq!(synced, payload);
assert_eq!(
target.get_required::<GhostlightAgentStateFixture>("epiphany.persona")?,
payload
@ -1739,19 +1745,23 @@ fn raw_snapshot_replication_hydrates_same_schema_rust_aliases() -> Result<()> {
target.pull_all_backing_stores()?;
let applied = alias_registry
.apply_raw_snapshot_response::<GhostlightAgentStateUiFixture>(&mut target, &raw_snapshot)?;
.sync_raw_document_from_snapshot_response::<GhostlightAgentStateUiFixture>(
&mut target,
&raw_snapshot,
"epiphany.persona",
)?;
assert_eq!(
applied,
vec![GhostlightAgentStateUiFixture {
GhostlightAgentStateUiFixture {
schema_version: "ghostlight.agent_state.v0".to_string(),
agent_id: "epiphany.persona".to_string(),
display_name: "Persona".to_string(),
}]
}
);
assert_eq!(
target.get_required::<GhostlightAgentStateUiFixture>("epiphany.persona")?,
applied[0]
applied
);
Ok(())
}