Skip to content

Commit

Permalink
implemented new method to add a collection of edges into the graph vault
Browse files Browse the repository at this point in the history
  • Loading branch information
carvilsi committed Sep 4, 2024
1 parent f2d71e9 commit b894545
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,23 @@ impl Graphs {
graphs_memory_watcher(self);
}

// TODO: create method to add a collection of Edges
/// Adds a collection of Edges to the Graphs' vault
/// for the provided graphs vault name
/// if does not exists it creates a new entry
/// at vault.
/// If None name is provided, the current one
/// is use for the addition.
pub fn add_edges(&mut self, edges: &mut Vec<Edge>, vault_name: Option<&str>) {
let current_vault = self.select_vault_label(vault_name);
if let Some(e) = self.vault.get_mut(&current_vault) {
e.append(edges);
} else {
self.insert(&current_vault);
let v = self.vault.get_mut(&current_vault).unwrap();
v.append(edges);
}
graphs_memory_watcher(self);
}

/// Retrieves the collection of edges
/// the default one or by name
Expand Down
26 changes: 26 additions & 0 deletions tests/graphs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,29 @@ fn should_not_find_vertex_by_id_vault_does_not_exists() {
let e = graphs.find_vertex_by_id("foobar", Some("!Exists"));
assert_eq!(e, Err("provided vault does not exists"));
}

#[test]
fn should_add_a_collection_of_edges() {
let mut graphs = Graphs::init("collection-edges");

let v1 = Vertex::new("v1");
let v2 = Vertex::new("v2");
let v3 = Vertex::new("v3");

let mut edges: Vec<Edge> = Vec::new();

let e1 = Edge::create(&v1, "v1->v2", &v2);
edges.push(e1);
let e2 = Edge::create(&v1, "v1->v3", &v3);
edges.push(e2);
let e3 = Edge::create(&v2, "v2->v1", &v1);
edges.push(e3);
let e4 = Edge::create(&v2, "v2->v3", &v3);
edges.push(e4);

graphs.add_edges(&mut edges, None);

let stats = graphs.get_stats();
assert_eq!(stats.get_total_edges(), 4);
assert_eq!(stats.get_total_vertices(), 8);
}

0 comments on commit b894545

Please sign in to comment.