lora_store/snapshot.rs
1//! Snapshot value types — the portable payload + metadata + error
2//! vocabulary every backend speaks.
3//!
4//! The on-disk codec lives in the [`lora-snapshot`] crate (column-
5//! oriented, optionally compressed and authenticated). `lora-store` is
6//! deliberately codec-free: backends produce a [`SnapshotPayload`]
7//! through their inherent helpers (e.g. [`super::InMemoryGraph::snapshot_payload`]),
8//! the database layer encodes it via `lora-snapshot`.
9
10use serde::{Deserialize, Serialize};
11use thiserror::Error;
12
13use crate::{NodeId, NodeRecord, RelationshipId, RelationshipRecord};
14
15/// Portable representation of an entire store state.
16///
17/// Backends produce and consume this struct through inherent helpers;
18/// the byte-level codec is owned by `lora-snapshot`.
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20pub struct SnapshotPayload {
21 pub next_node_id: NodeId,
22 pub next_rel_id: RelationshipId,
23 pub nodes: Vec<NodeRecord>,
24 pub relationships: Vec<RelationshipRecord>,
25}
26
27impl SnapshotPayload {
28 pub fn empty() -> Self {
29 Self {
30 next_node_id: 0,
31 next_rel_id: 0,
32 nodes: Vec::new(),
33 relationships: Vec::new(),
34 }
35 }
36}
37
38/// Metadata reported by snapshot encode / decode entry points. Kept
39/// small and stable so callers can log / diff it without reflecting on
40/// the payload.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct SnapshotMeta {
43 /// Format version the payload is written in.
44 pub format_version: u32,
45 /// Number of nodes in the snapshot.
46 pub node_count: usize,
47 /// Number of relationships in the snapshot.
48 pub relationship_count: usize,
49 /// WAL log position captured alongside the snapshot, if any. `None` for
50 /// pure (non-checkpoint) snapshots.
51 pub wal_lsn: Option<u64>,
52}
53
54/// Errors a backend may surface while building or restoring a snapshot
55/// payload. Codec-level errors live in [`lora-snapshot`]; these are the
56/// store-side payload-shaped failures.
57#[derive(Debug, Error)]
58pub enum SnapshotError {
59 #[error("snapshot I/O error: {0}")]
60 Io(#[from] std::io::Error),
61
62 #[error("snapshot payload could not be decoded: {0}")]
63 Decode(String),
64
65 #[error("snapshot payload could not be encoded: {0}")]
66 Encode(String),
67}