forked from kent/consciousness
Consolidate capnp serialization in one place: - capnp_enum! and capnp_message! macros - read_text/read_uuid helpers - Type-to-capnp mappings - from_capnp_migrate migration impls types.rs now only has pure Rust types and helpers. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
55 lines
1.9 KiB
Rust
55 lines
1.9 KiB
Rust
// Read-only access abstraction for the memory store
|
|
|
|
use super::types::*;
|
|
use super::Store;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// StoreView: read-only access trait for search and graph code.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
pub trait StoreView {
|
|
/// Iterate all nodes. Callback receives (key, content, weight).
|
|
fn for_each_node<F: FnMut(&str, &str, f32)>(&self, f: F);
|
|
|
|
/// Iterate all nodes with metadata. Callback receives (key, node_type, timestamp).
|
|
fn for_each_node_meta<F: FnMut(&str, NodeType, i64)>(&self, f: F);
|
|
|
|
/// Iterate all relations. Callback receives (source_key, target_key, strength, rel_type).
|
|
fn for_each_relation<F: FnMut(&str, &str, f32, RelationType)>(&self, f: F);
|
|
|
|
/// Node weight by key, or the default weight if missing.
|
|
fn node_weight(&self, key: &str) -> f64;
|
|
|
|
/// Node content by key.
|
|
fn node_content(&self, key: &str) -> Option<&str>;
|
|
}
|
|
|
|
impl StoreView for Store {
|
|
fn for_each_node<F: FnMut(&str, &str, f32)>(&self, mut f: F) {
|
|
for (key, node) in &self.nodes {
|
|
f(key, &node.content, node.weight);
|
|
}
|
|
}
|
|
|
|
fn for_each_node_meta<F: FnMut(&str, NodeType, i64)>(&self, mut f: F) {
|
|
for (key, node) in &self.nodes {
|
|
f(key, node.node_type, node.timestamp);
|
|
}
|
|
}
|
|
|
|
fn for_each_relation<F: FnMut(&str, &str, f32, RelationType)>(&self, mut f: F) {
|
|
for rel in &self.relations {
|
|
if rel.deleted { continue; }
|
|
f(&rel.source_key, &rel.target_key, rel.strength, rel.rel_type);
|
|
}
|
|
}
|
|
|
|
fn node_weight(&self, key: &str) -> f64 {
|
|
let cfg = crate::config::get();
|
|
self.nodes.get(key).map(|n| n.weight as f64).unwrap_or(cfg.default_node_weight)
|
|
}
|
|
|
|
fn node_content(&self, key: &str) -> Option<&str> {
|
|
self.nodes.get(key).map(|n| n.content.as_str())
|
|
}
|
|
}
|