-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CodeGenSugar.scala
143 lines (128 loc) · 6.59 KB
/
CodeGenSugar.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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.compiler.v3_2.codegen.ir
import java.util.Collections
import java.util.concurrent.atomic.AtomicInteger
import org.mockito.Mockito._
import org.neo4j.cypher.internal.compiler.v3_2.codegen.{Namer, _}
import org.neo4j.cypher.internal.compiler.v3_2.executionplan.ExecutionPlanBuilder.tracer
import org.neo4j.cypher.internal.compiler.v3_2.executionplan._
import org.neo4j.cypher.internal.compiler.v3_2.planDescription.{Id, InternalPlanDescription}
import org.neo4j.cypher.internal.compiler.v3_2.planner.logical.plans.LogicalPlan
import org.neo4j.cypher.internal.compiler.v3_2.spi._
import org.neo4j.cypher.internal.compiler.v3_2.{CostBasedPlannerName, ExecutionMode, NormalMode, TaskCloser}
import org.neo4j.cypher.internal.frontend.v3_2.SemanticTable
import org.neo4j.cypher.internal.spi.v3_2.{TransactionBoundQueryContext, TransactionalContextWrapper}
import org.neo4j.cypher.internal.spi.v3_2.TransactionBoundQueryContext.IndexSearchMonitor
import org.neo4j.cypher.internal.spi.v3_2.codegen.GeneratedQueryStructure
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.api.security.AnonymousContext
import org.neo4j.kernel.api.{KernelTransaction, Statement}
import org.neo4j.kernel.impl.coreapi.PropertyContainerLocker
import org.neo4j.time.Clocks
import org.neo4j.kernel.impl.query.Neo4jTransactionalContextFactory
import org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo
import org.scalatest.mock.MockitoSugar
import scala.collection.JavaConversions
trait CodeGenSugar extends MockitoSugar {
private val semanticTable = mock[SemanticTable]
def compile(plan: LogicalPlan): CompiledPlan = {
val statistics: GraphStatistics = mock[GraphStatistics]
val context = mock[PlanContext]
doReturn(statistics).when(context).statistics
new CodeGenerator(GeneratedQueryStructure, Clocks.systemClock())
.generate(plan, context, semanticTable, CostBasedPlannerName.default)
}
def compileAndExecute(plan: LogicalPlan,
graphDb: GraphDatabaseQueryService,
mode: ExecutionMode = NormalMode) = {
executeCompiled(compile(plan), graphDb, mode)
}
def executeCompiled(plan: CompiledPlan,
graphDb: GraphDatabaseQueryService,
mode: ExecutionMode = NormalMode): InternalExecutionResult = {
val tx = graphDb.beginTransaction(KernelTransaction.Type.explicit, AnonymousContext.read())
try {
val locker: PropertyContainerLocker = new PropertyContainerLocker
val contextFactory = Neo4jTransactionalContextFactory.create(graphDb, locker)
val transactionalContext = TransactionalContextWrapper(contextFactory.newContext(ClientConnectionInfo.EMBEDDED_CONNECTION, tx,
"no query text exists for this test", Collections.emptyMap()))
val queryContext = new TransactionBoundQueryContext(transactionalContext)(mock[IndexSearchMonitor])
val result = plan.executionResultBuilder(queryContext, mode, tracer(mode), Map.empty, new TaskCloser)
tx.success()
result.size
result
} finally {
tx.close()
}
}
def evaluate(instructions: Seq[Instruction],
qtx: QueryContext = mockQueryContext(),
columns: Seq[String] = Seq.empty,
params: Map[String, AnyRef] = Map.empty,
operatorIds: Map[String, Id] = Map.empty): List[Map[String, Object]] = {
val clazz = compile(instructions, columns, operatorIds)
val result = newInstance(clazz, queryContext = qtx, params = params)
evaluate(result)
}
def evaluate(result: InternalExecutionResult): List[Map[String, Object]] = {
var rows = List.empty[Map[String, Object]]
val columns: List[String] = result.columns
result.accept(new InternalResultVisitor[RuntimeException] {
override def visit(row: InternalResultRow): Boolean = {
rows = rows :+ columns.map(key => (key, row.get(key))).toMap
true
}
})
rows
}
def codeGenConfiguration = CodeGenConfiguration(mode = ByteCodeMode)
def compile(instructions: Seq[Instruction], columns: Seq[String], operatorIds: Map[String, Id] = Map.empty): GeneratedQuery = {
//In reality the same namer should be used for construction Instruction as in generating code
//these tests separate the concerns so we give this namer non-standard prefixes
CodeGenerator.generateCode(GeneratedQueryStructure)(instructions, operatorIds, columns, codeGenConfiguration)(
new CodeGenContext(new SemanticTable(), Map.empty, new Namer(
new AtomicInteger(0), varPrefix = "TEST_VAR", methodPrefix = "TEST_METHOD"))).query
}
def newInstance(clazz: GeneratedQuery,
taskCloser: TaskCloser = new TaskCloser,
queryContext: QueryContext = mockQueryContext(),
graphdb: GraphDatabaseService = null,
executionMode: ExecutionMode = null,
provider: Provider[InternalPlanDescription] = null,
queryExecutionTracer: QueryExecutionTracer = QueryExecutionTracer.NONE,
params: Map[String, AnyRef] = Map.empty): InternalExecutionResult = {
val generated = clazz.execute(taskCloser, queryContext,
executionMode, provider, queryExecutionTracer, JavaConversions.mapAsJavaMap(params))
new CompiledExecutionResult(taskCloser, queryContext, generated, provider)
}
def insertStatic(clazz: Class[GeneratedQueryExecution], mappings: (String, Id)*) = mappings.foreach {
case (name, id) => setStaticField(clazz, name, id)
}
private def mockQueryContext() = {
val qc = mock[QueryContext]
val transactionalContext = mock[TransactionalContextWrapper]
val statement = mock[Statement]
when(qc.transactionalContext).thenReturn(transactionalContext)
when(transactionalContext.statement).thenReturn(statement)
qc
}
}