-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StateHandlingStatementOperations.java
1702 lines (1529 loc) · 67.5 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.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongResourceIterator;
import org.neo4j.cursor.Cursor;
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.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.constraints.NodePropertyExistenceConstraint;
import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.constraints.RelationshipPropertyConstraint;
import org.neo4j.kernel.api.constraints.RelationshipPropertyExistenceConstraint;
import org.neo4j.kernel.api.constraints.UniquenessConstraint;
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.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.ConstraintValidationKernelException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintVerificationFailedKernelException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException;
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.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.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema.RelationshipPropertyDescriptor;
import org.neo4j.kernel.api.schema_new.IndexQuery;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaBoundary;
import org.neo4j.kernel.api.schema_new.SchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema_new.SchemaUtil;
import org.neo4j.kernel.api.schema_new.index.IndexBoundary;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
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.store.RelationshipIterator;
import org.neo4j.kernel.impl.index.IndexEntityType;
import org.neo4j.kernel.impl.index.LegacyIndexStore;
import org.neo4j.kernel.impl.util.Validators;
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.StorageStatement;
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 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.schema_new.index.NewIndexDescriptor.Filter.GENERAL;
import static org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor.Filter.UNIQUE;
import static org.neo4j.kernel.impl.util.Cursors.count;
import static org.neo4j.kernel.impl.util.Cursors.empty;
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;
public StateHandlingStatementOperations(
StoreReadLayer storeLayer, AutoIndexing propertyTrackers,
ConstraintIndexCreator constraintIndexCreator,
LegacyIndexStore legacyIndexStore )
{
this.storeLayer = storeLayer;
this.autoIndexing = propertyTrackers;
this.constraintIndexCreator = constraintIndexCreator;
this.legacyIndexStore = legacyIndexStore;
}
// <Cursors>
@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 )
{
Cursor<NodeItem> cursor = statement.getStoreStatement().acquireSingleNodeCursor( nodeId );
if ( statement.hasTxStateWithChanges() )
{
return statement.txState().augmentSingleNodeCursor( cursor, nodeId );
}
return cursor;
}
@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 )
{
Cursor<RelationshipItem> cursor =
statement.getStoreStatement().acquireSingleRelationshipCursor( relationshipId );
if ( statement.hasTxStateWithChanges() )
{
return statement.txState().augmentSingleRelationshipCursor( cursor, relationshipId );
}
return cursor;
}
@Override
public Cursor<RelationshipItem> relationshipCursorGetAll( KernelStatement statement )
{
Cursor<RelationshipItem> cursor = statement.getStoreStatement().relationshipsGetAllCursor();
if ( statement.hasTxStateWithChanges() )
{
return statement.txState().augmentRelationshipsGetAllCursor( cursor );
}
return cursor;
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node,
Direction direction )
{
Cursor<RelationshipItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.nodeGetRelationships( statement.getStoreStatement(), node, direction );
}
if ( !statement.hasTxStateWithChanges() )
{
return cursor;
}
NodeState nodeState = statement.txState().getNodeState( node.id() );
return statement.txState().augmentNodeRelationshipCursor( cursor, nodeState, direction );
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node, Direction direction,
PrimitiveIntSet relTypes )
{
Cursor<RelationshipItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.nodeGetRelationships( statement.getStoreStatement(), node, direction, relTypes );
}
if ( !statement.hasTxStateWithChanges() )
{
return cursor;
}
NodeState nodeState = statement.txState().getNodeState( node.id() );
return statement.txState().augmentNodeRelationshipCursor( cursor, nodeState, direction, relTypes );
}
// </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() );
try ( Cursor<PropertyItem> properties = node.properties() )
{
while ( properties.next() )
{
PropertyItem propertyItem = properties.get();
NodePropertyDescriptor nodePropDescriptor =
new NodePropertyDescriptor( labelId, propertyItem.propertyKeyId() );
NewIndexDescriptor descriptor = indexGetForLabelAndPropertyKey( state, nodePropDescriptor );
if ( descriptor != null )
{
DefinedProperty after = Property.property( propertyItem.propertyKeyId(),
propertyItem.value() );
state.txState().indexDoUpdateProperty( SchemaBoundary.map( nodePropDescriptor ), node.id(), null, after );
}
}
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() );
try ( Cursor<PropertyItem> properties = node.properties() )
{
while ( properties.next() )
{
PropertyItem propItem = properties.get();
DefinedProperty property = Property.property( propItem.propertyKeyId(), propItem.value() );
indexUpdateProperty( state, node.id(),
SchemaDescriptorFactory.forLabel( labelId, property.propertyKeyId() ), property, null );
}
}
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 NewIndexDescriptor indexCreate( KernelStatement state, NodePropertyDescriptor descriptor )
{
state.txState().indexRuleDoAdd( IndexBoundary.map( descriptor ) );
return IndexBoundary.map( descriptor );
}
@Override
public void indexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
state.txState().indexDoDrop( descriptor );
}
@Override
public void uniqueIndexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
state.txState().indexDoDrop( descriptor );
}
@Override
public UniquenessConstraint uniquePropertyConstraintCreate( KernelStatement state, NodePropertyDescriptor descriptor )
throws CreateConstraintFailureException
{
UniquenessConstraint constraint = new UniquenessConstraint( descriptor );
try
{
if ( state.hasTxStateWithChanges() &&
state.txState().constraintIndexDoUnRemove( constraint ) ) // ..., 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
for ( Iterator<NodePropertyConstraint> it = storeLayer.constraintsGetForLabelAndPropertyKey(
SchemaBoundary.map( descriptor) );
it.hasNext(); )
{
if ( it.next().equals( constraint ) )
{
return constraint;
}
}
long indexId = constraintIndexCreator.createUniquenessConstraintIndex( state, this, constraint.descriptor() );
state.txState().constraintDoAdd( constraint, indexId );
}
return constraint;
}
catch ( ConstraintVerificationFailedKernelException | DropIndexFailureException | TransactionFailureException
e )
{
throw new CreateConstraintFailureException( constraint, e );
}
}
@Override
public NodePropertyExistenceConstraint nodePropertyExistenceConstraintCreate( KernelStatement state,
NodePropertyDescriptor descriptor )
throws CreateConstraintFailureException
{
NodePropertyExistenceConstraint constraint = new NodePropertyExistenceConstraint( descriptor );
state.txState().constraintDoAdd( constraint );
return constraint;
}
@Override
public RelationshipPropertyExistenceConstraint relationshipPropertyExistenceConstraintCreate( KernelStatement state,
RelationshipPropertyDescriptor descriptor ) throws AlreadyConstrainedException, CreateConstraintFailureException
{
RelationshipPropertyExistenceConstraint constraint =
new RelationshipPropertyExistenceConstraint( descriptor );
state.txState().constraintDoAdd( constraint );
return constraint;
}
@Override
public Iterator<NodePropertyConstraint> constraintsGetForLabelAndPropertyKey( KernelStatement state,
NodePropertyDescriptor descriptor )
{
Iterator<NodePropertyConstraint> constraints =
storeLayer.constraintsGetForLabelAndPropertyKey( SchemaBoundary.map( descriptor ) );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForLabelAndProperty( descriptor ).apply( constraints );
}
return constraints;
}
@Override
public Iterator<NodePropertyConstraint> constraintsGetForLabel( KernelStatement state, int labelId )
{
Iterator<NodePropertyConstraint> constraints = storeLayer.constraintsGetForLabel( labelId );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForLabel( labelId ).apply( constraints );
}
return constraints;
}
@Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipTypeAndPropertyKey(
KernelStatement state, RelationshipPropertyDescriptor descriptor )
{
Iterator<RelationshipPropertyConstraint> constraints =
storeLayer.constraintsGetForRelationshipTypeAndPropertyKey( SchemaBoundary.map( descriptor ) );
if ( state.hasTxStateWithChanges() )
{
return state.txState()
.constraintsChangesForRelationshipTypeAndProperty( descriptor )
.apply( constraints );
}
return constraints;
}
@Override
public Iterator<RelationshipPropertyConstraint> constraintsGetForRelationshipType( KernelStatement state,
int typeId )
{
Iterator<RelationshipPropertyConstraint> constraints = storeLayer.constraintsGetForRelationshipType( typeId );
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChangesForRelationshipType( typeId ).apply( constraints );
}
return constraints;
}
@Override
public Iterator<PropertyConstraint> constraintsGetAll( KernelStatement state )
{
Iterator<PropertyConstraint> constraints = storeLayer.constraintsGetAll();
if ( state.hasTxStateWithChanges() )
{
return state.txState().constraintsChanges().apply( constraints );
}
return constraints;
}
@Override
public void constraintDrop( KernelStatement state, NodePropertyConstraint constraint )
{
state.txState().constraintDoDrop( constraint );
}
@Override
public void constraintDrop( KernelStatement state, RelationshipPropertyConstraint constraint )
throws DropConstraintFailureException
{
state.txState().constraintDoDrop( constraint );
}
@Override
public NewIndexDescriptor indexGetForLabelAndPropertyKey( KernelStatement state, NodePropertyDescriptor descriptor )
{
return indexGetForLabelAndPropertyKey( state, SchemaBoundary.map( descriptor ) );
}
private NewIndexDescriptor indexGetForLabelAndPropertyKey( KernelStatement state, LabelSchemaDescriptor descriptor )
{
NewIndexDescriptor indexDescriptor = storeLayer.indexGetForLabelAndPropertyKey( descriptor );
Iterator<NewIndexDescriptor> rules = iterator( indexDescriptor );
if ( state.hasTxStateWithChanges() )
{
rules = filter(
SchemaDescriptor.equalTo( descriptor ),
state.txState().indexDiffSetsByLabel( descriptor.getLabelId(), GENERAL ).apply( rules ) );
}
return singleOrNull( rules );
}
@Override
public InternalIndexState indexGetState( KernelStatement state, NewIndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
// If index is in our state, then return populating
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor, state.txState().indexDiffSetsByLabel( descriptor.schema().getLabelId(),
NewIndexDescriptor.Filter.ANY ) ) )
{
return InternalIndexState.POPULATING;
}
}
return storeLayer.indexGetState( descriptor.schema() );
}
@Override
public PopulationProgress indexGetPopulationProgress( KernelStatement state, NewIndexDescriptor descriptor ) throws
IndexNotFoundKernelException
{
// If index is in our state, then return 0%
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().getLabelId(),
NewIndexDescriptor.Filter.ANY ) ) )
{
return PopulationProgress.NONE;
}
}
return storeLayer.indexGetPopulationProgress( descriptor.schema() );
}
private boolean checkIndexState( NewIndexDescriptor index, ReadableDiffSets<NewIndexDescriptor> 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<NewIndexDescriptor> indexesGetForLabel( KernelStatement state, int labelId )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexDiffSetsByLabel( labelId, GENERAL )
.apply( storeLayer.indexesGetForLabel( labelId ) );
}
return storeLayer.indexesGetForLabel( labelId );
}
@Override
public Iterator<NewIndexDescriptor> indexesGetAll( KernelStatement state )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexChanges( GENERAL ).apply( storeLayer.indexesGetAll() );
}
return storeLayer.indexesGetAll();
}
@Override
public Iterator<NewIndexDescriptor> uniqueIndexesGetForLabel( KernelStatement state, int labelId )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexDiffSetsByLabel( labelId, UNIQUE )
.apply( storeLayer.uniquenessIndexesGetForLabel( labelId ) );
}
return storeLayer.uniquenessIndexesGetForLabel( labelId );
}
@Override
public Iterator<NewIndexDescriptor> uniqueIndexesGetAll( KernelStatement state )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexChanges( UNIQUE ).apply( storeLayer.uniquenessIndexesGetAll() );
}
return storeLayer.uniquenessIndexesGetAll();
}
@Override
public long nodeGetFromUniqueIndexSeek( KernelStatement state, NewIndexDescriptor index, Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
IndexReader reader = state.getStoreStatement().getFreshIndexReader( 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. */
IndexQuery.ExactPredicate query = IndexQuery.exact( index.schema().getPropertyId(), value );
PrimitiveLongResourceIterator committed = resourceIterator(
reader.query( query ), reader );
PrimitiveLongIterator exactMatches = filterExactIndexMatches( state, index, value, committed );
PrimitiveLongIterator changesFiltered = filterIndexStateChangesForScanOrSeek( state, index, value,
exactMatches );
return single( resourceIterator( changesFiltered, committed ), NO_SUCH_NODE );
}
@Override
public PrimitiveLongIterator indexQuery( KernelStatement state, NewIndexDescriptor index,
IndexQuery...predicates ) throws IndexNotFoundKernelException
{
StorageStatement storeStatement = state.getStoreStatement();
IndexReader reader = storeStatement.getIndexReader( index );
PrimitiveLongIterator committed = reader.query( predicates );
assert predicates.length == 1: "composite indexes not yet supported";
IndexQuery predicate = predicates[0];
Object filterValue = null;
switch ( predicate.type() )
{
case exact:
filterValue = ((IndexQuery.ExactPredicate) predicate).value();
committed = filterExactIndexMatches( state, index, filterValue, committed );
case stringSuffix:
case stringContains:
case exists:
return filterIndexStateChangesForScanOrSeek( state, index, filterValue, committed );
case rangeNumeric:
{
IndexQuery.NumberRangePredicate numPred = (IndexQuery.NumberRangePredicate) predicate;
PrimitiveLongIterator exactMatches =
filterExactRangeMatches( state, index, committed, numPred.from(), numPred.fromInclusive(),
numPred.to(), numPred.toInclusive() );
return filterIndexStateChangesForRangeSeekByNumber( state, index, numPred.from(),
numPred.fromInclusive(), numPred.to(), numPred.toInclusive(),
exactMatches );
}
case rangeString:
{
IndexQuery.StringRangePredicate strPred = (IndexQuery.StringRangePredicate) predicate;
return filterIndexStateChangesForRangeSeekByString(
state, index, strPred.from(), strPred.fromInclusive(), strPred.to(),
strPred.toInclusive(), committed );
}
case stringPrefix:
{
IndexQuery.StringPrefixPredicate strPred = (IndexQuery.StringPrefixPredicate) predicate;
return filterIndexStateChangesForRangeSeekByPrefix( state, index, strPred.prefix(), committed );
}
default:
throw new RuntimeException( "Query not supported: " + Arrays.toString( predicates ) );
}
}
@Override
public long nodesCountIndexed( KernelStatement statement, NewIndexDescriptor index, long nodeId, Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
IndexReader reader = statement.getStoreStatement().getIndexReader( index );
return reader.countIndexedNodes( nodeId, value );
}
private PrimitiveLongIterator filterExactIndexMatches( final KernelStatement state, NewIndexDescriptor index,
Object value, PrimitiveLongIterator committed )
{
// TODO: composite index
if ( index.schema().getPropertyIds().length == 1 )
{
return LookupFilter.exactIndexMatches( this, state, committed, index.schema().getPropertyIds()[0], value );
}
else
{
return PrimitiveLongCollections.emptyIterator();
}
}
private PrimitiveLongIterator filterExactRangeMatches( final KernelStatement state, NewIndexDescriptor index,
PrimitiveLongIterator committed, Number lower, boolean includeLower, Number upper, boolean includeUpper )
{
// TODO: composite index
if ( index.schema().getPropertyIds().length == 1 )
{
return LookupFilter
.exactRangeMatches( this, state, committed, index.schema().getPropertyIds()[0], lower, includeLower,
upper, includeUpper );
}
else
{
return PrimitiveLongCollections.emptyIterator();
}
}
private PrimitiveLongIterator filterIndexStateChangesForScanOrSeek( KernelStatement state, NewIndexDescriptor index,
Object value, PrimitiveLongIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
ReadableDiffSets<Long> labelPropertyChanges =
state.txState().indexUpdatesForScanOrSeek( index, value );
ReadableDiffSets<Long> nodes = state.txState().addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChanges.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongIterator filterIndexStateChangesForRangeSeekByNumber( KernelStatement state,
NewIndexDescriptor 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,
NewIndexDescriptor 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,
NewIndexDescriptor 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();
Property existingProperty;
try ( Cursor<PropertyItem> properties = node.property( property.propertyKeyId() ) )
{
if ( !properties.next() )
{
autoIndexing.nodes().propertyAdded( ops, nodeId, property );
existingProperty = Property.noProperty( property.propertyKeyId(), EntityType.NODE, node.id() );
}
else
{
existingProperty = Property.property( properties.get().propertyKeyId(), properties.get().value() );
autoIndexing.nodes().propertyChanged( ops, nodeId, existingProperty, property );
}
}
state.txState().nodeDoReplaceProperty( node.id(), existingProperty, property );
DefinedProperty before = definedPropertyOrNull( existingProperty );
indexesUpdateProperty( state, node, property.propertyKeyId(), before, 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 = relationship.property( 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 );
}
}
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 );
state.txState().graphDoReplaceProperty( existingProperty, property );
return existingProperty;
}
@Override
public Property nodeRemoveProperty( KernelStatement state, long nodeId, int propertyKeyId )
throws EntityNotFoundException, InvalidTransactionTypeKernelException, AutoIndexingKernelException
{
DataWriteOperations ops = state.dataWriteOperations();
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
Property existingProperty;
try ( Cursor<PropertyItem> properties = node.property( propertyKeyId ) )
{
if ( !properties.next() )
{
existingProperty = Property.noProperty( propertyKeyId, EntityType.NODE, node.id() );
}
else
{
existingProperty = Property.property( properties.get().propertyKeyId(), properties.get().value() );
autoIndexing.nodes().propertyRemoved( ops, nodeId, propertyKeyId );
state.txState().nodeDoRemoveProperty( node.id(), (DefinedProperty) existingProperty );
indexesUpdateProperty( state, node, propertyKeyId, (DefinedProperty) existingProperty, null );
}
}
return existingProperty;
}
}
@Override
public Property relationshipRemoveProperty( KernelStatement state,
long relationshipId,
int propertyKeyId )
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 = relationship.property( propertyKeyId ) )
{
if ( !properties.next() )
{
existingProperty = Property.noProperty( propertyKeyId, EntityType.RELATIONSHIP, relationship.id() );
}
else
{
existingProperty = Property.property( properties.get().propertyKeyId(), properties.get().value() );
autoIndexing.relationships().propertyRemoved( ops, relationshipId, propertyKeyId );
state.txState().relationshipDoRemoveProperty( relationship.id(),
(DefinedProperty) existingProperty );
}