wolf4ood / gremlin-rs

Gremlin Rust
Apache License 2.0
108 stars 30 forks source link

Add sideEffect step #164

Closed buck-ross closed 1 year ago

buck-ross commented 1 year ago

Requirement

I'm looking for a way to replicate the following gremlin-groovy query (and similarly structured queries) in gremlin-rs:

g.addV('person').property('name', 'Bob').
    sideEffect(addE('parent').to(__.V().hasLabel('person').has('name', 'Alice'))).
    project('id', 'name').by(id()).by('name')

In plain English, the requirement is as follows:

  1. Perform an initial operation on a vertex
  2. Create an edge from the current vertex to another vertex in a side effect
  3. Continue operating on the original vertex

From what I can tell, the only parts of this that are missing from this package are the anonymous id() traversal (which I have a work-around for), and the sideEffect(...) traversal (which I can't seem to work around).

Implementation

If we ignore the part of the gremlin spec that deals with passing arbitrary lambdas to sideEffect(), and focus on just passing anonymous traversals, this doesn't seem like it would be very difficult to implement. I imagine that the implementation might look something like the following for the traversal builder, although I haven't tested it:

pub fn side_effect<A>(mut self, traversal: &A) -> Self
where
    A: Into<Bytecode>,
{
    self.bytecode.add_source(
        String::from("sideEffect"),
        vec![traversal.to_gvalue()],
    );
    self
}

Please let me know your thoughts on this addition. If is well received, I will attempt to implement it myself & submit a PR.

wolf4ood commented 1 year ago

Closed by this https://github.com/wolf4ood/gremlin-rs/pull/183