Skip to content

Commit

Permalink
Replace GDS and GDAPI with new simplified GDS
Browse files Browse the repository at this point in the history
  • Loading branch information
craigtaverner committed Feb 17, 2016
1 parent 2dea14f commit 10b6dee
Show file tree
Hide file tree
Showing 66 changed files with 389 additions and 410 deletions.
Expand Up @@ -25,9 +25,9 @@ import org.neo4j.cypher._
import org.neo4j.cypher.internal.ExecutionEngine
import org.neo4j.cypher.internal.compiler.v3_0.test_helpers.CreateTempFileTestSupport
import org.neo4j.cypher.internal.frontend.v3_0.helpers.StringHelper.RichString
import org.neo4j.cypher.javacompat.internal.GraphDatabaseCypherService
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.graphdb.security.URLAccessRule
import org.neo4j.kernel.GraphDatabaseAPI
import org.neo4j.test.TestGraphDatabaseFactory
import org.scalatest.BeforeAndAfterAll
import org.neo4j.graphdb.config.Configuration
Expand Down Expand Up @@ -354,10 +354,10 @@ class LoadCsvAcceptanceTest
}

test("should fail for file urls if local file access disallowed") {
val db = new TestGraphDatabaseFactory()
val db = new GraphDatabaseCypherService(new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder()
.setConfig(GraphDatabaseSettings.allow_file_urls, "false")
.newGraphDatabase()
.newGraphDatabase())

intercept[LoadExternalResourceException] {
new ExecutionEngine(db).execute(s"LOAD CSV FROM 'file:///tmp/blah.csv' AS line CREATE (a {name:line[0]})")
Expand All @@ -371,10 +371,10 @@ class LoadCsvAcceptanceTest
writer.println("something")
)

val db = new TestGraphDatabaseFactory()
val db = new GraphDatabaseCypherService(new TestGraphDatabaseFactory()
.newImpermanentDatabaseBuilder()
.setConfig(GraphDatabaseSettings.load_csv_file_url_root, dir.toString)
.newGraphDatabase()
.newGraphDatabase())

val result = new ExecutionEngine(db).execute(s"LOAD CSV FROM 'file:///tmp/blah.csv' AS line RETURN line[0] AS field")
result.toList should equal(List(Map("field" -> "something")))
Expand All @@ -396,12 +396,12 @@ class LoadCsvAcceptanceTest
}
})

val db = new TestGraphDatabaseFactory()
val db = new GraphDatabaseCypherService(new TestGraphDatabaseFactory()
.addURLAccessRule( "testproto", new URLAccessRule {
override def validate(config: Configuration, url: URL): URL = url
})
.newImpermanentDatabaseBuilder()
.newGraphDatabase()
.newGraphDatabase())

val result = new ExecutionEngine(db).execute(s"LOAD CSV FROM 'testproto://foo.bar' AS line RETURN line[0] AS field")
result.toList should equal(List(Map("field" -> "something")))
Expand Down
Expand Up @@ -24,11 +24,11 @@ import java.util
import org.neo4j.cypher._
import org.neo4j.cypher.internal.{PlanDescription, ExecutionEngine}
import org.neo4j.cypher.internal.compiler.v3_0.planner.logical.idp.IDPSolverMonitor
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.cypher.javacompat.internal.GraphDatabaseCypherService
import org.neo4j.graphdb.config.Setting
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.graphdb.factory.GraphDatabaseSettings.{cypher_idp_solver_duration_threshold, cypher_idp_solver_table_threshold}
import org.neo4j.kernel.{GraphDatabaseAPI, monitoring}
import org.neo4j.kernel.{GraphDatabaseQueryService, monitoring}
import org.neo4j.test.ImpermanentGraphDatabase

import scala.collection.JavaConverters._
Expand Down Expand Up @@ -191,7 +191,7 @@ class MatchLongPatternAcceptanceTest extends ExecutionEngineFunSuite with QueryS
val config = databaseConfig() + (configKey -> configValue.toString)
runWithConfig(config.toSeq: _*) {
(engine, db) =>
graph = db.asInstanceOf[GraphDatabaseAPI]
graph = db
makeLargeMatrixDataset(100)
val monitor = TestIDPSolverMonitor()
val monitors: monitoring.Monitors = graph.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.monitoring.Monitors])
Expand Down Expand Up @@ -232,12 +232,12 @@ class MatchLongPatternAcceptanceTest extends ExecutionEngineFunSuite with QueryS
}
}

private def runWithConfig(m: (Setting[_], String)*)(run: (ExecutionEngine, GraphDatabaseService) => Unit) = {
private def runWithConfig(m: (Setting[_], String)*)(run: (ExecutionEngine, GraphDatabaseQueryService) => Unit) = {
val config: util.Map[String, String] = m.map {
case (setting, value) => setting.name() -> value
}.toMap.asJava

val graph = new ImpermanentGraphDatabase(config)
val graph = new GraphDatabaseCypherService(new ImpermanentGraphDatabase(config))
try {
val engine = new ExecutionEngine(graph)
run(engine, graph)
Expand Down
Expand Up @@ -34,8 +34,8 @@ import org.neo4j.cypher.internal.frontend.v3_0.ast.Statement
import org.neo4j.cypher.internal.frontend.v3_0.notification.InternalNotification
import org.neo4j.cypher.internal.frontend.v3_0.parser.CypherParser
import org.neo4j.cypher.internal.frontend.v3_0.{InputPosition, SemanticTable, inSequence}
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.helpers.Clock
import org.neo4j.kernel.GraphDatabaseQueryService

trait AstRewritingMonitor {
def abortedRewriting(obj: AnyRef)
Expand Down Expand Up @@ -71,7 +71,7 @@ case class CypherCompilerConfiguration(queryCacheSize: Int,
object CypherCompilerFactory {
val monitorTag = "cypher3.0"

def costBasedCompiler(graph: GraphDatabaseService, config: CypherCompilerConfiguration, clock: Clock, structure: CodeStructure[GeneratedQuery],
def costBasedCompiler(graph: GraphDatabaseQueryService, config: CypherCompilerConfiguration, clock: Clock, structure: CodeStructure[GeneratedQuery],
monitors: Monitors, logger: InfoLogger,
rewriterSequencer: (String) => RewriterStepSequencer,
plannerName: Option[CostBasedPlannerName],
Expand Down Expand Up @@ -117,7 +117,7 @@ object CypherCompilerFactory {
new CypherCompiler(parser, checker, execPlanBuilder, rewriter, cache, planCacheFactory, cacheMonitor, monitors)
}

def ruleBasedCompiler(graph: GraphDatabaseService,
def ruleBasedCompiler(graph: GraphDatabaseQueryService,
config: CypherCompilerConfiguration, clock: Clock, monitors: Monitors,
rewriterSequencer: (String) => RewriterStepSequencer): CypherCompiler = {
val parser = new CypherParser
Expand Down
Expand Up @@ -36,9 +36,8 @@ import org.neo4j.cypher.internal.compiler.v3_0.{ExecutionMode, ProfileMode, _}
import org.neo4j.cypher.internal.frontend.v3_0.PeriodicCommitInOpenTransactionException
import org.neo4j.cypher.internal.frontend.v3_0.ast.Statement
import org.neo4j.cypher.internal.frontend.v3_0.notification.InternalNotification
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.helpers.Clock
import org.neo4j.kernel.api.{Statement => KernelStatement}
import org.neo4j.kernel.GraphDatabaseQueryService


trait RunnablePlan {
Expand Down Expand Up @@ -95,7 +94,7 @@ trait ExecutablePlanBuilder {
createFingerprintReference: (Option[PlanFingerprint]) => PlanFingerprintReference): ExecutionPlan
}

class ExecutionPlanBuilder(graph: GraphDatabaseService,
class ExecutionPlanBuilder(graph: GraphDatabaseQueryService,
clock: Clock,
executionPlanBuilder: ExecutablePlanBuilder,
createFingerprintReference: Option[PlanFingerprint] => PlanFingerprintReference)
Expand Down
Expand Up @@ -19,12 +19,12 @@
*/
package org.neo4j.cypher.internal.compiler.v3_0.pipes

import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.GraphDatabaseQueryService
import org.scalatest.Assertions


class LazyIterator[T](count: Int, f: (Int, GraphDatabaseService) => T) extends Iterator[T]() with Assertions {
var db: Option[GraphDatabaseService] = None
class LazyIterator[T](count: Int, f: (Int, GraphDatabaseQueryService) => T) extends Iterator[T]() with Assertions {
var db: Option[GraphDatabaseQueryService] = None

def this(count: Int, f: Int => T) = {
this(count, (count: Int, _) => f(count))
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.cypher.internal.javacompat;

import org.neo4j.cypher.javacompat.internal.GraphDatabaseCypherService;
import org.neo4j.helpers.Service;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.impl.logging.LogService;
Expand All @@ -37,6 +38,6 @@ public CypherEngineProvider()
protected QueryExecutionEngine createEngine( GraphDatabaseAPI graphAPI )
{
LogService logService = graphAPI.getDependencyResolver().resolveDependency( LogService.class );
return new ExecutionEngine( graphAPI, logService.getInternalLogProvider() );
return new ExecutionEngine( new GraphDatabaseCypherService( graphAPI ), logService.getInternalLogProvider() );
}
}
Expand Up @@ -22,9 +22,8 @@
import java.util.Map;

import org.neo4j.cypher.CypherException;
import org.neo4j.graphdb.GraphDatabaseService;

import org.neo4j.graphdb.Result;
import org.neo4j.kernel.GraphDatabaseQueryService;
import org.neo4j.kernel.impl.query.QueryExecutionEngine;
import org.neo4j.kernel.impl.query.QueryExecutionKernelException;
import org.neo4j.kernel.impl.query.QuerySession;
Expand All @@ -46,7 +45,7 @@ public class ExecutionEngine implements QueryExecutionEngine
* Creates an execution engine around the give graph database
* @param database The database to wrap
*/
public ExecutionEngine( GraphDatabaseService database )
public ExecutionEngine( GraphDatabaseQueryService database )
{
inner = createInnerEngine( database, NullLogProvider.getInstance() );
}
Expand All @@ -56,12 +55,12 @@ public ExecutionEngine( GraphDatabaseService database )
* @param database The database to wrap
* @param logProvider A {@link LogProvider} for cypher-statements
*/
public ExecutionEngine( GraphDatabaseService database, LogProvider logProvider )
public ExecutionEngine( GraphDatabaseQueryService database, LogProvider logProvider )
{
inner = createInnerEngine( database, logProvider );
}

protected org.neo4j.cypher.internal.ExecutionEngine createInnerEngine( GraphDatabaseService database, LogProvider logProvider )
protected org.neo4j.cypher.internal.ExecutionEngine createInnerEngine( GraphDatabaseQueryService database, LogProvider logProvider )
{
return new org.neo4j.cypher.internal.ExecutionEngine( database, logProvider );
}
Expand Down
Expand Up @@ -30,8 +30,6 @@
import org.neo4j.graphdb.security.URLAccessValidationError;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.GraphDatabaseQueryService;
import org.neo4j.kernel.api.AccessMode;
import org.neo4j.kernel.api.KernelTransaction;

public class GraphDatabaseCypherService implements GraphDatabaseQueryService
{
Expand Down
Expand Up @@ -23,10 +23,9 @@ import org.neo4j.cypher.internal.compatibility.exceptionHandlerFor3_0
import org.neo4j.cypher.internal.compiler.v3_0._
import org.neo4j.cypher.internal.frontend.v3_0.InputPosition
import org.neo4j.cypher.{InvalidArgumentException, SyntaxException, _}
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.helpers.Clock
import org.neo4j.kernel.GraphDatabaseAPI
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.api.KernelAPI
import org.neo4j.kernel.configuration.Config
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
Expand Down Expand Up @@ -62,7 +61,7 @@ case class PreParsedQuery(statement: String, rawStatement: String, version: Cyph
}
}

class CypherCompiler(graph: GraphDatabaseService,
class CypherCompiler(graph: GraphDatabaseQueryService,
kernelAPI: KernelAPI,
kernelMonitors: KernelMonitors,
configuredVersion: CypherVersion,
Expand Down Expand Up @@ -159,9 +158,9 @@ class CypherCompiler(graph: GraphDatabaseService,
getSetting(graph, setting, DEFAULT_QUERY_PLAN_TTL)
}

private def getSetting[A](gds: GraphDatabaseService, configLookup: Config => A, default: A): A = gds match {
private def getSetting[A](gds: GraphDatabaseQueryService, configLookup: Config => A, default: A): A = gds match {
// TODO: Cypher should not be pulling out components from casted interfaces, it should ask for Config as a dep
case (gdbApi:GraphDatabaseAPI) => configLookup(gdbApi.getDependencyResolver.resolveDependency(classOf[Config]))
case (gdbApi:GraphDatabaseQueryService) => configLookup(gdbApi.getDependencyResolver.resolveDependency(classOf[Config]))
case _ => default
}
}
Expand Up @@ -27,33 +27,31 @@ import org.neo4j.cypher.internal.compiler.v3_0.helpers.JavaResultValueConverter
import org.neo4j.cypher.internal.compiler.v3_0.prettifier.Prettifier
import org.neo4j.cypher.internal.compiler.v3_0.{LRUCache => LRUCachev3_0, _}
import org.neo4j.cypher.internal.tracing.{CompilationTracer, TimingCompilationTracer}
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.graphdb.config.Setting
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.kernel.configuration.Config
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade
import org.neo4j.kernel.impl.query.{QueryEngineProvider, QueryExecutionMonitor, QuerySession}
import org.neo4j.kernel.{GraphDatabaseAPI, api, monitoring}
import org.neo4j.kernel.{GraphDatabaseQueryService, api, monitoring}
import org.neo4j.logging.{LogProvider, NullLogProvider}

import scala.collection.JavaConverters._

trait StringCacheMonitor extends CypherCacheMonitor[String, api.Statement]

/**
* This class construct and initialize both the cypher compiler and the cypher runtime, which is a very expensive
* operation so please make sure this will be constructed only once and properly reused.
*/
class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = NullLogProvider.getInstance()) {
class ExecutionEngine(graph: GraphDatabaseQueryService, logProvider: LogProvider = NullLogProvider.getInstance()) {

require(graph != null, "Can't work with a null graph database")

// true means we run inside REST server
protected val isServer = false
protected val graphAPI = graph.asInstanceOf[GraphDatabaseAPI]
protected val kernel = graphAPI.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.api.KernelAPI])
private val lastCommittedTxId = LastCommittedTxIdProvider(graphAPI)
protected val kernelMonitors: monitoring.Monitors = graphAPI.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.monitoring.Monitors])
protected val kernel = graph.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.api.KernelAPI])
private val lastCommittedTxId = LastCommittedTxIdProvider(graph)
protected val kernelMonitors: monitoring.Monitors = graph.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.monitoring.Monitors])
private val compilationTracer: CompilationTracer = {
if(optGraphSetting(graph, GraphDatabaseSettings.cypher_compiler_tracing, FALSE))
new TimingCompilationTracer(kernelMonitors.newMonitor(classOf[TimingCompilationTracer.EventListener]))
Expand Down Expand Up @@ -94,7 +92,7 @@ class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = Nu
executionMonitor.startQueryExecution(session, query, javaParams)

val (preparedPlanExecution, txInfo) = planQuery(query)
preparedPlanExecution.profile(graphAPI, txInfo, params, session)
preparedPlanExecution.profile(graph, txInfo, params, session)
}

@throws(classOf[SyntaxException])
Expand All @@ -120,7 +118,7 @@ class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = Nu
val javaParams = javaValues.asDeepJavaResultMap(params).asInstanceOf[JavaMap[String, AnyRef]]
executionMonitor.startQueryExecution(session, query, javaParams)
val (preparedPlanExecution, txInfo) = planQuery(query)
preparedPlanExecution.execute(graphAPI, txInfo, params, session)
preparedPlanExecution.execute(graph, txInfo, params, session)
}

@throws(classOf[SyntaxException])
Expand Down Expand Up @@ -207,9 +205,7 @@ class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = Nu
throw new IllegalStateException("Could not execute query due to insanely frequent schema changes")
}

private val txBridge = graph.asInstanceOf[GraphDatabaseAPI]
.getDependencyResolver
.resolveDependency(classOf[ThreadToStatementContextBridge])
private val txBridge = graph.getDependencyResolver.resolveDependency(classOf[ThreadToStatementContextBridge])

private def getOrCreateFromSchemaState[V](statement: api.Statement, creator: => V) = {
val javaCreator = new java.util.function.Function[ExecutionEngine, V]() {
Expand Down Expand Up @@ -258,18 +254,9 @@ class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = Nu
GraphDatabaseSettings.query_cache_size.getDefaultValue.toInt
)

private def optGraphSetting[V](graph: GraphDatabaseService, setting: Setting[V], defaultValue: V): V = {
def optGraphAs[T <: GraphDatabaseService : Manifest]: PartialFunction[GraphDatabaseService, T] = {
case (db: T) => db
}
optGraphAs[GraphDatabaseFacade]
.andThen(g => {
// TODO: Config should be passed in as a dependency to Cypher, not pulled out of casted interfaces
val config: Config = g.getDependencyResolver.resolveDependency(classOf[Config])
Option(config.get(setting))
})
.andThen(_.getOrElse(defaultValue))
.applyOrElse(graph, (_: GraphDatabaseService) => defaultValue)
private def optGraphSetting[V](graph: GraphDatabaseQueryService, setting: Setting[V], defaultValue: V): V = {
val config = graph.getDependencyResolver.resolveDependency(classOf[Config])
Option(config.get(setting)).getOrElse(defaultValue)
}

}
Expand Down
Expand Up @@ -20,14 +20,14 @@
package org.neo4j.cypher.internal

import org.neo4j.graphdb.Transaction
import org.neo4j.kernel.GraphDatabaseAPI
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.api.Statement
import org.neo4j.kernel.impl.query.QuerySession

final case class TransactionInfo(tx: Transaction, isTopLevelTx: Boolean, statement: Statement)

trait ExecutionPlan {
def run(graph: GraphDatabaseAPI, txInfo: TransactionInfo, executionMode: CypherExecutionMode, params: Map[String, Any], session: QuerySession): ExtendedExecutionResult
def run(graph: GraphDatabaseQueryService, txInfo: TransactionInfo, executionMode: CypherExecutionMode, params: Map[String, Any], session: QuerySession): ExtendedExecutionResult

def isPeriodicCommit: Boolean

Expand Down
Expand Up @@ -19,13 +19,12 @@
*/
package org.neo4j.cypher.internal

import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.GraphDatabaseAPI
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore

case class LastCommittedTxIdProvider(db: GraphDatabaseService) extends (() => Long) {
case class LastCommittedTxIdProvider(db: GraphDatabaseQueryService) extends (() => Long) {

private val resolver = db.asInstanceOf[GraphDatabaseAPI].getDependencyResolver
private val resolver = db.getDependencyResolver

override def apply(): Long = {
val txIdStore = resolver.resolveDependency(classOf[TransactionIdStore])
Expand Down
Expand Up @@ -22,7 +22,7 @@ package org.neo4j.cypher.internal
import org.neo4j.cypher.internal.compatibility.{CompatibilityFor2_3, CompatibilityFor2_3Cost, CompatibilityFor2_3Rule, CompatibilityFor3_0, CompatibilityFor3_0Cost, CompatibilityFor3_0Rule}
import org.neo4j.cypher.internal.compiler.v3_0.CypherCompilerConfiguration
import org.neo4j.cypher.{CypherPlanner, CypherRuntime, CypherUpdateStrategy}
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.api.KernelAPI
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.logging.Log
Expand All @@ -33,7 +33,7 @@ sealed trait PlannerSpec
final case class PlannerSpec_v2_3(planner: CypherPlanner, runtime: CypherRuntime) extends PlannerSpec
final case class PlannerSpec_v3_0(planner: CypherPlanner, runtime: CypherRuntime, updateStrategy: CypherUpdateStrategy) extends PlannerSpec

class PlannerFactory(graph: GraphDatabaseService, kernelAPI: KernelAPI, kernelMonitors: KernelMonitors, log: Log,
class PlannerFactory(graph: GraphDatabaseQueryService, kernelAPI: KernelAPI, kernelMonitors: KernelMonitors, log: Log,
config: CypherCompilerConfiguration) {

import helpers.wrappersFor2_3._
Expand Down

0 comments on commit 10b6dee

Please sign in to comment.