-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConstraintEnforcingEntityOperations.java
533 lines (472 loc) · 21.9 KB
/
ConstraintEnforcingEntityOperations.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
/*
* 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;
import java.util.Iterator;
import org.neo4j.collection.primitive.PrimitiveIntCollection;
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.helpers.Strings;
import org.neo4j.kernel.api.exceptions.EntityNotFoundException;
import org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException;
import org.neo4j.kernel.api.exceptions.KernelException;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.exceptions.legacyindex.AutoIndexingKernelException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException;
import org.neo4j.kernel.api.exceptions.schema.AlreadyIndexedException;
import org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException;
import org.neo4j.kernel.api.exceptions.schema.CreateConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropConstraintFailureException;
import org.neo4j.kernel.api.exceptions.schema.DropIndexFailureException;
import org.neo4j.kernel.api.exceptions.schema.IndexBrokenKernelException;
import org.neo4j.kernel.api.exceptions.schema.UnableToValidateConstraintException;
import org.neo4j.kernel.api.exceptions.schema.UniquePropertyValueValidationException;
import org.neo4j.kernel.api.properties.DefinedProperty;
import org.neo4j.kernel.api.properties.Property;
import org.neo4j.kernel.api.schema.NodePropertyDescriptor;
import org.neo4j.kernel.api.schema_new.IndexQuery;
import org.neo4j.kernel.api.schema_new.LabelSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.RelationTypeSchemaDescriptor;
import org.neo4j.kernel.api.schema_new.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.NodeExistenceConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.RelExistenceConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.constaints.UniquenessConstraintDescriptor;
import org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor;
import org.neo4j.kernel.impl.api.operations.EntityOperations;
import org.neo4j.kernel.impl.api.operations.EntityReadOperations;
import org.neo4j.kernel.impl.api.operations.EntityWriteOperations;
import org.neo4j.kernel.impl.api.operations.SchemaReadOperations;
import org.neo4j.kernel.impl.api.operations.SchemaWriteOperations;
import org.neo4j.kernel.impl.api.store.NodeLoadingIterator;
import org.neo4j.kernel.impl.constraints.ConstraintSemantics;
import org.neo4j.kernel.impl.locking.LockTracer;
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.storageengine.api.Direction;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.PropertyItem;
import org.neo4j.storageengine.api.RelationshipItem;
import static org.neo4j.kernel.api.StatementConstants.NO_SUCH_NODE;
import static org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor.Type.UNIQUE;
import static org.neo4j.kernel.impl.locking.ResourceTypes.INDEX_ENTRY;
import static org.neo4j.kernel.impl.locking.ResourceTypes.indexEntryResourceId;
public class ConstraintEnforcingEntityOperations implements EntityOperations, SchemaWriteOperations
{
private final EntityWriteOperations entityWriteOperations;
private final EntityReadOperations entityReadOperations;
private final SchemaWriteOperations schemaWriteOperations;
private final SchemaReadOperations schemaReadOperations;
private final ConstraintSemantics constraintSemantics;
public ConstraintEnforcingEntityOperations(
ConstraintSemantics constraintSemantics, EntityWriteOperations entityWriteOperations,
EntityReadOperations entityReadOperations,
SchemaWriteOperations schemaWriteOperations,
SchemaReadOperations schemaReadOperations )
{
this.constraintSemantics = constraintSemantics;
this.entityWriteOperations = entityWriteOperations;
this.entityReadOperations = entityReadOperations;
this.schemaWriteOperations = schemaWriteOperations;
this.schemaReadOperations = schemaReadOperations;
}
@Override
public boolean nodeAddLabel( KernelStatement state, long nodeId, int labelId )
throws EntityNotFoundException, ConstraintValidationException
{
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
Iterator<ConstraintDescriptor> constraints = schemaReadOperations.constraintsGetForLabel( state, labelId );
while ( constraints.hasNext() )
{
ConstraintDescriptor constraint = constraints.next();
if ( constraint.type() == UNIQUE )
{
UniquenessConstraintDescriptor uniqueConstraint = (UniquenessConstraintDescriptor) constraint;
// TODO: Support composite indexes
Object propertyValue = nodeGetProperty( state, node, uniqueConstraint.schema().getPropertyId() );
if ( propertyValue != null )
{
// TODO: Support composite indexes
validateNoExistingNodeWithLabelAndProperty( state, uniqueConstraint, propertyValue, node.id() );
}
}
}
}
return entityWriteOperations.nodeAddLabel( state, nodeId, labelId );
}
@Override
public Property nodeSetProperty( KernelStatement state, long nodeId, DefinedProperty property )
throws EntityNotFoundException, AutoIndexingKernelException,
InvalidTransactionTypeKernelException, ConstraintValidationException
{
try ( Cursor<NodeItem> cursor = nodeCursorById( state, nodeId ) )
{
NodeItem node = cursor.get();
node.labels().visitKeys( labelId ->
{
int propertyKeyId = property.propertyKeyId();
Iterator<ConstraintDescriptor> constraints =
schemaReadOperations.constraintsGetForSchema( state,
SchemaDescriptorFactory.forLabel( labelId, propertyKeyId ) );
while ( constraints.hasNext() )
{
ConstraintDescriptor constraint = constraints.next();
if ( constraint.type() == UNIQUE )
{
validateNoExistingNodeWithLabelAndProperty(
state, (UniquenessConstraintDescriptor) constraint, property.value(), node.id() );
}
}
return false;
} );
}
return entityWriteOperations.nodeSetProperty( state, nodeId, property );
}
private void validateNoExistingNodeWithLabelAndProperty(
KernelStatement state,
UniquenessConstraintDescriptor constraint,
Object value,
long modifiedNode
) throws ConstraintValidationException
{
try
{
// TODO: Support composite constraints
NewIndexDescriptor index = constraint.ownedIndexDescriptor();
assertIndexOnline( state, index );
int labelId = index.schema().getLabelId();
int propertyId = index.schema().getPropertyId();
state.locks().optimistic().acquireExclusive(
state.lockTracer(),
INDEX_ENTRY,
indexEntryResourceId( labelId, propertyId, Strings.prettyPrint( value ) )
);
long existing = entityReadOperations.nodeGetFromUniqueIndexSeek( state, index, value );
if ( existing != NO_SUCH_NODE && existing != modifiedNode )
{
throw new UniquePropertyValueValidationException( constraint,
ConstraintValidationException.Phase.VALIDATION,
new IndexEntryConflictException( existing, NO_SUCH_NODE, value ) );
}
}
catch ( IndexNotFoundKernelException | IndexBrokenKernelException e )
{
throw new UnableToValidateConstraintException( constraint, e );
}
}
private void assertIndexOnline( KernelStatement state, NewIndexDescriptor indexDescriptor )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
switch ( schemaReadOperations.indexGetState( state, indexDescriptor ) )
{
case ONLINE:
return;
default:
throw new IndexBrokenKernelException( schemaReadOperations.indexGetFailure( state, indexDescriptor ) );
}
}
// Simply delegate the rest of the invocations
@Override
public void nodeDelete( KernelStatement state, long nodeId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
entityWriteOperations.nodeDelete( state, nodeId );
}
@Override
public int nodeDetachDelete( KernelStatement state, long nodeId ) throws EntityNotFoundException,
AutoIndexingKernelException, InvalidTransactionTypeKernelException, KernelException
{
return entityWriteOperations.nodeDetachDelete( state, nodeId );
}
@Override
public long relationshipCreate( KernelStatement statement,
int relationshipTypeId,
long startNodeId,
long endNodeId )
throws EntityNotFoundException
{
return entityWriteOperations.relationshipCreate( statement, relationshipTypeId, startNodeId, endNodeId );
}
@Override
public void relationshipDelete( KernelStatement state, long relationshipId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
entityWriteOperations.relationshipDelete( state, relationshipId );
}
@Override
public boolean nodeRemoveLabel( KernelStatement state, long nodeId, int labelId ) throws EntityNotFoundException
{
return entityWriteOperations.nodeRemoveLabel( state, nodeId, labelId );
}
@Override
public Property relationshipSetProperty( KernelStatement state,
long relationshipId,
DefinedProperty property ) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
return entityWriteOperations.relationshipSetProperty( state, relationshipId, property );
}
@Override
public Property graphSetProperty( KernelStatement state, DefinedProperty property )
{
return entityWriteOperations.graphSetProperty( state, property );
}
@Override
public Property nodeRemoveProperty( KernelStatement state, long nodeId, int propertyKeyId )
throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
return entityWriteOperations.nodeRemoveProperty( state, nodeId, propertyKeyId );
}
@Override
public Property relationshipRemoveProperty( KernelStatement state,
long relationshipId,
int propertyKeyId ) throws EntityNotFoundException, AutoIndexingKernelException, InvalidTransactionTypeKernelException
{
return entityWriteOperations.relationshipRemoveProperty( state, relationshipId, propertyKeyId );
}
@Override
public Property graphRemoveProperty( KernelStatement state, int propertyKeyId )
{
return entityWriteOperations.graphRemoveProperty( state, propertyKeyId );
}
@Override
public PrimitiveLongIterator nodesGetForLabel( KernelStatement state, int labelId )
{
return entityReadOperations.nodesGetForLabel( state, labelId );
}
@Override
public PrimitiveLongIterator indexQuery( KernelStatement statement, NewIndexDescriptor index,
IndexQuery[] predicates ) throws IndexNotFoundKernelException
{
return entityReadOperations.indexQuery( statement, index, predicates );
}
@Override
public long nodeGetFromUniqueIndexSeek(
KernelStatement state,
NewIndexDescriptor index,
Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
assertIndexOnline( state, index );
// TODO: Support composite index, either by allowing value to be an array, or by creating a new method
int labelId = index.schema().getLabelId();
int propertyKeyId = index.schema().getPropertyId();
String stringVal = "";
if ( null != value )
{
DefinedProperty property = Property.property( propertyKeyId, value );
stringVal = property.valueAsString();
}
// If we find the node - hold a shared lock. If we don't find a node - hold an exclusive lock.
// If locks are deferred than both shared and exclusive locks will be taken only at commit time.
Locks.Client locks = state.locks().optimistic();
LockTracer lockTracer = state.lockTracer();
long indexEntryId = indexEntryResourceId( labelId, propertyKeyId, stringVal );
locks.acquireShared( lockTracer, INDEX_ENTRY, indexEntryId );
long nodeId = entityReadOperations.nodeGetFromUniqueIndexSeek( state, index, value );
if ( NO_SUCH_NODE == nodeId )
{
locks.releaseShared( INDEX_ENTRY, indexEntryId );
locks.acquireExclusive( lockTracer, INDEX_ENTRY, indexEntryId );
nodeId = entityReadOperations.nodeGetFromUniqueIndexSeek( state, index, value );
if ( NO_SUCH_NODE != nodeId ) // we found it under the exclusive lock
{
// downgrade to a shared lock
locks.acquireShared( lockTracer, INDEX_ENTRY, indexEntryId );
locks.releaseExclusive( INDEX_ENTRY, indexEntryId );
}
}
return nodeId;
}
@Override
public long nodesCountIndexed( KernelStatement statement, NewIndexDescriptor index, long nodeId, Object value )
throws IndexNotFoundKernelException, IndexBrokenKernelException
{
return entityReadOperations.nodesCountIndexed( statement, index, nodeId, value );
}
@Override
public boolean graphHasProperty( KernelStatement state, int propertyKeyId )
{
return entityReadOperations.graphHasProperty( state, propertyKeyId );
}
@Override
public Object graphGetProperty( KernelStatement state, int propertyKeyId )
{
return entityReadOperations.graphGetProperty( state, propertyKeyId );
}
@Override
public PrimitiveIntIterator graphGetPropertyKeys( KernelStatement state )
{
return entityReadOperations.graphGetPropertyKeys( state );
}
@Override
public long nodeCreate( KernelStatement statement )
{
return entityWriteOperations.nodeCreate( statement );
}
@Override
public PrimitiveLongIterator nodesGetAll( KernelStatement state )
{
return entityReadOperations.nodesGetAll( state );
}
@Override
public PrimitiveLongIterator relationshipsGetAll( KernelStatement state )
{
return entityReadOperations.relationshipsGetAll( state );
}
@Override
public <EXCEPTION extends Exception> void relationshipVisit( KernelStatement statement,
long relId, RelationshipVisitor<EXCEPTION> visitor )
throws EntityNotFoundException, EXCEPTION
{
entityReadOperations.relationshipVisit( statement, relId, visitor );
}
@Override
public Cursor<NodeItem> nodeCursorById( KernelStatement statement, long nodeId ) throws EntityNotFoundException
{
return entityReadOperations.nodeCursorById( statement, nodeId );
}
@Override
public Cursor<RelationshipItem> relationshipCursorById( KernelStatement statement, long relId ) throws EntityNotFoundException
{
return entityReadOperations.relationshipCursorById( statement, relId );
}
@Override
public Cursor<RelationshipItem> relationshipCursorGetAll( KernelStatement statement )
{
return entityReadOperations.relationshipCursorGetAll( statement );
}
@Override
public Cursor<PropertyItem> nodeGetProperties( KernelStatement statement, NodeItem node )
{
return entityReadOperations.nodeGetProperties( statement, node );
}
@Override
public Object nodeGetProperty( KernelStatement statement, NodeItem node, int propertyKeyId )
{
return entityReadOperations.nodeGetProperty( statement, node, propertyKeyId );
}
@Override
public boolean nodeHasProperty( KernelStatement statement, NodeItem node, int propertyKeyId )
{
return entityReadOperations.nodeHasProperty( statement, node, propertyKeyId );
}
@Override
public PrimitiveIntCollection nodeGetPropertyKeys( KernelStatement statement, NodeItem node )
{
return entityReadOperations.nodeGetPropertyKeys( statement, node );
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node,
Direction direction )
{
return entityReadOperations.nodeGetRelationships( statement, node, direction );
}
@Override
public Cursor<RelationshipItem> nodeGetRelationships( KernelStatement statement, NodeItem node, Direction direction,
PrimitiveIntSet relTypes )
{
return entityReadOperations.nodeGetRelationships( statement, node, direction, relTypes );
}
@Override
public NewIndexDescriptor indexCreate( KernelStatement state, NodePropertyDescriptor descriptor )
throws AlreadyIndexedException, AlreadyConstrainedException
{
return schemaWriteOperations.indexCreate( state, descriptor );
}
@Override
public void indexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
schemaWriteOperations.indexDrop( state, descriptor );
}
@Override
public void uniqueIndexDrop( KernelStatement state, NewIndexDescriptor descriptor ) throws DropIndexFailureException
{
schemaWriteOperations.uniqueIndexDrop( state, descriptor );
}
@Override
public UniquenessConstraintDescriptor uniquePropertyConstraintCreate( KernelStatement state, LabelSchemaDescriptor descriptor )
throws AlreadyConstrainedException, CreateConstraintFailureException, AlreadyIndexedException
{
return schemaWriteOperations.uniquePropertyConstraintCreate( state, descriptor );
}
@Override
public NodeExistenceConstraintDescriptor nodePropertyExistenceConstraintCreate(
KernelStatement state,
LabelSchemaDescriptor descriptor
) throws AlreadyConstrainedException, CreateConstraintFailureException
{
Iterator<Cursor<NodeItem>> nodes = new NodeLoadingIterator( nodesGetForLabel( state, descriptor.getLabelId() ),
( id ) -> nodeCursorById( state, id ) );
constraintSemantics.validateNodePropertyExistenceConstraint( nodes, descriptor,
(node, propertyKey) -> entityReadOperations.nodeHasProperty(state, node, propertyKey) );
return schemaWriteOperations.nodePropertyExistenceConstraintCreate( state, descriptor );
}
@Override
public RelExistenceConstraintDescriptor relationshipPropertyExistenceConstraintCreate(
KernelStatement state,
RelationTypeSchemaDescriptor descriptor
) throws AlreadyConstrainedException, CreateConstraintFailureException
{
try ( Cursor<RelationshipItem> cursor = relationshipCursorGetAll( state ) )
{
constraintSemantics.validateExistenceConstraint( cursor, descriptor );
}
return schemaWriteOperations.relationshipPropertyExistenceConstraintCreate( state, descriptor );
}
@Override
public void constraintDrop( KernelStatement state, ConstraintDescriptor constraint )
throws DropConstraintFailureException
{
schemaWriteOperations.constraintDrop( state, constraint );
}
@Override
public long nodesGetCount( KernelStatement statement )
{
return entityReadOperations.nodesGetCount( statement );
}
@Override
public long relationshipsGetCount( KernelStatement statement )
{
return entityReadOperations.relationshipsGetCount( statement );
}
@Override
public boolean nodeExists( KernelStatement statement, long id )
{
return entityReadOperations.nodeExists( statement, id );
}
@Override
public PrimitiveIntSet relationshipTypes( KernelStatement statement, NodeItem nodeItem )
{
return entityReadOperations.relationshipTypes( statement, nodeItem );
}
@Override
public int degree( KernelStatement statement, NodeItem nodeItem, Direction direction )
{
return entityReadOperations.degree( statement, nodeItem, direction );
}
@Override
public int degree( KernelStatement statement, NodeItem nodeItem, Direction direction, int relType )
{
return entityReadOperations.degree( statement, nodeItem, direction, relType );
}
}