/
TxState.java
1212 lines (1066 loc) · 39.1 KB
/
TxState.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-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.api.state;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntObjectMap;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.function.Function;
import org.neo4j.function.Predicate;
import org.neo4j.graphdb.Direction;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.kernel.api.constraints.UniquenessConstraint;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.properties.DefinedProperty;
import org.neo4j.kernel.api.properties.Property;
import org.neo4j.kernel.api.txstate.ReadableTxState;
import org.neo4j.kernel.api.txstate.RelationshipChangeVisitorAdapter;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.api.txstate.TxStateVisitor;
import org.neo4j.kernel.api.txstate.UpdateTriState;
import org.neo4j.kernel.impl.api.RelationshipVisitor;
import org.neo4j.kernel.impl.api.store.RelationshipIterator;
import org.neo4j.kernel.impl.util.diffsets.DiffSets;
import org.neo4j.kernel.impl.util.diffsets.DiffSetsVisitor;
import org.neo4j.kernel.impl.util.diffsets.ReadableDiffSets;
import org.neo4j.kernel.impl.util.diffsets.ReadableRelationshipDiffSets;
import org.neo4j.kernel.impl.util.diffsets.RelationshipDiffSets;
import static org.neo4j.helpers.collection.Iterables.map;
import static org.neo4j.kernel.api.properties.Property.property;
/**
* This class contains transaction-local changes to the graph. These changes can then be used to augment reads from the
* committed state of the database (to make the local changes appear in local transaction read operations). At commit
* time a visitor is sent into this class to convert the end result of the tx changes into a physical changeset.
*
* See {@link org.neo4j.kernel.impl.api.KernelTransactionImplementation} for how this happens.
*
* This class is very large, as it has been used as a gathering point to consolidate all transaction state knowledge
* into one component. Now that that work is done, this class should be refactored to increase transparency in how it
* works.
*/
public final class TxState implements TransactionState, RelationshipVisitor.Home
{
private Map<Integer/*Label ID*/, LabelState.Mutable> labelStatesMap;
private static final LabelState.Defaults LABEL_STATE = new LabelState.Defaults()
{
@Override
Map<Integer, LabelState.Mutable> getMap( TxState state )
{
return state.labelStatesMap;
}
@Override
void setMap( TxState state, Map<Integer, LabelState.Mutable> map )
{
state.labelStatesMap = map;
}
};
private Map<Long/*Node ID*/, NodeState.Mutable> nodeStatesMap;
private static final NodeState.Defaults NODE_STATE = new NodeState.Defaults()
{
@Override
Map<Long, NodeState.Mutable> getMap( TxState state )
{
return state.nodeStatesMap;
}
@Override
void setMap( TxState state, Map<Long, NodeState.Mutable> map )
{
state.nodeStatesMap = map;
}
};
private Map<Long/*Relationship ID*/, RelationshipState.Mutable> relationshipStatesMap;
private static final RelationshipState.Defaults RELATIONSHIP_STATE = new RelationshipState.Defaults()
{
@Override
Map<Long, RelationshipState.Mutable> getMap( TxState state )
{
return state.relationshipStatesMap;
}
@Override
void setMap( TxState state, Map<Long, RelationshipState.Mutable> map )
{
state.relationshipStatesMap = map;
}
};
private Map<Integer/*Token ID*/,String> createdLabelTokens;
private Map<Integer/*Token ID*/,String> createdPropertyKeyTokens;
private Map<Integer/*Token ID*/,String> createdRelationshipTypeTokens;
private GraphState graphState;
private DiffSets<IndexDescriptor> indexChanges;
private DiffSets<IndexDescriptor> constraintIndexChanges;
private DiffSets<UniquenessConstraint> constraintsChanges;
private PropertyChanges propertyChangesForNodes;
// Tracks added and removed nodes, not modified nodes
private DiffSets<Long> nodes;
// Tracks added and removed relationships, not modified relationships
private RelationshipDiffSets<Long> relationships;
// This is temporary. It is needed until we've removed nodes and rels from the global cache, to tell
// that they were created and then deleted in the same tx. This is here just to set a save point to
// get a large set of changes in, and is meant to be removed in the coming days in a follow-up commit.
private final Set<Long> nodesDeletedInTx = new HashSet<>();
private final Set<Long> relationshipsDeletedInTx = new HashSet<>();
private Map<UniquenessConstraint, Long> createdConstraintIndexesByConstraint;
private Map<String, Map<String, String>> createdNodeLegacyIndexes;
private Map<String, Map<String, String>> createdRelationshipLegacyIndexes;
private PrimitiveIntObjectMap<Map<DefinedProperty, DiffSets<Long>>> indexUpdates;
private boolean hasChanges;
@Override
public void accept( final TxStateVisitor visitor )
{
// Created nodes
if ( nodes != null )
{
nodes.accept( createdNodesVisitor( visitor ) );
}
if ( relationships != null )
{
// Created relationships
relationships.accept( createdRelationshipsVisitor( this, visitor ) );
// Deleted relationships
relationships.accept( deletedRelationshipsVisitor( visitor ) );
}
// Deleted nodes
if ( nodes != null )
{
nodes.accept( deletedNodesVisitor( visitor ) );
}
for ( NodeState node : modifiedNodes() )
{
node.accept( nodeVisitor( visitor ) );
}
for ( RelationshipState rel : modifiedRelationships() )
{
rel.accept( relVisitor( visitor ) );
}
if ( graphState != null )
{
graphState.accept( graphPropertyVisitor( visitor ) );
}
if ( indexChanges != null )
{
indexChanges.accept( indexVisitor( visitor, false ) );
}
if ( constraintIndexChanges != null )
{
constraintIndexChanges.accept( indexVisitor( visitor, true ) );
}
if ( constraintsChanges != null )
{
constraintsChanges.accept( constraintsVisitor( visitor ) );
}
if ( createdLabelTokens != null )
{
for ( Map.Entry<Integer,String> entry : createdLabelTokens.entrySet() )
{
visitor.visitCreatedLabelToken( entry.getValue(), entry.getKey() );
}
}
if ( createdPropertyKeyTokens != null )
{
for ( Map.Entry<Integer,String> entry : createdPropertyKeyTokens.entrySet() )
{
visitor.visitCreatedPropertyKeyToken( entry.getValue(), entry.getKey() );
}
}
if ( createdRelationshipTypeTokens != null )
{
for ( Map.Entry<Integer,String> entry : createdRelationshipTypeTokens.entrySet() )
{
visitor.visitCreatedRelationshipTypeToken( entry.getValue(), entry.getKey() );
}
}
if ( createdNodeLegacyIndexes != null )
{
for ( Map.Entry<String,Map<String,String>> entry : createdNodeLegacyIndexes.entrySet() )
{
visitor.visitCreatedNodeLegacyIndex( entry.getKey(), entry.getValue() );
}
}
if ( createdRelationshipLegacyIndexes != null )
{
for ( Map.Entry<String,Map<String,String>> entry : createdRelationshipLegacyIndexes.entrySet() )
{
visitor.visitCreatedRelationshipLegacyIndex( entry.getKey(), entry.getValue() );
}
}
}
private static DiffSetsVisitor<Long> deletedNodesVisitor( final TxStateVisitor visitor )
{
return new DiffSetsVisitor.Adapter<Long>()
{
@Override
public void visitRemoved( Long element )
{
visitor.visitDeletedNode( element );
}
};
}
private static DiffSetsVisitor<Long> createdNodesVisitor( final TxStateVisitor visitor )
{
return new DiffSetsVisitor.Adapter<Long>()
{
@Override
public void visitAdded( Long element )
{
visitor.visitCreatedNode( element );
}
};
}
private static DiffSetsVisitor<Long> deletedRelationshipsVisitor( final TxStateVisitor visitor )
{
return new DiffSetsVisitor.Adapter<Long>()
{
@Override
public void visitRemoved( Long id )
{
visitor.visitDeletedRelationship( id );
}
};
}
private static DiffSetsVisitor<Long> createdRelationshipsVisitor( ReadableTxState tx, final TxStateVisitor visitor )
{
return new RelationshipChangeVisitorAdapter( tx )
{
@Override
protected void visitAddedRelationship( long relationshipId, int type, long startNode, long endNode )
{
visitor.visitCreatedRelationship( relationshipId, type, startNode, endNode );
}
};
}
private static DiffSetsVisitor<UniquenessConstraint> constraintsVisitor( final TxStateVisitor visitor )
{
return new DiffSetsVisitor<UniquenessConstraint>()
{
@Override
public void visitAdded( UniquenessConstraint element )
{
visitor.visitAddedConstraint( element );
}
@Override
public void visitRemoved( UniquenessConstraint element )
{
visitor.visitRemovedConstraint( element );
}
};
}
private static DiffSetsVisitor<IndexDescriptor> indexVisitor( final TxStateVisitor visitor, final boolean forConstraint )
{
return new DiffSetsVisitor<IndexDescriptor>()
{
@Override
public void visitAdded( IndexDescriptor element )
{
visitor.visitAddedIndex( element, forConstraint );
}
@Override
public void visitRemoved( IndexDescriptor element )
{
visitor.visitRemovedIndex( element, forConstraint );
}
};
}
private static NodeState.Visitor nodeVisitor( final TxStateVisitor visitor )
{
return new NodeState.Visitor()
{
@Override
public void visitLabelChanges( long nodeId, Set<Integer> added, Set<Integer> removed )
{
visitor.visitNodeLabelChanges( nodeId, added, removed );
}
@Override
public void visitPropertyChanges( long entityId, Iterator<DefinedProperty> added,
Iterator<DefinedProperty> changed, Iterator<Integer> removed)
{
visitor.visitNodePropertyChanges( entityId, added, changed, removed );
}
@Override
public void visitRelationshipChanges( long nodeId, RelationshipChangesForNode added,
RelationshipChangesForNode removed )
{
visitor.visitNodeRelationshipChanges( nodeId, added, removed );
}
};
}
private static PropertyContainerState.Visitor relVisitor( final TxStateVisitor visitor )
{
return new PropertyContainerState.Visitor()
{
@Override
public void visitPropertyChanges( long entityId, Iterator<DefinedProperty> added,
Iterator<DefinedProperty> changed, Iterator<Integer> removed)
{
visitor.visitRelPropertyChanges( entityId, added, changed, removed );
}
};
}
private static PropertyContainerState.Visitor graphPropertyVisitor( final TxStateVisitor visitor )
{
return new PropertyContainerState.Visitor()
{
@Override
public void visitPropertyChanges( long entityId, Iterator<DefinedProperty> added,
Iterator<DefinedProperty> changed, Iterator<Integer> removed)
{
visitor.visitGraphPropertyChanges( added, changed, removed );
}
};
}
@Override
public boolean hasChanges()
{
return hasChanges;
}
@Override
public Iterable<NodeState> modifiedNodes()
{
return NODE_STATE.values( this );
}
private DiffSets<Long> getOrCreateLabelStateNodeDiffSets( int labelId )
{
return LABEL_STATE.getOrCreate( this, labelId ).getOrCreateNodeDiffSets();
}
@Override
public ReadableDiffSets<Integer> nodeStateLabelDiffSets( long nodeId )
{
return NODE_STATE.get( this, nodeId ).labelDiffSets();
}
private DiffSets<Integer> getOrCreateNodeStateLabelDiffSets( long nodeId )
{
return getOrCreateNodeState( nodeId ).getOrCreateLabelDiffSets();
}
@Override
public Iterator<DefinedProperty> augmentNodeProperties( long nodeId, Iterator<DefinedProperty> original )
{
return NODE_STATE.get( this, nodeId ).augmentProperties( original );
}
@Override
public Iterator<DefinedProperty> augmentRelationshipProperties( long relId, Iterator<DefinedProperty> original )
{
return RELATIONSHIP_STATE.get( this, relId ).augmentProperties( original );
}
@Override
public Iterator<DefinedProperty> augmentGraphProperties( Iterator<DefinedProperty> original )
{
if ( graphState != null )
{
return graphState.augmentProperties( original );
}
return original;
}
@Override
public Iterator<DefinedProperty> addedAndChangedNodeProperties( long nodeId )
{
return NODE_STATE.get( this, nodeId ).addedAndChangedProperties();
}
@Override
public Iterator<DefinedProperty> addedAndChangedRelationshipProperties( long relId )
{
return RELATIONSHIP_STATE.get( this, relId ).addedAndChangedProperties();
}
@Override
public boolean nodeIsAddedInThisTx( long nodeId )
{
return nodes != null && nodes.isAdded( nodeId );
}
@Override
public boolean relationshipIsAddedInThisTx( long relationshipId )
{
return relationships != null && relationships.isAdded( relationshipId );
}
@Override
public void nodeDoCreate( long id )
{
nodes().add( id );
hasChanges = true;
}
@Override
public void nodeDoDelete( long nodeId )
{
if ( nodes().remove( nodeId ) )
{
nodesDeletedInTx.add( nodeId );
}
if ( nodeStatesMap != null )
{
NodeState.Mutable nodeState = nodeStatesMap.remove( nodeId );
if ( nodeState != null )
{
ReadableDiffSets<Integer> diff = nodeState.labelDiffSets();
for ( Integer label : diff.getAdded() )
{
getOrCreateLabelStateNodeDiffSets( label ).remove( nodeId );
}
nodeState.clearIndexDiffs( nodeId );
nodeState.clear();
}
}
hasChanges = true;
}
@Override
public void relationshipDoCreate( long id, int relationshipTypeId, long startNodeId, long endNodeId )
{
relationships().add( id );
if ( startNodeId == endNodeId )
{
getOrCreateNodeState( startNodeId ).addRelationship( id, relationshipTypeId, Direction.BOTH );
}
else
{
getOrCreateNodeState( startNodeId ).addRelationship( id, relationshipTypeId, Direction.OUTGOING );
getOrCreateNodeState( endNodeId ).addRelationship( id, relationshipTypeId, Direction.INCOMING );
}
getOrCreateRelationshipState( id ).setMetaData( startNodeId, endNodeId, relationshipTypeId );
hasChanges = true;
}
@Override
public boolean nodeIsDeletedInThisTx( long nodeId )
{
return addedAndRemovedNodes().isRemoved( nodeId )
// Temporary until we've stopped adding nodes to the global cache during tx.
|| nodesDeletedInTx.contains( nodeId );
}
@Override
public boolean nodeModifiedInThisTx( long nodeId )
{
return nodeIsAddedInThisTx( nodeId ) || nodeIsDeletedInThisTx( nodeId ) || hasNodeState( nodeId );
}
@Override
public void relationshipDoDelete( long id, int type, long startNodeId, long endNodeId )
{
if ( relationships().remove( id ) )
{
relationshipsDeletedInTx.add( id );
}
if ( startNodeId == endNodeId )
{
getOrCreateNodeState( startNodeId ).removeRelationship( id, type, Direction.BOTH );
}
else
{
getOrCreateNodeState( startNodeId ).removeRelationship( id, type, Direction.OUTGOING );
getOrCreateNodeState( endNodeId ).removeRelationship( id, type, Direction.INCOMING );
}
if ( relationshipStatesMap != null )
{
RelationshipState.Mutable removed = relationshipStatesMap.remove( id );
if ( removed != null )
{
removed.clear();
}
}
hasChanges = true;
}
@Override
public void relationshipDoDeleteAddedInThisTx( long relationshipId )
{
RELATIONSHIP_STATE.get( this, relationshipId ).accept( new RelationshipVisitor<RuntimeException>()
{
@Override
public void visit( long relId, int type, long startNode, long endNode )
{
relationshipDoDelete( relId, type, startNode, endNode );
}
} );
}
@Override
public boolean relationshipIsDeletedInThisTx( long relationshipId )
{
return addedAndRemovedRelationships().isRemoved( relationshipId )
// Temporary until we stop adding rels to the global cache during tx
|| relationshipsDeletedInTx.contains( relationshipId );
}
@Override
public void nodeDoReplaceProperty( long nodeId, Property replacedProperty, DefinedProperty newProperty )
{
if ( replacedProperty.isDefined() )
{
getOrCreateNodeState( nodeId ).changeProperty( newProperty );
nodePropertyChanges().changeProperty( nodeId, replacedProperty.propertyKeyId(),
((DefinedProperty) replacedProperty).value(), newProperty.value() );
}
else
{
NodeState.Mutable nodeState = getOrCreateNodeState( nodeId );
nodeState.addProperty( newProperty );
nodePropertyChanges().addProperty( nodeId, newProperty.propertyKeyId(), newProperty.value() );
}
hasChanges = true;
}
@Override
public void relationshipDoReplaceProperty( long relationshipId, Property replacedProperty, DefinedProperty newProperty )
{
if ( replacedProperty.isDefined() )
{
getOrCreateRelationshipState( relationshipId ).changeProperty( newProperty );
}
else
{
getOrCreateRelationshipState( relationshipId ).addProperty( newProperty );
}
hasChanges = true;
}
@Override
public void graphDoReplaceProperty( Property replacedProperty, DefinedProperty newProperty )
{
if ( replacedProperty.isDefined() )
{
getOrCreateGraphState().changeProperty( newProperty );
}
else
{
getOrCreateGraphState().addProperty( newProperty );
}
hasChanges = true;
}
@Override
public void nodeDoRemoveProperty( long nodeId, DefinedProperty removedProperty )
{
getOrCreateNodeState( nodeId ).removeProperty( removedProperty );
nodePropertyChanges().removeProperty( nodeId, removedProperty.propertyKeyId(),
removedProperty.value() );
hasChanges = true;
}
@Override
public void relationshipDoRemoveProperty( long relationshipId, DefinedProperty removedProperty )
{
getOrCreateRelationshipState( relationshipId ).removeProperty( removedProperty );
hasChanges = true;
}
@Override
public void graphDoRemoveProperty( DefinedProperty removedProperty )
{
getOrCreateGraphState().removeProperty( removedProperty );
hasChanges = true;
}
@Override
public void nodeDoAddLabel( int labelId, long nodeId )
{
getOrCreateLabelStateNodeDiffSets( labelId ).add( nodeId );
getOrCreateNodeStateLabelDiffSets( nodeId ).add( labelId );
hasChanges = true;
}
@Override
public void nodeDoRemoveLabel( int labelId, long nodeId )
{
getOrCreateLabelStateNodeDiffSets( labelId ).remove( nodeId );
getOrCreateNodeStateLabelDiffSets( nodeId ).remove( labelId );
hasChanges = true;
}
@Override
public void labelDoCreateForName( String labelName, int id )
{
if ( createdLabelTokens == null )
{
createdLabelTokens = new HashMap<>();
}
createdLabelTokens.put( id, labelName );
hasChanges = true;
}
@Override
public void propertyKeyDoCreateForName( String propertyKeyName, int id )
{
if ( createdPropertyKeyTokens == null )
{
createdPropertyKeyTokens = new HashMap<>();
}
createdPropertyKeyTokens.put( id, propertyKeyName );
hasChanges = true;
}
@Override
public void relationshipTypeDoCreateForName( String labelName, int id )
{
if ( createdRelationshipTypeTokens == null )
{
createdRelationshipTypeTokens = new HashMap<>();
}
createdRelationshipTypeTokens.put( id, labelName );
hasChanges = true;
}
@Override
public UpdateTriState labelState( long nodeId, int labelId )
{
return NODE_STATE.get( this, nodeId ).labelState( labelId );
}
@Override
public ReadableDiffSets<Long> nodesWithLabelChanged( int labelId )
{
return LABEL_STATE.get( this, labelId ).nodeDiffSets();
}
@Override
public void indexRuleDoAdd( IndexDescriptor descriptor )
{
DiffSets<IndexDescriptor> diff = indexChangesDiffSets();
if ( diff.unRemove( descriptor ) )
{
getOrCreateLabelState( descriptor.getLabelId() ).getOrCreateIndexChanges().unRemove( descriptor );
}
else
{
diff.add( descriptor );
getOrCreateLabelState( descriptor.getLabelId() ).getOrCreateIndexChanges().add( descriptor );
}
hasChanges = true;
}
@Override
public void constraintIndexRuleDoAdd( IndexDescriptor descriptor )
{
constraintIndexChangesDiffSets().add( descriptor );
getOrCreateLabelState( descriptor.getLabelId() ).getOrCreateConstraintIndexChanges().add( descriptor );
hasChanges = true;
}
@Override
public void indexDoDrop( IndexDescriptor descriptor )
{
indexChangesDiffSets().remove( descriptor );
getOrCreateLabelState( descriptor.getLabelId() ).getOrCreateIndexChanges().remove( descriptor );
hasChanges = true;
}
@Override
public void constraintIndexDoDrop( IndexDescriptor descriptor )
{
constraintIndexChangesDiffSets().remove( descriptor );
getOrCreateLabelState( descriptor.getLabelId() ).getOrCreateConstraintIndexChanges().remove( descriptor );
hasChanges = true;
}
@Override
public ReadableDiffSets<IndexDescriptor> indexDiffSetsByLabel( int labelId )
{
return LABEL_STATE.get( this, labelId ).indexChanges();
}
@Override
public ReadableDiffSets<IndexDescriptor> constraintIndexDiffSetsByLabel( int labelId )
{
return LABEL_STATE.get( this, labelId ).constraintIndexChanges();
}
@Override
public ReadableDiffSets<IndexDescriptor> indexChanges()
{
return ReadableDiffSets.Empty.ifNull( indexChanges );
}
private DiffSets<IndexDescriptor> indexChangesDiffSets()
{
if ( indexChanges == null )
{
indexChanges = new DiffSets<>();
}
return indexChanges;
}
@Override
public ReadableDiffSets<IndexDescriptor> constraintIndexChanges()
{
return ReadableDiffSets.Empty.ifNull( constraintIndexChanges );
}
private DiffSets<IndexDescriptor> constraintIndexChangesDiffSets()
{
if ( constraintIndexChanges == null )
{
constraintIndexChanges = new DiffSets<>();
}
return constraintIndexChanges;
}
@Override
public ReadableDiffSets<Long> nodesWithChangedProperty( int propertyKeyId, Object value )
{
return propertyChangesForNodes != null
? propertyChangesForNodes.changesForProperty( propertyKeyId, value )
: ReadableDiffSets.Empty.<Long>instance();
}
@Override
public ReadableDiffSets<Long> addedAndRemovedNodes()
{
return ReadableDiffSets.Empty.ifNull( nodes );
}
private DiffSets<Long> nodes()
{
if ( nodes == null )
{
nodes = new DiffSets<>();
}
return nodes;
}
@Override
public RelationshipIterator augmentRelationships( long nodeId, Direction direction, RelationshipIterator rels )
{
NodeState state;
if ( nodeStatesMap != null && (state = nodeStatesMap.get( nodeId )) != null )
{
rels = state.augmentRelationships( direction, rels );
// TODO: This should be handled by the augment call above
rels = addedAndRemovedRelationships().augmentWithRemovals( rels );
}
return rels;
}
@Override
public RelationshipIterator augmentRelationships( long nodeId, Direction direction, int[] types, RelationshipIterator rels )
{
NodeState state;
if ( nodeStatesMap != null && (state = nodeStatesMap.get( nodeId )) != null )
{
rels = state.augmentRelationships( direction, types, rels );
// TODO: This should be handled by the augment call above
rels = addedAndRemovedRelationships().augmentWithRemovals( rels );
}
return rels;
}
@Override
public PrimitiveLongIterator addedRelationships( long nodeId, int[] types, Direction direction )
{
return NODE_STATE.get( this, nodeId ).addedRelationships( direction, types );
}
@Override
public int augmentNodeDegree( long nodeId, int degree, Direction direction )
{
return NODE_STATE.get( this, nodeId ).augmentDegree( direction, degree );
}
@Override
public int augmentNodeDegree( long nodeId, int degree, Direction direction, int typeId )
{
return NODE_STATE.get( this, nodeId ).augmentDegree( direction, degree, typeId );
}
@Override
public PrimitiveIntIterator nodeRelationshipTypes( long nodeId )
{
return NODE_STATE.get( this, nodeId ).relationshipTypes();
}
@Override
public ReadableRelationshipDiffSets<Long> addedAndRemovedRelationships()
{
return ReadableRelationshipDiffSets.Empty.ifNull( relationships );
}
private RelationshipDiffSets<Long> relationships()
{
if ( relationships == null )
{
relationships = new RelationshipDiffSets<>( this );
}
return relationships;
}
@Override
public Iterable<RelationshipState> modifiedRelationships()
{
return RELATIONSHIP_STATE.values( this );
}
private LabelState.Mutable getOrCreateLabelState( int labelId )
{
return LABEL_STATE.getOrCreate( this, labelId ) ;
}
private NodeState.Mutable getOrCreateNodeState( long nodeId )
{
return NODE_STATE.getOrCreate( this, nodeId );
}
private RelationshipState.Mutable getOrCreateRelationshipState( long relationshipId )
{
return RELATIONSHIP_STATE.getOrCreate( this, relationshipId );
}
private GraphState getOrCreateGraphState()
{
if ( graphState == null )
{
graphState = new GraphState();
}
return graphState;
}
@Override
public void constraintDoAdd( UniquenessConstraint constraint, long indexId )
{
constraintsChangesDiffSets().add( constraint );
createdConstraintIndexesByConstraint().put( constraint, indexId );
getOrCreateLabelState( constraint.label() ).getOrCreateConstraintsChanges().add( constraint );
hasChanges = true;
}
@Override
public ReadableDiffSets<UniquenessConstraint> constraintsChangesForLabelAndProperty( int labelId, final int propertyKey )
{
return LABEL_STATE.get( this, labelId ).constraintsChanges().filterAdded(
new Predicate<UniquenessConstraint>()
{
@Override
public boolean test( UniquenessConstraint item )
{
return item.propertyKeyId() == propertyKey;
}
} );
}
@Override
public ReadableDiffSets<UniquenessConstraint> constraintsChangesForLabel( int labelId )
{
return LABEL_STATE.get( this, labelId ).constraintsChanges();
}
@Override
public ReadableDiffSets<UniquenessConstraint> constraintsChanges()
{
return ReadableDiffSets.Empty.ifNull( constraintsChanges );
}
private DiffSets<UniquenessConstraint> constraintsChangesDiffSets()
{
if ( constraintsChanges == null )
{
constraintsChanges = new DiffSets<>();
}
return constraintsChanges;
}
@Override
public void constraintDoDrop( UniquenessConstraint constraint )
{
constraintsChangesDiffSets().remove( constraint );
constraintIndexDoDrop( new IndexDescriptor( constraint.label(), constraint.propertyKeyId() ));
getOrCreateLabelState( constraint.label() ).getOrCreateConstraintsChanges().remove( constraint );
hasChanges = true;
}
@Override
public boolean constraintDoUnRemove( UniquenessConstraint constraint )
{
if ( constraintsChangesDiffSets().unRemove( constraint ) )
{
getOrCreateLabelState( constraint.label() ).getOrCreateConstraintsChanges().unRemove( constraint );
return true;
}
return false;
}
@Override
public boolean constraintIndexDoUnRemove( IndexDescriptor index )
{
if ( constraintIndexChangesDiffSets().unRemove( index ) )
{
LABEL_STATE.getOrCreate( this, index.getLabelId() ).getOrCreateConstraintIndexChanges().unRemove( index );
return true;
}
return false;
}
@Override
public Iterable<IndexDescriptor> constraintIndexesCreatedInTx()
{
if ( createdConstraintIndexesByConstraint != null && !createdConstraintIndexesByConstraint.isEmpty() )
{
return map( new Function<UniquenessConstraint,IndexDescriptor>()
{
@Override
public IndexDescriptor apply( UniquenessConstraint constraint )
{
return new IndexDescriptor( constraint.label(), constraint.propertyKeyId() );
}
}, createdConstraintIndexesByConstraint.keySet() );
}
return Iterables.empty();
}
@Override
public Long indexCreatedForConstraint( UniquenessConstraint constraint )
{
return createdConstraintIndexesByConstraint == null ? null :
createdConstraintIndexesByConstraint.get( constraint );