Pacer provides a graph object that abstracts the underlying graph database.
Let's see the common methods you will use on such a graph object,
We can get vertices and edges from the graph using the following methods:
Get the vertex with the specified id (or
nil if there is no such vertex).
Get the edge with the specified id (or
nil if there is no such edge).
Get all vertices.
Get all edges.
e methods return a route object.
Routes are a main concept in Pacer, and we will look at them in detail later on.
For now, all you need to know is that a route is a collection of items.
Create and return a vertex object.
properties (hash) argument is optional.
Note: In most graph databases, you must call
create_vertex inside a transaction.
Edges are created by calling the
add_edges_to method on a vertex object.
Most graph databases support transactions (the in-memory TinkerGraph is the exception).
In fact, they require any operation that changes the graph to be wrapped in a transaction .
You can do that by passing a block to the
# Code that changes the graph goes here ...
Committing & rolling back
For full flexibility, this method passes a
rollback objects to the block.
You can commit/rollback the transaction by calling the
call method on the appropriate object:
g.transaction do |commit_obj, rollback_obj|
# Make some changes to the graph ...
# If you want to save the changes
# If something went wrong
In most cases, we traverse through the graph as follows:
1. Search for a starting point (e.g. a vertex).
2. Start traversing by following edges.
In order for the first step (search) to be fast, graph databases (and databases in general) use indices.
For example, if your application frequently searches for vertices based on their
email property, you may want to create an index for this property.
Once an index is created, Pacer makes things easy by auto-selecting the best available index.
Indices can be created using
create_key_index for either vertices or edges. By default, all indices are at the scope of the entire graph, though there may be the possibility
of using other strategies in vendor-specific code. Indices will be built when created, so this call can take a significant amount of time on a large
g.create_key_index(:property_name) By default, vertex indices are created.
g.create_key_index(:property_name, :vertex, opts_hash) Explicitly creating vertex index with implementation-specific options passed through
g.create_key_index(:property_name, :edge) Create an edge index