Skip to content

Instantly share code, notes, and snippets.

@ixaxaar
Created August 10, 2016 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ixaxaar/4ac3fb9c58d09ebcaaac79f35b9eb755 to your computer and use it in GitHub Desktop.
Save ixaxaar/4ac3fb9c58d09ebcaaac79f35b9eb755 to your computer and use it in GitHub Desktop.

Traversals

Traversal[S,E] which implements Iterator[E] where S:Start, E:End

Step[S,E] //  an individual function applied to S to yield E . Steps are chained within a traversal.
TraversalStrategy // interceptor methods to alter the execution of the traversal (e.g. query re­writing).
TraversalSideEffects // key/value pairs that can be used to store global information about the traversal.
Traverser[T] // the object propagating through the Traversal currently representing an object of type T .

Lambda

map(Function[Traverser[S], E])
flatMap(Function[Traverser[S], Iterator[E]])
filter(Predicate[Traverser[S]])
sideEffect(Consumer[Traverser[S]])
branch(Function[Traverser[S],M])

AddEdge, AddVertex and AddProperty

addE(...)
addV(...)
property(...)

Aggregate

  • used to aggregate all the objects at a particular point of traversal into a Collection
aggregate[String]

And, As, By, Has, Is

g.V().and(
  outE('knows'),
  values('age').is(lt(30))
).values('name')

g.V().where(outE('created').and().outE('knows')).values('name')
  • With as(), it is possible to provide a label to the step that can later be accessed by steps and data structures that make use of such labels — e.g., select(), match(), and path.
g.V().as('a').out('created').as('b').select('a','b')
g.V().as('a').out('created').as('b').select('a','b').by('name')
  • If a step is able to accept traversals, functions, comparators, etc. then by() is the means by which they are added.
g.V().hasLabel('software').as('a','b','c').
  select('a','b','c').
  by('name').
  by('lang').
  by(__.in('created').values('name').fold())
  • It is possible to filter vertices, edges, and vertex properties based on their properties using has()-step (filter).
g.V().hasLabel('person').out().has('name',within('vadas','josh'))

g.V().has('name',within('josh','marko')).valueMap()

g.V().has('age',inside(20,30)).values('age')
  • It is possible to filter scalar values using is()-step (filter).
g.V().where(__.in('created').count().is(1)).values('name')

g.V().where(__.in('created').values('age').
  mean().is(inside(30d, 35d))).values('name')

Barrier

  • The barrier()-step (barrier) turns the the lazy traversal pipeline into a bulk-synchronous pipeline.
g.V().sideEffect{println "first: ${it}"}.sideEffect{println "second: ${it}"}.iterate()

Coalesce

  • The coalesce()-step evaluates the provided traversals in order and returns the first traversal that emits at least one element.
g.V(1).coalesce(outE('knows'), outE('created')).inV().path().by('name').by(label)

g.V(1).coalesce(outE('created'), outE('knows')).inV().path().by('name').by(label)

g.V().hasLabel('person').coalesce(values('nickname'), values('name'))

Count

The count()-step (map) counts the total number of represented traversers in the streams (i.e. the bulk count).

g.V().count()

g.V().hasLabel('person').count()

g.V().hasLabel('person').outE('created').count().map {it.get() * 10}.path()

Choose

  • The choose()-step (branch) routes the current traverser to a particular traversal branch option. With choose(), it is possible to implement if/else-based semantics as well as more complicated selections.
g.V().hasLabel('person').
               choose(values('age').is(lte(30)),
                 __.in(),
                 __.out()).values('name')
                 
g.V().hasLabel('person').
               choose(values('age')).
                 option(27, __.in()).
                 option(32, __.out()).values('name')
                 
g.V().hasLabel('person').
               choose(values('name')).
                 option('marko', values('age')).
                 option('josh', values('name')).
                 option('vadas', valueMap()).
                 option('peter', label())

Coin

  • To randomly filter out a traverser, use the coin()-step (filter). The provided double argument biases the "coin toss."
g.V().coin(0.5)

Constant

  • To specify a constant value for a traverser, use the constant()-step (map)
g.V().choose(hasLabel('person'),
             values('name'),
             constant('inhuman'))

CyclicPath

  • The step analyzes the path of the traverser thus far and if there are any repeats, the traverser is filtered out over the traversal computation.
g.V(1).both().both().cyclicPath()

Dedup

g.V().values('lang').dedup()

Drop

  • The drop()-step (filter/sideEffect) is used to remove element and properties from the graph (i.e. remove).
g.V().outE().drop()

g.V().properties('name').drop()

Explain

g.V().hasLabel('person').outE().identity().inV().count().is(gt(5)).explain()

Fold

  • There are situations when the traversal stream needs a "barrier" to aggregate all the objects and emit a computation that is a function of the aggregate. The fold()-step (map) is one particular instance of this.
g.V(1).out('knows').values('name').fold()

g.V(1).out('knows').values('name').fold().next().getClass()

Group

  • The group()-step (map/sideEffect) is a sideEffect that organizes the objects according to some function of the object.
g.V().group().by(label).by('name')

g.V().group().by(label).by(count())

GroupCount

  • When it is important to know how many times a particular object has been at a particular part of a traversal, groupCount()-step (map/sideEffect) is used.
g.V().hasLabel('person').values('age').groupCount()

Inject

  • makes it possible to insert objects arbitrarily into a traversal stream
g.V(4).out().values('name').inject('daniel')

g.V(4).out().values('name').inject('daniel').map {it.get().length()}

Limit

  • The limit()-step is analogous to range()-step save that the lower end range is set to 0.
g.V().limit(2).toString()

Match

.V().match(
                 __.as('a').out('created').as('b'),
                 __.as('b').has('name', 'lop'),
                 __.as('b').in('created').as('c'),
                 __.as('c').has('age', 29)).
               select('a','c').by('name')

g.V().match(
                 __.as('a').out('created').has('name', 'lop').as('b'),
                 __.as('b').in('created').has('age', 29).as('c')).
               select('a','c').by('name')

g.V().match(
                 __.as('a').out('created').as('b'),
                 __.as('b').in('created').as('c')).
                 where('a', neq('c')).
               select('a','c').by('name')

Max, Min, Mean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment