Skip to content

Commit

Permalink
Implemented None step
Browse files Browse the repository at this point in the history
  • Loading branch information
criminosis committed Sep 17, 2024
1 parent c10ceaa commit 6a94a88
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
15 changes: 14 additions & 1 deletion gremlin-client/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
process::traversal::Bytecode,
structure::{TextP, P as Predicate},
structure::{Null, TextP, P as Predicate},
Edge, GKey, GValue, GremlinError, GremlinResult, IntermediateRepr, List, Map, Metric, Path,
Property, Token, TraversalExplanation, TraversalMetrics, Vertex, VertexProperty, GID,
};
Expand Down Expand Up @@ -134,6 +134,19 @@ impl_from_gvalue!(IntermediateRepr, GValue::IntermediateRepr);
impl_from_gvalue!(chrono::DateTime<chrono::Utc>, GValue::Date);
impl_from_gvalue!(Traverser, GValue::Traverser);

impl FromGValue for Null {
fn from_gvalue(v: GValue) -> GremlinResult<Self> {
match v {
GValue::Null => Ok(crate::structure::Null {}),
_ => Err(GremlinError::Cast(format!(
"Cannot convert {:?} to {}",
v,
stringify!($t)
))),
}
}
}

impl FromGValue for GKey {
fn from_gvalue(v: GValue) -> GremlinResult<GKey> {
match v {
Expand Down
6 changes: 6 additions & 0 deletions gremlin-client/src/process/traversal/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ impl TraversalBuilder {
self
}

pub fn none(mut self) -> Self {
self.bytecode.add_step(String::from("none"), vec![]);

self
}

pub fn label(mut self) -> Self {
self.bytecode.add_step(String::from("label"), vec![]);

Expand Down
12 changes: 11 additions & 1 deletion gremlin-client/src/process/traversal/graph_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::process::traversal::strategies::{
RemoteStrategy, TraversalStrategies, TraversalStrategy,
};
use crate::process::traversal::{Bytecode, Scope, TraversalBuilder, WRITE_OPERATORS};
use crate::structure::{Cardinality, Labels};
use crate::structure::{Cardinality, Labels, Null};
use crate::{
structure::GIDs, structure::GProperty, structure::IntoPredicate, Edge, GValue, GremlinClient,
List, Map, Path, Vertex,
Expand Down Expand Up @@ -302,6 +302,16 @@ impl<S, E: FromGValue, T: Terminator<E>> GraphTraversal<S, E, T> {
GraphTraversal::new(self.terminator, self.builder)
}

///Filters all objects from the traversal stream. Generally only useful for applying traversal sideffects and avoiding unwanted response I/O
pub fn none(mut self) -> GraphTraversal<S, Null, T>
where
T: Terminator<Null>,
{
self.builder = self.builder.none();

GraphTraversal::new(self.terminator, self.builder)
}

pub fn label(mut self) -> GraphTraversal<S, String, T>
where
T: Terminator<String>,
Expand Down
2 changes: 2 additions & 0 deletions gremlin-client/src/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod macros;
mod map;
mod merge;
mod metrics;
mod null;
mod p;
mod path;
mod pop;
Expand All @@ -28,6 +29,7 @@ pub use self::edge::Edge;
pub use self::gid::{GIDs, GID};
pub use self::list::List;
pub use self::metrics::{IntermediateRepr, Metric, TraversalExplanation, TraversalMetrics};
pub use self::null::Null;
pub use self::path::Path;
pub use self::property::Property;
pub use self::result::GResultSet;
Expand Down
2 changes: 2 additions & 0 deletions gremlin-client/src/structure/null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug, Clone)]
pub struct Null {}
31 changes: 31 additions & 0 deletions gremlin-client/tests/integration_traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,37 @@ fn test_coalesce_unfold() {
);
}

#[test]
fn test_none_step() {
let client = graph();

drop_vertices(&client, "test_none_step").unwrap();

let g = traversal().with_remote(client);

//The addition of a None step however should not IO a vertex back
let mut iter = g
.add_v("test_none_step")
.none()
.iter()
.expect("Should get a iter back");
assert!(
iter.next().is_none(),
"But the iter should have no elements because of the None step"
);

//Make sure the vertex is present in the graph
let vertex_count = g
.v(())
.has_label("test_none_step")
.count()
.next()
.ok()
.flatten()
.expect("Should have gotten a response");
assert_eq!(1, vertex_count);
}

#[test]
#[cfg(feature = "derive")]
fn test_traversal_vertex_mapping() {
Expand Down

0 comments on commit 6a94a88

Please sign in to comment.