-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransactionCountingStateVisitor.java
180 lines (163 loc) · 7.09 KB
/
TransactionCountingStateVisitor.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
/*
* 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.api.txstate;
import java.util.Set;
import java.util.function.IntConsumer;
import org.neo4j.collection.primitive.PrimitiveIntCollection;
import org.neo4j.collection.primitive.PrimitiveIntCollections;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveIntVisitor;
import org.neo4j.cursor.Cursor;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationKernelException;
import org.neo4j.kernel.impl.api.CountsRecordState;
import org.neo4j.kernel.impl.api.RelationshipDataExtractor;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import org.neo4j.storageengine.api.txstate.TxStateVisitor;
import static org.neo4j.collection.primitive.PrimitiveIntCollections.consume;
import static org.neo4j.kernel.api.ReadOperations.ANY_LABEL;
import static org.neo4j.kernel.api.ReadOperations.ANY_RELATIONSHIP_TYPE;
public class TransactionCountingStateVisitor extends TxStateVisitor.Delegator
{
private final RelationshipDataExtractor edge = new RelationshipDataExtractor();
private final StoreReadLayer storeLayer;
private final StorageStatement statement;
private final CountsRecordState counts;
private final ReadableTransactionState txState;
public TransactionCountingStateVisitor( TxStateVisitor next, StoreReadLayer storeLayer, StorageStatement statement,
ReadableTransactionState txState, CountsRecordState counts )
{
super( next );
this.storeLayer = storeLayer;
this.statement = statement;
this.txState = txState;
this.counts = counts;
}
@Override
public void visitCreatedNode( long id )
{
counts.incrementNodeCount( ANY_LABEL, 1 );
super.visitCreatedNode( id );
}
@Override
public void visitDeletedNode( long id )
{
counts.incrementNodeCount( ANY_LABEL, -1 );
statement.acquireSingleNodeCursor( id ).forAll( this::decrementCountForLabelsAndRelationships );
super.visitDeletedNode( id );
}
private void decrementCountForLabelsAndRelationships( NodeItem node )
{
PrimitiveIntSet labelIds = node.labels();
labelIds.visitKeys( labelId ->
{
counts.incrementNodeCount( labelId, -1 );
return true;
} );
node.degrees().forAll(
degree -> updateRelationshipsCountsFromDegrees( labelIds, degree.type(), -degree.outgoing(),
-degree.incoming() ) );
}
@Override
public void visitCreatedRelationship( long id, int type, long startNode, long endNode )
throws ConstraintValidationKernelException
{
updateRelationshipCount( startNode, type, endNode, 1 );
super.visitCreatedRelationship( id, type, startNode, endNode );
}
@Override
public void visitDeletedRelationship( long id )
{
try
{
storeLayer.relationshipVisit( id, edge );
updateRelationshipCount( edge.startNode(), edge.type(), edge.endNode(), -1 );
}
catch ( EntityNotFoundException e )
{
throw new IllegalStateException( "Relationship being deleted should exist along with its nodes.", e );
}
super.visitDeletedRelationship( id );
}
@Override
public void visitNodeLabelChanges( long id, final Set<Integer> added, final Set<Integer> removed )
throws ConstraintValidationKernelException
{
// update counts
if ( !(added.isEmpty() && removed.isEmpty()) )
{
for ( Integer label : added )
{
counts.incrementNodeCount( label, 1 );
}
for ( Integer label : removed )
{
counts.incrementNodeCount( label, -1 );
}
// get the relationship counts from *before* this transaction,
// the relationship changes will compensate for what happens during the transaction
statement.acquireSingleNodeCursor( id ).forAll( node -> node.degrees().forAll( degree ->
{
for ( Integer label : added )
{
updateRelationshipsCountsFromDegrees( degree.type(), label, degree.outgoing(), degree.incoming() );
}
for ( Integer label : removed )
{
updateRelationshipsCountsFromDegrees( degree.type(), label, -degree.outgoing(),
-degree.incoming() );
}
} ) );
}
super.visitNodeLabelChanges( id, added, removed );
}
private void updateRelationshipsCountsFromDegrees( PrimitiveIntCollection labels, int type, long outgoing,
long incoming )
{
labels.visitKeys( ( label ) -> updateRelationshipsCountsFromDegrees( type, label, outgoing, incoming ) );
}
private boolean updateRelationshipsCountsFromDegrees( int type, int label, long outgoing, long incoming )
{
// untyped
counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, outgoing );
counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, incoming );
// typed
counts.incrementRelationshipCount( label, type, ANY_LABEL, outgoing );
counts.incrementRelationshipCount( ANY_LABEL, type, label, incoming );
return false;
}
private void updateRelationshipCount( long startNode, int type, long endNode, int delta )
{
updateRelationshipsCountsFromDegrees( type, ANY_LABEL, delta, 0 );
visitLabels( startNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, delta, 0 ) );
visitLabels( endNode, ( labelId ) -> updateRelationshipsCountsFromDegrees( type, labelId, 0, delta ) );
}
private void visitLabels( long nodeId, PrimitiveIntVisitor<RuntimeException> visitor )
{
nodeCursor( statement, nodeId ).forAll( node -> node.labels().visitKeys( visitor ) );
}
private Cursor<NodeItem> nodeCursor( StorageStatement statement, long nodeId )
{
return txState.augmentSingleNodeCursor( statement.acquireSingleNodeCursor( nodeId ), nodeId );
}
}