Skip to content

Commit

Permalink
added method find_edges_with_vertex_attr_vec_u8_equals_to for graphs …
Browse files Browse the repository at this point in the history
…query
  • Loading branch information
carvilsi committed Sep 8, 2024
1 parent 6de7dc5 commit 620350f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 33 deletions.
93 changes: 60 additions & 33 deletions src/graphs/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ impl Graphs {
Err("provided vault does not exists")
}
}

/// Returns a collection of edges that matches any attribute vertex by key
/// Returns a collection of edges like any attribute vertex key
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_key(
pub fn find_edges_with_vertex_attr_key_like(
&mut self,
attr_k: &str,
vault_name: Option<&str>,
) -> Result<Vec<&Edge>, &'static str> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
let edges = edges
let vrtcs = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_key(attr_k))
.filter(|edge| edge.has_vertex_with_attr_key_like(attr_k))
.collect::<Vec<&Edge>>();
if !edges.is_empty() {
Ok(edges)
if !vrtcs.is_empty() {
Ok(vrtcs)
} else {
error!("Any edge found for attribute: {}", attr_k);
Err("Any edge found for attribute")
Expand All @@ -78,9 +78,9 @@ impl Graphs {
}
}

/// Returns a collection of edges that matches a string attribute vertex by key
/// Returns a collection of edges that matches any attribute vertex by key
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_str_key(
pub fn find_edges_with_vertex_attr_key(
&mut self,
attr_k: &str,
vault_name: Option<&str>,
Expand All @@ -89,7 +89,7 @@ impl Graphs {
if let Some(edges) = self.vault.get(&current_vault) {
let edges = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_str_key(attr_k))
.filter(|edge| edge.has_vertex_with_attr_key(attr_k))
.collect::<Vec<&Edge>>();
if !edges.is_empty() {
Ok(edges)
Expand All @@ -102,9 +102,9 @@ impl Graphs {
}
}

/// Returns a collection of edges that matches a vector u8 attribute vertex by key
/// Returns a collection of edges that matches a string attribute vertex by key
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_vec_u8_key(
pub fn find_edges_with_vertex_attr_str_key(
&mut self,
attr_k: &str,
vault_name: Option<&str>,
Expand All @@ -113,7 +113,7 @@ impl Graphs {
if let Some(edges) = self.vault.get(&current_vault) {
let edges = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_vec_u8_key(attr_k))
.filter(|edge| edge.has_vertex_with_attr_str_key(attr_k))
.collect::<Vec<&Edge>>();
if !edges.is_empty() {
Ok(edges)
Expand Down Expand Up @@ -149,19 +149,24 @@ impl Graphs {
Err("provided vault does not exists")
}
}

/// Returns a collection of edges like vector u8 attribute vertex key


/// Returns a collection of edges that matches a string attribute vertex
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_vec_u8_key_like(
&mut self,
pub fn find_edges_with_vertex_attr_str_equals_to<T>(
&self,
attr_k: &str,
attr_v: T,
vault_name: Option<&str>,
) -> Result<Vec<&Edge>, &'static str> {
) -> Result<Vec<&Edge>, &'static str>
where
T: std::fmt::Display + std::clone::Clone,
{
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
let vrtcs = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_vec_u8_key_like(attr_k))
.filter(|edge| edge.has_vertex_with_attr_str_value_equals_to(attr_k, attr_v.clone()))
.collect::<Vec<&Edge>>();
if !vrtcs.is_empty() {
Ok(vrtcs)
Expand All @@ -172,11 +177,35 @@ impl Graphs {
} else {
Err("provided vault does not exists")
}
}
}

/// Returns a collection of edges like any attribute vertex key
/// Returns a collection of edges that matches a vector u8 attribute vertex by key
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_key_like(
pub fn find_edges_with_vertex_attr_vec_u8_key(
&mut self,
attr_k: &str,
vault_name: Option<&str>,
) -> Result<Vec<&Edge>, &'static str> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
let edges = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_vec_u8_key(attr_k))
.collect::<Vec<&Edge>>();
if !edges.is_empty() {
Ok(edges)
} else {
error!("Any edge found for attribute: {}", attr_k);
Err("Any edge found for attribute")
}
} else {
Err("provided vault does not exists")
}
}

/// Returns a collection of edges like vector u8 attribute vertex key
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_vec_u8_key_like(
&mut self,
attr_k: &str,
vault_name: Option<&str>,
Expand All @@ -185,7 +214,7 @@ impl Graphs {
if let Some(edges) = self.vault.get(&current_vault) {
let vrtcs = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_key_like(attr_k))
.filter(|edge| edge.has_vertex_with_attr_vec_u8_key_like(attr_k))
.collect::<Vec<&Edge>>();
if !vrtcs.is_empty() {
Ok(vrtcs)
Expand All @@ -197,23 +226,20 @@ impl Graphs {
Err("provided vault does not exists")
}
}
/// Returns a collection of edges that matches a string attribute vertex

/// Returns a collection of edges where vector u8 attribute value is equals to
/// for some provided vault_name or default when None
pub fn find_edges_with_vertex_attr_str_equals_to<T>(
&self,
pub fn find_edges_with_vertex_attr_vec_u8_equals_to(
&mut self,
attr_k: &str,
attr_v: T,
attr_v: &Vec<u8>,
vault_name: Option<&str>,
) -> Result<Vec<&Edge>, &'static str>
where
T: std::fmt::Display + std::clone::Clone,
{
) -> Result<Vec<&Edge>, &'static str> {
let current_vault = self.select_vault_label(vault_name);
if let Some(edges) = self.vault.get(&current_vault) {
let vrtcs = edges
.iter()
.filter(|edge| edge.has_vertex_with_attr_str_value_equals_to(attr_k, attr_v.clone()))
.filter(|edge| edge.has_vertex_with_attr_vec_u8_value_equals_to(attr_k, attr_v))
.collect::<Vec<&Edge>>();
if !vrtcs.is_empty() {
Ok(vrtcs)
Expand All @@ -226,6 +252,7 @@ impl Graphs {
}
}


/// Returns an Edge that provided id matches with Edge Id, or From, To vertices
/// for some provided vault_name or default when None
pub fn find_edge_by_id(
Expand Down
33 changes: 33 additions & 0 deletions tests/graphs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,36 @@ fn should_not_find_edges_with_vertex_that_has_any_attr_like_since_vault_doest_no
let e = graphs.find_edges_with_vertex_attr_key_like("phone", Some("!Exists"));
assert_eq!(e, Err("provided vault does not exists"));
}

#[test]
fn should_find_edges_with_vertex_that_has_vec_u8_attr_equals_to() {
let mut graphs = prepare_graphs_test();
let vec_u8_attr: Vec<u8> = vec![3, 1, 3, 3, 7];
let res = graphs.find_edges_with_vertex_attr_vec_u8_equals_to("code", &vec_u8_attr, None).unwrap();
assert_eq!(res.len(), 2);
assert_eq!(res[0].get_from_vertex().get_label(), "Fred".to_string());
}

#[test]
fn should_not_find_edges_with_vertex_that_has_vec_u8_attr_key_does_not_exists_equals_to() {
let mut graphs = prepare_graphs_test();
let vec_u8_attr: Vec<u8> = vec![3, 1, 3, 3, 7];
let e = graphs.find_edges_with_vertex_attr_vec_u8_equals_to("edoc", &vec_u8_attr, None);
assert_eq!(e, Err("Any edge found for attribute"));
}

#[test]
fn should_not_find_edges_with_vertex_that_has_vec_u8_attr_value_not_equals_to() {
let mut graphs = prepare_graphs_test();
let vec_u8_attr_nok: Vec<u8> = vec![1, 3, 3, 7];
let e = graphs.find_edges_with_vertex_attr_vec_u8_equals_to("code", &vec_u8_attr_nok, None);
assert_eq!(e, Err("Any edge found for attribute"));
}

#[test]
fn should_not_find_edges_with_vertex_that_has_vec_u8_attr_equals_to_vault_does_not_exists() {
let mut graphs = prepare_graphs_test();
let vec_u8_attr_nok: Vec<u8> = vec![1, 3, 3, 7];
let e = graphs.find_edges_with_vertex_attr_vec_u8_equals_to("code", &vec_u8_attr_nok, Some("!Exists"));
assert_eq!(e, Err("provided vault does not exists"));
}

0 comments on commit 620350f

Please sign in to comment.