// 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(&self, f: F); /// Iterate all nodes with metadata. Callback receives (key, node_type, timestamp). fn for_each_node_meta(&self, f: F); /// Iterate all relations. Callback receives (source_key, target_key, strength, rel_type). fn for_each_relation(&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(&self, mut f: F) { for (key, node) in &self.nodes { f(key, &node.content, node.weight); } } fn for_each_node_meta(&self, mut f: F) { for (key, node) in &self.nodes { f(key, node.node_type, node.timestamp); } } fn for_each_relation(&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()) } }