- Add get_node() and contains_key() methods that read via redb index - Migrate all store/ reads to use index lookup - Remove HashMap cache updates from mutations (write-through to capnp+index only) - Remove replay_nodes() - load no longer builds HashMap - Update db_is_healthy to validate by spot-checking offsets - Fix set_weight bug: now persists weight changes to capnp Store.nodes HashMap still exists for code outside store/ module, but store/ itself no longer uses it. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
123 lines
3.9 KiB
Rust
123 lines
3.9 KiB
Rust
// Append-only Cap'n Proto storage + redb indices
|
|
//
|
|
// capnp logs are the source of truth:
|
|
// nodes.capnp - ContentNode messages
|
|
// relations.capnp - Relation messages
|
|
//
|
|
// redb provides indexed access; Store struct holds in-memory state.
|
|
//
|
|
// Module layout:
|
|
// types.rs — Node, Relation, enums, path/time helpers
|
|
// capnp.rs — serialization macros, log IO (load, replay, append, fsck)
|
|
// index.rs — redb index operations
|
|
// ops.rs — mutations (upsert, delete, rename, etc.)
|
|
// view.rs — StoreView trait for read-only access
|
|
|
|
mod types;
|
|
mod index;
|
|
mod capnp;
|
|
mod ops;
|
|
mod view;
|
|
|
|
// Re-export everything callers need
|
|
pub use types::{
|
|
memory_dir, nodes_path,
|
|
now_epoch, epoch_to_local, format_date, format_datetime, format_datetime_space, compact_timestamp, today,
|
|
Node, Relation, NodeType, RelationType,
|
|
new_node, new_relation,
|
|
};
|
|
pub use view::StoreView;
|
|
pub use capnp::fsck;
|
|
pub use ops::current_provenance;
|
|
|
|
use crate::graph::{self, Graph};
|
|
|
|
use std::collections::HashMap;
|
|
use anyhow::{bail, Result};
|
|
|
|
/// Strip .md suffix from a key, handling both bare keys and section keys.
|
|
/// "identity.md" → "identity", "foo.md#section" → "foo#section", "identity" → "identity"
|
|
pub fn strip_md_suffix(key: &str) -> String {
|
|
if let Some((file, section)) = key.split_once('#') {
|
|
let bare = file.strip_suffix(".md").unwrap_or(file);
|
|
format!("{}#{}", bare, section)
|
|
} else {
|
|
key.strip_suffix(".md").unwrap_or(key).to_string()
|
|
}
|
|
}
|
|
|
|
// The full in-memory store
|
|
pub struct Store {
|
|
pub nodes: HashMap<String, Node>, // key → latest node
|
|
pub uuid_to_key: HashMap<[u8; 16], String>, // uuid → key (rebuilt from nodes)
|
|
pub relations: Vec<Relation>, // all active relations
|
|
/// Log sizes at load time — used for staleness detection.
|
|
pub(crate) loaded_nodes_size: u64,
|
|
pub(crate) loaded_rels_size: u64,
|
|
/// redb index database
|
|
pub(crate) db: Option<redb::Database>,
|
|
}
|
|
|
|
impl Default for Store {
|
|
fn default() -> Self {
|
|
Store {
|
|
nodes: HashMap::new(),
|
|
uuid_to_key: HashMap::new(),
|
|
relations: Vec::new(),
|
|
loaded_nodes_size: 0,
|
|
loaded_rels_size: 0,
|
|
db: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Store {
|
|
pub fn build_graph(&self) -> Graph {
|
|
graph::build_graph(self)
|
|
}
|
|
|
|
/// Get a node by key, reading from capnp via the index.
|
|
pub fn get_node(&self, key: &str) -> Result<Option<Node>> {
|
|
let db = self.db.as_ref()
|
|
.ok_or_else(|| anyhow::anyhow!("store not loaded"))?;
|
|
|
|
match index::get_offset(db, key)? {
|
|
Some(offset) => Ok(Some(capnp::read_node_at_offset(offset)?)),
|
|
None => Ok(None),
|
|
}
|
|
}
|
|
|
|
/// Check if a node exists by key.
|
|
pub fn contains_key(&self, key: &str) -> Result<bool> {
|
|
let db = self.db.as_ref()
|
|
.ok_or_else(|| anyhow::anyhow!("store not loaded"))?;
|
|
index::contains_key(db, key)
|
|
}
|
|
|
|
pub fn resolve_key(&self, target: &str) -> Result<String> {
|
|
// Strip .md suffix if present — keys no longer use it
|
|
let bare = strip_md_suffix(target);
|
|
|
|
if self.contains_key(&bare)? {
|
|
return Ok(bare);
|
|
}
|
|
|
|
let db = self.db.as_ref()
|
|
.ok_or_else(|| anyhow::anyhow!("store not loaded"))?;
|
|
let all_keys = index::all_keys(db)?;
|
|
|
|
let matches: Vec<_> = all_keys.iter()
|
|
.filter(|k| k.to_lowercase().contains(&target.to_lowercase()))
|
|
.cloned().collect();
|
|
|
|
match matches.len() {
|
|
0 => bail!("No entry for '{}'. Run 'init'?", target),
|
|
1 => Ok(matches[0].clone()),
|
|
n if n <= 10 => {
|
|
let list = matches.join("\n ");
|
|
bail!("Ambiguous '{}'. Matches:\n {}", target, list)
|
|
}
|
|
n => bail!("Too many matches for '{}' ({}). Be more specific.", target, n),
|
|
}
|
|
}
|
|
}
|