forked from kent/consciousness
store: remove Vec<Relation>, dedup uses index iteration
The relations Vec is gone from Store. dedup now iterates via edges_for_uuid() instead of mutating in-memory Vec — removes/re-adds edges through the index directly. Removed load_relations_vec() and clear_relations() — no longer needed. Added helper methods: edges_for_uuid, index_relation, remove_relation_from_index. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
c2de14dcab
commit
2548ca059d
5 changed files with 105 additions and 78 deletions
|
|
@ -272,12 +272,7 @@ impl Store {
|
|||
store.loaded_nodes_size = fs::metadata(&nodes_p).map(|m| m.len()).unwrap_or(0);
|
||||
store.loaded_rels_size = fs::metadata(&rels_p).map(|m| m.len()).unwrap_or(0);
|
||||
|
||||
// Drop edges referencing deleted/missing nodes
|
||||
let db = store.db.as_ref().unwrap();
|
||||
store.relations.retain(|r|
|
||||
index::contains_key(db, &r.source_key).unwrap_or(false) &&
|
||||
index::contains_key(db, &r.target_key).unwrap_or(false)
|
||||
);
|
||||
// Orphan edges filtered naturally during for_each_relation (unresolvable UUIDs skipped)
|
||||
|
||||
Ok(store)
|
||||
}
|
||||
|
|
@ -359,13 +354,10 @@ impl Store {
|
|||
}
|
||||
}
|
||||
|
||||
self.relations = by_uuid.into_values()
|
||||
.filter(|r| !r.deleted)
|
||||
.collect();
|
||||
|
||||
// Index relations in redb
|
||||
// Index relations directly (no Vec intermediate)
|
||||
if let Some(db) = &self.db {
|
||||
for rel in &self.relations {
|
||||
for rel in by_uuid.into_values() {
|
||||
if rel.deleted { continue; }
|
||||
index::index_relation(db, &rel.source, &rel.target, rel.strength, rel.rel_type as u8)?;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -209,18 +209,6 @@ pub fn remove_relation(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Clear all relations from the index.
|
||||
pub fn clear_relations(db: &Database) -> Result<()> {
|
||||
let txn = db.begin_write()?;
|
||||
{
|
||||
// Drop and recreate the table
|
||||
txn.delete_multimap_table(RELS)?;
|
||||
let _ = txn.open_multimap_table(RELS)?;
|
||||
}
|
||||
txn.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get all edges for a node. Returns (other_uuid, strength, rel_type, is_outgoing).
|
||||
pub fn edges_for_node(db: &Database, node_uuid: &[u8; 16]) -> Result<Vec<([u8; 16], f32, u8, bool)>> {
|
||||
let txn = db.begin_read()?;
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ pub fn strip_md_suffix(key: &str) -> String {
|
|||
|
||||
// The full in-memory store
|
||||
pub struct Store {
|
||||
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,
|
||||
|
|
@ -58,7 +57,6 @@ pub struct Store {
|
|||
impl Default for Store {
|
||||
fn default() -> Self {
|
||||
Store {
|
||||
relations: Vec::new(),
|
||||
loaded_nodes_size: 0,
|
||||
loaded_rels_size: 0,
|
||||
db: None,
|
||||
|
|
@ -130,14 +128,25 @@ impl Store {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Rebuild relation index from Vec. Call after mutations that modify relations.
|
||||
pub fn reindex_relations(&self) -> Result<()> {
|
||||
/// Get all edges for a node by UUID. Returns (other_uuid, strength, rel_type, is_outgoing).
|
||||
pub fn edges_for_uuid(&self, uuid: &[u8; 16]) -> Result<Vec<([u8; 16], f32, u8, bool)>> {
|
||||
let db = self.db.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("store not loaded"))?;
|
||||
index::edges_for_node(db, uuid)
|
||||
}
|
||||
|
||||
/// Add a relation to the index.
|
||||
pub fn index_relation(&self, source: &[u8; 16], target: &[u8; 16], strength: f32, rel_type: u8) -> Result<()> {
|
||||
if let Some(db) = self.db.as_ref() {
|
||||
index::clear_relations(db)?;
|
||||
for rel in &self.relations {
|
||||
if rel.deleted { continue; }
|
||||
index::index_relation(db, &rel.source, &rel.target, rel.strength, rel.rel_type as u8)?;
|
||||
}
|
||||
index::index_relation(db, source, target, strength, rel_type)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a relation from the index.
|
||||
pub fn remove_relation_from_index(&self, source: &[u8; 16], target: &[u8; 16], strength: f32, rel_type: u8) -> Result<()> {
|
||||
if let Some(db) = self.db.as_ref() {
|
||||
index::remove_relation(db, source, target, strength, rel_type)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,13 +28,12 @@ impl Store {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a relation (appends to log + updates cache + indexes)
|
||||
/// Add a relation (appends to log + indexes)
|
||||
pub fn add_relation(&mut self, rel: Relation) -> Result<()> {
|
||||
self.append_relations(std::slice::from_ref(&rel))?;
|
||||
if let Some(db) = &self.db {
|
||||
index::index_relation(db, &rel.source, &rel.target, rel.strength, rel.rel_type as u8)?;
|
||||
}
|
||||
self.relations.push(rel);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue