-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CompiledExecutionResult.scala
273 lines (215 loc) · 10.2 KB
/
CompiledExecutionResult.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2002-2015 "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_0.executionplan
import java.io.{PrintWriter, StringWriter}
import java.util
import org.neo4j.cypher.internal.compiler.v3_0._
import org.neo4j.cypher.internal.compiler.v3_0.commands.values.KeyToken
import org.neo4j.cypher.internal.compiler.v3_0.helpers.IsCollection
import org.neo4j.cypher.internal.compiler.v3_0.helpers.JavaConversionSupport.asScala
import org.neo4j.cypher.internal.compiler.v3_0.planDescription.InternalPlanDescription
import org.neo4j.cypher.internal.compiler.v3_0.planDescription.InternalPlanDescription.Arguments.{Planner, Runtime}
import org.neo4j.cypher.internal.frontend.v3_0.helpers.Eagerly
import org.neo4j.cypher.internal.frontend.v3_0.notification.InternalNotification
import org.neo4j.cypher.internal.frontend.v3_0.{EntityNotFoundException, ProfilerStatisticsNotReadyException}
import org.neo4j.graphdb.QueryExecutionType._
import org.neo4j.graphdb.Result.{ResultRow, ResultVisitor}
import org.neo4j.graphdb._
import org.neo4j.kernel.api.Statement
import scala.collection.{Map, mutable}
trait SuccessfulCloseable {
def success(): Unit
def close(): Unit
}
/**
* Main class for compiled execution results, implements everything in InternalExecutionResult
* except `javaColumns` and `accept` which delegates to the injected compiled code.
*/
class CompiledExecutionResult(taskCloser: TaskCloser,
statement: Statement,
compiledCode: GeneratedQueryExecution,
description: org.neo4j.function.Supplier[InternalPlanDescription])
extends InternalExecutionResult with SuccessfulCloseable {
self =>
import scala.collection.JavaConverters._
private lazy val innerIterator: util.Iterator[util.Map[String, Any]] = {
val list = new util.ArrayList[util.Map[String, Any]]()
doInAccept(populateResults(list))
list.iterator()
}
compiledCode.setSuccessfulCloseable(self)
override def columns: List[String] = javaColumns.asScala.toList
override final def javaColumnAs[T](column: String): ResourceIterator[T] = new WrappingResourceIterator[T] {
def hasNext = self.hasNext
def next() = extractJavaColumn(column, innerIterator.next()).asInstanceOf[T]
}
override def columnAs[T](column: String): Iterator[T] = map { case m => extractColumn(column, m).asInstanceOf[T] }
override def javaIterator: ResourceIterator[util.Map[String, Any]] = new WrappingResourceIterator[util.Map[String, Any]] {
def hasNext = self.hasNext
def next() = innerIterator.next()
}
override def dumpToString(): String = {
val stringWriter = new StringWriter()
val writer = new PrintWriter(stringWriter)
dumpToString(writer)
writer.close()
stringWriter.getBuffer.toString
}
override def dumpToString(writer: PrintWriter) = {
val builder = Seq.newBuilder[Map[String, String]]
doInAccept(populateDumpToStringResults(builder))
formatOutput(writer, columns, builder.result(), queryStatistics())
}
/*
* NOTE: This should ony be used for testing, it creates an InternalExecutionResult
* where you can call both toList and dumpToString
*/
def toEagerIterableResult(planner: PlannerName, runtime: RuntimeName): InternalExecutionResult = {
val dumpToStringBuilder = Seq.newBuilder[Map[String, String]]
val result = new util.ArrayList[util.Map[String, Any]]()
doInAccept{ (row) =>
populateResults(result)(row)
populateDumpToStringResults(dumpToStringBuilder)(row)
}
val iterator = result.iterator()
new CompiledExecutionResult(taskCloser, statement, compiledCode, description) {
override def javaColumns: util.List[String] = self.javaColumns
override def accept[EX <: Exception](visitor: ResultVisitor[EX]): Unit = throw new UnsupportedOperationException
override def executionPlanDescription(): InternalPlanDescription = self.executionPlanDescription().addArgument(Planner(planner.name)).addArgument(Runtime(runtime.name))
override def toList = result.asScala.map(m => Eagerly.immutableMapValues(m.asScala, materializeAsScala)).toList
override def dumpToString(writer: PrintWriter) = formatOutput(writer, columns, dumpToStringBuilder.result(), queryStatistics())
override def next() = Eagerly.immutableMapValues(iterator.next().asScala, materializeAsScala)
override def hasNext = iterator.hasNext
override def javaIterator: ResourceIterator[util.Map[String, Any]] = new WrappingResourceIterator[util.Map[String, Any]] {
def hasNext = iterator.hasNext
def next() = iterator.next()
}
}
}
override def queryStatistics() = InternalQueryStatistics()
private var successful = false
def success() = {
successful = true
}
override def close() = {
taskCloser.close(success = successful)
}
val mode = executionMode
override def planDescriptionRequested: Boolean = executionMode == ExplainMode || executionMode == ProfileMode
override def executionType = if (executionMode == ProfileMode) {
profiled(queryType)
} else {
query(queryType)
}
override def notifications = Iterable.empty[InternalNotification]
override def hasNext = innerIterator.hasNext
override def next() = Eagerly.immutableMapValues(innerIterator.next().asScala, materializeAsScala)
//TODO when allowing writes this should be moved to the generated class
protected def queryType: QueryType = QueryType.READ_ONLY
private def populateResults(results: util.List[util.Map[String, Any]])(row: ResultRow) = {
val map = new util.HashMap[String, Any]()
columns.foreach(c => map.put(c, row.get(c)))
results.add(map)
}
private def populateDumpToStringResults(builder: mutable.Builder[Map[String, String], Seq[Map[String, String]]])(row: ResultRow) = {
val map = new mutable.HashMap[String, String]()
columns.foreach(c => map.put(c, text(row.get(c))))
builder += map
}
private def props(x: PropertyContainer): String = {
val readOperations = statement.readOperations()
val (properties, propFcn, id) = x match {
case n: Node => (asScala(readOperations.nodeGetPropertyKeys(n.getId)), readOperations.nodeGetProperty _, n.getId )
case r: Relationship => (asScala(readOperations.relationshipGetPropertyKeys(r.getId)), readOperations.relationshipGetProperty _, r.getId)
}
val keyValStrings = properties.
map(pkId => readOperations.propertyKeyGetName(pkId) + ":" + text(propFcn(id, pkId)))
keyValStrings.mkString("{", ",", "}")
}
def text(a: Any): String = a match {
case x: Node => x.toString + props(x)
case x: Relationship => ":" + x.getType.name() + "[" + x.getId + "]" + props(x)
case x if x.isInstanceOf[Map[_, _]] => makeString(x.asInstanceOf[Map[String, Any]])
case x if x.isInstanceOf[java.util.Map[_, _]] => makeString(x.asInstanceOf[java.util.Map[String, Any]].asScala)
case IsCollection(coll) => coll.map(elem => text(elem)).mkString("[", ",", "]")
case x: String => "\"" + x + "\""
case v: KeyToken => v.name
case Some(x) => x.toString
case null => "<null>"
case x => x.toString
}
def makeString(m: Map[String, Any]) = m.map {
case (k, v) => k + " -> " + text(v)
}.mkString("{", ", ", "}")
private def doInAccept[T](body: ResultRow => T) = {
if (!taskCloser.isClosed) {
accept(new ResultVisitor[RuntimeException] {
override def visit(row: ResultRow): Boolean = {
body(row)
true
}
})
} else {
throw new IllegalStateException("Unable to accept visitors after resources have been closed.")
}
}
private def materializeAsScala(v: Any): Any = {
val scalaValue = makeValueScalaCompatible(v)
scalaValue match {
case (x: Stream[_]) => x.map(materializeAsScala).toList
case (x: Map[_, _]) => Eagerly.immutableMapValues(x, materializeAsScala)
case (x: Iterable[_]) => x.map(materializeAsScala)
case x => x
}
}
private def makeValueScalaCompatible(value: Any): Any = value match {
case list: java.util.List[_] => list.asScala.map(makeValueScalaCompatible).toList
case map: java.util.Map[_, _] => Eagerly.immutableMapValues(map.asScala, makeValueScalaCompatible)
case x => x
}
private def extractColumn[T](column: String, data: Map[String, Any]): Any = {
data.getOrElse(column, {
throw columnNotFound(column, data)
})
}
private def extractJavaColumn[T](column: String, data: util.Map[String, Any]): Any = {
val value = data.get(column)
if (value == null) {
throw columnNotFound(column, data.asScala)
}
value
}
private def columnNotFound(column: String, data: Map[String, Any]) =
new EntityNotFoundException("No column named '" + column + "' was found. Found: " + data.keys.mkString("(\"", "\", \"", "\")"))
private trait WrappingResourceIterator[T] extends ResourceIterator[T] {
def remove() { throw new UnsupportedOperationException("remove") }
def close() { self.close() }
}
// *** Delegate to compiled code
def executionMode: ExecutionMode = compiledCode.executionMode()
override def javaColumns: util.List[String] = compiledCode.javaColumns()
//todo this should not depend on external visitor
override def accept[EX <: Exception](visitor: ResultVisitor[EX]): Unit =
compiledCode.accept(visitor)
override def executionPlanDescription(): InternalPlanDescription = {
if (!taskCloser.isClosed) throw new ProfilerStatisticsNotReadyException
compiledCode.executionPlanDescription()
}
}