-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StateHandlingStatementOperations.java
1679 lines (1499 loc) · 67.3 KB
/
StateHandlingStatementOperations.java
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.kernel.impl.api;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntCollection;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveIntStack;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongResourceIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.kernel.api.DataWriteOperations;
import org.neo4j.kernel.api.LegacyIndex;
import org.neo4j.kernel.api.LegacyIndexHits;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.legacyindex.AutoIndexingKernelException;
import org.neo4j.kernel.api.exceptions.legacyindex.LegacyIndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException;
import org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException;
import org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.exceptions.schema.TooManyLabelsException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.legacyindex.AutoIndexing;
import org.neo4j.kernel.api.properties.DefinedProperty;
import org.neo4j.kernel.api.properties.Property;
import org.neo4j.kernel.api.properties.PropertyKeyIdIterator;
import org.neo4j.kernel.api.schema.IndexQuery;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.OrderedPropertyValues;
import org.neo4j.kernel.api.schema.RelationTypeSchemaDescriptor;
import org.neo4j.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.schema.SchemaUtil;
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptorFactory;
import org.neo4j.kernel.api.schema.constaints.IndexBackedConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.NodeExistenceConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.NodeKeyConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.RelExistenceConstraintDescriptor;
import org.neo4j.kernel.api.schema.constaints.UniquenessConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptorFactory;
import org.neo4j.kernel.api.txstate.TransactionCountingStateVisitor;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.operations.CountsOperations;
import org.neo4j.kernel.impl.api.operations.EntityOperations;
import org.neo4j.kernel.impl.api.operations.KeyReadOperations;
import org.neo4j.kernel.impl.api.operations.KeyWriteOperations;
import org.neo4j.kernel.impl.api.operations.LegacyIndexReadOperations;
import org.neo4j.kernel.impl.api.operations.LegacyIndexWriteOperations;
import org.neo4j.kernel.impl.api.operations.SchemaReadOperations;
import org.neo4j.kernel.impl.api.operations.SchemaWriteOperations;
import org.neo4j.kernel.impl.api.state.ConstraintIndexCreator;
import org.neo4j.kernel.impl.api.state.IndexTxStateUpdater;
import org.neo4j.kernel.impl.api.store.RelationshipIterator;
import org.neo4j.kernel.impl.index.IndexEntityType;
import org.neo4j.kernel.impl.index.LegacyIndexStore;
import org.neo4j.register.Register.DoubleLongRegister;
import org.neo4j.storageengine.api.Direction;
import org.neo4j.storageengine.api.EntityType;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.PropertyItem;
import org.neo4j.storageengine.api.RelationshipItem;
import org.neo4j.storageengine.api.StorageProperty;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.Token;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.PopulationProgress;
import org.neo4j.storageengine.api.txstate.NodeState;
import org.neo4j.storageengine.api.txstate.ReadableDiffSets;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.RelationshipState;
import static java.lang.String.format;
import static org.neo4j.collection.primitive.PrimitiveIntCollections.filter;
import static org.neo4j.collection.primitive.PrimitiveLongCollections.resourceIterator;
import static org.neo4j.collection.primitive.PrimitiveLongCollections.single;
import static org.neo4j.helpers.collection.Iterators.filter;
import static org.neo4j.helpers.collection.Iterators.iterator;
import static org.neo4j.helpers.collection.Iterators.singleOrNull;
import static org.neo4j.kernel.api.StatementConstants.NO_SUCH_NODE;
import static org.neo4j.kernel.api.properties.DefinedProperty.NO_SUCH_PROPERTY;
import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.ADDED_LABEL;
import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.REMOVED_LABEL;
import static org.neo4j.register.Registers.newDoubleLongRegister;
import static org.neo4j.storageengine.api.txstate.TxStateVisitor.EMPTY;
public class StateHandlingStatementOperations
implements KeyReadOperations, KeyWriteOperations, EntityOperations, SchemaReadOperations, SchemaWriteOperations,
CountsOperations, LegacyIndexReadOperations, LegacyIndexWriteOperations
{
private final StoreReadLayer storeLayer;
private final AutoIndexing autoIndexing;
private final ConstraintIndexCreator constraintIndexCreator;
private final LegacyIndexStore legacyIndexStore;
private final IndexTxStateUpdater indexTxStateUpdater;
public StateHandlingStatementOperations( StoreReadLayer storeLayer, AutoIndexing propertyTrackers,
ConstraintIndexCreator constraintIndexCreator, LegacyIndexStore legacyIndexStore )
{
this.storeLayer = storeLayer;
this.autoIndexing = propertyTrackers;
this.constraintIndexCreator = constraintIndexCreator;
this.legacyIndexStore = legacyIndexStore;
this.indexTxStateUpdater = new IndexTxStateUpdater( storeLayer, this );
}
// <Cursors>
@Override
public Cursor<NodeItem> nodeGetAllCursor( KernelStatement statement )
{
TransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.nodeGetAllCursor( statement.getStoreStatement(), state );
}
@Override
public Cursor<NodeItem> nodeCursorById( KernelStatement statement, long nodeId ) throws EntityNotFoundException
{
Cursor<NodeItem> node = nodeCursor( statement, nodeId );
if ( !node.next() )
{
node.close();
throw new EntityNotFoundException( EntityType.NODE, nodeId );
}
return node;
}
private Cursor<NodeItem> nodeCursor( KernelStatement statement, long nodeId )
{
TransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.nodeCursor( statement.getStoreStatement(), nodeId, state );
}
@Override
public Cursor<RelationshipItem> relationshipCursorById( KernelStatement statement, long relationshipId )
throws EntityNotFoundException
{
Cursor<RelationshipItem> relationship = relationshipCursor( statement, relationshipId );
if ( !relationship.next() )
{
relationship.close();
throw new EntityNotFoundException( EntityType.RELATIONSHIP, relationshipId );
}
return relationship;
}
private Cursor<RelationshipItem> relationshipCursor( KernelStatement statement, long relationshipId )
{
ReadableTransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.relationshipCursor( statement.getStoreStatement(), relationshipId, state );
}
@Override
public Cursor<RelationshipItem> relationshipCursorGetAll( KernelStatement statement )
{
ReadableTransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.relationshipsGetAllCursor( statement.getStoreStatement(), state );
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node,
Direction direction )
{
ReadableTransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.nodeGetRelationships( statement.getStoreStatement(), node, direction, state );
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node, Direction direction,
int[] relTypes )
{
ReadableTransactionState state = statement.hasTxStateWithChanges() ? statement.txState() : null;
return storeLayer.nodeGetRelationships( statement.getStoreStatement(), node, direction, relTypes, state );
}
@Override
public Cursor<PropertyItem> nodeGetProperties( KernelStatement statement, NodeItem node )
{
NodeState state = statement.hasTxStateWithChanges() ? statement.txState().getNodeState( node.id() ) : null;
return storeLayer.nodeGetProperties( statement.getStoreStatement(), node, state );
}
@Override
public PrimitiveIntCollection nodeGetPropertyKeys( KernelStatement statement, NodeItem node )
{
PrimitiveIntStack keys = new PrimitiveIntStack();
try ( Cursor<PropertyItem> properties = nodeGetProperties( statement, node ) )
{
while ( properties.next() )
{
keys.push( properties.get().propertyKeyId() );
}
}
return keys;
}
@Override
public Object nodeGetProperty( KernelStatement statement, NodeItem node, int propertyKeyId )
{
try ( Cursor<PropertyItem> cursor = nodeGetPropertyCursor( statement, node, propertyKeyId ) )
{
if ( cursor.next() )
{
return cursor.get().value();
}
}
catch ( NotFoundException e )
{
return null;
}
return null;
}
@Override
public boolean nodeHasProperty( KernelStatement statement, NodeItem node, int propertyKeyId )
{
try ( Cursor<PropertyItem> cursor = nodeGetPropertyCursor( statement, node, propertyKeyId ) )
{
return cursor.next();
}
}
private Cursor<PropertyItem> nodeGetPropertyCursor( KernelStatement statement, NodeItem node, int propertyKeyId )
{
NodeState state = statement.hasTxStateWithChanges() ? statement.txState().getNodeState( node.id() ) : null;
return storeLayer.nodeGetProperty( statement.getStoreStatement(), node, propertyKeyId, state );
}
@Override
public Cursor<PropertyItem> relationshipGetProperties( KernelStatement statement, RelationshipItem relationship )
{
RelationshipState state = statement.hasTxStateWithChanges()
? statement.txState().getRelationshipState( relationship.id() )
: null;
return storeLayer.relationshipGetProperties( statement.getStoreStatement(), relationship, state );
}
@Override
public PrimitiveIntCollection relationshipGetPropertyKeys( KernelStatement statement,
RelationshipItem relationship )
{
PrimitiveIntStack keys = new PrimitiveIntStack();
try ( Cursor<PropertyItem> properties = relationshipGetProperties( statement, relationship ) )
{
while ( properties.next() )
{
keys.push( properties.get().propertyKeyId() );
}
}
return keys;
}
@Override
public Object relationshipGetProperty( KernelStatement statement, RelationshipItem relationship, int propertyKeyId )
{
try ( Cursor<PropertyItem> cursor = relationshipGetPropertyCursor( statement, relationship, propertyKeyId ) )
{
if ( cursor.next() )
{
return cursor.get().value();
}
}
catch ( NotFoundException e )
{
return null;
}
return null;
}
@Override
public boolean relationshipHasProperty( KernelStatement statement, RelationshipItem relationship,
int propertyKeyId )
{
try ( Cursor<PropertyItem> cursor = relationshipGetPropertyCursor( statement, relationship, propertyKeyId ) )
{
return cursor.next();
}
}
private Cursor<PropertyItem> relationshipGetPropertyCursor( KernelStatement statement,
RelationshipItem relationship, int propertyKeyId )
{
RelationshipState state = statement.hasTxStateWithChanges()
? statement.txState().getRelationshipState( relationship.id() )
: null;
return storeLayer.relationshipGetProperty( statement.getStoreStatement(), relationship, propertyKeyId, state );
}
// </Cursors>
@Override
public long nodeCreate( KernelStatement state )
{
long nodeId = storeLayer.reserveNode();
state.txState().nodeDoCreate( nodeId );
return nodeId;
}
@Override
public void nodeDelete( KernelStatement state, long nodeId )
throws AutoIndexingKernelException, EntityNotFoundException, InvalidTransactionTypeKernelException
{
autoIndexing.nodes().entityRemoved( state.dataWriteOperations(), nodeId );
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
state.txState().nodeDoDelete( cursor.get().id() );
}
}
@Override
public int nodeDetachDelete( KernelStatement state, long nodeId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
nodeDelete( state, nodeId );
return 0;
}
@Override
public long relationshipCreate( KernelStatement state, int relationshipTypeId, long startNodeId, long endNodeId )
throws EntityNotFoundException
{
try ( Cursor<NodeItem> startNode = nodeCursorById( state, startNodeId ) )
{
try ( Cursor<NodeItem> endNode = nodeCursorById( state, endNodeId ) )
{
long id = storeLayer.reserveRelationship();
state.txState()
.relationshipDoCreate( id, relationshipTypeId, startNode.get().id(), endNode.get().id() );
return id;
}
}
}
@Override
public void relationshipDelete( final KernelStatement state, long relationshipId )
throws EntityNotFoundException, InvalidTransactionTypeKernelException, AutoIndexingKernelException
{
try ( Cursor<RelationshipItem> cursor = relationshipCursorById( state, relationshipId ) )
{
RelationshipItem relationship = cursor.get();
// NOTE: We implicitly delegate to neoStoreTransaction via txState.legacyState here. This is because that
// call returns modified properties, which node manager uses to update legacy tx state. This will be cleaned up
// once we've removed legacy tx state.
autoIndexing.relationships().entityRemoved( state.dataWriteOperations(), relationshipId );
final TransactionState txState = state.txState();
if ( txState.relationshipIsAddedInThisTx( relationship.id() ) )
{
txState.relationshipDoDeleteAddedInThisTx( relationship.id() );
}
else
{
txState.relationshipDoDelete( relationship.id(), relationship.type(), relationship.startNode(),
relationship.endNode() );
}
}
}
@Override
public PrimitiveLongIterator nodesGetAll( KernelStatement state )
{
PrimitiveLongIterator iterator = storeLayer.nodesGetAll();
return state.hasTxStateWithChanges() ? state.txState().augmentNodesGetAll( iterator ) : iterator;
}
@Override
public RelationshipIterator relationshipsGetAll( KernelStatement state )
{
RelationshipIterator iterator = storeLayer.relationshipsGetAll();
return state.hasTxStateWithChanges() ? state.txState().augmentRelationshipsGetAll( iterator ) : iterator;
}
@Override
public boolean nodeAddLabel( KernelStatement state, long nodeId, int labelId ) throws EntityNotFoundException
{
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
if ( node.hasLabel( labelId ) )
{
// Label is already in state or in store, no-op
return false;
}
state.txState().nodeDoAddLabel( labelId, node.id() );
indexTxStateUpdater.onLabelChange( state, labelId, node, ADDED_LABEL );
return true;
}
}
@Override
public boolean nodeRemoveLabel( KernelStatement state, long nodeId, int labelId ) throws EntityNotFoundException
{
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
if ( !node.hasLabel( labelId ) )
{
// Label does not exist in state or in store, no-op
return false;
}
state.txState().nodeDoRemoveLabel( labelId, node.id() );
indexTxStateUpdater.onLabelChange( state, labelId, node, REMOVED_LABEL );
return true;
}
}
@Override
public PrimitiveLongIterator nodesGetForLabel( KernelStatement state, int labelId )
{
if ( state.hasTxStateWithChanges() )
{
PrimitiveLongIterator wLabelChanges = state.txState().nodesWithLabelChanged( labelId )
.augment( storeLayer.nodesGetForLabel( state.getStoreStatement(), labelId ) );
return state.txState().addedAndRemovedNodes().augmentWithRemovals( wLabelChanges );
}
return storeLayer.nodesGetForLabel( state.getStoreStatement(), labelId );
}
@Override
public long nodesGetCount( KernelStatement state )
{
long base = storeLayer.nodesGetCount();
return state.hasTxStateWithChanges() ? base + state.txState().addedAndRemovedNodes().delta() : base;
}
@Override
public long relationshipsGetCount( KernelStatement state )
{
long base = storeLayer.relationshipsGetCount();
return state.hasTxStateWithChanges() ? base + state.txState().addedAndRemovedRelationships().delta() : base;
}
@Override
public IndexDescriptor indexCreate( KernelStatement state, LabelSchemaDescriptor descriptor )
{
IndexDescriptor indexDescriptor = IndexDescriptorFactory.forSchema( descriptor );
state.txState().indexRuleDoAdd( indexDescriptor );
return indexDescriptor;
}
@Override
public void indexDrop( KernelStatement state, IndexDescriptor descriptor ) throws DropIndexFailureException
{
state.txState().indexDoDrop( descriptor );
}
@Override
public void uniqueIndexDrop( KernelStatement state, IndexDescriptor descriptor ) throws DropIndexFailureException
{
state.txState().indexDoDrop( descriptor );
}
private IndexBackedConstraintDescriptor indexBackedConstraintCreate( KernelStatement state, IndexBackedConstraintDescriptor constraint )
throws CreateConstraintFailureException
{
LabelSchemaDescriptor descriptor = constraint.schema();
try
{
if ( state.hasTxStateWithChanges() &&
state.txState().indexDoUnRemove( constraint.ownedIndexDescriptor() ) ) // ..., DROP, *CREATE*
{ // creation is undoing a drop
if ( !state.txState().constraintDoUnRemove( constraint ) ) // CREATE, ..., DROP, *CREATE*
{ // ... the drop we are undoing did itself undo a prior create...
state.txState()
.constraintDoAdd( constraint, state.txState().indexCreatedForConstraint( constraint ) );
}
}
else // *CREATE*
{ // create from scratch
Iterator<ConstraintDescriptor> it = storeLayer.constraintsGetForSchema( descriptor );
while ( it.hasNext() )
{
if ( it.next().equals( constraint ) )
{
return constraint;
}
}
long indexId = constraintIndexCreator.createUniquenessConstraintIndex( state, this, descriptor );
state.txState().constraintDoAdd( constraint, indexId );
}
return constraint;
}
catch ( UniquePropertyValueValidationException | DropIndexFailureException | TransactionFailureException e )
{
throw new CreateConstraintFailureException( constraint, e );
}
}
@Override
public NodeKeyConstraintDescriptor nodeKeyConstraintCreate( KernelStatement state,
LabelSchemaDescriptor descriptor )
throws CreateConstraintFailureException
{
NodeKeyConstraintDescriptor constraint = ConstraintDescriptorFactory.nodeKeyForSchema( descriptor );
indexBackedConstraintCreate( state, constraint );
return constraint;
}
@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate( KernelStatement state, LabelSchemaDescriptor descriptor )
throws CreateConstraintFailureException
{
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema( descriptor );
indexBackedConstraintCreate( state, constraint );
return constraint;
}
@Override
public NodeExistenceConstraintDescriptor nodePropertyExistenceConstraintCreate(
KernelStatement state,
LabelSchemaDescriptor descriptor
) throws CreateConstraintFailureException
{
NodeExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema( descriptor );
state.txState().constraintDoAdd( constraint );
return constraint;
}
@Override
public RelExistenceConstraintDescriptor relationshipPropertyExistenceConstraintCreate(
KernelStatement state,
RelationTypeSchemaDescriptor descriptor
) throws AlreadyConstrainedException, CreateConstraintFailureException
{
RelExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema( descriptor );
state.txState().constraintDoAdd( constraint );
return constraint;
}
@Override
public Iterator<ConstraintDescriptor> constraintsGetForSchema( KernelStatement state, SchemaDescriptor descriptor )
{
Iterator<ConstraintDescriptor> constraints = storeLayer.constraintsGetForSchema( descriptor );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForSchema( descriptor ).apply( constraints );
}
return constraints;
}
@Override
public boolean constraintExists( KernelStatement state, ConstraintDescriptor descriptor )
{
boolean inStore = storeLayer.constraintExists( descriptor );
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<ConstraintDescriptor> diffSet =
state.txState().constraintsChangesForSchema( descriptor.schema() );
return diffSet.isAdded( descriptor ) || (inStore && !diffSet.isRemoved( descriptor ));
}
return inStore;
}
@Override
public Iterator<ConstraintDescriptor> constraintsGetForLabel( KernelStatement state, int labelId )
{
Iterator<ConstraintDescriptor> constraints = storeLayer.constraintsGetForLabel( labelId );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForLabel( labelId ).apply( constraints );
}
return constraints;
}
@Override
public Iterator<ConstraintDescriptor> constraintsGetForRelationshipType( KernelStatement state,
int typeId )
{
Iterator<ConstraintDescriptor> constraints = storeLayer.constraintsGetForRelationshipType( typeId );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForRelationshipType( typeId ).apply( constraints );
}
return constraints;
}
@Override
public Iterator<ConstraintDescriptor> constraintsGetAll( KernelStatement state )
{
Iterator<ConstraintDescriptor> constraints = storeLayer.constraintsGetAll();
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChanges().apply( constraints );
}
return constraints;
}
@Override
public void constraintDrop( KernelStatement state, ConstraintDescriptor constraint )
{
state.txState().constraintDoDrop( constraint );
}
@Override
public IndexDescriptor indexGetForSchema( KernelStatement state, LabelSchemaDescriptor descriptor )
{
IndexDescriptor indexDescriptor = storeLayer.indexGetForSchema( descriptor );
Iterator<IndexDescriptor> rules = iterator( indexDescriptor );
if ( state.hasTxStateWithChanges() )
{
rules = filter(
SchemaDescriptor.equalTo( descriptor ),
state.txState().indexDiffSetsByLabel( descriptor.getLabelId() ).apply( rules ) );
}
return singleOrNull( rules );
}
@Override
public InternalIndexState indexGetState( KernelStatement state, IndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
// If index is in our state, then return populating
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().getLabelId() ) ) )
{
return InternalIndexState.POPULATING;
}
}
return storeLayer.indexGetState( descriptor );
}
@Override
public PopulationProgress indexGetPopulationProgress( KernelStatement state, IndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
// If index is in our state, then return 0%
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().getLabelId() ) ) )
{
return PopulationProgress.NONE;
}
}
return storeLayer.indexGetPopulationProgress( descriptor.schema() );
}
private boolean checkIndexState( IndexDescriptor index, ReadableDiffSets<IndexDescriptor> diffSet )
throws IndexNotFoundKernelException
{
if ( diffSet.isAdded( index ) )
{
return true;
}
if ( diffSet.isRemoved( index ) )
{
throw new IndexNotFoundKernelException( format( "Index on %s has been dropped in this transaction.",
index.userDescription( SchemaUtil.idTokenNameLookup ) ) );
}
return false;
}
@Override
public Iterator<IndexDescriptor> indexesGetForLabel( KernelStatement state, int labelId )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexDiffSetsByLabel( labelId )
.apply( storeLayer.indexesGetForLabel( labelId ) );
}
return storeLayer.indexesGetForLabel( labelId );
}
@Override
public Iterator<IndexDescriptor> indexesGetAll( KernelStatement state )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexChanges().apply( storeLayer.indexesGetAll() );
}
return storeLayer.indexesGetAll();
}
@Override
public long nodeGetFromUniqueIndexSeek( KernelStatement statement, IndexDescriptor index,
IndexQuery.ExactPredicate... query )
throws IndexNotFoundKernelException, IndexBrokenKernelException, IndexNotApplicableKernelException
{
IndexReader reader = storeLayer.indexGetFreshReader( statement.getStoreStatement(), index );
/* Here we have an intricate scenario where we need to return the PrimitiveLongIterator
* since subsequent filtering will happen outside, but at the same time have the ability to
* close the IndexReader when done iterating over the lookup result. This is because we get
* a fresh reader that isn't associated with the current transaction and hence will not be
* automatically closed. */
PrimitiveLongResourceIterator committed = resourceIterator( reader.query( query ), reader );
PrimitiveLongIterator exactMatches = LookupFilter.exactIndexMatches( this, statement, committed, query );
PrimitiveLongIterator changesFiltered =
filterIndexStateChangesForSeek( statement, exactMatches, index, OrderedPropertyValues.of( query ) );
return single( resourceIterator( changesFiltered, committed ), NO_SUCH_NODE );
}
@Override
public PrimitiveLongIterator indexQuery( KernelStatement statement, IndexDescriptor index,
IndexQuery... predicates ) throws IndexNotFoundKernelException, IndexNotApplicableKernelException
{
IndexReader reader = storeLayer.indexGetReader( statement.getStoreStatement(), index );
PrimitiveLongIterator committed = reader.query( predicates );
PrimitiveLongIterator exactMatches = LookupFilter.exactIndexMatches( this, statement, committed, predicates );
IndexQuery firstPredicate = predicates[0];
switch ( firstPredicate.type() )
{
case exact:
IndexQuery.ExactPredicate[] exactPreds = assertOnlyExactPredicates( predicates );
return filterIndexStateChangesForSeek( statement, exactMatches, index, OrderedPropertyValues.of( exactPreds ) );
case stringSuffix:
case stringContains:
case exists:
return filterIndexStateChangesForScan( statement, exactMatches, index );
case rangeNumeric:
{
assertSinglePredicate( predicates );
IndexQuery.NumberRangePredicate numPred = (IndexQuery.NumberRangePredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByNumber( statement, index, numPred.from(),
numPred.fromInclusive(), numPred.to(), numPred.toInclusive(), exactMatches );
}
case rangeString:
{
assertSinglePredicate( predicates );
IndexQuery.StringRangePredicate strPred = (IndexQuery.StringRangePredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByString(
statement, index, strPred.from(), strPred.fromInclusive(), strPred.to(),
strPred.toInclusive(), committed );
}
case stringPrefix:
{
assertSinglePredicate( predicates );
IndexQuery.StringPrefixPredicate strPred = (IndexQuery.StringPrefixPredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByPrefix( statement, index, strPred.prefix(), committed );
}
default:
throw new UnsupportedOperationException( "Query not supported: " + Arrays.toString( predicates ) );
}
}
private IndexQuery.ExactPredicate[] assertOnlyExactPredicates( IndexQuery[] predicates )
{
IndexQuery.ExactPredicate[] exactPredicates;
if ( predicates.getClass() == IndexQuery.ExactPredicate[].class )
{
exactPredicates = (IndexQuery.ExactPredicate[]) predicates;
}
else
{
exactPredicates = new IndexQuery.ExactPredicate[predicates.length];
for ( int i = 0; i < predicates.length; i++ )
{
if ( predicates[i] instanceof IndexQuery.ExactPredicate )
{
exactPredicates[i] = (IndexQuery.ExactPredicate) predicates[i];
}
else
{
// TODO: what to throw?
throw new UnsupportedOperationException( "Query not supported: " + Arrays.toString( predicates ) );
}
}
}
return exactPredicates;
}
private void assertSinglePredicate( IndexQuery[] predicates )
{
if ( predicates.length != 1 )
{
// TODO: what to throw?
throw new UnsupportedOperationException( "Query not supported: " + Arrays.toString( predicates ) );
}
}
@Override
public long nodesCountIndexed( KernelStatement statement, IndexDescriptor index, long nodeId, Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
IndexReader reader = storeLayer.indexGetReader( statement.getStoreStatement(), index );
return reader.countIndexedNodes( nodeId, value );
}
private PrimitiveLongIterator filterIndexStateChangesForScan(
KernelStatement state, PrimitiveLongIterator nodeIds, IndexDescriptor index )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChanges =
state.txState().indexUpdatesForScan( index );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChanges.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongIterator filterIndexStateChangesForSeek(
KernelStatement state, PrimitiveLongIterator nodeIds, IndexDescriptor index,
OrderedPropertyValues propertyValues )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChanges = state.txState().indexUpdatesForSeek( index, propertyValues );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChanges.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongIterator filterIndexStateChangesForRangeSeekByNumber( KernelStatement state,
IndexDescriptor index,
Number lower, boolean includeLower,
Number upper, boolean includeUpper,
PrimitiveLongIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChangesForNumber =
state.txState().indexUpdatesForRangeSeekByNumber( index, lower, includeLower, upper, includeUpper );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChangesForNumber.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongIterator filterIndexStateChangesForRangeSeekByString( KernelStatement state,
IndexDescriptor index,
String lower, boolean includeLower,
String upper, boolean includeUpper,
PrimitiveLongIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChangesForString =
state.txState().indexUpdatesForRangeSeekByString( index, lower, includeLower, upper, includeUpper );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChangesForString.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongIterator filterIndexStateChangesForRangeSeekByPrefix( KernelStatement state,
IndexDescriptor index,
String prefix,
PrimitiveLongIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChangesForPrefix =
state.txState().indexUpdatesForRangeSeekByPrefix( index, prefix );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChangesForPrefix.augment( nodeIds ) );
}
return nodeIds;
}
@Override
public Property nodeSetProperty( KernelStatement state, long nodeId, DefinedProperty property )
throws EntityNotFoundException, InvalidTransactionTypeKernelException, AutoIndexingKernelException
{
DataWriteOperations ops = state.dataWriteOperations();
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
DefinedProperty existingProperty = NO_SUCH_PROPERTY;
try ( Cursor<PropertyItem> properties = nodeGetPropertyCursor( state, node, property.propertyKeyId() ) )
{
if ( !properties.next() )
{
autoIndexing.nodes().propertyAdded( ops, nodeId, property );
}
else
{
existingProperty = Property.property( properties.get().propertyKeyId(), properties.get().value() );
autoIndexing.nodes().propertyChanged( ops, nodeId, existingProperty, property );
}
}
if ( existingProperty == NO_SUCH_PROPERTY )
{
state.txState().nodeDoAddProperty( node.id(), property );
indexTxStateUpdater.onPropertyAdd( state, node, property );
return Property.noProperty( property.propertyKeyId(), EntityType.NODE, node.id() );
}
else
{
if ( !property.equals( existingProperty ) )
{
state.txState().nodeDoChangeProperty( node.id(), existingProperty, property );
indexTxStateUpdater.onPropertyChange( state, node, existingProperty, property );
}
return existingProperty;
}
}
}
@Override
public Property relationshipSetProperty( KernelStatement state, long relationshipId, DefinedProperty property )
throws EntityNotFoundException, InvalidTransactionTypeKernelException, AutoIndexingKernelException
{
DataWriteOperations ops = state.dataWriteOperations();
try ( Cursor<RelationshipItem> cursor = relationshipCursorById( state, relationshipId ) )
{
RelationshipItem relationship = cursor.get();
Property existingProperty;
try ( Cursor<PropertyItem> properties = relationshipGetPropertyCursor( state, relationship,
property.propertyKeyId() ) )
{
if ( !properties.next() )
{
autoIndexing.relationships().propertyAdded( ops, relationshipId, property );
existingProperty =
Property.noProperty( property.propertyKeyId(), EntityType.RELATIONSHIP, relationship.id() );
}
else
{
existingProperty = Property.property( properties.get().propertyKeyId(), properties.get().value() );
autoIndexing.relationships().propertyChanged( ops, relationshipId, existingProperty, property );
}
}
if ( !property.equals( existingProperty ) )
{
state.txState().relationshipDoReplaceProperty( relationship.id(), existingProperty, property );
}
return existingProperty;
}
}
@Override
public Property graphSetProperty( KernelStatement state, DefinedProperty property )
{
Object existingPropertyValue = graphGetProperty( state, property.propertyKeyId() );
Property existingProperty = existingPropertyValue == null ?
Property.noGraphProperty( property.propertyKeyId() ) :
Property.property( property.propertyKeyId(), existingPropertyValue );
if ( !property.equals( existingProperty ) )
{
state.txState().graphDoReplaceProperty( existingProperty, property );
}