<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; graph db</title>
	<atom:link href="http://blog.jayway.com/tag/graph-db/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Neo – a netbase</title>
		<link>http://blog.jayway.com/2007/02/01/neo-%e2%80%93-a-netbase/</link>
		<comments>http://blog.jayway.com/2007/02/01/neo-%e2%80%93-a-netbase/#comments</comments>
		<pubDate>Thu, 01 Feb 2007 10:32:01 +0000</pubDate>
		<dc:creator>Björn Granvik</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[graph db]]></category>
		<category><![CDATA[innovation]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=3617</guid>
		<description><![CDATA[Neo is a network-oriented database for semi-structured information. Too complicated, let us try again. Neo handles data in networks – nodes, relationships and properties – instead of tables. This means entirely new solutions for data that is difﬁ cult to handle in static tables. It could mean we can go agile all the way into [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Neo is a network-oriented database for semi-structured information.<br />
Too complicated, let us try again. Neo handles data in networks<br />
– nodes, relationships and properties – instead of tables. This means entirely new solutions for data that is difﬁ cult to handle in static tables. It could mean we can go agile all the way into the persistence layer. </strong></p>
<p>The relational database represents one of the most important developments in the<br />
history of computer science. Upon its arrival some 30 years ago, it revolutionized the<br />
way the industry views data management and today it is practically ubiquitous. </p>
<p>In fact, it is so taken for granted that we, as an industry, have stopped thinking. Could<br />
there be a better way to represent and store our data? In some cases the answer is<br />
– yes, absolutely. The relational database is showing its age. Some telltale signs: </p>
<ul>
<li> The mismatch between relational data and object oriented programming.
<li> Schema evolution – updating your data model when the domain model changes<br />
– is just so much manual labor.
<li> Semi-structured or network-oriented data is difﬁ cult to handle.
</ul>
<h2>The Neo Database</h2>
<p>Neo is a database that is built with a different philosophy. Where the relational da-<br />
tabase squeezes all data into static tables, Neo uses a ﬂ exible and dynamic network<br />
model. This model allows data to evolve more naturally as business requirements<br />
change. There’s no need for “alter table...” on your production databases after you<br />
introduce a new version of the business layer and no need to rewire and migrate<br />
your O/R mapping conﬁ gurations. The network will evolve along with your busi-<br />
ness logic. This spells agility.<br />
Neo is an embedded persistence engine, which means it’s a small, lightweight and<br />
non-intrusive Java library that is easy to include in your development environment.<br />
It has been designed for performance and scalability and has been proven to handle<br />
large networks of data (100+ millions of nodes, relationships and properties).<br />
Neo is a newly founded open source project, but the software is robust. It has<br />
been in commercial production in a highly demanding 24/7 environment for al-<br />
most four years and has full support for enterprise-grade features such as distributed<br />
ACID transactions, conﬁ gurable isolation levels and full transaction recovery.<br />
But so much for sweet talk, let’s cut to some code! </p>
<h2>Model and Code</h2>
<h3>Representation</h3>
<p>In the Neo model, everything is represented by nodes, relationships and properties.<br />
A relationship connects two nodes and has a well-deﬁ ned, mandatory type. Prop-<br />
erties are key-value pairs that are attached to both nodes and relationships. When<br />
you combine nodes, relationships between them and properties on both nodes and<br />
relationships they form a node space – a coherent network representing your busi-<br />
ness domain data.<br />
This may sound fancy, but it’s all very intuitive. Here is how a simple social network<br />
might be modeled: </p>
<p><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2009/12/Picture-89.png" alt="Figure 1" title="Figure 1" width="412" height="211" class="alignnone size-full wp-image-3618" /></p>
<p><strong>Figure 1:</strong> An example of a social network from a somewhat famous movie. Note the different type on the relation between Agent Smith and his creator The Architect. </p>
<p>Note how all nodes have integer identiﬁ ers and how all relationships have a type<br />
(KNOWS or CODED_BY). In this example, all nodes have a “name” property. But<br />
some nodes have other properties, for example, an “age” property (node 1) or a<br />
“last name” property (node 3). There’s no overall schema that forces all nodes to<br />
look the same. This allows Neo to capture so-called semi-structured information:<br />
information that has a small amount of mandatory attributes but many optional at-<br />
tributes. Furthermore, the relationships have properties as well. In this example, all<br />
relationships have an “age” property to describe how long two people have known<br />
each other and some relationships have a “disclosure” property to describe whether<br />
the acquaintance is secret. </p>
<p>Working with nodes and relationships is easy. The basic operations are as follows: </p>
<p><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2009/12/Picture-90.png" alt="Figure 2" title="Figure 2" width="387" height="114" class="alignnone size-full wp-image-3619" /></p>
<p>This is an intuitive representation of a network and probably similar to many other<br />
implementations that want to represent a network of data in an object-oriented<br />
language.<br />
It’s worth noting, however, that relationships in this model are full-blown objects<br />
and not just implicit associations between nodes. If you have another look at the<br />
social network example, you’ll see that there’s more information in the relationships<br />
between nodes than in the nodes themselves. The value of a network is in the con-<br />
nections between the nodes and Neo’s model captures that. </p>
<h3>Creating a Node Space</h3>
<p>And now, ﬁnally some code. Here’s how we would create the Matrix social network<br />
from ﬁgure 1: </p>
<pre>Transaction tx = Transaction.begin();
EmbeddedNeo neo = ... // Get factory
// Create Thomas ’Neo’ Anderson
Node mrAnderson = neo.createNode();
mrAnderson.setProperty( ”name”, ”Thomas Anderson” );
mrAnderson.setProperty( ”age”, 29 );
// Create Morpheus
Node morpheus = neo.createNode();
morpheus.setProperty( ”name”, ”Morpheus” );
morpheus.setProperty( ”rank”, ”Captain” );
morpheus.setProperty( ”occupation”, ”Total bad ass” );
// Create a relationship representing that they know each other
mrAnderson.createRelationshipTo( morpheus,
   MatrixRelationshipTypes.KNOWS );
// Create Trinity, Cypher, Agent Smith, Architect similarly
...
tx.commit();
</pre>
<p>As you can see in the code above: It is rather easy to construct the node space for our<br />
Matrix example. And, of course, our network is made persistent once we commit. </p>
<h3>Traversing a Node Space</h3>
<p>Now that we know how to represent our domain model in the node space, how do<br />
we get information out of it? Unlike a relational database, Neo does not support a<br />
declarative query language. Instead, Neo provides an object-oriented <em>traverser frame-<br />
work</em> that allows us to express complex queries in plain Java.<br />
Working with the traverser framework is very straight-forward. The core abstrac-<br />
tion is, unsurprisingly, the Traverser interface. A Traverser is a Java <code>Iterable</code> that<br />
encapsulates a “query” – i.e. a traversal on the node space such as <em>“give me all Mor-<br />
pheus’ friends and his friends’ friends”</em> or <em>“does Trinity know someone who is acquainted with an agent?”</em>. The most complex part of working with a Traverser is instantiating it. Here’s an example of how we would create a Traverser that will return all the<br />
(transitive) friends of the “Thomas Anderson” node of the example above: </p>
<pre>// Instantiate a traverser that returns all mrAnderson’s friends
Traverser friendsTraverser = mrAnderson.traverse(
    Traverser.Order.BREADTH_FIRST,
    StopEvaluator.END_OF_NETWORK,
    ReturnableEvaluator.ALL_BUT_START_NODE,
    MatrixRelationshipTypes.KNOWS,
    Direction.OUTGOING );</pre>
<p>Here we can see that traversers are created by invoking the <code>traverse(...)</code> method<br />
on a start node with a number of parameters. The parameters control the traver-<br />
sal and in this example they tell Neo to traverse the network breadth-ﬁrst (rather<br />
than depth-ﬁ rst), to traverse until it has covered all reachable nodes in the network<br />
<code>(StopEvaluator.END_OF_NETWORK)</code>, to return all nodes except the ﬁ rst <code>(Returna-<br />
bleEvaluator.ALL_BUT_START_NODE)</code>, , and to traverse all <strong>OUTGOING</strong> relation-<br />
ships of type <strong>KNOWS</strong>.<br />
How would we go about if we wanted to list the output of this traversal? After<br />
we’ve created a Traverser, working with it is as easy as working with any Java <code>Iter-<br />
able:</code></p>
<pre>// Traverse the node space and print out the result
for ( Node friend : friendsTraverser )
{
    System.out.println( friend.getProperty( “name” ) + “ at depth “ +
        friendsTraverser.currentPosition().getDepth() );
}
</pre>
<p>Running the traversal above on the Matrix example would yield the following out-<br />
put:</p>
<pre>$ bin/run-neo-example
Morpheus at depth 1
Trinity at depth 1
Cypher at depth 2
Agent Smith at depth 3
$
</pre>
<p>As you can see, the Traverser has started at the “Thomas Anderson” node and run<br />
through the entire network along the <strong>KNOWS</strong> relationship type, breadth ﬁ rst, and<br />
returned all nodes except the ﬁ rst one. “The Architect” is missing from this output<br />
since the relationship connecting him is of a different type, <strong>CODED_BY</strong>. This is a<br />
small, contrived example. But the code would work equally well on a network with<br />
hundreds of millions of nodes, relationships and properties.<br />
Now, let’s look at a more complex traversal. Going with our example, suppose<br />
that we wanted to ﬁ nd all “hackers of the Matrix,” where we deﬁ ne a hacker of the<br />
Matrix as any node that you reach through a <strong>CODED_BY</strong> relationship. How would<br />
we create a Traverser that gives us those nodes?<br />
First off, we want to traverse both our relationship types (<strong>KNOWS</strong> and <strong>COD-<br />
ED_BY</strong>). Secondly, we want to traverse until the end of the network and lastly, we<br />
want to return only nodes which we came to through a <strong>CODED_BY</strong> relationship.<br />
Here’s the code: </p>
<pre>
// Instantiate a traverser that returns all hackers of the Matrix
Traverser hackerTraverser = mrAnderson.traverse(
    Traverser.Order.BREADTH_FIRST,
    StopEvaluator.END_OF_NETWORK,
    new ReturnableEvaluator()
    {
        public boolean isReturnableNode( TraversalPosition pos )
        {
            <strong>return pos.getLastRelationshipTraversed().
                isType( MatrixRelationshipTypes.CODED_BY );</strong>
        }
 },
 MatrixRelationshipTypes.CODED_BY,
 Direction.OUTGOING,
 MatrixRelationshipTypes.KNOWS,
 Direction.OUTGOING ); </pre>
<p>Now it’s getting interesting! The <code>ReturnableEvaluator.ALL_BUT_START_NODE</code> con-<br />
stant from the previous example was actually a convenience implementation of the<br />
<code>ReturnableEvaluator</code> interface. This interface contains a single method and you<br />
can supply a custom implementation of it to the traverser framework. It turns out<br />
that this is a simple but powerful way to express complex queries.<br />
Setting aside the anonymous inner class cruft surrounding the code in bold, we<br />
basically pass in a snippet of code that checks whether we traversed a relationship of<br />
type <strong>CODED_BY</strong> to get to the current node. If this statement is evaluated to “true”<br />
then the current node will be included in the set of nodes that is returned from the<br />
traverser.<br />
When executed with a simple print loop, the above code prints the following: </p>
<pre>$ bin/run-neo-example
The Architect
$
</pre>
<p>StopEvaluators work the same way. In our experience, writing custom evaluators<br />
is very easy. Even the most advanced applications we have developed with Neo<br />
–  applications that traverse extremely large and complex networks – are based on<br />
evaluators that are rarely more than a few lines of code. </p>
<h2>Conclusion</h2>
<p>Neo is not a silver bullet and some areas needs to improve, for instance tools, stand-<br />
ardizing the model and a query language.<br />
However, if your data is naturally ordered in a network or is semi-structured or you<br />
just need to go truly agile, give the Neo database a run for your money. We hope<br />
you ﬁnd it, as we do, to be an elegant and ﬂ exible alternative that is both robust and<br />
fast. </p>
<p>Emil Eifrém, Neo Technology<br />
Björn Granvik, Jayway</p>
<h2>Links </h2>
<p>Neo speciﬁcation<br />
<a href="www.neo4j.org">www.neo4j.org</a></p>
<p><em>Originally published in <a href="http://jayway.se/jayview">JayView</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2007/02/01/neo-%e2%80%93-a-netbase/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

