Social networks (know as social graphs out on the web) are natural to model with a graph. This example shows a very simple social model that connects friends and keeps track of status updates.
The data model for a social network is pretty simple: Persons with names and StatusUpdates with timestamped text. These entities are then connected by specific relationships.
The StatusUpdate list for a Person is a linked list. The head of the list (the most recent status) is found by following status. Each subsequent StatusUpdate is connected by next.
Here's an example where Andreas Kollegger micro-blogged his way to work in the morning:
TraversalDescription traversal = Traversal.description(). depthFirst(). relationships( NEXT ). filter( Traversal.returnAll() );
This gives us a traverser that will start at one StatusUpdate, and will follow the chain of updates until they run out. Traversers are lazy loading, so it's performant even when dealing with thousands of statuses - they are not loaded until we actually consume them.
Once we have friends, and they have status messages, we might want to read our friends status' messages, in reverse time order - latest first. To do this, we go through these steps:
Animated, the sequence looks like this.
The code looks like:
PositionedIterator<StatusUpdate> first = statuses.get(0); StatusUpdate returnVal = first.current(); if ( !first.hasNext() ) { statuses.remove( 0 ); } else { first.next(); sort(); } return returnVal;