-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StateHandlingStatementOperations.java
1787 lines (1602 loc) · 69.7 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-2018 "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.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongResourceIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.internal.kernel.api.InternalIndexState;
import org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
import org.neo4j.internal.kernel.api.exceptions.explicitindex.AutoIndexingKernelException;
import org.neo4j.internal.kernel.api.exceptions.explicitindex.ExplicitIndexNotFoundKernelException;
import org.neo4j.internal.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.internal.kernel.api.exceptions.schema.TooManyLabelsException;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.RelationTypeSchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaDescriptor;
import org.neo4j.internal.kernel.api.schema.SchemaUtil;
import org.neo4j.internal.kernel.api.schema.constraints.ConstraintDescriptor;
import org.neo4j.kernel.api.DataWriteOperations;
import org.neo4j.kernel.api.ExplicitIndex;
import org.neo4j.kernel.api.ExplicitIndexHits;
import org.neo4j.kernel.api.Statement;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
import org.neo4j.internal.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.schema.AlreadyConstrainedException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.explicitindex.AutoIndexing;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.properties.PropertyKeyIdIterator;
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.SchemaIndexDescriptor;
import org.neo4j.kernel.api.schema.index.SchemaIndexDescriptorFactory;
import org.neo4j.kernel.api.txstate.TransactionCountingStateVisitor;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.index.IndexingService;
import org.neo4j.kernel.impl.api.operations.CountsOperations;
import org.neo4j.kernel.impl.api.operations.EntityOperations;
import org.neo4j.kernel.impl.api.operations.ExplicitIndexReadOperations;
import org.neo4j.kernel.impl.api.operations.ExplicitIndexWriteOperations;
import org.neo4j.kernel.impl.api.operations.KeyReadOperations;
import org.neo4j.kernel.impl.api.operations.KeyWriteOperations;
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.ExplicitIndexStore;
import org.neo4j.kernel.impl.index.IndexEntityType;
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.PrimitiveLongReadableDiffSets;
import org.neo4j.storageengine.api.txstate.ReadableDiffSets;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.ValueTuple;
import org.neo4j.values.storable.Values;
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.function.Predicates.any;
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.impl.api.state.IndexTxStateUpdater.LabelChangeType.ADDED_LABEL;
import static org.neo4j.kernel.impl.api.state.IndexTxStateUpdater.LabelChangeType.REMOVED_LABEL;
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, ExplicitIndexReadOperations, ExplicitIndexWriteOperations
{
private final StoreReadLayer storeLayer;
private final AutoIndexing autoIndexing;
private final ConstraintIndexCreator constraintIndexCreator;
private final ExplicitIndexStore explicitIndexStore;
private final IndexTxStateUpdater indexTxStateUpdater;
public StateHandlingStatementOperations(
StoreReadLayer storeLayer, AutoIndexing propertyTrackers,
ConstraintIndexCreator constraintIndexCreator,
ExplicitIndexStore explicitIndexStore,
IndexingService indexingService )
{
this.storeLayer = storeLayer;
this.autoIndexing = propertyTrackers;
this.constraintIndexCreator = constraintIndexCreator;
this.explicitIndexStore = explicitIndexStore;
this.indexTxStateUpdater = new IndexTxStateUpdater( storeLayer, this, indexingService );
}
// <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,
int[] relTypes )
{
Cursor<RelationshipItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.nodeGetRelationships( statement.getStoreStatement(), node, direction, any( relTypes ) );
}
if ( !statement.hasTxStateWithChanges() )
{
return cursor;
}
NodeState nodeState = statement.txState().getNodeState( node.id() );
return statement.txState().augmentNodeRelationshipCursor( cursor, nodeState, direction, relTypes );
}
@Override
public Cursor<PropertyItem> nodeGetProperties( KernelStatement statement, NodeItem node )
{
Cursor<PropertyItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.nodeGetProperties( statement.getStoreStatement(), node, statement );
}
return statement.hasTxStateWithChanges()
? statement.txState().augmentPropertyCursor( cursor, statement.txState().getNodeState( node.id() ) )
: cursor;
}
@Override
public PrimitiveIntSet nodeGetPropertyKeys( KernelStatement statement, NodeItem node )
{
final PrimitiveIntSet keys = Primitive.intSet();
try ( Cursor<PropertyItem> properties = nodeGetProperties( statement, node ) )
{
while ( properties.next() )
{
keys.add( properties.get().propertyKeyId() );
}
}
return keys;
}
@Override
public Value 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 Values.NO_VALUE;
}
return Values.NO_VALUE;
}
@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 )
{
Cursor<PropertyItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().nodeIsAddedInThisTx( node.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.nodeGetProperty( statement.getStoreStatement(), node, propertyKeyId, statement );
}
return statement.hasTxStateWithChanges()
? statement.txState().augmentSinglePropertyCursor(
cursor, statement.txState().getNodeState( node.id() ), propertyKeyId )
: cursor;
}
@Override
public Cursor<PropertyItem> relationshipGetProperties( KernelStatement statement, RelationshipItem relationship )
{
Cursor<PropertyItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().relationshipIsAddedInThisTx( relationship.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer.relationshipGetProperties( statement.getStoreStatement(), relationship, statement );
}
return statement.hasTxStateWithChanges()
? statement.txState()
.augmentPropertyCursor( cursor, statement.txState().getRelationshipState( relationship.id() ) )
: cursor;
}
@Override
public PrimitiveIntSet relationshipGetPropertyKeys( KernelStatement statement,
RelationshipItem relationship )
{
final PrimitiveIntSet keys = Primitive.intSet();
try ( Cursor<PropertyItem> properties = relationshipGetProperties( statement, relationship ) )
{
while ( properties.next() )
{
keys.add( properties.get().propertyKeyId() );
}
}
return keys;
}
@Override
public Value 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 Values.NO_VALUE;
}
return Values.NO_VALUE;
}
@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 )
{
Cursor<PropertyItem> cursor;
if ( statement.hasTxStateWithChanges() && statement.txState().relationshipIsAddedInThisTx( relationship.id() ) )
{
cursor = empty();
}
else
{
cursor = storeLayer
.relationshipGetProperty( statement.getStoreStatement(), relationship, propertyKeyId, statement );
}
return statement.hasTxStateWithChanges()
? statement.txState().augmentSinglePropertyCursor( cursor, statement.txState()
.getRelationshipState( relationship.id() ), propertyKeyId )
: cursor;
}
// </Cursors>
@Override
public long nodeCreate( KernelStatement state )
{
long nodeId = state.getStoreStatement().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 = state.getStoreStatement().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();
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 PrimitiveLongResourceIterator nodesGetForLabel( KernelStatement state, int labelId )
{
PrimitiveLongResourceIterator committed = storeLayer.nodesGetForLabel( state.getStoreStatement(), labelId );
if ( state.hasTxStateWithChanges() )
{
PrimitiveLongResourceIterator wLabelChanges = state.txState().nodesWithLabelChanged( labelId ).augment( committed );
return state.txState().addedAndRemovedNodes().augmentWithRemovals( wLabelChanges );
}
return committed;
}
@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 SchemaIndexDescriptor indexCreate( KernelStatement state, SchemaDescriptor descriptor )
{
SchemaIndexDescriptor schemaIndexDescriptor = SchemaIndexDescriptorFactory.forSchema( descriptor );
state.txState().indexRuleDoAdd( schemaIndexDescriptor );
return schemaIndexDescriptor;
}
@Override
public void indexDrop( KernelStatement state, SchemaIndexDescriptor descriptor )
{
state.txState().indexDoDrop( descriptor );
}
@Override
public void uniqueIndexDrop( KernelStatement state, SchemaIndexDescriptor descriptor )
{
state.txState().indexDoDrop( descriptor );
}
private void 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;
}
}
long indexId = constraintIndexCreator.createUniquenessConstraintIndex( state, this, descriptor );
if ( !constraintExists( state, constraint ) )
{
// This looks weird, but since we release the label lock while awaiting population of the index
// backing this constraint there can be someone else getting ahead of us, creating this exact constraint
// before we do, so now getting out here under the lock we must check again and if it exists
// we must at this point consider this an idempotent operation because we verified earlier
// that it didn't exist and went on to create it.
state.txState().constraintDoAdd( constraint, indexId );
}
}
}
catch ( UniquePropertyValueValidationException | TransactionFailureException | AlreadyConstrainedException 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, SchemaDescriptor descriptor )
throws CreateConstraintFailureException
{
UniquenessConstraintDescriptor constraint = ConstraintDescriptorFactory.uniqueForSchema( descriptor );
indexBackedConstraintCreate( state, constraint );
return constraint;
}
@Override
public NodeExistenceConstraintDescriptor nodePropertyExistenceConstraintCreate(
KernelStatement state,
LabelSchemaDescriptor descriptor
)
{
NodeExistenceConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForSchema( descriptor );
state.txState().constraintDoAdd( constraint );
return constraint;
}
@Override
public RelExistenceConstraintDescriptor relationshipPropertyExistenceConstraintCreate(
KernelStatement state,
RelationTypeSchemaDescriptor descriptor
)
{
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 SchemaIndexDescriptor indexGetForSchema( KernelStatement state, SchemaDescriptor descriptor )
{
SchemaIndexDescriptor schemaIndexDescriptor = storeLayer.indexGetForSchema( descriptor );
Iterator<SchemaIndexDescriptor> rules = iterator( schemaIndexDescriptor );
if ( state.hasTxStateWithChanges() )
{
rules = filter(
SchemaDescriptor.equalTo( descriptor ),
state.txState().indexDiffSetsByLabel( descriptor.keyId() ).apply( rules ) );
}
return singleOrNull( rules );
}
@Override
public InternalIndexState indexGetState( KernelStatement state, SchemaIndexDescriptor descriptor )
throws IndexNotFoundKernelException
{
// If index is in our state, then return populating
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().keyId() ) ) )
{
return InternalIndexState.POPULATING;
}
}
return storeLayer.indexGetState( descriptor );
}
@Override
public IndexProvider.Descriptor indexGetProviderDescriptor( KernelStatement state, SchemaIndexDescriptor descriptor ) throws IndexNotFoundKernelException
{
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().keyId() ) ) )
{
return IndexProvider.UNDECIDED;
}
}
return storeLayer.indexGetProviderDescriptor( descriptor );
}
@Override
public PopulationProgress indexGetPopulationProgress( KernelStatement state, SchemaIndexDescriptor descriptor ) throws
IndexNotFoundKernelException
{
// If index is in our state, then return 0%
if ( state.hasTxStateWithChanges() )
{
if ( checkIndexState( descriptor,
state.txState().indexDiffSetsByLabel( descriptor.schema().keyId() ) ) )
{
return PopulationProgress.NONE;
}
}
return storeLayer.indexGetPopulationProgress( descriptor.schema() );
}
private boolean checkIndexState( SchemaIndexDescriptor index, ReadableDiffSets<SchemaIndexDescriptor> 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<SchemaIndexDescriptor> indexesGetForLabel( KernelStatement state, int labelId )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexDiffSetsByLabel( labelId )
.apply( storeLayer.indexesGetForLabel( labelId ) );
}
return storeLayer.indexesGetForLabel( labelId );
}
@Override
public Iterator<SchemaIndexDescriptor> indexesGetAll( KernelStatement state )
{
if ( state.hasTxStateWithChanges() )
{
return state.txState().indexChanges().apply( storeLayer.indexesGetAll() );
}
return storeLayer.indexesGetAll();
}
@Override
public long nodeGetFromUniqueIndexSeek(
KernelStatement state, SchemaIndexDescriptor index, IndexQuery.ExactPredicate... query )
throws IndexNotFoundKernelException, IndexNotApplicableKernelException
{
try ( 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. */
PrimitiveLongResourceIterator committed = reader.query( query );
PrimitiveLongResourceIterator exactMatches = reader.hasFullValuePrecision( query )
? committed : LookupFilter.exactIndexMatches( this, state, committed, query );
PrimitiveLongResourceIterator changesFiltered =
filterIndexStateChangesForSeek( state, exactMatches, index, IndexQuery.asValueTuple( query ) );
return single( resourceIterator( changesFiltered, committed ), NO_SUCH_NODE );
}
}
@Override
public PrimitiveLongResourceIterator indexQuery( KernelStatement state, SchemaIndexDescriptor index, IndexQuery... predicates )
throws IndexNotFoundKernelException, IndexNotApplicableKernelException
{
StorageStatement storeStatement = state.getStoreStatement();
IndexReader reader = storeStatement.getIndexReader( index );
PrimitiveLongResourceIterator committed = reader.query( predicates );
PrimitiveLongResourceIterator exactMatches = reader.hasFullValuePrecision( predicates )
? committed : LookupFilter.exactIndexMatches( this, state, committed, predicates );
IndexQuery firstPredicate = predicates[0];
switch ( firstPredicate.type() )
{
case exact:
IndexQuery.ExactPredicate[] exactPreds = assertOnlyExactPredicates( predicates );
return filterIndexStateChangesForSeek( state, exactMatches, index, IndexQuery.asValueTuple( exactPreds ) );
case stringSuffix:
case stringContains:
case exists:
return filterIndexStateChangesForScan( state, exactMatches, index );
case range:
assertSinglePredicate( predicates );
IndexQuery.RangePredicate<?> rangePred = (IndexQuery.RangePredicate<?>) firstPredicate;
return filterIndexStateChangesForRangeSeek( state, index, rangePred.valueGroup(),
rangePred.fromValue(), rangePred.fromInclusive(),
rangePred.toValue(), rangePred.toInclusive(),
exactMatches );
case stringPrefix:
{
assertSinglePredicate( predicates );
IndexQuery.StringPrefixPredicate strPred = (IndexQuery.StringPrefixPredicate) firstPredicate;
return filterIndexStateChangesForRangeSeekByPrefix( state, index, strPred.prefix(), committed );
}
default:
throw new UnsupportedOperationException( "Query not supported: " + Arrays.toString( predicates ) );
}
}
public static 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, SchemaIndexDescriptor index, long nodeId, Value value )
throws IndexNotFoundKernelException
{
IndexReader reader = statement.getStoreStatement().getIndexReader( index );
return reader.countIndexedNodes( nodeId, value );
}
private PrimitiveLongResourceIterator filterIndexStateChangesForScan(
KernelStatement state, PrimitiveLongResourceIterator nodeIds, SchemaIndexDescriptor index )
{
if ( state.hasTxStateWithChanges() )
{
PrimitiveLongReadableDiffSets 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 PrimitiveLongResourceIterator filterIndexStateChangesForSeek(
KernelStatement state, PrimitiveLongResourceIterator nodeIds, SchemaIndexDescriptor index,
ValueTuple propertyValues )
{
if ( state.hasTxStateWithChanges() )
{
PrimitiveLongReadableDiffSets 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 PrimitiveLongResourceIterator filterIndexStateChangesForRangeSeek( KernelStatement state,
SchemaIndexDescriptor index, ValueGroup valueGroup,
Value lower, boolean includeLower,
Value upper, boolean includeUpper,
PrimitiveLongResourceIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
TransactionState txState = state.txState();
PrimitiveLongReadableDiffSets labelPropertyChangesForString = txState.indexUpdatesForRangeSeek(
index, valueGroup, lower, includeLower, upper, includeUpper );
ReadableDiffSets<Long> nodes = txState.addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChangesForString.augment( nodeIds ) );
}
return nodeIds;
}
private PrimitiveLongResourceIterator filterIndexStateChangesForRangeSeekByPrefix( KernelStatement state,
SchemaIndexDescriptor index,
String prefix,
PrimitiveLongResourceIterator nodeIds )
{
if ( state.hasTxStateWithChanges() )
{
TransactionState txState = state.txState();
PrimitiveLongReadableDiffSets labelPropertyChangesForPrefix =
txState.indexUpdatesForRangeSeekByPrefix( index, prefix );
ReadableDiffSets<Long> nodes = txState.addedAndRemovedNodes();
// Apply to actual index lookup
return nodes.augmentWithRemovals( labelPropertyChangesForPrefix.augment( nodeIds ) );
}
return nodeIds;
}