-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ReadOperations.java
485 lines (393 loc) · 21.1 KB
/
ReadOperations.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
/*
* 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;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.graphdb.Direction;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexNotApplicableKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.legacyindex.LegacyIndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.kernel.api.proc.QualifiedName;
import org.neo4j.kernel.api.proc.UserFunctionSignature;
import org.neo4j.kernel.api.schema.IndexQuery;
import org.neo4j.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema.SchemaDescriptor;
import org.neo4j.kernel.api.schema.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.RelationshipVisitor;
import org.neo4j.kernel.impl.api.store.RelationshipIterator;
import org.neo4j.register.Register.DoubleLongRegister;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.PropertyItem;
import org.neo4j.storageengine.api.RelationshipItem;
import org.neo4j.storageengine.api.Token;
import org.neo4j.storageengine.api.lock.ResourceType;
import org.neo4j.storageengine.api.schema.PopulationProgress;
import org.neo4j.values.Value;
/**
* Defines all types of read operations that can be done from the {@link KernelAPI}.
*/
public interface ReadOperations
{
int ANY_LABEL = -1;
int ANY_RELATIONSHIP_TYPE = -1;
int NO_SUCH_LABEL = -1;
int NO_SUCH_PROPERTY_KEY = -1;
//===========================================
//== TOKEN OPERATIONS =======================
//===========================================
/** Returns a label id for a label name. If the label doesn't exist, {@link #NO_SUCH_LABEL} will be returned. */
int labelGetForName( String labelName );
/** Returns the label name for the given label id. */
String labelGetName( int labelId ) throws LabelNotFoundKernelException;
/** Returns the labels currently stored in the database * */
Iterator<Token> labelsGetAllTokens(); // TODO: Token is a store level concern, should not make it this far up the stack
/**
* Returns a property key id for the given property key. If the property key doesn't exist,
* {@link StatementConstants#NO_SUCH_PROPERTY_KEY} will be returned.
*/
int propertyKeyGetForName( String propertyKeyName );
/** Returns the name of a property given its property key id */
String propertyKeyGetName( int propertyKeyId ) throws PropertyKeyIdNotFoundKernelException;
/** Returns the property keys currently stored in the database */
Iterator<Token> propertyKeyGetAllTokens();
int relationshipTypeGetForName( String relationshipTypeName );
String relationshipTypeGetName( int relationshipTypeId ) throws RelationshipTypeIdNotFoundKernelException;
/** Returns the relationship types currently stored in the database */
Iterator<Token> relationshipTypesGetAllTokens();
int labelCount();
int propertyKeyCount();
int relationshipTypeCount();
//===========================================
//== DATA OPERATIONS ========================
//===========================================
/**
* @param labelId the label id of the label that returned nodes are guaranteed to have
* @return ids of all nodes that have the given label
*/
PrimitiveLongIterator nodesGetForLabel( int labelId );
/**
* Queries the given index with the given index query.
*
* @param index the index to query against.
* @param predicates array of the {@link IndexQuery} predicates to query for.
* @return ids of the matching nodes
* @throws org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException if no such index is found.
*/
PrimitiveLongIterator indexQuery( IndexDescriptor index, IndexQuery... predicates )
throws IndexNotFoundKernelException, IndexNotApplicableKernelException;
/**
* @return an iterator over all nodes in the database.
*/
PrimitiveLongIterator nodesGetAll();
/**
* @return an iterator over all relationships in the database.
*/
PrimitiveLongIterator relationshipsGetAll();
RelationshipIterator nodeGetRelationships( long nodeId, Direction direction, int[] relTypes )
throws EntityNotFoundException;
RelationshipIterator nodeGetRelationships( long nodeId, Direction direction ) throws EntityNotFoundException;
/**
* Returns node id of unique node found in the given unique index for value or
* {@link StatementConstants#NO_SUCH_NODE} if the index does not contain a
* matching node.
* <p/>
* If a node is found, a READ lock for the index entry will be held. If no node
* is found (if {@link StatementConstants#NO_SUCH_NODE} was returned), a WRITE
* lock for the index entry will be held. This is to facilitate unique creation
* of nodes, to build get-or-create semantics on top of this method.
*
* @throws org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException if no such index found.
*/
long nodeGetFromUniqueIndexSeek( IndexDescriptor index, IndexQuery.ExactPredicate... predicates ) throws IndexNotFoundKernelException,
IndexBrokenKernelException, IndexNotApplicableKernelException;
long nodesCountIndexed( IndexDescriptor index, long nodeId, Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException;
boolean nodeExists( long nodeId );
/**
* Checks if a node is labeled with a certain label or not. Returns
* {@code true} if the node is labeled with the label, otherwise {@code false.}
*/
boolean nodeHasLabel( long nodeId, int labelId ) throws EntityNotFoundException;
int nodeGetDegree( long nodeId, Direction direction, int relType ) throws EntityNotFoundException;
int nodeGetDegree( long nodeId, Direction direction ) throws EntityNotFoundException;
boolean nodeIsDense( long nodeId ) throws EntityNotFoundException;
/**
* Returns all labels set on node with id {@code nodeId}.
* If the node has no labels an empty {@link Iterable} will be returned.
*/
PrimitiveIntIterator nodeGetLabels( long nodeId ) throws EntityNotFoundException;
PrimitiveIntIterator nodeGetPropertyKeys( long nodeId ) throws EntityNotFoundException;
PrimitiveIntIterator relationshipGetPropertyKeys( long relationshipId ) throws EntityNotFoundException;
PrimitiveIntIterator graphGetPropertyKeys();
PrimitiveIntIterator nodeGetRelationshipTypes( long nodeId ) throws EntityNotFoundException;
boolean nodeHasProperty( long nodeId, int propertyKeyId ) throws EntityNotFoundException;
Value nodeGetProperty( long nodeId, int propertyKeyId ) throws EntityNotFoundException;
boolean relationshipHasProperty( long relationshipId, int propertyKeyId ) throws EntityNotFoundException;
Value relationshipGetProperty( long relationshipId, int propertyKeyId ) throws EntityNotFoundException;
boolean graphHasProperty( int propertyKeyId );
Value graphGetProperty( int propertyKeyId );
<EXCEPTION extends Exception> void relationshipVisit( long relId, RelationshipVisitor<EXCEPTION> visitor )
throws EntityNotFoundException, EXCEPTION;
long nodesGetCount();
long relationshipsGetCount();
//===========================================
//== CURSOR ACCESS OPERATIONS ===============
//===========================================
Cursor<NodeItem> nodeCursorById( long nodeId ) throws EntityNotFoundException;
Cursor<RelationshipItem> relationshipCursorById( long relId ) throws EntityNotFoundException;
Cursor<PropertyItem> nodeGetProperties( NodeItem node );
Cursor<PropertyItem> relationshipGetProperties( RelationshipItem relationship );
//===========================================
//== SCHEMA OPERATIONS ======================
//===========================================
/** Returns the index rule for the given LabelSchemaDescriptor. */
IndexDescriptor indexGetForSchema( LabelSchemaDescriptor descriptor )
throws SchemaRuleNotFoundException;
/** Get all indexes for a label. */
Iterator<IndexDescriptor> indexesGetForLabel( int labelId );
/** Returns all indexes. */
Iterator<IndexDescriptor> indexesGetAll();
/** Retrieve the state of an index. */
InternalIndexState indexGetState( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Retrieve the population progress of an index. */
PopulationProgress indexGetPopulationProgress( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Get the index size. */
long indexSize( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Calculate the index unique values percentage (range: {@code 0.0} exclusive to {@code 1.0} inclusive). */
double indexUniqueValuesSelectivity( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/** Returns the failure description of a failed index. */
String indexGetFailure( IndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/**
* Get all constraints applicable to label and propertyKey.
*/
Iterator<ConstraintDescriptor> constraintsGetForSchema( SchemaDescriptor descriptor );
/**
* Get all constraints applicable to label.
*/
Iterator<ConstraintDescriptor> constraintsGetForLabel( int labelId );
/**
* Get all constraints applicable to relationship type.
*/
Iterator<ConstraintDescriptor> constraintsGetForRelationshipType( int typeId );
/**
* Get all constraints.
*/
Iterator<ConstraintDescriptor> constraintsGetAll();
/**
* Get the owning constraint for a constraint index. Returns null if the index does not have an owning constraint.
*/
Long indexGetOwningUniquenessConstraintId( IndexDescriptor index ) throws SchemaRuleNotFoundException;
<K, V> V schemaStateGetOrCreate( K key, Function<K, V> creator );
void schemaStateFlush();
//===========================================
//== LOCKING OPERATIONS =====================
//===========================================
void acquireExclusive( ResourceType type, long id );
void acquireShared( ResourceType type, long id );
void releaseExclusive( ResourceType type, long id );
void releaseShared( ResourceType type, long id );
//===========================================
//== LEGACY INDEX OPERATIONS ================
//===========================================
Map<String, String> nodeLegacyIndexGetConfiguration( String indexName )
throws LegacyIndexNotFoundKernelException;
Map<String, String> relationshipLegacyIndexGetConfiguration( String indexName )
throws LegacyIndexNotFoundKernelException;
LegacyIndexHits nodeLegacyIndexGet( String indexName, String key, Object value )
throws LegacyIndexNotFoundKernelException;
LegacyIndexHits nodeLegacyIndexQuery( String indexName, String key, Object queryOrQueryObject )
throws LegacyIndexNotFoundKernelException;
LegacyIndexHits nodeLegacyIndexQuery( String indexName, Object queryOrQueryObject )
throws LegacyIndexNotFoundKernelException;
/**
* @param startNode -1 if ignored.
* @param endNode -1 if ignored.
*/
LegacyIndexHits relationshipLegacyIndexGet( String name, String key, Object valueOrNull, long startNode,
long endNode ) throws LegacyIndexNotFoundKernelException;
/**
* @param startNode -1 if ignored.
* @param endNode -1 if ignored.
*/
LegacyIndexHits relationshipLegacyIndexQuery( String indexName, String key, Object queryOrQueryObject,
long startNode, long endNode )
throws LegacyIndexNotFoundKernelException;
/**
* @param startNode -1 if ignored.
* @param endNode -1 if ignored.
*/
LegacyIndexHits relationshipLegacyIndexQuery( String indexName, Object queryOrQueryObject,
long startNode, long endNode )
throws LegacyIndexNotFoundKernelException;
String[] nodeLegacyIndexesGetAll();
String[] relationshipLegacyIndexesGetAll();
//===========================================
//== COUNTS OPERATIONS ======================
//===========================================
/**
* The number of nodes in the graph, including anything changed in the transaction state.
*
* If the label parameter is {@link #ANY_LABEL}, this method returns the total number of nodes in the graph, i.e.
* {@code MATCH (n) RETURN count(n)}.
*
* If the label parameter is set to any other value, this method returns the number of nodes that has that label,
* i.e. {@code MATCH (n:LBL) RETURN count(n)}.
*
* @param labelId the label to get the count for, or {@link #ANY_LABEL} to get the total number of nodes.
* @return the number of matching nodes in the graph.
*/
long countsForNode( int labelId );
/**
* The number of nodes in the graph, without taking into account anything in the transaction state.
*
* If the label parameter is {@link #ANY_LABEL}, this method returns the total number of nodes in the graph, i.e.
* {@code MATCH (n) RETURN count(n)}.
*
* If the label parameter is set to any other value, this method returns the number of nodes that has that label,
* i.e. {@code MATCH (n:LBL) RETURN count(n)}.
*
* @param labelId the label to get the count for, or {@link #ANY_LABEL} to get the total number of nodes.
* @return the number of matching nodes in the graph.
*/
long countsForNodeWithoutTxState( int labelId );
/**
* The number of relationships in the graph, including anything changed in the transaction state.
*
* Returns the number of relationships in the graph that matches the specified pattern,
* {@code (:startLabelId)-[:typeId]->(:endLabelId)}, like so:
*
* <table>
* <thead>
* <tr><th>{@code startLabelId}</th><th>{@code typeId}</th> <th>{@code endLabelId}</th>
* <td></td> <th>Pattern</th> <td></td></tr>
* </thead>
* <tdata>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@code REL}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r:REL]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@code LHS}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code (:LHS)-[r]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@code RHS}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r]->(:RHS)}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@code LHS}</td> <td>{@code REL}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code (:LHS)-[r:REL]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@code REL}</td> <td>{@code RHS}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r:REL]->(:RHS)}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* </tdata>
* </table>
*
* @param startLabelId the label of the start node of relationships to get the count for, or {@link #ANY_LABEL}.
* @param typeId the type of relationships to get a count for, or {@link #ANY_RELATIONSHIP_TYPE}.
* @param endLabelId the label of the end node of relationships to get the count for, or {@link #ANY_LABEL}.
* @return the number of matching relationships in the graph.
*/
long countsForRelationship( int startLabelId, int typeId, int endLabelId );
/**
* The number of relationships in the graph, without taking into account anything in the transaction state.
*
* Returns the number of relationships in the graph that matches the specified pattern,
* {@code (:startLabelId)-[:typeId]->(:endLabelId)}, like so:
*
* <table>
* <thead>
* <tr><th>{@code startLabelId}</th><th>{@code typeId}</th> <th>{@code endLabelId}</th>
* <td></td> <th>Pattern</th> <td></td></tr>
* </thead>
* <tdata>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@code REL}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r:REL]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@code LHS}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code (:LHS)-[r]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@link #ANY_RELATIONSHIP_TYPE}</td> <td>{@code RHS}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r]->(:RHS)}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@code LHS}</td> <td>{@code REL}</td> <td>{@link #ANY_LABEL}</td>
* <td>{@code MATCH}</td> <td>{@code (:LHS)-[r:REL]->()}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* <tr>
* <td>{@link #ANY_LABEL}</td> <td>{@code REL}</td> <td>{@code RHS}</td>
* <td>{@code MATCH}</td> <td>{@code ()-[r:REL]->(:RHS)}</td> <td>{@code RETURN count(r)}</td>
* </tr>
* </tdata>
* </table>
*
* @param startLabelId the label of the start node of relationships to get the count for, or {@link #ANY_LABEL}.
* @param typeId the type of relationships to get a count for, or {@link #ANY_RELATIONSHIP_TYPE}.
* @param endLabelId the label of the end node of relationships to get the count for, or {@link #ANY_LABEL}.
* @return the number of matching relationships in the graph.
*/
long countsForRelationshipWithoutTxState( int startLabelId, int typeId, int endLabelId );
DoubleLongRegister indexUpdatesAndSize( IndexDescriptor index, DoubleLongRegister target )
throws IndexNotFoundKernelException;
DoubleLongRegister indexSample( IndexDescriptor index, DoubleLongRegister target )
throws IndexNotFoundKernelException;
//===========================================
//== PROCEDURE OPERATIONS ===================
//===========================================
/** Fetch a procedure given its signature. */
ProcedureSignature procedureGet( QualifiedName name ) throws ProcedureException;
/** Fetch a function given its signature, or <code>empty</code> if no such function exists*/
Optional<UserFunctionSignature> functionGet( QualifiedName name );
/** Fetch an aggregation function given its signature, or <code>empty</code> if no such function exists*/
Optional<UserFunctionSignature> aggregationFunctionGet( QualifiedName name );
/** Fetch all registered procedures */
Set<UserFunctionSignature> functionsGetAll();
/** Fetch all registered procedures */
Set<ProcedureSignature> proceduresGetAll();
}