-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StoreAbstractNodeCursor.java
585 lines (527 loc) · 18.9 KB
/
StoreAbstractNodeCursor.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
/*
* 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.IntSupplier;
import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntObjectMap;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.cursor.Cursor;
import org.neo4j.cursor.IntValue;
import org.neo4j.kernel.api.AssertOpen;
import org.neo4j.kernel.api.cursor.NodeItemHelper;
import org.neo4j.kernel.impl.locking.Lock;
import org.neo4j.kernel.impl.locking.LockService;
import org.neo4j.kernel.impl.store.InvalidRecordException;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.RecordCursors;
import org.neo4j.kernel.impl.store.RecordStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.NodeRecord;
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.kernel.impl.util.InstanceCache;
import org.neo4j.storageengine.api.DegreeItem;
import org.neo4j.storageengine.api.Direction;
import org.neo4j.storageengine.api.LabelItem;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.PropertyItem;
import org.neo4j.storageengine.api.RelationshipItem;
import static org.neo4j.kernel.impl.locking.LockService.NO_LOCK_SERVICE;
import static org.neo4j.kernel.impl.store.record.RecordLoad.CHECK;
import static org.neo4j.kernel.impl.store.record.RecordLoad.FORCE;
/**
* Base cursor for nodes.
*/
public abstract class StoreAbstractNodeCursor extends NodeItemHelper implements Cursor<NodeItem>, NodeItem
{
protected final NodeRecord nodeRecord;
protected final NodeStore nodeStore;
protected final RelationshipStore relationshipStore;
protected final RecordStore<RelationshipGroupRecord> relationshipGroupStore;
protected final StoreStatement storeStatement;
private final LockService lockService;
private final InstanceCache<StoreLabelCursor> labelCursor;
private final InstanceCache<StoreSingleLabelCursor> singleLabelCursor;
private final InstanceCache<StoreNodeRelationshipCursor> nodeRelationshipCursor;
private final InstanceCache<StoreSinglePropertyCursor> singlePropertyCursor;
private final InstanceCache<StorePropertyCursor> allPropertyCursor;
protected final RecordCursors cursors;
private AssertOpen assertOpen;
public StoreAbstractNodeCursor( NodeRecord nodeRecord,
final NeoStores neoStores,
final StoreStatement storeStatement,
final RecordCursors cursors,
final LockService lockService )
{
this.nodeRecord = nodeRecord;
this.cursors = cursors;
this.nodeStore = neoStores.getNodeStore();
this.relationshipStore = neoStores.getRelationshipStore();
this.relationshipGroupStore = neoStores.getRelationshipGroupStore();
this.storeStatement = storeStatement;
this.lockService = lockService;
labelCursor = new InstanceCache<StoreLabelCursor>()
{
@Override
protected StoreLabelCursor create()
{
return new StoreLabelCursor( cursors.label(), this );
}
};
singleLabelCursor = new InstanceCache<StoreSingleLabelCursor>()
{
@Override
protected StoreSingleLabelCursor create()
{
return new StoreSingleLabelCursor( cursors.label(), this );
}
};
nodeRelationshipCursor = new InstanceCache<StoreNodeRelationshipCursor>()
{
@Override
protected StoreNodeRelationshipCursor create()
{
return new StoreNodeRelationshipCursor( relationshipStore.newRecord(),
relationshipGroupStore.newRecord(), this, cursors, lockService );
}
};
singlePropertyCursor = new InstanceCache<StoreSinglePropertyCursor>()
{
@Override
protected StoreSinglePropertyCursor create()
{
return new StoreSinglePropertyCursor( cursors, this );
}
};
allPropertyCursor = new InstanceCache<StorePropertyCursor>()
{
@Override
protected StorePropertyCursor create()
{
return new StorePropertyCursor( cursors, allPropertyCursor );
}
};
}
protected void initialize( AssertOpen assertOpen )
{
this.assertOpen = assertOpen;
}
@Override
public NodeItem get()
{
return this;
}
@Override
public long id()
{
return nodeRecord.getId();
}
@Override
public Cursor<LabelItem> labels()
{
return labelCursor.get().init( nodeRecord );
}
@Override
public Cursor<LabelItem> label( int labelId )
{
return singleLabelCursor.get().init( nodeRecord, labelId );
}
private Lock shortLivedReadLock()
{
Lock lock = lockService.acquireNodeLock( nodeRecord.getId(), LockService.LockType.READ_LOCK );
if ( lockService != NO_LOCK_SERVICE )
{
boolean success = false;
try
{
// It's safer to re-read the node record here, specifically nextProp, after acquiring the lock
if ( !cursors.node().next( nodeRecord.getId(), nodeRecord, CHECK ) )
{
// So it looks like the node has been deleted. The current behavior of NodeStore#loadRecord
// is to only set the inUse field on loading an unused record. This should (and will)
// change to be more of a centralized behavior by the stores. Anyway, setting this pointer
// to the primitive equivalent of null the property cursor will just look empty from the
// outside and the releasing of the lock will be done as usual.
nodeRecord.setNextProp( Record.NO_NEXT_PROPERTY.intValue() );
}
success = true;
}
finally
{
if ( !success )
{
lock.release();
}
}
}
return lock;
}
@Override
public Cursor<PropertyItem> properties()
{
return allPropertyCursor.get().init( nodeRecord.getNextProp(), shortLivedReadLock(), assertOpen );
}
@Override
public Cursor<PropertyItem> property( int propertyKeyId )
{
return singlePropertyCursor.get().init( nodeRecord.getNextProp(), propertyKeyId, shortLivedReadLock(),
assertOpen );
}
@Override
public Cursor<RelationshipItem> relationships( Direction direction )
{
return nodeRelationshipCursor.get().init( nodeRecord.isDense(), nodeRecord.getNextRel(), nodeRecord.getId(),
direction, assertOpen );
}
@Override
public Cursor<RelationshipItem> relationships( Direction direction, int... relTypes )
{
return nodeRelationshipCursor.get().init( nodeRecord.isDense(), nodeRecord.getNextRel(), nodeRecord.getId(),
direction, assertOpen, relTypes );
}
@Override
public Cursor<IntSupplier> relationshipTypes()
{
if ( nodeRecord.isDense() )
{
return new Cursor<IntSupplier>()
{
private long groupId = nodeRecord.getNextRel();
private final IntValue value = new IntValue();
private final RelationshipGroupRecord group = relationshipGroupStore.newRecord();
@Override
public boolean next()
{
while ( groupId != Record.NO_NEXT_RELATIONSHIP.intValue() )
{
boolean groupRecordInUse = cursors.relationshipGroup().next( groupId, group, FORCE );
groupId = group.getNext();
if ( groupRecordInUse )
{
value.setValue( group.getType() );
return true;
}
}
return false;
}
@Override
public void close()
{
}
@Override
public IntSupplier get()
{
return value;
}
};
}
else
{
final Cursor<RelationshipItem> relationships = relationships( Direction.BOTH );
return new Cursor<IntSupplier>()
{
private final PrimitiveIntSet foundTypes = Primitive.intSet( 5 );
private final IntValue value = new IntValue();
@Override
public boolean next()
{
while ( relationships.next() )
{
if ( !foundTypes.contains( relationships.get().type() ) )
{
foundTypes.add( relationships.get().type() );
value.setValue( relationships.get().type() );
return true;
}
}
return false;
}
@Override
public void close()
{
}
@Override
public IntSupplier get()
{
return value;
}
};
}
}
@Override
public int degree( Direction direction )
{
if ( nodeRecord.isDense() )
{
long groupId = nodeRecord.getNextRel();
long count = 0;
RelationshipGroupRecord group = relationshipGroupStore.newRecord();
RelationshipRecord relationship = relationshipStore.newRecord();
while ( groupId != Record.NO_NEXT_RELATIONSHIP.intValue() )
{
boolean groupRecordInUse = cursors.relationshipGroup().next( groupId, group, FORCE );
if ( groupRecordInUse )
{
count += nodeDegreeByDirection( group, direction, relationship );
}
groupId = group.getNext();
}
return (int) count;
}
else
{
try ( Cursor<RelationshipItem> relationship = relationships( direction ) )
{
int count = 0;
while ( relationship.next() )
{
count++;
}
return count;
}
}
}
@Override
public int degree( Direction direction, int relType )
{
if ( nodeRecord.isDense() )
{
long groupId = nodeRecord.getNextRel();
RelationshipGroupRecord group = relationshipGroupStore.newRecord();
RelationshipRecord relationship = relationshipStore.newRecord();
while ( groupId != Record.NO_NEXT_RELATIONSHIP.intValue() )
{
boolean groupRecordInUse = cursors.relationshipGroup().next( groupId, group, FORCE );
if ( groupRecordInUse && group.getType() == relType )
{
return (int) nodeDegreeByDirection( group, direction, relationship );
}
groupId = group.getNext();
}
return 0;
}
else
{
try ( Cursor<RelationshipItem> relationship = relationships( direction, relType ) )
{
int count = 0;
while ( relationship.next() )
{
count++;
}
return count;
}
}
}
@Override
public Cursor<DegreeItem> degrees()
{
if ( nodeRecord.isDense() )
{
long groupId = nodeRecord.getNextRel();
return new DegreeItemDenseCursor( groupId );
}
else
{
final PrimitiveIntObjectMap<int[]> degrees = Primitive.intObjectMap( 5 );
try ( Cursor<RelationshipItem> relationship = relationships( Direction.BOTH ) )
{
while ( relationship.next() )
{
RelationshipItem rel = relationship.get();
int[] byType = degrees.get( rel.type() );
if ( byType == null )
{
degrees.put( rel.type(), byType = new int[3] );
}
byType[directionOf( nodeRecord.getId(), rel.id(), rel.startNode(), rel.endNode() ).ordinal()]++;
}
}
final PrimitiveIntIterator keys = degrees.iterator();
return new DegreeItemIterator( keys, degrees );
}
}
@Override
public boolean isDense()
{
return nodeRecord.isDense();
}
private long nodeDegreeByDirection( RelationshipGroupRecord group, Direction direction,
RelationshipRecord relationship )
{
long loopCount = countByFirstPrevPointer( group.getFirstLoop(), relationship );
switch ( direction )
{
case OUTGOING:
return countByFirstPrevPointer( group.getFirstOut(), relationship ) + loopCount;
case INCOMING:
return countByFirstPrevPointer( group.getFirstIn(), relationship ) + loopCount;
case BOTH:
return countByFirstPrevPointer( group.getFirstOut(), relationship ) +
countByFirstPrevPointer( group.getFirstIn(), relationship ) + loopCount;
default:
throw new IllegalArgumentException( direction.name() );
}
}
private long countByFirstPrevPointer( long relationshipId, RelationshipRecord record )
{
if ( relationshipId == Record.NO_NEXT_RELATIONSHIP.intValue() )
{
return 0;
}
cursors.relationship().next( relationshipId, record, FORCE );
if ( record.getFirstNode() == nodeRecord.getId() )
{
return record.getFirstPrevRel();
}
if ( record.getSecondNode() == nodeRecord.getId() )
{
return record.getSecondPrevRel();
}
throw new InvalidRecordException( "Node " + nodeRecord.getId() + " neither start nor end node of " + record );
}
private Direction directionOf( long nodeId, long relationshipId, long startNode, long endNode )
{
if ( startNode == nodeId )
{
return endNode == nodeId ? Direction.BOTH : Direction.OUTGOING;
}
if ( endNode == nodeId )
{
return Direction.INCOMING;
}
throw new InvalidRecordException( "Node " + nodeId + " neither start nor end node of relationship " +
relationshipId + " with startNode:" + startNode + " and endNode:" + endNode );
}
private static class DegreeItemIterator implements Cursor<DegreeItem>, DegreeItem
{
private final PrimitiveIntObjectMap<int[]> degrees;
private PrimitiveIntIterator keys;
private int type;
private int outgoing;
private int incoming;
public DegreeItemIterator( PrimitiveIntIterator keys, PrimitiveIntObjectMap<int[]> degrees )
{
this.keys = keys;
this.degrees = degrees;
}
@Override
public void close()
{
keys = null;
}
@Override
public int type()
{
return type;
}
@Override
public long outgoing()
{
return outgoing;
}
@Override
public long incoming()
{
return incoming;
}
@Override
public DegreeItem get()
{
if ( keys == null )
{
throw new IllegalStateException();
}
return this;
}
@Override
public boolean next()
{
if ( keys != null && keys.hasNext() )
{
type = keys.next();
int[] degreeValues = degrees.get( type );
outgoing = degreeValues[0] + degreeValues[2];
incoming = degreeValues[1] + degreeValues[2];
return true;
}
keys = null;
return false;
}
}
private class DegreeItemDenseCursor implements Cursor<DegreeItem>, DegreeItem
{
private long groupId;
private int type;
private long outgoing;
private long incoming;
private final RelationshipGroupRecord group = relationshipGroupStore.newRecord();
private final RelationshipRecord relationship = relationshipStore.newRecord();
public DegreeItemDenseCursor( long groupId )
{
this.groupId = groupId;
}
@Override
public boolean next()
{
while ( groupId != Record.NO_NEXT_RELATIONSHIP.intValue() )
{
boolean groupRecordInUse = cursors.relationshipGroup().next( groupId, group, FORCE );
groupId = group.getNext();
if ( groupRecordInUse )
{
this.type = group.getType();
long loop = countByFirstPrevPointer( group.getFirstLoop(), relationship );
this.outgoing = countByFirstPrevPointer( group.getFirstOut(), relationship ) + loop;
this.incoming = countByFirstPrevPointer( group.getFirstIn(), relationship ) + loop;
return true;
}
}
return false;
}
@Override
public void close()
{
}
@Override
public DegreeItem get()
{
return this;
}
@Override
public int type()
{
return type;
}
@Override
public long outgoing()
{
return outgoing;
}
@Override
public long incoming()
{
return incoming;
}
}
}