-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransactionCountingStateVisitor.java
166 lines (149 loc) · 6.61 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
/*
* 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 org.neo4j.collection.primitive.PrimitiveIntCollection;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveIntVisitor;
import org.neo4j.cursor.Cursor;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.kernel.impl.api.CountsRecordState;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.RelationshipItem;
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.kernel.api.ReadOperations.ANY_LABEL;
import static org.neo4j.kernel.api.ReadOperations.ANY_RELATIONSHIP_TYPE;
public class TransactionCountingStateVisitor extends TxStateVisitor.Delegator
{
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 );
storeLayer.nodeGetSingleCursor( statement, id, ReadableTransactionState.EMPTY )
.forAll( this::decrementCountForLabelsAndRelationships );
super.visitDeletedNode( id );
}
private void decrementCountForLabelsAndRelationships( NodeItem node )
{
PrimitiveIntSet labelIds = node.labels();
labelIds.visitKeys( labelId ->
{
counts.incrementNodeCount( labelId, -1 );
return false;
} );
storeLayer.degrees( statement, node,
( type, out, in ) -> updateRelationshipsCountsFromDegrees( labelIds, type, -out, -in ) );
}
@Override
public void visitCreatedRelationship( long id, int type, long startNode, long endNode )
throws ConstraintValidationException
{
updateRelationshipCount( startNode, type, endNode, 1 );
super.visitCreatedRelationship( id, type, startNode, endNode );
}
@Override
public void visitDeletedRelationship( long id )
{
try ( Cursor<RelationshipItem> cursor = storeLayer
.relationshipCursor( statement, id, ReadableTransactionState.EMPTY ) )
{
if ( !cursor.next() )
{
throw new IllegalStateException( "Relationship being deleted should exist along with its nodes." );
}
RelationshipItem relationship = cursor.get();
updateRelationshipCount( relationship.startNode(), relationship.type(), relationship.endNode(), -1 );
}
super.visitDeletedRelationship( id );
}
@Override
public void visitNodeLabelChanges( long id, final Set<Integer> added, final Set<Integer> removed )
throws ConstraintValidationException
{
// 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
storeLayer.nodeGetSingleCursor( statement, id, ReadableTransactionState.EMPTY )
.forAll( node -> storeLayer.degrees( statement, node, ( type, out, in ) ->
{
added.forEach( label -> updateRelationshipsCountsFromDegrees( type, label, out, in ) );
removed.forEach(
label -> updateRelationshipsCountsFromDegrees( type, label, -out, -in ) );
} ) );
}
super.visitNodeLabelChanges( id, added, removed );
}
private void updateRelationshipsCountsFromDegrees( PrimitiveIntCollection labels, int type, long out, long in )
{
labels.visitKeys( label -> updateRelationshipsCountsFromDegrees( type, label, out, in ) );
}
private boolean updateRelationshipsCountsFromDegrees( int type, int label, long out, long in )
{
// untyped
counts.incrementRelationshipCount( label, ANY_RELATIONSHIP_TYPE, ANY_LABEL, out );
counts.incrementRelationshipCount( ANY_LABEL, ANY_RELATIONSHIP_TYPE, label, in );
// typed
counts.incrementRelationshipCount( label, type, ANY_LABEL, out );
counts.incrementRelationshipCount( ANY_LABEL, type, label, in );
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 )
{
storeLayer.nodeGetSingleCursor( statement, nodeId, txState ).forAll( node -> node.labels().visitKeys( visitor ) );
}
}