Jo4neo’s “get most recent” feature

January 10th, 2010 § 2

“show the latest ….” is a common prefix to user stories these days.  Others have noted the same and given this symptom a moniker; ”The real time web“.  Typically we just throw things into a table with an indexed timestamp column and query accordingly.

In jo4neo, finding the most recent additions requires two simple steps:

  1. annotation your type with @neo(recency=true)
  2. use the ObjectGraph.getMostRecent() method to retrieve the latest inserts.

In this small example, we’re indicating to jo4neo that we’d like it to remember our “Post” inserts in most recent order.

Post.java

@neo(recency=true)
public class Post extends NeoBean
 {
...

Let’s step back and pretend we’ve been given a quick assignment in our C programming class.  We’ve been asked to allow the user to enter strings, and at any given point, allow the user to ask for the latest “n” inputs given.  Basically we want the last thing in to be the first thing out…LIFO.  That reminds us of the stack we just learned about, and after an hour of happy coding, we have a solution.

neo4j is a natural at storing stacks, queues, or any structure that can be composed from simple linked lists.  One simple technique for remembering new nodes in LIFO ordering is to build a linked list, inserting from the head.  Here’s the jo4neo that performs this head insertion:

 private void recencyStack(Node newNode, Node metanode) {
    Node latest=null;
    for(Relationship r : metanode.getRelationships(
       JO4NEO_NEXT_MOST_RECENT, Direction.OUTGOING)) {
       latest = r.getEndNode();
       r.delete();
    }
    if (latest!=null)
       newNode.createRelationshipTo(latest,
          JO4NEO_NEXT_MOST_RECENT);

    metanode.createRelationshipTo(newNode,
       JO4NEO_NEXT_MOST_RECENT);
 }

The HubAction.java class demonstrates how easy it becomes to query for the most recent additions of a particular class.  In this case we’re getting 5 blog posts to show on the “hub” or front page.

Tagged: , , ,

§ 2 Responses to “Jo4neo’s “get most recent” feature”

  • Jeppe Cramon says:

    Very cool – jo4neo is shaping up very nice.
    I was wondering – have you considered implementing something like a PersistenceContext from Hibernate, where you within a transaction is guaranteed to get the same instance back every time you query/navigate to the an instance already loaded in the graph?

    Like if I fetch a list of objects under my root and later, in the same transaction, query for the same objects using find() – I would assume performance would be better (?) if jo4neo cached the beans, so identical object were the same instance (so == would work on them) ?

    /Jeppe

  • admin says:

    Jeppe,

    I have thought about this…it’s the end goal, and would be the result of implementing JPA, which requires this behavior, but it does raise the bar a tad. once jo4neo is 1.0, dependable and well tested we’ll take that step.

    Taylor

  • § Leave a Reply

What's this?

You are currently reading Jo4neo’s “get most recent” feature at The Web Semantic.

meta