contextgraph_core/store.rs
1//! Storage traits. `CommitStore` owns the content-addressed blob graph;
2//! `RefStore` owns branch pointers. Both are behind traits so unit tests can
3//! run against fast in-memory fakes while production code uses SQLite.
4
5use crate::commit::{Commit, CommitId};
6use crate::error::Result;
7use async_trait::async_trait;
8
9/// Content-addressed storage for commits, plus the parent/child index needed
10/// for ancestry walks (`log`, `bisect`) without touching blob contents.
11#[async_trait]
12pub trait CommitStore: Send + Sync {
13 /// Stores a commit. Content-addressed and idempotent: storing
14 /// byte-identical content twice returns the same id without creating a
15 /// second entry. Enforces invariant 2 (acyclic, no dangling parents) by
16 /// rejecting any non-root commit whose parents are not already present.
17 async fn put(&self, commit: Commit) -> Result<CommitId>;
18
19 /// Fetches a commit by id, if it exists.
20 async fn get(&self, id: &CommitId) -> Result<Option<Commit>>;
21
22 /// True if a commit with this id has been stored.
23 async fn contains(&self, id: &CommitId) -> Result<bool>;
24
25 /// Direct children of a commit (reverse edge index), for ancestry walks.
26 async fn children(&self, id: &CommitId) -> Result<Vec<CommitId>>;
27
28 /// Total number of stored commits (used by gc / diagnostics).
29 async fn len(&self) -> Result<usize>;
30
31 async fn is_empty(&self) -> Result<bool> {
32 Ok(self.len().await? == 0)
33 }
34
35 /// All commit ids currently stored. Used by `gc` to compute reachability.
36 async fn all_ids(&self) -> Result<Vec<CommitId>>;
37
38 /// Removes commits by id unconditionally. Callers (e.g. `gc`) are
39 /// responsible for having already proven these ids are unreachable from
40 /// any branch. Not exposed as a normal write-path operation.
41 async fn remove_many(&self, ids: &[CommitId]) -> Result<()>;
42}
43
44/// Named, mutable pointers to commits — the only mutable state in the system.
45#[async_trait]
46pub trait RefStore: Send + Sync {
47 async fn get_branch(&self, name: &str) -> Result<Option<CommitId>>;
48
49 /// Creates or moves a branch to point at `commit_id`. Callers are
50 /// responsible for validating that `commit_id` exists before calling
51 /// this (see `ContextGraph::branch`/`move_branch`).
52 async fn set_branch(&self, name: &str, commit_id: CommitId) -> Result<()>;
53
54 async fn delete_branch(&self, name: &str) -> Result<()>;
55
56 async fn list_branches(&self) -> Result<Vec<(String, CommitId)>>;
57}