-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeRelationshipCursor.java
318 lines (287 loc) · 11.8 KB
/
NodeRelationshipCursor.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
/*
* 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.store;
import java.util.function.Consumer;
import java.util.function.IntPredicate;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.InvalidRecordException;
import org.neo4j.kernel.impl.store.RelationshipGroupStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.storageengine.api.Direction;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import static org.neo4j.function.Predicates.ALWAYS_TRUE_INT;
import static org.neo4j.function.Predicates.any;
import static org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP;
import static org.neo4j.kernel.impl.store.record.Record.NULL_REFERENCE;
import static org.neo4j.kernel.impl.store.record.RecordLoad.FORCE;
/**
* Cursor over the chain of relationships from one node.
* <p/>
* This cursor handles both dense and non-dense nodes as source.
*/
public class NodeRelationshipCursor extends AbstractIteratorRelationshipCursor
{
private final RelationshipGroupRecord groupRecord;
private final RelationshipGroupStore relationshipGroupStore;
private final Consumer<NodeRelationshipCursor> instanceCache;
private final PageCursor groupStorePageCursor;
private boolean isDense;
private long relationshipId;
private long fromNodeId;
private Direction direction;
private IntPredicate allowedTypes;
private int groupChainIndex;
private boolean end;
public NodeRelationshipCursor( RelationshipStore relationshipStore,
RelationshipGroupStore relationshipGroupStore,
Consumer<NodeRelationshipCursor> instanceCache,
LockService lockService )
{
super( relationshipStore, lockService );
this.relationshipGroupStore = relationshipGroupStore;
this.groupRecord = relationshipGroupStore.newRecord();
this.groupStorePageCursor = relationshipGroupStore.newPageCursor();
this.instanceCache = instanceCache;
}
public NodeRelationshipCursor init( boolean isDense, long firstRelId, long fromNodeId, Direction direction,
ReadableTransactionState state )
{
PrimitiveLongIterator addedNodeRelationships = addedNodeRelationships( fromNodeId, direction, null, state );
return init( isDense, firstRelId, fromNodeId, direction, ALWAYS_TRUE_INT, state, addedNodeRelationships );
}
public NodeRelationshipCursor init( boolean isDense, long firstRelId, long fromNodeId, Direction direction,
int[] allowedTypes, ReadableTransactionState state )
{
PrimitiveLongIterator addedNodeRelationships =
addedNodeRelationships( fromNodeId, direction, allowedTypes, state );
return init( isDense, firstRelId, fromNodeId, direction, any( allowedTypes ), state, addedNodeRelationships );
}
private NodeRelationshipCursor init( boolean isDense, long firstRelId, long fromNodeId, Direction direction,
IntPredicate allowedTypes, ReadableTransactionState state, PrimitiveLongIterator addedNodeRelationships )
{
internalInitTxState( state, addedNodeRelationships );
this.isDense = isDense;
this.relationshipId = firstRelId;
this.fromNodeId = fromNodeId;
this.direction = direction;
this.allowedTypes = allowedTypes;
this.end = false;
if ( isDense && relationshipId != Record.NO_NEXT_RELATIONSHIP.intValue() )
{
relationshipGroupStore.readRecord( firstRelId, groupRecord, FORCE, groupStorePageCursor );
relationshipId = nextChainStart();
}
else
{
relationshipId = firstRelId;
}
return this;
}
private PrimitiveLongIterator addedNodeRelationships( long fromNodeId, Direction direction,
int[] allowedTypes, ReadableTransactionState state )
{
if ( state == null )
{
return null;
}
return allowedTypes == null ? state.getNodeState( fromNodeId ).getAddedRelationships( direction )
: state.getNodeState( fromNodeId ).getAddedRelationships( direction, allowedTypes );
}
@Override
protected boolean doFetchNext()
{
while ( relationshipId != NO_NEXT_RELATIONSHIP.intValue() )
{
RelationshipRecord record =
relationshipStore.readRecord( relationshipId, relationshipRecord, FORCE, cursor );
// If we end up on a relationship record that isn't in use there's a good chance there
// have been a concurrent transaction deleting this record under our feet. Since we don't
// reuse relationship ids we can still trust the pointers in this unused record and try
// to chase a used record down the line.
try
{
// Direction check
if ( record.inUse() )
{
if ( direction != Direction.BOTH )
{
switch ( direction )
{
case INCOMING:
{
if ( record.getSecondNode() != fromNodeId )
{
continue;
}
break;
}
case OUTGOING:
{
if ( record.getFirstNode() != fromNodeId )
{
continue;
}
break;
}
default:
throw new IllegalStateException( "Unknown direction: " + direction );
}
}
// Type check
if ( !allowedTypes.test( record.getType() ) )
{
continue;
}
return true;
}
}
finally
{
// Pick next relationship
if ( record.getFirstNode() == fromNodeId )
{
relationshipId = record.getFirstNextRel();
}
else if ( record.getSecondNode() == fromNodeId )
{
relationshipId = record.getSecondNextRel();
}
else
{
throw new InvalidRecordException( "While loading relationships for Node[" + fromNodeId +
"] a Relationship[" + record.getId() +
"] was encountered that had startNode:" +
" " +
record.getFirstNode() + " and endNode: " +
record.getSecondNode() +
", i.e. which had neither start nor end node as the node we're loading relationships for" );
}
// If there are no more relationships, and this is from a dense node, then
// traverse the next group
if ( relationshipId == NO_NEXT_RELATIONSHIP.intValue() && isDense )
{
relationshipId = nextChainStart();
}
}
}
return false;
}
@Override
public void close()
{
super.close();
instanceCache.accept( this );
}
private long nextChainStart()
{
while ( !end )
{
// We check inUse flag here since we can actually follow pointers in unused records
// to guard for and overcome concurrent deletes in the relationship group chain
if ( groupRecord.inUse() && allowedTypes.test( groupRecord.getType() ) )
{
// Go to the next chain (direction) within this group
while ( groupChainIndex < GROUP_CHAINS.length )
{
GroupChain groupChain = GROUP_CHAINS[groupChainIndex++];
long chainStart = groupChain.chainStart( groupRecord );
if ( !NULL_REFERENCE.is( chainStart ) &&
(direction == Direction.BOTH || groupChain.matchesDirection( direction )) )
{
return chainStart;
}
}
}
// Go to the next group
if ( !NULL_REFERENCE.is( groupRecord.getNext() ) )
{
relationshipGroupStore.readRecord( groupRecord.getNext(), groupRecord, FORCE, groupStorePageCursor );
}
else
{
end = true;
}
groupChainIndex = 0;
}
return NULL_REFERENCE.intValue();
}
private enum GroupChain
{
OUT
{
@Override
long chainStart( RelationshipGroupRecord groupRecord )
{
return groupRecord.getFirstOut();
}
@Override
boolean matchesDirection( Direction direction )
{
return direction == Direction.OUTGOING;
}
},
IN
{
@Override
long chainStart( RelationshipGroupRecord groupRecord )
{
return groupRecord.getFirstIn();
}
@Override
boolean matchesDirection( Direction direction )
{
return direction == Direction.INCOMING;
}
},
LOOP
{
@Override
long chainStart( RelationshipGroupRecord groupRecord )
{
return groupRecord.getFirstLoop();
}
@Override
boolean matchesDirection( Direction direction )
{
return true;
}
};
abstract long chainStart( RelationshipGroupRecord groupRecord );
abstract boolean matchesDirection( Direction direction );
}
private static final GroupChain[] GROUP_CHAINS = GroupChain.values();
@Override
public String toString()
{
return String
.format( "RelationShipItem[id=%d, type=%d, start=%d, end=%d]", id(), type(), startNode(), endNode() );
}
@Override
public void dispose()
{
super.dispose();
groupStorePageCursor.close();
}
}