-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StoreReadLayer.java
390 lines (327 loc) · 15.7 KB
/
StoreReadLayer.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
/*
* 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.storageengine.api;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.IntPredicate;
import org.neo4j.collection.primitive.PrimitiveIntIterator;
import org.neo4j.collection.primitive.PrimitiveIntSet;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.LabelNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException;
import org.neo4j.kernel.api.exceptions.schema.TooManyLabelsException;
import org.neo4j.kernel.api.index.InternalIndexState;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.DegreeVisitor;
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.schema.PopulationProgress;
/**
* Abstraction for reading committed data from {@link StorageEngine store}.
*/
public interface StoreReadLayer
{
/**
* @return new {@link StorageStatement}, which can be used after a call to {@link StorageStatement#acquire()}.
*/
StorageStatement newStatement();
/**
* @param labelId label to list indexes for.
* @return {@link NewIndexDescriptor} associated with the given {@code labelId}.
*/
Iterator<NewIndexDescriptor> indexesGetForLabel( int labelId );
/**
* @return all {@link NewIndexDescriptor} in storage.
*/
Iterator<NewIndexDescriptor> indexesGetAll();
/**
* @param labelId label to list indexes related to uniqueness constraints for.
* @return {@link NewIndexDescriptor} related to uniqueness constraints associated with the given {@code labelId}.
*/
Iterator<NewIndexDescriptor> uniquenessIndexesGetForLabel( int labelId );
/**
* @return all {@link NewIndexDescriptor} related to uniqueness constraints.
*/
Iterator<NewIndexDescriptor> uniquenessIndexesGetAll();
/**
* @param index {@link NewIndexDescriptor} to get related uniqueness constraint for.
* @return schema rule id of uniqueness constraint that owns the given {@code index}, or {@code null}
* if the given index isn't related to a uniqueness constraint.
* @throws SchemaRuleNotFoundException if there's no such index matching the given {@code index} in storage.
*/
Long indexGetOwningUniquenessConstraintId( NewIndexDescriptor index )
throws SchemaRuleNotFoundException;
/**
* @param index {@link NewIndexDescriptor} to get schema rule id for.
* @return schema rule id for matching index.
* @throws SchemaRuleNotFoundException if no such index exists in storage.
*/
long indexGetCommittedId( NewIndexDescriptor index )
throws SchemaRuleNotFoundException;
/**
* @return iterator with property keys of all stored graph properties.
*/
PrimitiveIntIterator graphGetPropertyKeys();
/**
* @param propertyKeyId property key id to get graph property for.
* @return property value of graph property with key {@code propertyKeyId}, or {@code null} if not found.
*/
Object graphGetProperty( int propertyKeyId );
/**
* @return all stored graph properties.
*/
Iterator<StorageProperty> graphGetAllProperties();
/**
* @param descriptor describing the label and property key (or keys) defining the requested constraint.
* @return node property constraints associated with the label and one or more property keys token ids.
*/
Iterator<ConstraintDescriptor> constraintsGetForSchema( SchemaDescriptor descriptor );
boolean constraintExists( ConstraintDescriptor descriptor );
/**
* @param labelId label token id.
* @return node property constraints associated with the label token id.
*/
Iterator<ConstraintDescriptor> constraintsGetForLabel( int labelId );
/**
* @param typeId relationship type token id .
* @return relationship property constraints associated with the relationship type token id.
*/
Iterator<ConstraintDescriptor> constraintsGetForRelationshipType( int typeId );
/**
* @return all stored property constraints.
*/
Iterator<ConstraintDescriptor> constraintsGetAll();
PrimitiveLongIterator nodesGetForLabel( StorageStatement statement, int labelId );
/**
* Looks for a stored index by given {@code descriptor}
*
* @param descriptor a description of the node .
* @return {@link NewIndexDescriptor} for matching index, or {@code null} if not found. TODO should throw exception.
*/
NewIndexDescriptor indexGetForLabelAndPropertyKey( LabelSchemaDescriptor descriptor );
/**
* Returns state of a stored index.
*
* @param descriptor {@link LabelSchemaDescriptor} to get state for.
* @return {@link InternalIndexState} for index.
* @throws IndexNotFoundKernelException if index not found.
*/
InternalIndexState indexGetState( NewIndexDescriptor descriptor ) throws IndexNotFoundKernelException;
/**
* @param descriptor {@link LabelSchemaDescriptor} to get population progress for.
* @return progress of index population, which is the initial state of an index when it's created.
* @throws IndexNotFoundKernelException if index not found.
*/
PopulationProgress indexGetPopulationProgress( LabelSchemaDescriptor descriptor ) throws IndexNotFoundKernelException;
/**
* Returns any failure that happened during population or operation of an index. Such failures
* are persisted and can be accessed even after restart.
*
* @param descriptor {@link LabelSchemaDescriptor} to get failure for.
* @return failure of an index, or {@code null} if index is working as it should.
* @throws IndexNotFoundKernelException if index not found.
*/
String indexGetFailure( LabelSchemaDescriptor descriptor ) throws IndexNotFoundKernelException;
/**
* @param labelName name of label.
* @return token id of label.
*/
int labelGetForName( String labelName );
/**
* @param labelId label id to get name for.
* @return label name for given label id.
* @throws LabelNotFoundKernelException if no label by {@code labelId} was found.
*/
String labelGetName( int labelId ) throws LabelNotFoundKernelException;
/**
* @param propertyKeyName name of property key.
* @return token id of property key.
*/
int propertyKeyGetForName( String propertyKeyName );
/**
* Gets property key token id for the given {@code propertyKeyName}, or creates one if there is no
* existing property key with the given name.
*
* @param propertyKeyName name of property key.
* @return property key token id for the given name, created if need be.
*/
int propertyKeyGetOrCreateForName( String propertyKeyName );
/**
* @param propertyKeyId property key to get name for.
* @return property key name for given property key id.
* @throws PropertyKeyIdNotFoundKernelException if no property key by {@code propertyKeyId} was found.
*/
String propertyKeyGetName( int propertyKeyId ) throws PropertyKeyIdNotFoundKernelException;
/**
* @return all stored property key tokens.
*/
Iterator<Token> propertyKeyGetAllTokens();
/**
* @return all stored label tokens.
*/
Iterator<Token> labelsGetAllTokens();
/**
* @return all stored relationship type tokens.
*/
Iterator<Token> relationshipTypeGetAllTokens();
/**
* @param relationshipTypeName name of relationship type.
* @return token id of relationship type.
*/
int relationshipTypeGetForName( String relationshipTypeName );
/**
* @param relationshipTypeId relationship type id to get name for.
* @return relationship type name of given relationship type id.
* @throws RelationshipTypeIdNotFoundKernelException if no label by {@code labelId} was found.
*/
String relationshipTypeGetName( int relationshipTypeId ) throws RelationshipTypeIdNotFoundKernelException;
/**
* Gets label token id for the given {@code labelName}, or creates one if there is no
* existing label with the given name.
*
* @param labelName name of label.
* @return label token id for the given name, created if need be.
* @throws TooManyLabelsException if creating this label would have exceeded storage limitations for
* number of stored labels.
*/
int labelGetOrCreateForName( String labelName ) throws TooManyLabelsException;
/**
* Gets relationship type token id for the given {@code relationshipTypeName}, or creates one if there is no
* existing relationship type with the given name.
*
* @param relationshipTypeName name of relationship type.
* @return relationship type token id for the given name, created if need be.
*/
int relationshipTypeGetOrCreateForName( String relationshipTypeName );
/**
* Visits data about a relationship. The given {@code relationshipVisitor} will be notified.
*
* @param relationshipId the id of the relationship to access.
* @param relationshipVisitor {@link RelationshipVisitor} which will see the relationship data.
* @throws EntityNotFoundException if no relationship exists by the given {@code relationshipId}.
*/
<EXCEPTION extends Exception> void relationshipVisit( long relationshipId,
RelationshipVisitor<EXCEPTION> relationshipVisitor ) throws EntityNotFoundException, EXCEPTION;
/**
* @return ids of all stored nodes.
*/
PrimitiveLongIterator nodesGetAll();
/**
* @return ids of all stored relationships. The returned iterator can optionally visit data about
* each relationship returned.
*/
RelationshipIterator relationshipsGetAll();
Cursor<RelationshipItem> nodeGetRelationships( StorageStatement statement, NodeItem nodeItem, Direction direction );
Cursor<RelationshipItem> nodeGetRelationships( StorageStatement statement, NodeItem nodeItem, Direction direction,
IntPredicate typeIds );
Cursor<PropertyItem> nodeGetProperties( StorageStatement statement, NodeItem node );
Cursor<PropertyItem> nodeGetProperty( StorageStatement statement, NodeItem node, int propertyKeyId );
Cursor<PropertyItem> relationshipGetProperties( StorageStatement statement, RelationshipItem relationship );
Cursor<PropertyItem> relationshipGetProperty( StorageStatement statement, RelationshipItem relationshipItem, int propertyKeyId );
/**
* Reserves a node id for future use to store a node. The reason for it being exposed here is that
* internal ids of nodes and relationships are publicly accessible all the way out to the user.
* This will likely change in the future though.
*
* @return a reserved node id for future use.
*/
long reserveNode();
/**
* Reserves a relationship id for future use to store a relationship. The reason for it being exposed here is that
* internal ids of nodes and relationships are publicly accessible all the way out to the user.
* This will likely change in the future though.
*
* @return a reserved relationship id for future use.
*/
long reserveRelationship();
/**
* Releases a previously {@link #reserveNode() reserved} node id if it turns out to not actually being used,
* for example in the event of a transaction rolling back.
*
* @param id reserved node id to release.
*/
void releaseNode( long id );
/**
* Releases a previously {@link #reserveRelationship() reserved} relationship id if it turns out to not
* actually being used, for example in the event of a transaction rolling back.
*
* @param id reserved relationship id to release.
*/
void releaseRelationship( long id );
/**
* Returns number of stored nodes labeled with the label represented by {@code labelId}.
*
* @param labelId label id to match.
* @return number of stored nodes with this label.
*/
long countsForNode( int labelId );
/**
* Returns number of stored relationships of a certain {@code typeId} whose start/end nodes are labeled
* with the {@code startLabelId} and {@code endLabelId} respectively.
*
* @param startLabelId label id of start nodes to match.
* @param typeId relationship type id to match.
* @param endLabelId label id of end nodes to match.
* @return number of stored relationships matching these criteria.
*/
long countsForRelationship( int startLabelId, int typeId, int endLabelId );
/**
* Returns size of index, i.e. number of entities in that index.
*
* @param descriptor {@link LabelSchemaDescriptor} to return size for.
* @return number of entities in the given index.
* @throws IndexNotFoundKernelException if no such index exists.
*/
long indexSize( LabelSchemaDescriptor descriptor ) throws IndexNotFoundKernelException;
/**
* Returns percentage of values in the given {@code index} are unique. A value of {@code 1.0} means that
* all values in the index are unique, e.g. that there are no duplicate values. A value of, say {@code 0.9}
* means that 10% of the values are duplicates.
*
* @param descriptor {@link LabelSchemaDescriptor} to get uniqueness percentage for.
* @return percentage of values being unique in this index, max {@code 1.0} for all unique.
* @throws IndexNotFoundKernelException if no such index exists.
*/
double indexUniqueValuesPercentage( LabelSchemaDescriptor descriptor ) throws IndexNotFoundKernelException;
long nodesGetCount();
long relationshipsGetCount();
int labelCount();
int propertyKeyCount();
int relationshipTypeCount();
DoubleLongRegister indexUpdatesAndSize( LabelSchemaDescriptor descriptor, DoubleLongRegister target )
throws IndexNotFoundKernelException;
DoubleLongRegister indexSample( LabelSchemaDescriptor descriptor, DoubleLongRegister target )
throws IndexNotFoundKernelException;
boolean nodeExists( long id );
PrimitiveIntSet relationshipTypes( StorageStatement statement, NodeItem node );
void degrees( StorageStatement statement, NodeItem nodeItem, DegreeVisitor visitor );
int degreeRelationshipsInGroup( StorageStatement storeStatement, long id, long groupId, Direction direction,
Integer relType );
<T> T getOrCreateSchemaDependantState( Class<T> type, Function<StoreReadLayer, T> factory );
}