Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
upgrade to Neoj4 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Neubauer committed Mar 24, 2010
1 parent 10877ae commit 8aa8c26
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 97 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Expand Up @@ -72,20 +72,20 @@
<!-- Neo4j graph database --> <!-- Neo4j graph database -->
<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>neo</artifactId> <artifactId>neo4j-kernel</artifactId>
<version>1.0-b10</version> <version>1.0</version>
</dependency> </dependency>


<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>shell</artifactId> <artifactId>neo4j-shell</artifactId>
<version>1.0-b10</version> <version>1.0</version>
</dependency> </dependency>


<dependency> <dependency>
<groupId>org.neo4j</groupId> <groupId>org.neo4j</groupId>
<artifactId>neo-utils</artifactId> <artifactId>neo4j-utils</artifactId>
<version>1.0-b10-SNAPSHOT</version> <version>1.0</version>
</dependency> </dependency>


<!-- General utilities --> <!-- General utilities -->
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/eptcomputing/neo4j/version/NodeImplBase.java
Expand Up @@ -4,15 +4,15 @@
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;


import org.neo4j.api.core.Direction; import org.neo4j.graphdb.Direction;
import org.neo4j.api.core.Node; import org.neo4j.graphdb.Node;
import org.neo4j.api.core.NotFoundException; import org.neo4j.graphdb.NotFoundException;
import org.neo4j.api.core.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.api.core.RelationshipType; import org.neo4j.graphdb.RelationshipType;
import org.neo4j.api.core.ReturnableEvaluator; import org.neo4j.graphdb.ReturnableEvaluator;
import org.neo4j.api.core.StopEvaluator; import org.neo4j.graphdb.StopEvaluator;
import org.neo4j.api.core.Traverser; import org.neo4j.graphdb.Traverser;
import org.neo4j.api.core.Traverser.Order; import org.neo4j.graphdb.Traverser.Order;




/** /**
Expand Down
@@ -1,7 +1,8 @@

package com.eptcomputing.neo4j.version; package com.eptcomputing.neo4j.version;


import org.neo4j.api.core.NotFoundException; import org.neo4j.graphdb.NotFoundException;
import org.neo4j.api.core.PropertyContainer; import org.neo4j.graphdb.PropertyContainer;


abstract class PrimitiveBase implements PropertyContainer { abstract class PrimitiveBase implements PropertyContainer {


Expand Down
@@ -1,8 +1,8 @@
package com.eptcomputing.neo4j.version; package com.eptcomputing.neo4j.version;


import org.neo4j.api.core.Node; import org.neo4j.graphdb.Node;
import org.neo4j.api.core.Relationship; import org.neo4j.graphdb.Relationship;
import org.neo4j.api.core.RelationshipType; import org.neo4j.graphdb.RelationshipType;




/** /**
Expand Down
@@ -1,6 +1,6 @@
package com.eptcomputing.neo4j package com.eptcomputing.neo4j


import org.neo4j.api.core._ import org.neo4j.graphdb._


/** /**
* Extend your class with this trait to get really neat new notation for creating * Extend your class with this trait to get really neat new notation for creating
Expand Down
15 changes: 8 additions & 7 deletions src/main/scala/com/eptcomputing/neo4j/Neo4jServer.scala
Expand Up @@ -5,15 +5,16 @@ import java.util.{TreeMap, Properties}
import java.util.logging.Logger import java.util.logging.Logger
import javax.servlet.ServletContext import javax.servlet.ServletContext
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.neo4j.api.core.{EmbeddedNeo, NeoService} import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.EmbeddedGraphDatabase


/** /**
* Wrapper around a singleton instance of Neo4j embedded server. * Wrapper around a singleton instance of Neo4j embedded server.
*/ */
object NeoServer { object Neo4jServer {


private val log = Logger.getLogger(this.getClass.getName) private val log = Logger.getLogger(this.getClass.getName)
private var neo: NeoService = null private var neo: GraphDatabaseService = null


/** /**
* Initialize Neo4j with configuration stored in a properties file specified via a * Initialize Neo4j with configuration stored in a properties file specified via a
Expand Down Expand Up @@ -57,7 +58,7 @@ object NeoServer {
case e: IOException => log.warning("Cannot read Neo4j configuration: " + e) case e: IOException => log.warning("Cannot read Neo4j configuration: " + e)
} }
val environment = System.getProperty("neo4j.env", "development") val environment = System.getProperty("neo4j.env", "development")
NeoServer.startup(neoConfig, environment) Neo4jServer.startup(neoConfig, environment)
} }


/** /**
Expand All @@ -82,7 +83,7 @@ object NeoServer {
try { FileUtils.deleteDirectory(new File(neoPath)) } catch { case _: IOException => } try { FileUtils.deleteDirectory(new File(neoPath)) } catch { case _: IOException => }
} }


neo = new EmbeddedNeo(neoPath) neo = new EmbeddedGraphDatabase(neoPath)


// Setup shell if required // Setup shell if required
if (isTrue(prop("shell.enabled", "false"))) { if (isTrue(prop("shell.enabled", "false"))) {
Expand All @@ -100,7 +101,7 @@ object NeoServer {
// Register a shutdown hook to ensure Neo4j is cleanly shut down before the JVM exits // Register a shutdown hook to ensure Neo4j is cleanly shut down before the JVM exits
Runtime.getRuntime.addShutdownHook(new Thread() { Runtime.getRuntime.addShutdownHook(new Thread() {
override def run() { override def run() {
NeoServer.shutdown Neo4jServer.shutdown
} }
}) })
} }
Expand All @@ -121,7 +122,7 @@ object NeoServer {
* Execute instructions within a Neo4j transaction; rollback if exception is raised and * Execute instructions within a Neo4j transaction; rollback if exception is raised and
* commit otherwise; and return the return value from the operation. * commit otherwise; and return the return value from the operation.
*/ */
def exec[T<:Any](operation: NeoService => T): T = { def exec[T<:Any](operation: GraphDatabaseService => T): T = {
val tx = synchronized { val tx = synchronized {
if (neo == null) startup if (neo == null) startup
neo.beginTx neo.beginTx
Expand Down
Expand Up @@ -3,13 +3,13 @@ package com.eptcomputing.neo4j.rest
import scala.collection.mutable.HashSet import scala.collection.mutable.HashSet
import java.util.logging.Logger import java.util.logging.Logger


import org.neo4j.api.core._ import org.neo4j.graphdb._
import org.codehaus.jettison.json.{JSONObject, JSONArray, JSONException} import org.codehaus.jettison.json.{JSONObject, JSONArray, JSONException}


/** /**
* Provides helpers for converting Neo4j nodes to/from JSON. * Provides helpers for converting Neo4j nodes to/from JSON.
*/ */
object NeoJsonConverter extends IteratorConverters { object Neo4jJsonConverter extends IteratorConverters {


private val log = Logger.getLogger(this.getClass.getName) private val log = Logger.getLogger(this.getClass.getName)


Expand Down Expand Up @@ -71,7 +71,7 @@ object NeoJsonConverter extends IteratorConverters {
* of that node are updated to match the JSON description; if null, a new node is created. * of that node are updated to match the JSON description; if null, a new node is created.
* In either case, the up-to-date node is returned. * In either case, the up-to-date node is returned.
*/ */
def jsonToNeo(json: JSONObject, neo: NeoService, existingNode: Node): Node = { def jsonToNeo(json: JSONObject, neo: GraphDatabaseService, existingNode: Node): Node = {
val specialProps = Array("_id", "_url", "_in", "_out") val specialProps = Array("_id", "_url", "_in", "_out")
val node = if (existingNode == null) neo.createNode else existingNode val node = if (existingNode == null) neo.createNode else existingNode
jsonPropertiesToNeo(json, node, specialProps) jsonPropertiesToNeo(json, node, specialProps)
Expand Down Expand Up @@ -129,7 +129,7 @@ object NeoJsonConverter extends IteratorConverters {
} }


/** Tries to convert a single JSON object, representing a relationship, into Neo. */ /** Tries to convert a single JSON object, representing a relationship, into Neo. */
private def jsonRelationshipToNeo(obj: Object, neo: NeoService, node: Node, name: String, private def jsonRelationshipToNeo(obj: Object, neo: GraphDatabaseService, node: Node, name: String,
direction: Direction, unprocessedRels: HashSet[Long]) { direction: Direction, unprocessedRels: HashSet[Long]) {
val relType = DynamicRelationshipType.withName(name) val relType = DynamicRelationshipType.withName(name)


Expand Down
32 changes: 16 additions & 16 deletions src/main/scala/com/eptcomputing/neo4j/rest/Neo4jResource.scala
Expand Up @@ -5,10 +5,10 @@ import javax.ws.rs._
import javax.ws.rs.core._ import javax.ws.rs.core._


import org.codehaus.jettison.json.JSONObject import org.codehaus.jettison.json.JSONObject
import org.neo4j.api.core.{NeoService, Node} import org.neo4j.graphdb.{GraphDatabaseService, Node}


import com.eptcomputing.neo4j.NeoServer import com.eptcomputing.neo4j.Neo4jServer
import NeoJsonConverter._ import Neo4jJsonConverter._


/** /**
* A template for a CRUD (Create/Read/Update/Delete) RESTful JSON resource. You can * A template for a CRUD (Create/Read/Update/Delete) RESTful JSON resource. You can
Expand All @@ -22,26 +22,26 @@ abstract class NeoResource extends RequiredParam {
* JSON object. Should return the newly created node. Is called within a Neo4j * JSON object. Should return the newly created node. Is called within a Neo4j
* transaction. * transaction.
*/ */
def create(neo: NeoService, json: JSONObject): Node def create(neo: GraphDatabaseService, json: JSONObject): Node


/** /**
* Override this method to perform mapping from a Neo4j node to a JSON object * Override this method to perform mapping from a Neo4j node to a JSON object
* for output. Should return the desired JSON serialisation, or an object which * for output. Should return the desired JSON serialisation, or an object which
* Jersey is able to serialise to JSON automatically. * Jersey is able to serialise to JSON automatically.
*/ */
def read(neo: NeoService, node: Node): Any def read(neo: GraphDatabaseService, node: Node): Any


/** /**
* Override this method to perform the overwriting of a Neo4j node with new data. * Override this method to perform the overwriting of a Neo4j node with new data.
*/ */
def update(neo: NeoService, existing: Node, newValue: JSONObject) def update(neo: GraphDatabaseService, existing: Node, newValue: JSONObject)


/** /**
* Override this method to perform the deletion of a Neo4j node. Should return * Override this method to perform the deletion of a Neo4j node. Should return
* a JSON serialisation of the node in its most recent version before it was * a JSON serialisation of the node in its most recent version before it was
* deleted (or an object which Jersey is able to serialise to JSON automatically). * deleted (or an object which Jersey is able to serialise to JSON automatically).
*/ */
def delete(neo: NeoService, node: Node): Any def delete(neo: GraphDatabaseService, node: Node): Any


/** /**
* <tt>POST /neo_resource</tt> with a JSON document as body creates a new entity * <tt>POST /neo_resource</tt> with a JSON document as body creates a new entity
Expand All @@ -52,7 +52,7 @@ abstract class NeoResource extends RequiredParam {
@Consumes(Array(MediaType.APPLICATION_JSON)) @Consumes(Array(MediaType.APPLICATION_JSON))
@Produces(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON))
def createJSON(json: JSONObject) = { def createJSON(json: JSONObject) = {
NeoServer.exec { neo => Neo4jServer.exec { neo =>
val node = create(neo, json) val node = create(neo, json)
val uri = UriBuilder.fromResource(this.getClass).path("{id}").build(new java.lang.Long(node.getId)) val uri = UriBuilder.fromResource(this.getClass).path("{id}").build(new java.lang.Long(node.getId))
Response.created(uri).entity(read(neo, node)).build Response.created(uri).entity(read(neo, node)).build
Expand All @@ -67,7 +67,7 @@ abstract class NeoResource extends RequiredParam {
@Produces(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON))
def readJSON(@PathParam("id") node: NeoNodeParam) = { def readJSON(@PathParam("id") node: NeoNodeParam) = {
requiredParam("id", node) requiredParam("id", node)
NeoServer.exec { neo => read(neo, node.getNode(neo)) } Neo4jServer.exec { neo => read(neo, node.getNode(neo)) }
} }


/** /**
Expand All @@ -80,7 +80,7 @@ abstract class NeoResource extends RequiredParam {
@Produces(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON))
def updateJSON(@PathParam("id") node: NeoNodeParam, json: JSONObject) = { def updateJSON(@PathParam("id") node: NeoNodeParam, json: JSONObject) = {
requiredParam("id", node) requiredParam("id", node)
NeoServer.exec { neo => Neo4jServer.exec { neo =>
val neoNode = node.getNode(neo) val neoNode = node.getNode(neo)
update(neo, neoNode, json) update(neo, neoNode, json)
read(neo, neoNode) read(neo, neoNode)
Expand All @@ -95,27 +95,27 @@ abstract class NeoResource extends RequiredParam {
@Produces(Array(MediaType.APPLICATION_JSON)) @Produces(Array(MediaType.APPLICATION_JSON))
def deleteJSON(@PathParam("id") node: NeoNodeParam) = { def deleteJSON(@PathParam("id") node: NeoNodeParam) = {
requiredParam("id", node) requiredParam("id", node)
NeoServer.exec { neo => delete(neo, node.getNode(neo)) } Neo4jServer.exec { neo => delete(neo, node.getNode(neo)) }
} }
} }




/** /**
* Simple default implementation of a CRUD resource which maps to a single Neo4j node. * Simple default implementation of a CRUD resource which maps to a single Neo4j node.
* See <tt>NeoJsonConverter</tt> for the format used. * See <tt>Neo4jJsonConverter</tt> for the format used.
*/ */
class SimpleNeoResource extends NeoResource with IteratorConverters { class SimpleNeoResource extends NeoResource with IteratorConverters {


def create(neo: NeoService, json: JSONObject) = def create(neo: GraphDatabaseService, json: JSONObject) =
jsonToNeo(json, neo, null) jsonToNeo(json, neo, null)


def read(neo: NeoService, node: Node) = def read(neo: GraphDatabaseService, node: Node) =
neoToJson(node) neoToJson(node)


def update(neo: NeoService, existing: Node, newValue: JSONObject) = def update(neo: GraphDatabaseService, existing: Node, newValue: JSONObject) =
jsonToNeo(newValue, neo, existing) jsonToNeo(newValue, neo, existing)


def delete(neo: NeoService, node: Node) = { def delete(neo: GraphDatabaseService, node: Node) = {
val json = neoToJson(node) val json = neoToJson(node)
node.getRelationships.foreach{_.delete} node.getRelationships.foreach{_.delete}
node.delete node.delete
Expand Down
Expand Up @@ -3,7 +3,7 @@ package com.eptcomputing.neo4j.rest
import javax.ws.rs._ import javax.ws.rs._
import javax.ws.rs.core._ import javax.ws.rs.core._


import org.neo4j.api.core.{NeoService, Node, NotFoundException} import org.neo4j.graphdb.{GraphDatabaseService, Node, NotFoundException}


/** /**
* Encapsulates the value of a parameter passed to the API (e.g. type of a @FormParam or * Encapsulates the value of a parameter passed to the API (e.g. type of a @FormParam or
Expand Down Expand Up @@ -40,7 +40,7 @@ class NeoNodeParam(param: String) extends ParamType[Int](param) {
* Tries to find a node in a Neo server instance, raises HTTP 404 ("not found") if the node does * Tries to find a node in a Neo server instance, raises HTTP 404 ("not found") if the node does
* not exist. * not exist.
*/ */
def getNode(neo: NeoService): Node = { def getNode(neo: GraphDatabaseService): Node = {
try { try {
neo.getNodeById(value) neo.getNodeById(value)
} catch { } catch {
Expand Down
@@ -1,7 +1,7 @@
package com.eptcomputing.neo4j.version package com.eptcomputing.neo4j.version


import java.io.Serializable import java.io.Serializable
import org.neo4j.api.core._ import org.neo4j.graphdb._


// THIS IS WORK IN PROGRESS // THIS IS WORK IN PROGRESS
// //
Expand All @@ -13,7 +13,7 @@ import org.neo4j.api.core._
// //
// At the moment, these classes simply delegate to an underlying 'real' Neo4j object. // At the moment, these classes simply delegate to an underlying 'real' Neo4j object.


private class VersionedNeo(delegate: NeoService) extends NeoService { private class VersionedNeo(delegate: GraphDatabaseService) extends GraphDatabaseService {
def beginTx: Transaction = delegate.beginTx def beginTx: Transaction = delegate.beginTx


def createNode: Node = delegate.createNode def createNode: Node = delegate.createNode
Expand Down
24 changes: 14 additions & 10 deletions src/test/java/com/eptcomputing/neo4j/rest/Neo4jResourceTest.java
@@ -1,24 +1,28 @@
package com.eptcomputing.neo4j.rest; package com.eptcomputing.neo4j.rest;


import com.sun.jersey.api.client.ClientResponse; import static org.hamcrest.CoreMatchers.anyOf;
import com.sun.jersey.api.client.UniformInterfaceException; import static org.hamcrest.CoreMatchers.equalTo;
import com.sun.jersey.test.framework.JerseyTest; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;


import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.test.framework.JerseyTest;


/** /**
* Sadly, we cannot yet write Jersey client tests in Scala because of a bug in the compiler: * Sadly, we cannot yet write Jersey client tests in Scala because of a bug in the compiler:
* http://lampsvn.epfl.ch/trac/scala/ticket/1539 * http://lampsvn.epfl.ch/trac/scala/ticket/1539
*/ */
public class NeoResourceTest extends JerseyTest { public class Neo4jResourceTest extends JerseyTest {


public NeoResourceTest() throws Exception { public Neo4jResourceTest() throws Exception {
super("com.eptcomputing.neo4j.rest.test"); super("com.eptcomputing.neo4j.rest.test");
} }


Expand Down

0 comments on commit 8aa8c26

Please sign in to comment.