Operations on a Graph Database (Part 7 – Sets)
Graph Database Tutorial
Part 7: Sets
Sets are a core concept of most databases. For example, any SQL SELECT statement in a relational database produces a set. Sets apply to Graph Databases just as well and are just as useful:
The most frequently encountered set of nodes in a Graph Database is the result of a traversal. For example, in InfoGrid, all traversal operations result in a set like this:
MeshObject startNode = ...; // some start node MeshObjectSet neighborNodes = startNode.traverseToNeighbors();
We might as well have returned an array, or an Iterator over the members of the set, were it not for the fact that there are well-understood set operations that often make our jobs as developers much simpler: like set unification, intersection and so forth.
For example, in a social bookmarking application we might want to find out which sites both you and I have bookmarked. Code might look like this:
MeshObject me = ...; // node representing me
MeshObject you = ...; // node representing you
TraversalSpecification ME_TO_BOOKMARKS_SPEC = ...;
// how to get from a person to their bookmarks, see post on traversals
MeshObjectSet myBookmarks = me.traverse( ME_TO_BOOKMARKS_SPEC );
MeshObjectSet yourBookmarks = you.traverse( ME_TO_BOOKMARKS_SPEC );
// Bookmarks that you and I share
MeshObjectSet sharedBookmarks = myBookmarks.intersect( yourBookmarks );
Notice how simple this code is to understand? One of the powers of sets. Or, if you know what a “minus” operation is on a set, this is immediately obvious:
// Bookmarks unique to me MeshObjectSet myUniqueBookmarks = myBookmarks.minus( yourBookmarks );
This is clearly much simpler than writing imperative code which would have lots of loops and if/then/else’s and comparisons and perhaps indexes in it. (And seeing this might put some concerns to rest that NoSQL databases are primitive because they don’t have a SQL-like query language. I’d argue it’s less the language but the power of sets, and if you have sets you have a lot of power at your fingertips.)
To check out sets in InfoGrid, try package org.infogrid.mesh.set. Clearly much more can be done than we have so far in InfoGrid, but it’s a very useful start in our experience.

Leave a comment: