Skip to content

Commit

Permalink
Updated to work with Neo4j 2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenmuss committed Jul 25, 2014
1 parent 63345b3 commit 3a2505e
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 191 deletions.
2 changes: 0 additions & 2 deletions examples/eu/fakod/examples/TheMatrixTraversalHack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ object TheMatrix extends App with Neo4jWrapper with SingletonEmbeddedGraphDataba
implicit neo =>
val nodeMap = for ((name, prof) <- nodes) yield (name, createNode(Matrix(name, prof)))

getReferenceNode --> "ROOT" --> nodeMap("Neo")

nodeMap("Neo") --> "KNOWS" --> nodeMap("Trinity")
nodeMap("Neo") --> "KNOWS" --> nodeMap("Morpheus") --> "KNOWS" --> nodeMap("Trinity")
nodeMap("Morpheus") --> "KNOWS" --> nodeMap("Cypher") --> "KNOWS" --> nodeMap("Agent Smith")
Expand Down
40 changes: 20 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<groupId>eu.fakod</groupId>
<artifactId>neo4j-scala_2.10</artifactId>
<artifactId>neo4j-scala_2.11</artifactId>
<packaging>jar</packaging>
<name>Neo4j Scala</name>
<version>0.3.1-SNAPSHOT</version>
Expand All @@ -18,11 +18,11 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.10.3</scala.version>
<neo4j.version>1.9.4</neo4j.version>
<neo4j.shell.version>1.9.4</neo4j.shell.version>
<neo4j.cypher.version>1.9.4</neo4j.cypher.version>
<neo4j.rest.version>1.9</neo4j.rest.version>
<scala.version>2.11.2</scala.version>
<neo4j.version>2.1.2</neo4j.version>
<neo4j.shell.version>2.1.2</neo4j.shell.version>
<neo4j.cypher.version>2.1.2</neo4j.cypher.version>
<neo4j.rest.version>2.0.1</neo4j.rest.version>
</properties>

<licenses>
Expand Down Expand Up @@ -90,14 +90,14 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2_2.10</artifactId>
<version>2.3.3</version>
<artifactId>specs2_2.11</artifactId>
<version>2.3.13</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -139,7 +139,7 @@
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.0</version>
<configuration>
<recompileMode>incremental</recompileMode>
<args>
Expand Down Expand Up @@ -181,17 +181,17 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<version>3.4</version>
<configuration>
<reportPlugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.2</version>
<version>2.7</version>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<version>3.2.0</version>
<configuration>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
Expand All @@ -205,10 +205,10 @@

<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
Expand All @@ -223,7 +223,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<version>2.17</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<!--excludes>
Expand All @@ -238,7 +238,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<version>2.5</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
Expand All @@ -249,7 +249,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<version>2.3</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -266,7 +266,7 @@
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<version>3.2.0</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
Expand Down
31 changes: 19 additions & 12 deletions src/main/scala/eu/fakod/neo4jscala/DatabaseServiceProvider.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package eu.fakod.neo4jscala

import org.neo4j.kernel.EmbeddedGraphDatabase
import org.neo4j.kernel.impl.batchinsert.{BatchInserter, BatchInserterImpl}
import org.neo4j.rest.graphdb.RestGraphDatabase
import scala.collection.JavaConversions._

import java.net.URI
import java.util.{HashMap => jMap}

import org.neo4j.graphdb.factory.GraphDatabaseFactory
import org.neo4j.unsafe.batchinsert.{BatchInserters, BatchInserterImpl, BatchInserter}
import org.neo4j.rest.graphdb.RestGraphDatabase

/**
* Interface for a GraphDatabaseServiceProvider
* must be implemented by and Graph Database Service Provider
Expand Down Expand Up @@ -36,9 +39,11 @@ trait EmbeddedGraphDatabaseServiceProvider extends GraphDatabaseServiceProvider
* using an instance of an embedded graph database
*/
val ds: DatabaseService = {
import collection.JavaConversions.mapAsJavaMap
DatabaseServiceImpl(
new EmbeddedGraphDatabase(neo4jStoreDir, new jMap[String, String](configParams))
new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(neo4jStoreDir)
.setConfig(mapAsJavaMap(configParams))
.newGraphDatabase
)
}
}
Expand All @@ -52,10 +57,10 @@ private[neo4jscala] object SingeltonProvider {
def apply(neo4jStoreDir: String, configParams: Map[String, String]) = ds match {
case Some(x) => x
case None =>
import collection.JavaConversions.mapAsJavaMap
ds = Some(DatabaseServiceImpl(new EmbeddedGraphDatabase(
neo4jStoreDir, new jMap[String, String](configParams)))
)
ds = Some(DatabaseServiceImpl(new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(neo4jStoreDir)
.setConfig(new jMap[String, String](configParams))
.newGraphDatabase))
ds.get
}
}
Expand Down Expand Up @@ -92,11 +97,13 @@ private[neo4jscala] object SingeltonBatchProvider {
def apply(neo4jStoreDir: String) = inserter match {
case Some(x) => x
case None =>
inserter = Some(new BatchInserterImpl(neo4jStoreDir))
inserter = Some(BatchInserters.inserter(neo4jStoreDir))
inserter.get
}

lazy val ds: DatabaseService = DatabaseServiceImpl(inserter.get.getGraphDbService)
lazy val ds: DatabaseService = DatabaseServiceImpl(new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(inserter.get.getStoreDir)
.newGraphDatabase)
}

/**
Expand Down Expand Up @@ -147,4 +154,4 @@ trait RestGraphDatabaseServiceProvider extends GraphDatabaseServiceProvider {
case None => new RestGraphDatabase(uri.toString)
case Some((u, p)) => new RestGraphDatabase(uri.toString, u, p)
})
}
}
26 changes: 16 additions & 10 deletions src/main/scala/eu/fakod/neo4jscala/Neo4jBatchIndexProvider.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package eu.fakod.neo4jscala

import scala.language.implicitConversions
import org.neo4j.kernel.impl.batchinsert.BatchInserter
import collection.JavaConversions._
import collection.mutable.{SynchronizedMap, HashMap}
import scala.collection.concurrent.{Map => ConcurrentMap}

import java.util.{Map => juMap}
import sun.reflect.generics.reflectiveObjects.NotImplementedException

import org.neo4j.graphdb.index._
import org.neo4j.graphdb._
import org.neo4j.index.impl.lucene.{AbstractIndexHits, LuceneBatchInserterIndexProvider}
import collection.JavaConversions._
import sun.reflect.generics.reflectiveObjects.NotImplementedException
import collection.mutable.{SynchronizedMap, ConcurrentMap, HashMap}
import org.neo4j.graphdb.factory.GraphDatabaseFactory
import org.neo4j.index.impl.lucene.AbstractIndexHits
import org.neo4j.unsafe.batchinsert.{BatchInserterIndex, BatchInserterIndexProvider, BatchInserter, BatchInserters}
import org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider


/**
* provides Index access trait
Expand Down Expand Up @@ -111,13 +117,13 @@ private[neo4jscala] trait IndexCacheHelper {
*/
class BatchIndex(bii: BatchInserterIndex, bi: BatchInserter) extends Index[Node] with IndexCacheHelper {

private val gds = bi.getGraphDbService
private val gds = new GraphDatabaseFactory().newEmbeddedDatabase(bi.getStoreDir)

/**
* implicitly converts IndexHits[Long] to IndexHits[BatchNode]
*/
private implicit def toNodeIndexHits(hits: IndexHits[java.lang.Long]): IndexHits[Node] = {
val listOfNodes = for (l <- hits.iterator) yield gds.getNodeById(l)
val listOfNodes = for (l <- hits.iterator) yield gds.getNodeById(l.asInstanceOf[Long])
new ConstantScoreIterator[Node](listOfNodes.toList)
}

Expand Down Expand Up @@ -177,13 +183,13 @@ class BatchIndex(bii: BatchInserterIndex, bi: BatchInserter) extends Index[Node]
*/
class BatchRelationshipIndex(bii: BatchInserterIndex, bi: BatchInserter) extends RelationshipIndex with IndexCacheHelper {

private val gds = bi.getGraphDbService
private val gds = new GraphDatabaseFactory().newEmbeddedDatabase(bi.getStoreDir)

/**
* implicitly converts IndexHits[Long] to IndexHits[BatchRelationship]
*/
private implicit def toRelationshipIndexHits(hits: IndexHits[java.lang.Long]): IndexHits[Relationship] = {
val listOfNodes = for (l <- hits.iterator) yield gds.getRelationshipById(l)
val listOfNodes = for (l <- hits.iterator) yield gds.getRelationshipById(l.asInstanceOf[Long])
new ConstantScoreIterator[Relationship](listOfNodes.toList)
}

Expand Down Expand Up @@ -258,4 +264,4 @@ class ConstantScoreIterator[T](items: List[T], score: Float = Float.NaN) extends
iter.next
else
null.asInstanceOf[T]
}
}
52 changes: 30 additions & 22 deletions src/main/scala/eu/fakod/neo4jscala/Neo4jWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import util.CaseClassDeserializer
import collection.JavaConversions._
import CaseClassDeserializer._
import org.neo4j.graphdb._
import index.IndexManager
import org.neo4j.tooling.GlobalGraphOperations

/**
* Extend your class with this trait to get really neat new notation for creating
Expand Down Expand Up @@ -40,13 +40,14 @@ trait Neo4jWrapper extends GraphDatabaseServiceProvider with Neo4jWrapperImplici
/**
* creates a new Node from Database service
*/
def createNode(implicit ds: DatabaseService): Node = ds.gds.createNode
def createNode(labels: String*)(implicit ds: DatabaseService): Node =
ds.gds.createNode(labels.map(DynamicLabel.label(_)): _*)

/**
* convenience method to create and serialize a case class
*/
def createNode(cc: AnyRef)(implicit ds: DatabaseService): Node =
Neo4jWrapper.serialize(cc, createNode)
def createNode(cc: AnyRef, labels: String*)(implicit ds: DatabaseService): Node =
Neo4jWrapper.serialize(cc, createNode(labels: _*))

/**
* Looks up a node by id.
Expand All @@ -69,26 +70,33 @@ trait Neo4jWrapper extends GraphDatabaseServiceProvider with Neo4jWrapperImplici
ds.gds.getRelationshipById(id)

/**
* Returns the reference node, which is a "starting point" in the node
* space. Usually, a client attaches relationships to this node that leads
* into various parts of the node space. For more information about common
* node space organizational patterns, see the design guide at <a
* href="http://wiki.neo4j.org/content/Design_Guide"
* >wiki.neo4j.org/content/Design_Guide</a>.
* Returns all nodes in the node space.
*
* @return the reference node
* @throws NotFoundException if unable to get the reference node
* @return all nodes in the node space
*/
def getReferenceNode(implicit ds: DatabaseService): Node =
ds.gds.getReferenceNode
def getAllNodes(implicit ds: DatabaseService): Iterable[Node] =
GlobalGraphOperations.at(ds.gds).getAllNodes

/**
* Returns all nodes in the node space.
* Returns all nodes of the specified label.
*
* @return all nodes in the node space
* @param label the name of the label under which to confine the search
* @return all nodes with the specified label.
*/
def getAllNodes(implicit ds: DatabaseService): Iterable[Node] =
ds.gds.getAllNodes
def getAllNodesWithLabel(label: String)(implicit ds: DatabaseService): Iterable[Node] =
GlobalGraphOperations.at(ds.gds).getAllNodesWithLabel(DynamicLabel.label(label))

/**
* Returns all nodes with the specified labels matching the given predicate.
*
* @param label the label name to which to confine the search.
* @param key the node property whose value to match.
* @param value the property value to match. Must adhere to Neo4j allowed property types.
* @return All nodes matching the given predicate with the supplied label.
*/
def findNodesByLabelAndProperty(label: String, key: String, value: Any)(implicit ds: DatabaseService): Iterable[Node] =
ds.gds.findNodesByLabelAndProperty(DynamicLabel.label(label), key, value)


/**
* Returns all relationship types currently in the underlying store.
Expand All @@ -103,7 +111,7 @@ trait Neo4jWrapper extends GraphDatabaseServiceProvider with Neo4jWrapperImplici
* @return all relationship types in the underlying store
*/
def getRelationshipTypes(implicit ds: DatabaseService): Iterable[RelationshipType] =
ds.gds.getRelationshipTypes
GlobalGraphOperations.at(ds.gds).getAllRelationshipTypes

/**
* Shuts down Neo4j. After this method has been invoked, it's invalid to
Expand Down Expand Up @@ -153,7 +161,7 @@ object Neo4jWrapper extends Neo4jWrapperImplicits {
}

private def _toCCPossible[T: Manifest](pc: PropertyContainer): Option[Class[_]] = {
for (cpn <- pc[String](ClassPropertyName); c = Class.forName(cpn) if (manifest[T].erasure.isAssignableFrom(c)))
for (cpn <- pc[String](ClassPropertyName); c = Class.forName(cpn) if (manifest[T].runtimeClass.isAssignableFrom(c)))
return Some(c)
None
}
Expand All @@ -177,7 +185,7 @@ object Neo4jWrapper extends Neo4jWrapperImplicits {
toCC[T](pc) match {
case Some(t) => t
case _ => throw new IllegalArgumentException("given Case Class: " +
manifest[T].erasure.getName + " does not fit to serialized properties")
manifest[T].runtimeClass.getName + " does not fit to serialized properties")
}
}
}
Expand Down Expand Up @@ -246,4 +254,4 @@ private[neo4jscala] class RichPropertyContainer(propertyContainer: PropertyConta
case None =>
case _ => propertyContainer.setProperty(property, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ trait RestTypedTraverser extends TypedTraverserBase {
* @return String Java Script code snippet to be used for ReturnFilter or PruneEvaluator
*/
def isOfCaseClass_JS[T: Manifest] =
".getProperty('" + Neo4jWrapper.ClassPropertyName + "')==\"" + manifest[T].erasure.getName + "\";"
".getProperty('" + Neo4jWrapper.ClassPropertyName + "')==\"" + manifest[T].runtimeClass.getName + "\";"

/**
* creates Java Script code for server side startNode type test
Expand Down
Loading

0 comments on commit 3a2505e

Please sign in to comment.