Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[graph] Fix reuse edge index for node index #843 #844

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}