-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Read.java
395 lines (355 loc) · 13.9 KB
/
Read.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
/*
* 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.newapi;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.neo4j.internal.kernel.api.IndexPredicate;
import org.neo4j.internal.kernel.api.IndexReference;
import org.neo4j.internal.kernel.api.Scan;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.api.store.PropertyUtil;
import org.neo4j.kernel.impl.store.AbstractDynamicStore;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.RecordCursor;
import org.neo4j.kernel.impl.store.RecordStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.AbstractBaseRecord;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.kernel.impl.store.record.RelationshipGroupRecord;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.string.UTF8;
import org.neo4j.values.storable.ArrayValue;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
import static org.neo4j.kernel.impl.store.record.AbstractBaseRecord.NO_ID;
import static org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL;
public class Read implements org.neo4j.internal.kernel.api.Read
{
private final NeoStores stores;
private NodeStore nodeStore;
private RelationshipStore relationshipStore;
public Read( NeoStores stores )
{
this.stores = stores;
this.nodeStore = stores.getNodeStore();
this.relationshipStore = stores.getRelationshipStore();
}
@Override
public void nodeIndexSeek(
IndexReference index,
org.neo4j.internal.kernel.api.NodeValueIndexCursor cursor,
IndexPredicate... predicates )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void nodeIndexScan( IndexReference index, org.neo4j.internal.kernel.api.NodeValueIndexCursor cursor )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void nodeLabelScan( int label, org.neo4j.internal.kernel.api.NodeLabelIndexCursor cursor )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public Scan<org.neo4j.internal.kernel.api.NodeLabelIndexCursor> nodeLabelScan( int label )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void allNodesScan( org.neo4j.internal.kernel.api.NodeCursor cursor )
{
try
{
PageCursor pageCursor = nodeStore.openPageCursor( 0 );
((NodeCursor) cursor).scan( pageCursor );
}
catch ( IOException e )
{
throw new UnsupportedOperationException( "This exception is not implemented" );
}
}
@Override
public Scan<org.neo4j.internal.kernel.api.NodeCursor> allNodesScan()
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void singleNode( long reference, org.neo4j.internal.kernel.api.NodeCursor cursor )
{
try
{
PageCursor pageCursor = nodeStore.openPageCursor( reference );
((NodeCursor) cursor).single( reference, pageCursor );
}
catch ( IOException e )
{
throw new UnsupportedOperationException( "This exception is not implemented" );
}
}
@Override
public void singleRelationship( long reference, org.neo4j.internal.kernel.api.RelationshipScanCursor cursor )
{
try
{
PageCursor pageCursor = relationshipStore.openPageCursor( reference );
((RelationshipScanCursor) cursor).single( reference, pageCursor );
}
catch ( IOException e )
{
//TODO
throw new RuntimeException( e );
}
}
@Override
public void allRelationshipsScan( org.neo4j.internal.kernel.api.RelationshipScanCursor cursor )
{
try
{
PageCursor pageCursor = relationshipStore.openPageCursor( 0L );
((RelationshipScanCursor) cursor).scan( -1/*include all labels*/, pageCursor );
}
catch ( IOException e )
{
//TODO
throw new RuntimeException( e );
}
}
@Override
public Scan<org.neo4j.internal.kernel.api.RelationshipScanCursor> allRelationshipsScan()
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void relationshipLabelScan( int label, org.neo4j.internal.kernel.api.RelationshipScanCursor cursor )
{
try
{
PageCursor pageCursor = relationshipStore.openPageCursor( 0L );
((RelationshipScanCursor) cursor).scan( label, pageCursor );
}
catch ( IOException e )
{
//TODO
throw new RuntimeException( e );
}
}
@Override
public Scan<org.neo4j.internal.kernel.api.RelationshipScanCursor> relationshipLabelScan( int label )
{
throw new UnsupportedOperationException( "not implemented" );
}
@Override
public void relationshipGroups(
long nodeReference, long reference, org.neo4j.internal.kernel.api.RelationshipGroupCursor cursor )
{
if ( reference == NO_ID ) // there are no relationships for this node
{
cursor.close();
}
else if ( reference < NO_ID ) // the relationships for this node are not grouped
{
((RelationshipGroupCursor) cursor).buffer( nodeReference, invertReference( reference ) );
}
else // this is a normal group reference.
{
((RelationshipGroupCursor) cursor).direct( nodeReference, reference );
}
}
@Override
public void relationships(
long nodeReference, long reference, org.neo4j.internal.kernel.api.RelationshipTraversalCursor cursor )
{
/* TODO: There are actually five (5!) different ways a relationship traversal cursor can be initialized:
*
* 1. From a batched group in a detached way. This happens when the user manually retrieves the relationships
* references from the group cursor and passes it to this method and if the group cursor was based on having
* batched all the different types in the single (mixed) chain of relationships.
* In this case we should pass a reference marked with some flag to the first relationship in the chain that
* has the type of the current group in the group cursor. The traversal cursor then needs to read the type
* from that first record and use that type as a filter for when reading the rest of the chain.
* - NOTE: we probably have to do the same sort of filtering for direction - so we need a flag for that too.
*
* 2. From a batched group in a DIRECT way. This happens when the traversal cursor is initialized directly from
* the group cursor, in this case we can simply initialize the traversal cursor with the buffered state from
* the group cursor, so this method here does not have to be involved, and things become pretty simple.
*
* 3. Traversing all relationships - regardless of type - of a node that has grouped relationships. In this case
* the traversal cursor needs to traverse through the group records in order to get to the actual
* relationships. The initialization of the cursor (through this here method) should be with a FLAGGED
* reference to the (first) group record.
*
* 4. Traversing a single chain - this is what happens in the cases when
* a) Traversing all relationships of a node without grouped relationships.
* b) Traversing the relationships of a particular group of a node with grouped relationships.
*
* 5. There are no relationships - i.e. passing in NO_ID to this method.
*/
if ( reference == NO_ID ) // there are no relationships for this node
{
cursor.close();
}
else if ( reference < NO_ID ) // this reference is actually to a group record
{
((RelationshipTraversalCursor) cursor).groups( nodeReference, invertReference( reference ) );
}
else // this is a normal relationship reference
{
((RelationshipTraversalCursor) cursor).chain( nodeReference, reference );
}
}
@Override
public void nodeProperties( long reference, org.neo4j.internal.kernel.api.PropertyCursor cursor )
{
((PropertyCursor) cursor).init( reference );
}
@Override
public void relationshipProperties( long reference, org.neo4j.internal.kernel.api.PropertyCursor cursor )
{
((PropertyCursor) cursor).init( reference );
}
@Override
public void futureNodeReferenceRead( long reference )
{
}
@Override
public void futureRelationshipsReferenceRead( long reference )
{
}
@Override
public void futureNodePropertyReferenceRead( long reference )
{
}
@Override
public void futureRelationshipPropertyReferenceRead( long reference )
{
}
RecordCursor<DynamicRecord> labelCursor()
{
return newCursor( nodeStore.getDynamicLabelStore() );
}
private static <R extends AbstractBaseRecord> RecordCursor<R> newCursor( RecordStore<R> store )
{
return store.newRecordCursor( store.newRecord() ).acquire( store.getNumberOfReservedLowIds(), NORMAL );
}
void node( NodeRecord record, long reference, PageCursor pageCursor )
{
nodeStore.getRecordByCursor( reference, record, RecordLoad.CHECK, pageCursor );
}
void relationship( RelationshipRecord record, long reference, PageCursor pageCursor )
{
relationshipStore.getRecordByCursor( reference, record, RecordLoad.CHECK, pageCursor );
}
void relationship( RelationshipRecord record, long reference)
{
relationshipStore.getRecord( reference, record, RecordLoad.CHECK );
}
void property( PropertyRecord record, long reference )
{
stores.getPropertyStore().getRecord( reference, record, RecordLoad.NORMAL );
}
void group( RelationshipGroupRecord record, long reference )
{
throw new UnsupportedOperationException( "not implemented" );
}
long nodeHighMark()
{
return nodeStore.getHighestPossibleIdInUse();
}
long relationshipHighMark()
{
return relationshipStore.getHighestPossibleIdInUse();
}
TextValue string( PropertyCursor cursor, long reference )
{
ByteBuffer buffer =
cursor.buffer = readDynamic( stores.getPropertyStore().getStringStore(), reference, cursor.buffer );
buffer.flip();
return Values.stringValue( UTF8.decode( buffer.array(), 0, buffer.limit() ) );
}
ArrayValue array( PropertyCursor cursor, long reference )
{
ByteBuffer buffer =
cursor.buffer = readDynamic( stores.getPropertyStore().getArrayStore(), reference, cursor.buffer );
buffer.flip();
return PropertyUtil.readArrayFromBuffer( buffer );
}
/**
* Inverted references are used to signal that the reference is of a special type.
* <p>
* For example that it is a direct reference to a relationship record rather than a reference to relationship group
* record in {@link NodeCursor#relationshipGroupReference()}.
* <p>
* Since {@code -1} is used to encode {@link AbstractBaseRecord#NO_ID that the reference is invalid}, we reserve
* this value from the range by subtracting the reference from {@code -2} when inverting.
* <p>
* This function is its own inverse function.
*
* @param reference
* the reference to invert.
* @return the inverted reference.
*/
static long invertReference( long reference )
{
return -2 - reference;
}
private static ByteBuffer readDynamic( AbstractDynamicStore store, long reference, ByteBuffer buffer )
{
if ( buffer == null )
{
buffer = ByteBuffer.allocate( 512 );
}
else
{
buffer.clear();
}
DynamicRecord record = store.newRecord();
do
{
store.getRecord( reference, record, RecordLoad.NORMAL );
reference = record.getNextBlock();
byte[] data = record.getData();
if ( buffer.remaining() < data.length )
{
buffer = grow( buffer, data.length );
}
buffer.put( data, 0, data.length );
}
while ( reference != NO_ID );
return buffer;
}
private static ByteBuffer grow( ByteBuffer buffer, int required )
{
buffer.flip();
int capacity = buffer.capacity();
do
{
capacity *= 2;
}
while ( capacity - buffer.limit() < required );
return ByteBuffer.allocate( capacity ).order( ByteOrder.LITTLE_ENDIAN ).put( buffer );
}
}