Skip to content

Commit

Permalink
Shrink the CompilerFactory interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and alexaverbuch committed May 25, 2018
1 parent 86dd6b1 commit 8b1f2e6
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 189 deletions.
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.cypher.internal.javacompat;

import org.neo4j.cypher.internal.CommunityCompatibilityFactory;
import org.neo4j.cypher.internal.CommunityCompilerFactory;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Service;
Expand Down Expand Up @@ -57,29 +57,29 @@ protected QueryExecutionEngine createEngine( Dependencies deps, GraphDatabaseAPI
Monitors monitors = resolver.resolveDependency( Monitors.class );
Config config = resolver.resolveDependency( Config.class );
LogProvider logProvider = logService.getInternalLogProvider();
CommunityCompatibilityFactory compatibilityFactory =
new CommunityCompatibilityFactory( queryService, monitors, logProvider );
deps.satisfyDependencies( compatibilityFactory );
return createEngine( queryService, config, logProvider, compatibilityFactory );
CommunityCompilerFactory compilerFactory =
new CommunityCompilerFactory( queryService, monitors, logProvider );
deps.satisfyDependencies( compilerFactory );
return createEngine( queryService, config, logProvider, compilerFactory );
}

private QueryExecutionEngine createEngine( GraphDatabaseCypherService queryService, Config config,
LogProvider logProvider, CommunityCompatibilityFactory compatibilityFactory )
LogProvider logProvider, CommunityCompilerFactory compilerFactory )
{
return config.get( GraphDatabaseSettings.snapshot_query ) ?
snapshotEngine( queryService, config, logProvider, compatibilityFactory ) :
standardEngine( queryService, logProvider, compatibilityFactory );
snapshotEngine( queryService, config, logProvider, compilerFactory ) :
standardEngine( queryService, logProvider, compilerFactory );
}

private SnapshotExecutionEngine snapshotEngine( GraphDatabaseCypherService queryService, Config config,
LogProvider logProvider, CommunityCompatibilityFactory compatibilityFactory )
LogProvider logProvider, CommunityCompilerFactory compilerFactory )
{
return new SnapshotExecutionEngine( queryService, config, logProvider, compatibilityFactory );
return new SnapshotExecutionEngine( queryService, config, logProvider, compilerFactory );
}

private ExecutionEngine standardEngine( GraphDatabaseCypherService queryService, LogProvider logProvider,
CommunityCompatibilityFactory compatibilityFactory )
CommunityCompilerFactory compilerFactory )
{
return new ExecutionEngine( queryService, logProvider, compatibilityFactory );
return new ExecutionEngine( queryService, logProvider, compilerFactory );
}
}
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal

import org.neo4j.cypher.internal.compatibility.v2_3.helpers._
import org.neo4j.cypher.internal.compatibility.v3_1.helpers._
import org.neo4j.cypher.internal.compatibility.v3_3.{CommunityRuntimeContextCreator => CommunityRuntimeContextCreatorV3_3}
import org.neo4j.cypher.internal.compatibility.v3_5.Cypher35Compiler
import org.neo4j.cypher.internal.compatibility.v3_5.runtime.{CommunityRuntimeBuilder, CommunityRuntimeContextCreator => CommunityRuntimeContextCreatorv3_5}
import org.neo4j.cypher.internal.compatibility.{v2_3, v3_1, v3_3 => v3_3compat}
import org.neo4j.cypher.internal.compiler.v3_5.CypherPlannerConfiguration
import org.neo4j.cypher.internal.runtime.interpreted.LastCommittedTxIdProvider
import org.neo4j.cypher.{CypherPlannerOption, CypherRuntimeOption, CypherUpdateStrategy, CypherVersion}
import org.neo4j.helpers.Clock
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.logging.{Log, LogProvider}
import org.opencypher.v9_0.util.InvalidArgumentException

/**
* Factory which creates cypher compilers.
*/
trait CompilerFactory {
def createCompiler(cypherVersion: CypherVersion,
cypherPlanner: CypherPlannerOption,
cypherRuntime: CypherRuntimeOption,
cypherUpdateStrategy: CypherUpdateStrategy,
config: CypherPlannerConfiguration): Compiler
}

class CommunityCompilerFactory(graph: GraphDatabaseQueryService,
kernelMonitors: KernelMonitors,
logProvider: LogProvider
) extends CompilerFactory {

private val log: Log = logProvider.getLog(getClass)

override def createCompiler(cypherVersion: CypherVersion,
cypherPlanner: CypherPlannerOption,
cypherRuntime: CypherRuntimeOption,
cypherUpdateStrategy: CypherUpdateStrategy,
config: CypherPlannerConfiguration
): Compiler = {

(cypherVersion, cypherPlanner) match {

// 2.3
case (CypherVersion.v2_3, CypherPlannerOption.rule) =>
v2_3.Rule23Compiler(graph, as2_3(config), Clock.SYSTEM_CLOCK, kernelMonitors)
case (CypherVersion.v2_3, _) =>
v2_3.Cost23Compiler(graph, as2_3(config), Clock.SYSTEM_CLOCK, kernelMonitors, log, cypherPlanner, cypherRuntime)

// 3.1
case (CypherVersion.v3_1, CypherPlannerOption.rule) =>
v3_1.Rule31Compiler(graph, as3_1(config), CompilerEngineDelegator.CLOCK, kernelMonitors)
case (CypherVersion.v3_1, _) =>
v3_1.Cost31Compiler(graph, as3_1(config), CompilerEngineDelegator.CLOCK, kernelMonitors, log, cypherPlanner, cypherRuntime, cypherUpdateStrategy)

// 3.3 or 3.5 + rule
case (_, CypherPlannerOption.rule) =>
throw new InvalidArgumentException(s"The rule planner is no longer a valid planner option in Neo4j ${cypherVersion.name}. If you need to use it, please select compatibility mode Cypher 3.1")

// 3.3
case (CypherVersion.v3_3, _) =>
v3_3compat.Cypher33Compiler(config, CompilerEngineDelegator.CLOCK, kernelMonitors, log,
cypherPlanner, cypherRuntime, cypherUpdateStrategy, CommunityRuntimeBuilder,
CommunityRuntimeContextCreatorV3_3, CommunityRuntimeContextCreatorv3_5, LastCommittedTxIdProvider(graph))

// 3.5
case (CypherVersion.v3_5, _) =>
Cypher35Compiler(config, CompilerEngineDelegator.CLOCK, kernelMonitors, log,
cypherPlanner, cypherRuntime, cypherUpdateStrategy, CommunityRuntimeBuilder,
CommunityRuntimeContextCreatorv3_5, LastCommittedTxIdProvider(graph))
}
}
}
Expand Up @@ -19,91 +19,15 @@
*/
package org.neo4j.cypher.internal

import org.neo4j.cypher.internal.compatibility.v2_3.helpers._
import org.neo4j.cypher.internal.compatibility.v3_1.helpers._
import org.neo4j.cypher.internal.compatibility.v3_3.{CommunityRuntimeContextCreator => CommunityRuntimeContextCreatorV3_3}
import org.neo4j.cypher.internal.compatibility.v3_5.Cypher35Compiler
import org.neo4j.cypher.internal.compatibility.v3_5.runtime.{CommunityRuntimeBuilder, CommunityRuntimeContextCreator => CommunityRuntimeContextCreatorv3_5}
import org.neo4j.cypher.internal.compatibility.{v2_3, v3_1, v3_3 => v3_3compat}
import org.neo4j.cypher.internal.compiler.v3_5.CypherPlannerConfiguration
import org.neo4j.cypher.internal.runtime.interpreted.LastCommittedTxIdProvider
import org.opencypher.v9_0.util.InvalidArgumentException
import org.neo4j.cypher.{CypherPlannerOption, CypherRuntimeOption, CypherUpdateStrategy, CypherVersion}
import org.neo4j.helpers.Clock
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.impl.util.CopyOnWriteHashMap
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.logging.{Log, LogProvider}

sealed trait PlannerSpec
final case class PlannerSpec_v2_3(planner: CypherPlannerOption, runtime: CypherRuntimeOption) extends PlannerSpec
final case class PlannerSpec_v3_1(planner: CypherPlannerOption, runtime: CypherRuntimeOption, updateStrategy: CypherUpdateStrategy) extends PlannerSpec
final case class PlannerSpec_v3_3(planner: CypherPlannerOption, runtime: CypherRuntimeOption, updateStrategy: CypherUpdateStrategy) extends PlannerSpec
final case class PlannerSpec_v3_5(planner: CypherPlannerOption, runtime: CypherRuntimeOption, updateStrategy: CypherUpdateStrategy) extends PlannerSpec

trait CompilerFactory {
def create(spec: PlannerSpec_v2_3, config: CypherPlannerConfiguration): v2_3.Cypher23Compiler

def create(spec: PlannerSpec_v3_1, config: CypherPlannerConfiguration): v3_1.Cypher31Compiler

def create(spec: PlannerSpec_v3_3, config: CypherPlannerConfiguration): v3_3compat.Cypher33Compiler[_,_,_]

def create(spec: PlannerSpec_v3_5, config: CypherPlannerConfiguration): Cypher35Compiler[_,_]

def createCompiler(cypherVersion: CypherVersion,
cypherPlanner: CypherPlannerOption,
cypherRuntime: CypherRuntimeOption,
cypherUpdateStrategy: CypherUpdateStrategy,
config: CypherPlannerConfiguration): Compiler = {
cypherVersion match {
case CypherVersion.v2_3 => create(PlannerSpec_v2_3(cypherPlanner, cypherRuntime), config)
case CypherVersion.v3_1 => create(PlannerSpec_v3_1(cypherPlanner, cypherRuntime, cypherUpdateStrategy), config)
case CypherVersion.v3_3 => create(PlannerSpec_v3_3(cypherPlanner, cypherRuntime, cypherUpdateStrategy), config)
case CypherVersion.v3_5 => create(PlannerSpec_v3_5(cypherPlanner, cypherRuntime, cypherUpdateStrategy), config)
}
}
}

class CommunityCompatibilityFactory(graph: GraphDatabaseQueryService, kernelMonitors: KernelMonitors,
logProvider: LogProvider) extends CompilerFactory {

private val log: Log = logProvider.getLog(getClass)

override def create(spec: PlannerSpec_v2_3, config: CypherPlannerConfiguration): v2_3.Cypher23Compiler = spec.planner match {
case CypherPlannerOption.rule =>
v2_3.Rule23Compiler(graph, as2_3(config), Clock.SYSTEM_CLOCK, kernelMonitors)
case _ =>
v2_3.Cost23Compiler(graph, as2_3(config), Clock.SYSTEM_CLOCK, kernelMonitors, log, spec.planner, spec.runtime)
}

override def create(spec: PlannerSpec_v3_1, config: CypherPlannerConfiguration): v3_1.Cypher31Compiler = spec.planner match {
case CypherPlannerOption.rule =>
v3_1.Rule31Compiler(graph, as3_1(config), CompilerEngineDelegator.CLOCK, kernelMonitors)
case _ =>
v3_1.Cost31Compiler(graph, as3_1(config), CompilerEngineDelegator.CLOCK, kernelMonitors, log, spec.planner, spec.runtime, spec.updateStrategy)
}

override def create(spec: PlannerSpec_v3_3, config: CypherPlannerConfiguration): v3_3compat.Cypher33Compiler[_,_,_] =
(spec.planner, spec.runtime) match {
case (CypherPlannerOption.rule, _) =>
throw new InvalidArgumentException("The rule planner is no longer a valid planner option in Neo4j 3.3. If you need to use it, please select compatibility mode Cypher 3.1")
case _ =>
v3_3compat.Cypher33Compiler(config, CompilerEngineDelegator.CLOCK, kernelMonitors, log,
spec.planner, spec.runtime, spec.updateStrategy, CommunityRuntimeBuilder,
CommunityRuntimeContextCreatorV3_3, CommunityRuntimeContextCreatorv3_5, LastCommittedTxIdProvider(graph))
}

override def create(spec: PlannerSpec_v3_5, config: CypherPlannerConfiguration): Cypher35Compiler[_,_] =
(spec.planner, spec.runtime) match {
case (CypherPlannerOption.rule, _) =>
throw new InvalidArgumentException("The rule planner is no longer a valid planner option in Neo4j 3.4. If you need to use it, please select compatibility mode Cypher 3.1")
case _ =>
Cypher35Compiler(config, CompilerEngineDelegator.CLOCK, kernelMonitors, log,
spec.planner, spec.runtime, spec.updateStrategy, CommunityRuntimeBuilder,
CommunityRuntimeContextCreatorv3_5, LastCommittedTxIdProvider(graph))
}
}

/**
* Keeps track of all cypher compilers, and finds the relevant compiler for a preparsed query.
*
* @param factory factory to create compilers
*/
class CompilerLibrary(factory: CompilerFactory) {

private val compilers = new CopyOnWriteHashMap[CompilerKey, Compiler]
Expand Down
Expand Up @@ -22,11 +22,10 @@
import org.junit.Rule;
import org.junit.Test;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.neo4j.cypher.internal.CommunityCompatibilityFactory;
import org.neo4j.cypher.internal.CommunityCompilerFactory;
import org.neo4j.graphdb.Result;
import org.neo4j.internal.kernel.api.security.LoginContext;
import org.neo4j.kernel.GraphDatabaseQueryService;
Expand Down Expand Up @@ -62,9 +61,9 @@ public void shouldConvertListsAndMapsWhenPassingFromScalaToJava() throws Excepti
Monitors monitors = graph.getDependencyResolver().resolveDependency( Monitors.class );

NullLogProvider nullLogProvider = NullLogProvider.getInstance();
CommunityCompatibilityFactory compatibilityFactory =
new CommunityCompatibilityFactory( graph, monitors, nullLogProvider );
ExecutionEngine executionEngine = new ExecutionEngine( graph, nullLogProvider, compatibilityFactory );
CommunityCompilerFactory compilerFactory =
new CommunityCompilerFactory( graph, monitors, nullLogProvider );
ExecutionEngine executionEngine = new ExecutionEngine( graph, nullLogProvider, compilerFactory );

Result result;
try ( InternalTransaction tx = graph
Expand Down
Expand Up @@ -23,7 +23,7 @@ import java.util
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.atomic.AtomicBoolean

import org.neo4j.cypher.internal.{CommunityCompatibilityFactory, ExecutionEngine}
import org.neo4j.cypher.internal.ExecutionEngine
import org.neo4j.graphdb.{TransactionTerminatedException, TransientTransactionFailureException}
import org.neo4j.internal.kernel.api.Transaction.Type
import org.neo4j.internal.kernel.api.security.LoginContext.AUTH_DISABLED
Expand Down
Expand Up @@ -29,7 +29,7 @@ import org.neo4j.cypher._
import org.neo4j.cypher.internal.compiler.v3_5.planner.logical.idp.IDPSolverMonitor
import org.neo4j.cypher.internal.javacompat.GraphDatabaseCypherService
import org.neo4j.cypher.internal.runtime.planDescription.InternalPlanDescription
import org.neo4j.cypher.internal.{CommunityCompatibilityFactory, ExecutionEngine}
import org.neo4j.cypher.internal.{CommunityCompilerFactory, ExecutionEngine}
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}
Expand Down
Expand Up @@ -22,8 +22,8 @@
*/
package org.neo4j.cypher.internal.javacompat;

import org.neo4j.cypher.internal.CommunityCompatibilityFactory;
import org.neo4j.cypher.internal.EnterpriseCompatibilityFactory;
import org.neo4j.cypher.internal.CommunityCompilerFactory;
import org.neo4j.cypher.internal.EnterpriseCompilerFactory;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Service;
Expand Down Expand Up @@ -61,32 +61,33 @@ protected QueryExecutionEngine createEngine( Dependencies deps, GraphDatabaseAPI
Monitors monitors = resolver.resolveDependency( Monitors.class );
Config config = resolver.resolveDependency( Config.class );
LogProvider logProvider = logService.getInternalLogProvider();
CommunityCompatibilityFactory inner =
new CommunityCompatibilityFactory( queryService, monitors, logProvider );
CommunityCompilerFactory communityCompilerFactory =
new CommunityCompilerFactory( queryService, monitors, logProvider );

EnterpriseCompatibilityFactory compatibilityFactory =
new EnterpriseCompatibilityFactory( inner, queryService, monitors, logProvider );
deps.satisfyDependency( compatibilityFactory );
return createEngine( queryService, config, logProvider, compatibilityFactory );
EnterpriseCompilerFactory compilerFactory =
new EnterpriseCompilerFactory( communityCompilerFactory, queryService, monitors, logProvider );

deps.satisfyDependency( compilerFactory );
return createEngine( queryService, config, logProvider, compilerFactory );
}

private QueryExecutionEngine createEngine( GraphDatabaseCypherService queryService, Config config,
LogProvider logProvider, EnterpriseCompatibilityFactory compatibilityFactory )
LogProvider logProvider, EnterpriseCompilerFactory compilerFactory )
{
return config.get( GraphDatabaseSettings.snapshot_query ) ?
snapshotEngine( queryService, config, logProvider, compatibilityFactory ) :
standardEngine( queryService, logProvider, compatibilityFactory );
snapshotEngine( queryService, config, logProvider, compilerFactory ) :
standardEngine( queryService, logProvider, compilerFactory );
}

private SnapshotExecutionEngine snapshotEngine( GraphDatabaseCypherService queryService, Config config,
LogProvider logProvider, EnterpriseCompatibilityFactory compatibilityFactory )
LogProvider logProvider, EnterpriseCompilerFactory compilerFactory )
{
return new SnapshotExecutionEngine( queryService, config, logProvider, compatibilityFactory );
return new SnapshotExecutionEngine( queryService, config, logProvider, compilerFactory );
}

private ExecutionEngine standardEngine( GraphDatabaseCypherService queryService, LogProvider logProvider,
EnterpriseCompatibilityFactory compatibilityFactory )
EnterpriseCompilerFactory compilerFactory )
{
return new ExecutionEngine( queryService, logProvider, compatibilityFactory );
return new ExecutionEngine( queryService, logProvider, compilerFactory );
}
}

0 comments on commit 8b1f2e6

Please sign in to comment.