Skip to content

Commit

Permalink
refactoring and splitting graphs queries
Browse files Browse the repository at this point in the history
  • Loading branch information
carvilsi committed Sep 8, 2024
1 parent 434ff7c commit fbfba7e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
use crate::{edge::Edge, graphs_stats::GraphsStats, util::graphs_memory_watcher, vertex::Vertex};

mod persistence;
mod query;
mod queries;
mod stats;

/// A colection of Graph
Expand Down
108 changes: 11 additions & 97 deletions src/graphs/query.rs → src/graphs/queries/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use log::error;

use crate::edge::Edge;
use crate::graphs::Graphs;
use crate::vertex::Vertex;

impl Graphs {
/// Returns a collection of Edges that matches the relation
Expand All @@ -25,7 +24,7 @@ impl Graphs {
Err("Any edge found for relation")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -50,7 +49,7 @@ impl Graphs {
Err("Any edge found for relation")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -74,7 +73,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -98,7 +97,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -122,7 +121,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -146,7 +145,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand Down Expand Up @@ -175,7 +174,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -199,7 +198,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -223,7 +222,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -248,7 +247,7 @@ impl Graphs {
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -273,7 +272,7 @@ impl Graphs {
Err("edge not found")
}
} else {
Err("provided vault does not exists")
Err("Provided vault does not exists")
}
}

Expand All @@ -290,90 +289,5 @@ impl Graphs {
}
Err("edge not found")
}

/// Returns a Vertex that provided id matches with id of From, To vertices
/// for some provided vault_name or default when None
pub fn find_vertex_by_id(
&mut self,
id: &str,
vault_name: Option<&str>,
) -> Result<Vertex, &'static str> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if let Ok(vertex) = edge.find_vertex_by_id(id) {
return Ok(vertex);
}
}
Err("Vertex not found")
} else {
Err("provided vault does not exists")
}
}

/// Returns a Vertex that provided id matches with id of From, To vertices
/// on any graphs' vault
pub fn find_vertex_by_id_in_graphs(&mut self, id: &str) -> Result<Vertex, &'static str> {
for (vault_name, _edges) in self.vault.clone() {
if let Ok(vertex) = self.find_vertex_by_id(id, Some(vault_name.as_str())) {
return Ok(vertex);
}
}
Err("Vertex not found")
}

/// Retrieves all the vertices with incoming relation
/// for some provided vault_name or default when None
pub fn find_vertices_with_relation_in(
&self,
relation_in: &str,
vault_name: Option<&str>,
) -> Result<Vec<Vertex>, &'static str> {
let mut relations_in: Vec<Vertex> = Vec::new();
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if edge.get_relation() == relation_in
&& !relations_in.contains(&edge.get_to_vertex())
{
relations_in.push(edge.get_to_vertex().clone());
}
}
} else {
return Err("provided vault does not exists");
}
if !relations_in.is_empty() {
Ok(relations_in)
} else {
Err("any vertex found with relation in")
}
}

/// Retrieves all the vertices with outcoming relation
/// for some provided vault_name or default when None
pub fn find_vertices_with_relation_out(
&self,
relation_out: &str,
vault_name: Option<&str>,
) -> Result<Vec<Vertex>, &'static str> {
let mut relations_out: Vec<Vertex> = Vec::new();
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if edge.get_relation() == relation_out
&& !relations_out.contains(&edge.get_from_vertex())
{
relations_out.push(edge.get_from_vertex().clone());
}
}
} else {
return Err("provided vault does not exists");
}
if !relations_out.is_empty() {
Ok(relations_out)
} else {
Err("any vertex found with relation out")
}
}
}

2 changes: 2 additions & 0 deletions src/graphs/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod edges;
mod vertices;
89 changes: 89 additions & 0 deletions src/graphs/queries/vertices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::graphs::Graphs;
use crate::vertex::Vertex;

impl Graphs {
/// Returns a Vertex that provided id matches with id of From, To vertices
/// for some provided vault_name or default when None
pub fn find_vertex_by_id(
&mut self,
id: &str,
vault_name: Option<&str>,
) -> Result<Vertex, &'static str> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if let Ok(vertex) = edge.find_vertex_by_id(id) {
return Ok(vertex);
}
}
Err("Vertex not found")
} else {
Err("Provided vault does not exists")
}
}

/// Returns a Vertex that provided id matches with id of From, To vertices
/// on any graphs' vault
pub fn find_vertex_by_id_in_graphs(&mut self, id: &str) -> Result<Vertex, &'static str> {
for (vault_name, _edges) in self.vault.clone() {
if let Ok(vertex) = self.find_vertex_by_id(id, Some(vault_name.as_str())) {
return Ok(vertex);
}
}
Err("Vertex not found")
}

/// Retrieves all the vertices with incoming relation
/// for some provided vault_name or default when None
pub fn find_vertices_with_relation_in(
&self,
relation_in: &str,
vault_name: Option<&str>,
) -> Result<Vec<Vertex>, &'static str> {
let mut relations_in: Vec<Vertex> = Vec::new();
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if edge.get_relation() == relation_in
&& !relations_in.contains(&edge.get_to_vertex())
{
relations_in.push(edge.get_to_vertex().clone());
}
}
} else {
return Err("Provided vault does not exists");
}
if !relations_in.is_empty() {
Ok(relations_in)
} else {
Err("Any vertex found with relation in")
}
}

/// Retrieves all the vertices with outcoming relation
/// for some provided vault_name or default when None
pub fn find_vertices_with_relation_out(
&self,
relation_out: &str,
vault_name: Option<&str>,
) -> Result<Vec<Vertex>, &'static str> {
let mut relations_out: Vec<Vertex> = Vec::new();
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
for edge in edges {
if edge.get_relation() == relation_out
&& !relations_out.contains(&edge.get_from_vertex())
{
relations_out.push(edge.get_from_vertex().clone());
}
}
} else {
return Err("Provided vault does not exists");
}
if !relations_out.is_empty() {
Ok(relations_out)
} else {
Err("Any vertex found with relation out")
}
}
}
Loading

0 comments on commit fbfba7e

Please sign in to comment.