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

fix node type filter #1600

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion raphtory/src/db/api/view/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
storage::locked::LockedGraph,
view::{
internal::{CoreGraphOps, TimeSemantics},
TimeOps,
Base, TimeOps,

Check warning on line 14 in raphtory/src/db/api/view/node.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks / Rust Benchmark (ubuntu-latest)

unused import: `Base`
},
},
prelude::{EdgeViewOps, GraphViewOps, LayerOps},
Expand Down Expand Up @@ -91,7 +91,7 @@
/// Gets the history of the node (time that the node was added and times when changes were made to the node)
fn history(&self) -> Self::ValueType<Vec<i64>>;

/// Gets the history of the node (time that the node was added and times when changes were made to the node) as DateTime<Utc> objects if parseable

Check warning on line 94 in raphtory/src/db/api/view/node.rs

View workflow job for this annotation

GitHub Actions / Run Rust tests / Doc tests (ubuntu-latest)

unclosed HTML tag `Utc`
fn history_date_time(&self) -> Self::ValueType<Option<Vec<DateTime<Utc>>>>;

/// Get a view of the temporal properties of this node.
Expand Down
24 changes: 23 additions & 1 deletion raphtory/src/db/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ mod db_tests {
EdgeViewOps, Layer, LayerOps, NodeViewOps, TimeOps,
},
},
graph::{edge::EdgeView, edges::Edges, node::NodeView, path::PathFromNode},
graph::{
edge::EdgeView, edges::Edges, node::NodeView, nodes::Nodes, path::PathFromNode,
},
},
graphgen::random_attachment::random_attachment,
prelude::{AdditionOps, PropertyAdditionOps},
Expand Down Expand Up @@ -2154,6 +2156,26 @@ mod db_tests {
);
}

#[test]
fn test_node_type() {
let g = Graph::new();
g.add_node(1, 1, NO_PROPS, Some("a")).unwrap();
g.add_node(2, 2, NO_PROPS, Some("b")).unwrap();
g.add_node(2, 3, NO_PROPS, Some("b")).unwrap();
g.add_edge(3, 1, 2, NO_PROPS, None).unwrap();
g.add_edge(3, 3, 2, NO_PROPS, None).unwrap();

for node in g.nodes().filter_by_type(vec!["a"]) {
assert_eq!(node.degree(), 1);
}

for node in g.nodes() {
if node.node_type() == Some(ArcStr::from("a")) {
assert_eq!(node.degree(), 1);
}
}
}

#[test]
fn test_persistent_graph() {
let g = Graph::new();
Expand Down
32 changes: 30 additions & 2 deletions raphtory/src/db/graph/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@
prelude::*,
};

use crate::db::api::storage::locked::LockedGraph;
use crate::{core::ArcStr, db::api::storage::locked::LockedGraph};

Check warning on line 17 in raphtory/src/db/graph/nodes.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks / Rust Benchmark (ubuntu-latest)

unused import: `core::ArcStr`
use itertools::Itertools;

Check warning on line 18 in raphtory/src/db/graph/nodes.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks / Rust Benchmark (ubuntu-latest)

unused import: `itertools::Itertools`
use rayon::iter::ParallelIterator;
use std::{marker::PhantomData, sync::Arc};
use std::{
borrow::Borrow,

Check warning on line 21 in raphtory/src/db/graph/nodes.rs

View workflow job for this annotation

GitHub Actions / Run benchmarks / Rust Benchmark (ubuntu-latest)

unused imports: `FilterMap`, `FlatMap`, `Map`, `borrow::Borrow`
iter::{FilterMap, FlatMap, Map},
marker::PhantomData,
sync::Arc,
};

#[derive(Clone)]
pub struct Nodes<'graph, G, GH = G> {
Expand Down Expand Up @@ -96,6 +101,29 @@
))
}

pub fn filter_by_type<I: IntoIterator<Item = V>, V: AsRef<str>>(
&self,
node_types: I,
) -> BoxedLIter<'graph, NodeView<G, G>>
where
I::IntoIter: Send + Sync + 'graph,
V: Send + Sync + 'graph,
{
let base_graph = self.base_graph.clone();
node_types
.into_iter()
.flat_map(move |nt| {
base_graph.nodes().into_iter().filter_map(move |node| {
if nt.as_ref() == node.node_type()?.as_ref() {
Some(node)
} else {
None
}
})
})
.into_dyn_boxed()
}

pub fn collect(&self) -> Vec<NodeView<G, GH>> {
self.iter().collect()
}
Expand Down
Loading