Databases Reference
In-Depth Information
how the traversal is to proceed next. The following code snippet shows the Traversal
API in action:
Traversal . description ()
. relationships ( DoctorWhoRelationships . PLAYED , Direction . INCOMING )
. breadthFirst ()
. evaluator ( new Evaluator ()
{
public Evaluation evaluate ( Path path )
{
if ( path . endNode (). hasRelationship (
DoctorWhoRelationships . REGENERATED_TO ) )
{
return Evaluation . INCLUDE_AND_CONTINUE ;
}
else
{
return Evaluation . EXCLUDE_AND_CONTINUE ;
}
}
} );
With this snippet it's plain to see the predominantly declarative nature of the Traversal
API. The relationships() method declares that only PLAYED relationships in the IN
COMING direction may be traversed; thereafter, we declare that the traversal should be
executed in a breadthFirst() manner, meaning it will visit all nearest neighbors before
going further outward.
The Traversal API is declarative for graph structure; for our implementation of the
Evaluator , however, we drop down to the imperative Core API. Within the evaluator
we use the Core API to determine, given the path to the current node, whether or not
further hops through the graph are necessary (we can also use the Core API to modify
the graph from inside an evaluator). Again, the native graph structures inside the da‐
tabase bubble close to the surface here, with the graph primitives of nodes, relationships,
and properties taking center stage in the API.
Core API, Traversal Framework, or Cypher?
Given these several different methods for querying a graph, which should we choose?
The Core API allows developers to fine-tune their queries so that they exhibit high
affinity with the underlying graph. A well-written Core API query is often faster than
any other approach. The downside is that such queries can be verbose, requiring con‐
siderable developer effort. Moreover, their high affinity with the underlying graph makes
them tightly coupled to its structure: when the graph structure changes, they can often
break. Cypher can be more tolerant of structural changes—things such as variable-
length paths help mitigate variation and change.
 
Search WWH ::




Custom Search