This is a straightforward and rather naïve implementation of a Blueprints-enabled graph over Redis.
Blueprints is a database-agnostic library for handling graphs. Blueredis allows you to run Blueprints on top of redis. Also see Pipes and Gremlin to see what this integration will also allow you to do.
Requires Redis 2.x compatible version of JRedis (git clone this repo, and you'll get a jar)
Graph db = new RedisGraph();
String password = "pass";
Graph db1 = new RedisGraph(password);
String host = "127.0.0.1";
int port = 6379;
Graph db2 = new RedisGraph(host, port);
int database = 10;
Graph db3 = new RedisGraph(host, port, pass, database);
After this you work with the database as with any Blueprints-enabled graph.
RedisGraph handles id creation for you. Any id parameter passed to addVertex/addEdge will be ignored. All ids are of type long
By default all properties are saved and retrieved as strings, regardless of the type of object you pass to addVertex
. If you want to save actual objects/values, call graph.serializeProperties(true)
. Note though, that it uses Base64 encoding on top of Java serialization so it may take up a lot of space.
Currently transactions are not supported
Blueredis implements an index for both vertex and edge properties. This implementation is based on A fast, fuzzy, full-text index using Redis.
By default indexing is on. To turn it off, call graph.setIndexing(false)
. To turn it back on, call graph.setIndexing(true)
.
You may wish to implement your own indexing service. To do this:
- implement Blueprints'
Index
interface - make sure that your service's constructor accepts an instance of
RedisGraph
- call
graph.setIndexing(true, serviceInstance)
and pass an instance of your service to it
Each vertex is represented by the following keys:
vertex:ID
, vertex id. This one is used to test if a vertex existsvertex:ID:properties
, a hash of all vertex propertiesvertex:ID:edges:in
, a set of all incoming edgesvertex:ID:edges:out
, a set of all outgoing edges
Each edge is represented by the following keys:
edge:ID
, edge id. This one is used to test if an edge existsedge:ID:label
, a string containing the edge's labeledge:ID:in
, index of "in" vertexedge:ID:out
, index of "out" vertexedge:ID:properties
, a hash of all edge properties
This are just some keys that hold values necessary for Blueredis to work:
globals:next_vertex_id
, a counter that's incremented each time a new vertex is addedglobals:next_edge_id
, a counter that's incremented each time a new edge is addedglobals:vertices
, a sorted list of all vertex idsglobals:edges
, a sorted list of all edge ids
RedisGraph now passes tinkerpop's tests (these are included with the source).
In order to implement getVertices()
and getEdges
RedisGraph stores an ordered set of vertices and edges in global:edges
and global:vertices
. Because of this calling getVertices()
or getEdges
on a large set of either of these may be quite slow. This is the reason this implementation is both straightforward an naïve :)
Right now Blueredis does up to 1000 addVertex
calls per second on a 3.5 year-old MacBook Pro. Don't take these numbers seriously and do your own performance tests to see if it will suit your needs. At least not until a proper benchmark can be put into place.