User Tools

Site Tools


technology:databases:neo4j:experiment_3

Experiment #3

Now that I know neo4j can handle my needs as a developer, I'm looking for a higher-level abstraction so I don't have to deal with every little persistence detail in code.

Enter jo4neo - a persistence annotation layer that runs on top of POJOs or any other domain object. Time to test that out.

I created a few really simple domain objects: Artists, Albums, and Songs. Here are the simplified versions of those classes:

 1: /*..package and import statements here..*/
 2: public class Artist {
 3:     private transient Nodeid neoid;
 4:     @neo(index=true) private String id;
 5:     @neo (index=true) private String name;
 6:     @neo("album") private Collection<Album> albums;
 7:     /*..getters and setters here*/
 8: }
 9:
10: /*..package and import statements here..*/
11: public class Album {
12:     private transient Nodeid neoid;
13:     @neo(index = true)
14:     private String id;
15:     @neo(index = true)
16:     private String name;
17:     @neo("song")
18:     private Collection<Song> songs;
19:     /*..getters and setters here*/
20: }
21:
22: /*..package and import statements here..*/
23: public class Song {
24:     private transient Nodeid neoid;
25:     @neo(index = true)
26:     private String id;
27:     @neo(index = true)
28:     private String name;
29:     /*..getters and setters here*/
30: }

Notice how simple the annotations are. The only real intrusion on my domain object is the presence of the nodeid variable. The annotations determine whether fields are indexed, what their names are in the database, etc. Very simple.

So how about a little test? 30 artists, 4 albums each, 11 songs each.

 1: /* Jo4NeoPerformanceExercise
 2:  * Chris Freyer
 3:  * Chris@TheFreyers.net
 4:  * 7/23/2010
 5:  */
 6: package com.freyertech.jo4neotest.model;
 7:
 8: import java.io.BufferedReader;
 9: import java.io.IOException;
10: import java.io.InputStreamReader;
11: import java.util.Calendar;
12: import jo4neo.ObjectGraph;
13: import jo4neo.ObjectGraphFactory;
14: import org.neo4j.graphdb.GraphDatabaseService;
15: import org.neo4j.kernel.EmbeddedGraphDatabase;
16:
17:
18: public class Jo4NeoPerformanceExercise {
19:
20:     private static int NUM_ARTISTS = 30;
21:     private static int NUM_ALBUMS = 4;
22:     private static int NUM_SONGS = 11;
23:
24:     public static void main(String[] args) {
25:         long starttime = Calendar.getInstance().getTimeInMillis();
26:         GraphDatabaseService neo = new EmbeddedGraphDatabase("neo_store2");
27:         ObjectGraph graph = ObjectGraphFactory.instance().get(neo);
28:         try {
29:             setup(graph);
30:             queryDB(graph);
31:             cleanDB(graph);
32:         } catch (Exception e) {
33:             System.out.println("Exception:  " + e.getMessage());
34:             e.printStackTrace();
35:         } finally {
36:             graph.close();
37:             neo.shutdown();
38:         }
39:         long endtime = Calendar.getInstance().getTimeInMillis();
40:         output("Runtime:  " + (endtime - starttime) + "ms.");
41:     }
42:
43:     private static void setup(ObjectGraph graph) {
44:         output("Setting up test data...");
45:         //add new artists in loop
46:         for (int i = 0; i < NUM_ARTISTS; i++) {
47:             Artist art = new Artist();
48:             art.setName("Artist #" + i);
49:             art.setId("" + i);
50:             //add new albums for each artist
51:             for (int j = 0; j < NUM_ALBUMS; j++) {
52:                 Album al = new Album();
53:                 al.setId("" + i + "_" + j);
54:                 al.setName("Album #" + j);
55:                 art.getAlbums().add(al);
56:                 //add new songs for each album;
57:                 for (int k = 0; k < NUM_SONGS; k++) {
58:                     Song s = new Song();
59:                     s.setId("" + i + "_" + j + "_" + k);
60:                     s.setName("Song #" + k);
61:                     al.getSongs().add(s);
62:                 }
63:             }
64:             graph.persist(art);
65:         }
66:         output("done");
67:     }
68:
69:     private static void output(String s) {
70:         System.out.println(s + "\n");
71:     }
72:
73:     private static void cleanDB(ObjectGraph graph) {
74:         output("Deleting records...");
75:         for (Artist art : graph.get(Artist.class)) {
76:             graph.delete(art);
77:         }
78:         output("done.");
79:     }
80:
81:     private static void queryDB(ObjectGraph graph) {
82:         output("Walking through objects...");
83:         String format = "|%1$-12s|%2$-12s|%3$-12s|\n";
84:         for (Artist artist : graph.get(Artist.class)) {
85:             for (Album album : artist.getAlbums()) {
86:                 for (Song song : album.getSongs()) {
87:                     System.out.format(format, artist.getName(), album.getName(),song.getName());
88:                 }
89:             }
90:         }
91:     }
92: }

And here's the output:

run:
Setting up test data...

done

Walking through objects...

|Artist #29  |Album #0    |Song #0     |
|Artist #29  |Album #0    |Song #1     |
|Artist #29  |Album #0    |Song #2     |
|Artist #29  |Album #0    |Song #3     |
   (...lots of output deleted here for brevity...)
|Artist #0   |Album #3    |Song #9     |
|Artist #0   |Album #3    |Song #10    |
Deleting records...

done.

Runtime:  56672ms.

Summary

I was able to startup the database; create, query, and delete 1,320 business objects; and shutdown the database in just under a minute. Nice! But the important thing isn't just the running time. There's a decrease in code complexity as well.

The business objects I created are nothing more than POJOs with annotations. The jo4neo toolkit allows me to stay in the business domain and not be concerned about the database. As my business domain expands, this could have larger implications if I'm able to keep the complexity down. Less complexity = less time = lower cost of development.

Points of Interest

A couple of nice features:

  • you don't see ANY persistence code, except for the occasional graph.delete() or graph.persist() methods when using jo4neo.
  • the only time I leave POJO mode is when I do a direct search for objects by type (i.e. graph.get(Artist.class) )
  • Neo4J returns Java Collections, which lets me use Java 6 style loops (i.e. for (Artist artist : graph.get(Artist.class)))
  • The entire example (without domain objects) was 113 lines of code

So far I've spent ZERO time creating a database, and far less time than normal on persistence mapping. That leaves far more time than normal for domain object modelling. But I haven't needed (yet).

/home/cfreyer/public_html/data/pages/technology/databases/neo4j/experiment_3.txt · Last modified: 2010/08/03 12:04 by Chris Freyer