Skip to main content

adk_graph/checkpointers/
mod.rs

1//! Checkpointers for shared databases.
2//!
3//! Each backend sits behind the feature of the same name and passes the
4//! conformance suite (the `conformance` feature). They store the whole
5//! `Checkpoint` as JSON text next to the columns or keys they
6//! query, so a loaded checkpoint equals the saved one and new checkpoint fields need no
7//! schema change.
8//!
9//! | Backend | Feature | Version rule enforced by |
10//! |---------|---------|--------------------------|
11//! | `PostgresCheckpointer` | `postgres` | A guarded `INSERT` and a unique index on `(thread_id, version)` |
12//! | `RedisCheckpointer` | `redis` | One Lua script per save, reading the thread's highest version |
13//! | `MongoCheckpointer` | `mongodb` | A compare-and-set on the thread's head document and a unique index on `(thread_id, version)` |
14//! | `Neo4jCheckpointer` | `neo4j` | A write lock on the thread's node, taken before the head is read |
15//! | `FirestoreCheckpointer` | `firestore` | A read-write transaction over the thread's head document |
16
17#[cfg(any(
18    feature = "postgres",
19    feature = "redis",
20    feature = "mongodb",
21    feature = "neo4j",
22    feature = "firestore"
23))]
24use crate::{error::GraphError, state::Checkpoint};
25
26#[cfg(feature = "postgres")]
27pub mod postgres;
28
29#[cfg(feature = "redis")]
30pub mod redis;
31
32#[cfg(feature = "mongodb")]
33pub mod mongodb;
34
35#[cfg(feature = "neo4j")]
36pub mod neo4j;
37
38#[cfg(feature = "firestore")]
39pub mod firestore;
40
41#[cfg(any(
42    feature = "postgres",
43    feature = "redis",
44    feature = "mongodb",
45    feature = "neo4j",
46    feature = "firestore"
47))]
48fn storage_error(backend: &str, error: impl std::fmt::Display) -> GraphError {
49    GraphError::CheckpointError(format!("{backend} checkpointer: {error}"))
50}
51
52#[cfg(any(
53    feature = "postgres",
54    feature = "redis",
55    feature = "mongodb",
56    feature = "neo4j",
57    feature = "firestore"
58))]
59fn encode(checkpoint: &Checkpoint) -> crate::error::Result<String> {
60    Ok(serde_json::to_string(checkpoint)?)
61}
62
63#[cfg(any(
64    feature = "postgres",
65    feature = "redis",
66    feature = "mongodb",
67    feature = "neo4j",
68    feature = "firestore"
69))]
70fn decode(backend: &str, text: &str) -> crate::error::Result<Checkpoint> {
71    serde_json::from_str(text)
72        .map_err(|error| storage_error(backend, format!("stored checkpoint is corrupt: {error}")))
73}