Skip to content

Commit

Permalink
[graph] Fix reuse edge index for node index #843 (#844)
Browse files Browse the repository at this point in the history
* reset data at index

* Update graph.rs
  • Loading branch information
michaelvlach authored Dec 10, 2023
1 parent 34a986d commit 83a3b5b
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion agdb/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ where
let id = self.data.transaction(storage);
let index = GraphIndex::from(self.get_free_index(storage)?);
let count = self.data.node_count(storage)?;

self.data.set_node_count(storage, count + 1)?;
self.data.commit(storage, id)?;

Expand Down Expand Up @@ -636,7 +637,10 @@ where
let next_free = self.data.from_meta(storage, GraphIndex::default())?;
self.data.set_from_meta(storage, index, next_free)?;
self.data
.set_from_meta(storage, GraphIndex::default(), -index.0)
.set_from_meta(storage, GraphIndex::default(), -index.0)?;
self.data.set_from(storage, index, 0)?;
self.data.set_to(storage, index, 0)?;
self.data.set_to_meta(storage, index, 0)
}

fn get_free_index(&mut self, storage: &mut Storage<D>) -> Result<i64, DbError> {
Expand Down Expand Up @@ -1390,4 +1394,18 @@ mod tests {
assert!(graph.edge(&storage, edge2).is_some());
assert!(graph.edge(&storage, edge3).is_some());
}

#[test]
fn reuse_edge_index_for_node() {
let test_file = TestFile::new();
let mut storage = Storage::<FileStorageMemoryMapped>::new(test_file.file_name()).unwrap();
let mut graph = DbGraph::new(&mut storage).unwrap();

let n1 = graph.insert_node(&mut storage).unwrap();
let n2 = graph.insert_node(&mut storage).unwrap();
let e1 = graph.insert_edge(&mut storage, n1, n2).unwrap();
graph.remove_edge(&mut storage, e1).unwrap();
let n3 = graph.insert_node(&mut storage).unwrap();
assert!(graph.node(&storage, n3).is_some());
}
}

0 comments on commit 83a3b5b

Please sign in to comment.