-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransactionBoundQueryContext.scala
731 lines (604 loc) · 33.7 KB
/
TransactionBoundQueryContext.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* 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.spi.v3_1
import java.net.URL
import java.util.function.Predicate
import org.neo4j.collection.RawIterator
import org.neo4j.collection.primitive.PrimitiveLongIterator
import org.neo4j.collection.primitive.base.Empty.EMPTY_PRIMITIVE_LONG_COLLECTION
import org.neo4j.cypher.InternalException
import org.neo4j.cypher.internal.compiler.v3_1.MinMaxOrdering.{BY_NUMBER, BY_STRING, BY_VALUE}
import org.neo4j.cypher.internal.compiler.v3_1._
import org.neo4j.cypher.internal.compiler.v3_1.ast.convert.commands.DirectionConverter.toGraphDb
import org.neo4j.cypher.internal.compiler.v3_1.commands.expressions
import org.neo4j.cypher.internal.compiler.v3_1.commands.expressions.{KernelPredicate, OnlyDirectionExpander, TypeAndDirectionExpander}
import org.neo4j.cypher.internal.compiler.v3_1.helpers.JavaConversionSupport
import org.neo4j.cypher.internal.compiler.v3_1.helpers.JavaConversionSupport._
import org.neo4j.cypher.internal.compiler.v3_1.pipes.matching.PatternNode
import org.neo4j.cypher.internal.compiler.v3_1.spi.SchemaTypes.{IndexDescriptor, NodePropertyExistenceConstraint, RelationshipPropertyExistenceConstraint, UniquenessConstraint}
import org.neo4j.cypher.internal.compiler.v3_1.spi._
import org.neo4j.cypher.internal.frontend.v3_1.{Bound, EntityNotFoundException, FailedIndexException, SemanticDirection}
import org.neo4j.cypher.internal.javacompat.GraphDatabaseCypherService
import org.neo4j.cypher.internal.runtime.interpreted.BeansAPIRelationshipIterator
import org.neo4j.cypher.internal.spi.v3_1.TransactionBoundQueryContext.IndexSearchMonitor
import org.neo4j.graphalgo.impl.path.ShortestPath
import org.neo4j.graphalgo.impl.path.ShortestPath.ShortestPathPredicate
import org.neo4j.graphdb.RelationshipType._
import org.neo4j.graphdb._
import org.neo4j.graphdb.security.URLAccessValidationError
import org.neo4j.graphdb.traversal.{Evaluators, TraversalDescription, Uniqueness}
import org.neo4j.internal.kernel.api
import org.neo4j.internal.kernel.api.IndexQuery
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.api._
import org.neo4j.kernel.api.dbms.DbmsOperations
import org.neo4j.kernel.api.exceptions.ProcedureException
import org.neo4j.kernel.api.exceptions.schema.{AlreadyConstrainedException, AlreadyIndexedException}
import org.neo4j.kernel.api.index.InternalIndexState
import org.neo4j.kernel.api.proc.{QualifiedName => KernelQualifiedName}
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory
import org.neo4j.kernel.api.schema.index.IndexDescriptorFactory
import org.neo4j.kernel.impl.core.NodeManager
import org.neo4j.kernel.impl.locking.ResourceTypes
import org.neo4j.values.storable.Values
import scala.collection.Iterator
import scala.collection.JavaConverters._
final class TransactionBoundQueryContext(txContext: TransactionalContextWrapper)(implicit indexSearchMonitor: IndexSearchMonitor)
extends TransactionBoundTokenContext(txContext.statement) with QueryContext with SchemaDescriptorTranslation {
type EntityAccessor = NodeManager
val nodeOps = new NodeOperations
val relationshipOps = new RelationshipOperations
override lazy val entityAccessor = txContext.graph.getDependencyResolver.resolveDependency(classOf[NodeManager])
override def setLabelsOnNode(node: Long, labelIds: Iterator[Int]): Int = labelIds.foldLeft(0) {
case (count, labelId) => if (txContext.statement.dataWriteOperations().nodeAddLabel(node, labelId)) count + 1 else count
}
override def withAnyOpenQueryContext[T](work: (QueryContext) => T): T = {
if (txContext.isOpen) {
work(this)
} else {
val context = txContext.getOrBeginNewIfClosed()
var success = false
try {
val result = work(new TransactionBoundQueryContext(context))
success = true
result
} finally {
context.close(success)
}
}
}
override def createNode(): Node =
txContext.graph.createNode()
override def createRelationship(start: Node, end: Node, relType: String) =
start.createRelationshipTo(end, withName(relType))
override def createRelationship(start: Long, end: Long, relType: Int) = {
val relId = txContext.statement.dataWriteOperations().relationshipCreate(relType, start, end)
relationshipOps.getById(relId)
}
override def getOrCreateRelTypeId(relTypeName: String): Int =
txContext.statement.tokenWriteOperations().relationshipTypeGetOrCreateForName(relTypeName)
override def getLabelsForNode(node: Long) = try {
JavaConversionSupport.asScala(txContext.statement.readOperations().nodeGetLabels(node))
} catch {
case e: api.exceptions.EntityNotFoundException =>
if (nodeOps.isDeletedInThisTx(node))
throw new EntityNotFoundException(s"Node with id $node has been deleted in this transaction", e)
else
Iterator.empty
}
override def getPropertiesForNode(node: Long) =
JavaConversionSupport.asScala(txContext.statement.readOperations().nodeGetPropertyKeys(node))
override def getPropertiesForRelationship(relId: Long) =
JavaConversionSupport.asScala(txContext.statement.readOperations().relationshipGetPropertyKeys(relId))
override def isLabelSetOnNode(label: Int, node: Long) =
txContext.statement.readOperations().nodeHasLabel(node, label)
override def getOrCreateLabelId(labelName: String) =
txContext.statement.tokenWriteOperations().labelGetOrCreateForName(labelName)
def getRelationshipsForIds(node: Node, dir: SemanticDirection, types: Option[Seq[Int]]): Iterator[Relationship] = {
val relationships = types match {
case None =>
txContext.statement.readOperations().nodeGetRelationships(node.getId, toGraphDb(dir))
case Some(typeIds) =>
txContext.statement.readOperations().nodeGetRelationships(node.getId, toGraphDb(dir), typeIds.toArray)
}
new BeansAPIRelationshipIterator(relationships, entityAccessor)
}
override def indexSeek(index: IndexDescriptor, value: Any) = {
indexSearchMonitor.indexSeek(index, value)
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().indexQuery(index, IndexQuery.exact(index.propertyId, Values.of(value))))(nodeOps.getById)
}
override def indexSeekByRange(index: IndexDescriptor, value: Any) = value match {
case PrefixRange(prefix: String) =>
indexSeekByPrefixRange(index, prefix)
case range: InequalitySeekRange[Any] =>
indexSeekByPrefixRange(index, range)
case range =>
throw new InternalException(s"Unsupported index seek by range: $range")
}
private def indexSeekByPrefixRange(index: IndexDescriptor, range: InequalitySeekRange[Any]): scala.Iterator[Node] = {
val groupedRanges = range.groupBy { (bound: Bound[Any]) =>
bound.endPoint match {
case n: Number => classOf[Number]
case s: String => classOf[String]
case c: Character => classOf[String]
case _ => classOf[Any]
}
}
val optNumericRange = groupedRanges.get(classOf[Number]).map(_.asInstanceOf[InequalitySeekRange[Number]])
val optStringRange = groupedRanges.get(classOf[String]).map(_.mapBounds(_.toString))
val anyRange = groupedRanges.get(classOf[Any])
if (anyRange.nonEmpty) {
// If we get back an exclusion test, the range could return values otherwise it is empty
anyRange.get.inclusionTest[Any](BY_VALUE).map { test =>
throw new IllegalArgumentException("Cannot compare a property against values that are neither strings nor numbers.")
}.getOrElse(Iterator.empty)
} else {
(optNumericRange, optStringRange) match {
case (Some(numericRange), None) => indexSeekByNumericalRange(index, numericRange)
case (None, Some(stringRange)) => indexSeekByStringRange(index, stringRange)
case (Some(numericRange), Some(stringRange)) =>
// Consider MATCH (n:Person) WHERE n.prop < 1 AND n.prop > "London":
// The order of predicate evaluation is unspecified, i.e.
// LabelScan fby Filter(n.prop < 1) fby Filter(n.prop > "London") is a valid plan
// If the first filter returns no results, the plan returns no results.
// If the first filter returns any result, the following filter will fail since
// comparing string against numbers throws an exception. Same for the reverse case.
//
// Below we simulate this behaviour:
//
if (indexSeekByNumericalRange( index, numericRange ).isEmpty
|| indexSeekByStringRange(index, stringRange).isEmpty) {
Iterator.empty
} else {
throw new IllegalArgumentException(s"Cannot compare a property against both numbers and strings. They are incomparable.")
}
case (None, None) =>
// If we get here, the non-empty list of range bounds was partitioned into two empty ones
throw new IllegalStateException("Failed to partition range bounds")
}
}
}
private def indexSeekByPrefixRange(index: IndexDescriptor, prefix: String): scala.Iterator[Node] = {
val indexedNodes = txContext.statement.readOperations().indexQuery(index, IndexQuery.stringPrefix(index.propertyId, prefix))
JavaConversionSupport.mapToScalaENFXSafe(indexedNodes)(nodeOps.getById)
}
private def indexSeekByNumericalRange(index: IndexDescriptor, range: InequalitySeekRange[Number]): scala.Iterator[Node] = {
val readOps = txContext.statement.readOperations()
val matchingNodes: PrimitiveLongIterator = (range match {
case rangeLessThan: RangeLessThan[Number] =>
rangeLessThan.limit(BY_NUMBER).map { limit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, null, false, limit.endPoint, limit.isInclusive))
}
case rangeGreaterThan: RangeGreaterThan[Number] =>
rangeGreaterThan.limit(BY_NUMBER).map { limit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, limit.endPoint, limit.isInclusive, null, false))
}
case RangeBetween(rangeGreaterThan, rangeLessThan) =>
rangeGreaterThan.limit(BY_NUMBER).flatMap { greaterThanLimit =>
rangeLessThan.limit(BY_NUMBER).map { lessThanLimit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, greaterThanLimit.endPoint, greaterThanLimit.isInclusive, lessThanLimit.endPoint, lessThanLimit.isInclusive))
}
}
}).getOrElse(EMPTY_PRIMITIVE_LONG_COLLECTION.iterator)
JavaConversionSupport.mapToScalaENFXSafe(matchingNodes)(nodeOps.getById)
}
private def indexSeekByStringRange(index: IndexDescriptor, range: InequalitySeekRange[String]): scala.Iterator[Node] = {
val readOps = txContext.statement.readOperations()
val matchingNodes: PrimitiveLongIterator = range match {
case rangeLessThan: RangeLessThan[String] =>
rangeLessThan.limit(BY_STRING).map { limit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, null, false, limit.endPoint.asInstanceOf[String], limit.isInclusive))
}.getOrElse(EMPTY_PRIMITIVE_LONG_COLLECTION.iterator)
case rangeGreaterThan: RangeGreaterThan[String] =>
rangeGreaterThan.limit(BY_STRING).map { limit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, limit.endPoint.asInstanceOf[String], limit.isInclusive, null, false))
}.getOrElse(EMPTY_PRIMITIVE_LONG_COLLECTION.iterator)
case RangeBetween(rangeGreaterThan, rangeLessThan) =>
rangeGreaterThan.limit(BY_STRING).flatMap { greaterThanLimit =>
rangeLessThan.limit(BY_STRING).map { lessThanLimit =>
readOps.indexQuery(index, IndexQuery.range(index.propertyId, greaterThanLimit.endPoint.asInstanceOf[String], greaterThanLimit.isInclusive, lessThanLimit.endPoint.asInstanceOf[String], lessThanLimit.isInclusive))
}
}.getOrElse(EMPTY_PRIMITIVE_LONG_COLLECTION.iterator)
}
JavaConversionSupport.mapToScalaENFXSafe(matchingNodes)(nodeOps.getById)
}
override def indexScan(index: IndexDescriptor) =
mapToScalaENFXSafe(txContext.statement.readOperations().indexQuery(index, IndexQuery.exists(index.propertyId)))(nodeOps.getById)
override def indexScanByContains(index: IndexDescriptor, value: String) =
mapToScalaENFXSafe(txContext.statement.readOperations().indexQuery(index, IndexQuery.stringContains(index.propertyId, value)))(nodeOps.getById)
override def indexScanByEndsWith(index: IndexDescriptor, value: String) =
mapToScalaENFXSafe(txContext.statement.readOperations().indexQuery(index, IndexQuery.stringSuffix(index.propertyId, value)))(nodeOps.getById)
override def lockingUniqueIndexSeek(index: IndexDescriptor, value: Any): Option[Node] = {
indexSearchMonitor.lockingUniqueIndexSeek(index, value)
val nodeId = txContext.statement.readOperations().nodeGetFromUniqueIndexSeek(index, IndexQuery.exact(index.propertyId, value))
if (StatementConstants.NO_SUCH_NODE == nodeId) None else Some(nodeOps.getById(nodeId))
}
override def removeLabelsFromNode(node: Long, labelIds: Iterator[Int]): Int = labelIds.foldLeft(0) {
case (count, labelId) =>
if (txContext.statement.dataWriteOperations().nodeRemoveLabel(node, labelId)) count + 1 else count
}
override def getNodesByLabel(id: Int): Iterator[Node] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().nodesGetForLabel(id))(nodeOps.getById)
override def nodeGetDegree(node: Long, dir: SemanticDirection): Int =
txContext.statement.readOperations().nodeGetDegree(node, toGraphDb(dir))
override def nodeGetDegree(node: Long, dir: SemanticDirection, relTypeId: Int): Int =
txContext.statement.readOperations().nodeGetDegree(node, toGraphDb(dir), relTypeId)
override def nodeIsDense(node: Long): Boolean = txContext.statement.readOperations().nodeIsDense(node)
class NodeOperations extends BaseOperations[Node] {
override def delete(obj: Node) {
try {
txContext.statement.dataWriteOperations().nodeDelete(obj.getId)
} catch {
case _: api.exceptions.EntityNotFoundException => // node has been deleted by another transaction, oh well...
}
}
override def propertyKeyIds(id: Long): Iterator[Int] = try {
JavaConversionSupport.asScalaENFXSafe(txContext.statement.readOperations().nodeGetPropertyKeys(id))
} catch {
case _: api.exceptions.EntityNotFoundException => Iterator.empty
}
override def getProperty(id: Long, propertyKeyId: Int): Any = try {
txContext.statement.readOperations().nodeGetProperty(id, propertyKeyId).asObject()
} catch {
case e: api.exceptions.EntityNotFoundException =>
if (isDeletedInThisTx(id))
throw new EntityNotFoundException(s"Node with id $id has been deleted in this transaction", e)
else
null
}
override def hasProperty(id: Long, propertyKey: Int): Boolean = try {
txContext.statement.readOperations().nodeHasProperty(id, propertyKey)
} catch {
case _: api.exceptions.EntityNotFoundException => false
}
override def removeProperty(id: Long, propertyKeyId: Int): Unit = {
try {
txContext.statement.dataWriteOperations().nodeRemoveProperty(id, propertyKeyId)
} catch {
case _: api.exceptions.EntityNotFoundException => //ignore
}
}
override def setProperty(id: Long, propertyKeyId: Int, value: Any): Unit = {
try {
txContext.statement.dataWriteOperations().nodeSetProperty(id, propertyKeyId, Values.of(value) )
} catch {
case _: api.exceptions.EntityNotFoundException => //ignore
}
}
override def getById(id: Long) = try {
txContext.graph.getNodeById(id)
} catch {
case e: NotFoundException => throw new EntityNotFoundException(s"Node with id $id", e)
}
override def all: Iterator[Node] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().nodesGetAll())(getById)
override def indexGet(name: String, key: String, value: Any): Iterator[Node] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().nodeExplicitIndexGet(name, key, value))(getById)
override def indexQuery(name: String, query: Any): Iterator[Node] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().nodeExplicitIndexQuery(name, query))(getById)
override def isDeletedInThisTx(n: Node): Boolean = isDeletedInThisTx(n.getId)
def isDeletedInThisTx(id: Long): Boolean =
txContext.stateView.hasTxStateWithChanges && txContext.stateView.txState().nodeIsDeletedInThisTx(id)
override def acquireExclusiveLock(obj: Long) =
txContext.statement.readOperations().acquireExclusive(ResourceTypes.NODE, obj)
override def releaseExclusiveLock(obj: Long) =
txContext.statement.readOperations().releaseExclusive(ResourceTypes.NODE, obj)
}
class RelationshipOperations extends BaseOperations[Relationship] {
override def delete(obj: Relationship) {
try {
txContext.statement.dataWriteOperations().relationshipDelete(obj.getId)
} catch {
case _: api.exceptions.EntityNotFoundException => // node has been deleted by another transaction, oh well...
}
}
override def propertyKeyIds(id: Long): Iterator[Int] = try {
asScalaENFXSafe(txContext.statement.readOperations().relationshipGetPropertyKeys(id))
} catch {
case _: api.exceptions.EntityNotFoundException => Iterator.empty
}
override def getProperty(id: Long, propertyKeyId: Int): Any = try {
txContext.statement.readOperations().relationshipGetProperty(id, propertyKeyId).asObject()
} catch {
case e: api.exceptions.EntityNotFoundException =>
if (isDeletedInThisTx(id))
throw new EntityNotFoundException(s"Relationship with id $id has been deleted in this transaction", e)
else
null
}
override def hasProperty(id: Long, propertyKey: Int): Boolean = try {
txContext.statement.readOperations().relationshipHasProperty(id, propertyKey)
} catch {
case _: api.exceptions.EntityNotFoundException => false
}
override def removeProperty(id: Long, propertyKeyId: Int): Unit = {
try {
txContext.statement.dataWriteOperations().relationshipRemoveProperty(id, propertyKeyId)
} catch {
case _: api.exceptions.EntityNotFoundException => //ignore
}
}
override def setProperty(id: Long, propertyKeyId: Int, value: Any): Unit = {
try {
txContext.statement.dataWriteOperations().relationshipSetProperty(id, propertyKeyId, Values.of(value) )
} catch {
case _: api.exceptions.EntityNotFoundException => //ignore
}
}
override def getById(id: Long) = try {
txContext.graph.getRelationshipById(id)
} catch {
case e: NotFoundException => throw new EntityNotFoundException(s"Relationship with id $id", e)
}
override def all: Iterator[Relationship] = {
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().relationshipsGetAll())(getById)
}
override def indexGet(name: String, key: String, value: Any): Iterator[Relationship] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().relationshipExplicitIndexGet(name, key, value, -1, -1))(getById)
override def indexQuery(name: String, query: Any): Iterator[Relationship] =
JavaConversionSupport.mapToScalaENFXSafe(txContext.statement.readOperations().relationshipExplicitIndexQuery(name, query, -1, -1))(getById)
override def isDeletedInThisTx(r: Relationship): Boolean =
isDeletedInThisTx(r.getId)
def isDeletedInThisTx(id: Long): Boolean =
txContext.stateView.hasTxStateWithChanges && txContext.stateView.txState().relationshipIsDeletedInThisTx(id)
override def acquireExclusiveLock(obj: Long) =
txContext.statement.readOperations().acquireExclusive(ResourceTypes.RELATIONSHIP, obj)
override def releaseExclusiveLock(obj: Long) =
txContext.statement.readOperations().acquireExclusive(ResourceTypes.RELATIONSHIP, obj)
}
override def getOrCreatePropertyKeyId(propertyKey: String) =
txContext.statement.tokenWriteOperations().propertyKeyGetOrCreateForName(propertyKey)
abstract class BaseOperations[T <: PropertyContainer] extends Operations[T] {
def primitiveLongIteratorToScalaIterator(primitiveIterator: PrimitiveLongIterator): Iterator[Long] =
new Iterator[Long] {
override def hasNext: Boolean = primitiveIterator.hasNext
override def next(): Long = primitiveIterator.next
}
}
override def getOrCreateFromSchemaState[K, V](key: K, creator: => V) = {
val javaCreator = new java.util.function.Function[K, V]() {
override def apply(key: K) = creator
}
txContext.statement.readOperations().schemaStateGetOrCreate(key, javaCreator)
}
override def addIndexRule(labelId: Int, propertyKeyId: Int): IdempotentResult[IndexDescriptor] = try {
IdempotentResult(txContext.statement.schemaWriteOperations().indexCreate( SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
} catch {
case _: AlreadyIndexedException =>
val indexDescriptor = txContext.statement.readOperations().indexGetForSchema(SchemaDescriptorFactory.forLabel(labelId, propertyKeyId))
if(txContext.statement.readOperations().indexGetState(indexDescriptor) == InternalIndexState.FAILED)
throw new FailedIndexException(indexDescriptor.userDescription(tokenNameLookup))
IdempotentResult(indexDescriptor, wasCreated = false)
}
override def dropIndexRule(labelId: Int, propertyKeyId: Int) =
txContext.statement.schemaWriteOperations().indexDrop(IndexDescriptorFactory.forLabel( labelId, propertyKeyId ))
override def createUniqueConstraint(labelId: Int, propertyKeyId: Int): IdempotentResult[UniquenessConstraint] = try {
IdempotentResult(
txContext.statement.schemaWriteOperations().uniquePropertyConstraintCreate(
SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)))
} catch {
case existing: AlreadyConstrainedException =>
IdempotentResult(SchemaTypes.UniquenessConstraint(labelId, propertyKeyId), wasCreated = false)
}
override def dropUniqueConstraint(labelId: Int, propertyKeyId: Int) =
txContext.statement.schemaWriteOperations().constraintDrop(ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKeyId))
override def createNodePropertyExistenceConstraint(labelId: Int, propertyKeyId: Int): IdempotentResult[NodePropertyExistenceConstraint] =
try {
IdempotentResult(
txContext.statement.schemaWriteOperations().nodePropertyExistenceConstraintCreate(
SchemaDescriptorFactory.forLabel(labelId, propertyKeyId)
))
} catch {
case existing: AlreadyConstrainedException =>
IdempotentResult(SchemaTypes.NodePropertyExistenceConstraint(labelId, propertyKeyId), wasCreated = false)
}
override def dropNodePropertyExistenceConstraint(labelId: Int, propertyKeyId: Int) =
txContext.statement.schemaWriteOperations().constraintDrop(ConstraintDescriptorFactory.existsForLabel(labelId, propertyKeyId))
override def createRelationshipPropertyExistenceConstraint(relTypeId: Int, propertyKeyId: Int): IdempotentResult[RelationshipPropertyExistenceConstraint] =
try {
IdempotentResult(
txContext.statement.schemaWriteOperations().relationshipPropertyExistenceConstraintCreate(
SchemaDescriptorFactory.forRelType(relTypeId, propertyKeyId)
))
} catch {
case existing: AlreadyConstrainedException =>
IdempotentResult(SchemaTypes.RelationshipPropertyExistenceConstraint(relTypeId, propertyKeyId), wasCreated = false)
}
override def dropRelationshipPropertyExistenceConstraint(relTypeId: Int, propertyKeyId: Int) =
txContext.statement.schemaWriteOperations().constraintDrop(ConstraintDescriptorFactory.existsForRelType(relTypeId,propertyKeyId))
override def getImportURL(url: URL): Either[String,URL] = txContext.graph match {
case db: GraphDatabaseQueryService =>
try {
Right(db.validateURLAccess(url))
} catch {
case error: URLAccessValidationError => Left(error.getMessage)
}
}
override def relationshipStartNode(rel: Relationship) = rel.getStartNode
override def relationshipEndNode(rel: Relationship) = rel.getEndNode
private lazy val tokenNameLookup = new StatementTokenNameLookup(txContext.statement.readOperations())
// Legacy dependency between kernel and compiler
override def variableLengthPathExpand(node: PatternNode,
realNode: Node,
minHops: Option[Int],
maxHops: Option[Int],
direction: SemanticDirection,
relTypes: Seq[String]): Iterator[Path] = {
val depthEval = (minHops, maxHops) match {
case (None, None) => Evaluators.fromDepth(1)
case (Some(min), None) => Evaluators.fromDepth(min)
case (None, Some(max)) => Evaluators.includingDepths(1, max)
case (Some(min), Some(max)) => Evaluators.includingDepths(min, max)
}
// The RULE compiler makes use of older kernel API capabilities for variable length expanding
// TODO: Consider re-writing this using similar code to the COST var-length expand
val baseTraversalDescription: TraversalDescription = txContext.graph.asInstanceOf[GraphDatabaseCypherService]
.getGraphDatabaseService
.traversalDescription()
.evaluator(depthEval)
.uniqueness(Uniqueness.RELATIONSHIP_PATH)
val traversalDescription = if (relTypes.isEmpty) {
baseTraversalDescription.expand(PathExpanderBuilder.allTypes(toGraphDb(direction)).build())
} else {
val emptyExpander = PathExpanderBuilder.empty()
val expander = relTypes.foldLeft(emptyExpander) {
case (e, t) => e.add(RelationshipType.withName(t), toGraphDb(direction))
}
baseTraversalDescription.expand(expander.build())
}
traversalDescription.traverse(realNode).iterator().asScala
}
override def nodeCountByCountStore(labelId: Int): Long = {
txContext.statement.readOperations().countsForNode(labelId)
}
override def relationshipCountByCountStore(startLabelId: Int, typeId: Int, endLabelId: Int): Long = {
txContext.statement.readOperations().countsForRelationship(startLabelId, typeId, endLabelId)
}
override def lockNodes(nodeIds: Long*) =
nodeIds.sorted.foreach(txContext.statement.readOperations().acquireExclusive(ResourceTypes.NODE, _))
override def lockRelationships(relIds: Long*) =
relIds.sorted.foreach(txContext.statement.readOperations().acquireExclusive(ResourceTypes.RELATIONSHIP, _))
override def singleShortestPath(left: Node, right: Node, depth: Int, expander: expressions.Expander,
pathPredicate: KernelPredicate[Path],
filters: Seq[KernelPredicate[PropertyContainer]]): Option[Path] = {
val pathFinder = buildPathFinder(depth, expander, pathPredicate, filters)
Option(pathFinder.findSinglePath(left, right))
}
override def allShortestPath(left: Node, right: Node, depth: Int, expander: expressions.Expander,
pathPredicate: KernelPredicate[Path],
filters: Seq[KernelPredicate[PropertyContainer]]): scala.Iterator[Path] = {
val pathFinder = buildPathFinder(depth, expander, pathPredicate, filters)
pathFinder.findAllPaths(left, right).iterator().asScala
}
type KernelProcedureCall = (KernelQualifiedName, Array[AnyRef]) => RawIterator[Array[AnyRef], ProcedureException]
type KernelFunctionCall = (KernelQualifiedName, Array[AnyRef]) => AnyRef
private def shouldElevate(allowed: Array[String]): Boolean = {
// We have to be careful with elevation, since we cannot elevate permissions in a nested procedure call
// above the original allowed procedure mode. We enforce this by checking if mode is already an overridden mode.
val accessMode = txContext.securityContext.mode()
allowed.nonEmpty && !accessMode.isOverridden && accessMode.allowsProcedureWith(allowed)
}
override def callReadOnlyProcedure(name: QualifiedName, args: Seq[Any], allowed: Array[String]) = {
val call: KernelProcedureCall =
if (shouldElevate(allowed))
txContext.statement.procedureCallOperations.procedureCallReadOverride(_, _)
else
txContext.statement.procedureCallOperations.procedureCallRead(_, _)
callProcedure(name, args, call)
}
override def callReadWriteProcedure(name: QualifiedName, args: Seq[Any], allowed: Array[String]) = {
val call: KernelProcedureCall =
if (shouldElevate(allowed))
txContext.statement.procedureCallOperations.procedureCallWriteOverride(_, _)
else
txContext.statement.procedureCallOperations.procedureCallWrite(_, _)
callProcedure(name, args, call)
}
override def callSchemaWriteProcedure(name: QualifiedName, args: Seq[Any], allowed: Array[String]) = {
val call: KernelProcedureCall =
if (shouldElevate(allowed))
txContext.statement.procedureCallOperations.procedureCallSchemaOverride(_, _)
else
txContext.statement.procedureCallOperations.procedureCallSchema(_, _)
callProcedure(name, args, call)
}
override def callDbmsProcedure(name: QualifiedName, args: Seq[Any], allowed: Array[String]) = {
callProcedure(name, args, txContext.dbmsOperations.procedureCallDbms(_,_,txContext.securityContext))
}
private def callProcedure(name: QualifiedName, args: Seq[Any], call: KernelProcedureCall) = {
val kn = new KernelQualifiedName(name.namespace.asJava, name.name)
val toArray = args.map(_.asInstanceOf[AnyRef]).toArray
val read = call(kn, toArray)
new scala.Iterator[Array[AnyRef]] {
override def hasNext: Boolean = read.hasNext
override def next(): Array[AnyRef] = read.next
}
}
override def callFunction(name: QualifiedName, args: Seq[Any], allowed: Array[String]) = {
val call: KernelFunctionCall =
if (shouldElevate(allowed))
txContext.statement.procedureCallOperations.functionCallOverride(_, _)
else
txContext.statement.procedureCallOperations.functionCall(_, _)
callFunction(name, args, call)
}
private def callFunction(name: QualifiedName, args: Seq[Any],
call: KernelFunctionCall) = {
val kn = new KernelQualifiedName(name.namespace.asJava, name.name)
val toArray = args.map(_.asInstanceOf[AnyRef]).toArray
call(kn, toArray)
}
override def isGraphKernelResultValue(v: Any): Boolean = v.isInstanceOf[PropertyContainer] || v.isInstanceOf[Path]
private def buildPathFinder(depth: Int, expander: expressions.Expander, pathPredicate: KernelPredicate[Path],
filters: Seq[KernelPredicate[PropertyContainer]]): ShortestPath = {
val startExpander = expander match {
case OnlyDirectionExpander(_, _, dir) =>
PathExpanderBuilder.allTypes(toGraphDb(dir))
case TypeAndDirectionExpander(_,_,typDirs) =>
typDirs.foldLeft(PathExpanderBuilder.empty()) {
case (acc, (typ, dir)) => acc.add(RelationshipType.withName(typ), toGraphDb(dir))
}
}
val expanderWithNodeFilters = expander.nodeFilters.foldLeft(startExpander) {
case (acc, filter) => acc.addNodeFilter(new Predicate[PropertyContainer] {
override def test(t: PropertyContainer): Boolean = filter.test(t)
})
}
val expanderWithAllPredicates = expander.relFilters.foldLeft(expanderWithNodeFilters) {
case (acc, filter) => acc.addRelationshipFilter(new Predicate[PropertyContainer] {
override def test(t: PropertyContainer): Boolean = filter.test(t)
})
}
val shortestPathPredicate = new ShortestPathPredicate {
override def test(path: Path): Boolean = pathPredicate.test(path)
}
new ShortestPath(depth, expanderWithAllPredicates.build(), shortestPathPredicate) {
override protected def filterNextLevelNodes(nextNode: Node): Node =
if (filters.isEmpty) nextNode
else if (filters.forall(filter => filter test nextNode)) nextNode
else null
}
}
override def detachDeleteNode(node: Node): Int = {
try {
txContext.statement.dataWriteOperations().nodeDetachDelete(node.getId)
} catch {
case _: api.exceptions.EntityNotFoundException => 0 // node has been deleted by another transaction, oh well...
}
}
override def assertSchemaWritesAllowed(): Unit =
txContext.statement.schemaWriteOperations()
override def transactionalContext: QueryTransactionalContext = new QueryTransactionalContext {
override type ReadOps = ReadOperations
override type DbmsOps = DbmsOperations
override def commitAndRestartTx(): Unit = txContext.commitAndRestartTx()
override def readOperations: ReadOps = txContext.tc.readOperations()
override def isTopLevelTx: Boolean = txContext.isTopLevelTx
override def dbmsOperations: DbmsOperations = txContext.tc.dbmsOperations()
override def close(success: Boolean): Unit = txContext.tc.close(success)
}
}
object TransactionBoundQueryContext {
trait IndexSearchMonitor {
def indexSeek(index: IndexDescriptor, value: Any): Unit
def lockingUniqueIndexSeek(index: IndexDescriptor, value: Any): Unit
}
}