/** * Sorts indexes by type, returning first GENERAL indexes, followed by UNIQUE. Implementation is not suitable in * hot path. * * @param indexes Indexes to sort * @return sorted indexes */ static Iterator<IndexReference> sortByType( Iterator<IndexReference> indexes ) { List<IndexReference> materialized = Iterators.asList( indexes ); return Iterators.concat( Iterators.filter( i -> !i.isUnique(), materialized.iterator() ), Iterators.filter( IndexReference::isUnique, materialized.iterator() ) ); }
@Override public Iterator<T> apply( Iterator<? extends T> source ) { Iterator<T> result = (Iterator) source; if ( (removedElements != null && !removedElements.isEmpty()) || (addedElements != null && !addedElements.isEmpty()) ) { ensureFilterHasBeenCreated(); result = Iterators.filter( filter, result ); } if ( addedElements != null && !addedElements.isEmpty() ) { result = Iterators.concat( result, addedElements.iterator() ); } return result; }
public Iterator<ConstraintDescriptor> constraintsForLabel( final int label ) { return Iterators.filter( SchemaDescriptorPredicates.hasLabel( label ), constraints() ); }
public Iterator<ConstraintDescriptor> constraintsForRelationshipType( final int relTypeId ) { return Iterators.filter( SchemaDescriptorPredicates.hasRelType( relTypeId ), constraints() ); }
public Iterator<ConstraintDescriptor> constraintsForSchema( SchemaDescriptor descriptor ) { return Iterators.filter( SchemaDescriptor.equalTo( descriptor ), constraints() ); }
private Value generateUniqueValue( IndexEntryUpdate<IndexDescriptor>[] updates ) { return filter( skipExisting( updates ), valueCreatorUtil.randomUpdateGenerator( random ) ).next().values()[0]; }
filter( SchemaDescriptor.equalTo( schema ), diffSets.getAdded().iterator() ); if ( fromTxState.hasNext() )
private Relationship findRelationship( Node startNode, final Node endNode, final RelationshipDataLine relationship ) { return Iterators.singleOrNull( Iterators.filter( item -> item.getEndNode().equals( endNode ) && item.getProperty( "name" ).equals( relationship.name ), startNode.getRelationships( withName( relationship.type ) ).iterator() ) ); }
filter( SchemaDescriptor.equalTo( descriptor ), diffSets.getAdded().iterator() ); if ( fromTxState.hasNext() )
IndexDescriptor indexGetForSchema( SchemaDescriptor descriptor ) { IndexDescriptor indexDescriptor = storageReader.indexGetForSchema( descriptor ); Iterator<IndexDescriptor> indexes = iterator( indexDescriptor ); if ( ktx.hasTxStateWithChanges() ) { indexes = filter( SchemaDescriptor.equalTo( descriptor ), ktx.txState().indexDiffSetsBySchema( descriptor ).apply( indexes ) ); } return singleOrNull( indexes ); }
Iterator<ConstraintDescriptor> constraintsGetForProperty( int propertyKey ) { ktx.assertOpen(); Iterator<ConstraintDescriptor> constraints = storageReader.constraintsGetAll(); if ( ktx.hasTxStateWithChanges() ) { constraints = ktx.txState().constraintsChanges().apply( constraints ); } return Iterators.map( this::lockConstraint, Iterators.filter( hasProperty( propertyKey ), constraints ) ); }
@Test public void shouldIndexChange() throws Exception { // given IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType(); processAll( updates ); Iterator<IndexEntryUpdate<IndexDescriptor>> generator = filter( skipExisting( updates ), valueCreatorUtil.randomUpdateGenerator( random ) ); for ( int i = 0; i < updates.length; i++ ) { IndexEntryUpdate<IndexDescriptor> update = updates[i]; Value newValue = generator.next().values()[0]; updates[i] = change( update.getEntityId(), indexDescriptor, update.values()[0], newValue ); } // when processAll( updates ); // then forceAndCloseAccessor(); verifyUpdates( updates ); }
SchemaRule constraintRule = single( filter( rule -> rule instanceof ConstraintRule, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, constraintRule.getId() ); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setOwnerNull( schemaStore, (StoreIndexDescriptor) indexRule );
@Test public void shouldDropUniquenessConstraintWhereConstraintRecordIsMissing() throws Exception { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof ConstraintRule, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, indexRule.getId() ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { // We don't use single() here, because it is okay for the schema cache reload to clean up after us. db.schema().getConstraints( label ).forEach( ConstraintDefinition::drop ); db.schema().getIndexes( label ).forEach( IndexDefinition::drop ); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
@Test public void shouldDropUniquenessConstraintWithBackingIndexHavingNoOwner() throws Exception { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setOwnerNull( schemaStore, (StoreIndexDescriptor) indexRule ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { single( db.schema().getConstraints( label ).iterator() ).drop(); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
@Test public void shouldDropUniquenessConstraintWithBackingIndexNotInUse() { // given try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label ).assertPropertyIsUnique( key ).create(); tx.success(); } // when intentionally breaking the schema by setting the backing index rule to unused RecordStorageEngine storageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class ); SchemaStore schemaStore = storageEngine.testAccessNeoStores().getSchemaStore(); SchemaRule indexRule = single( filter( rule -> rule instanceof StoreIndexDescriptor, schemaStore.loadAllSchemaRules() ) ); setSchemaRecordNotInUse( schemaStore, indexRule.getId() ); // At this point the SchemaCache doesn't know about this change so we have to reload it storageEngine.loadSchemaCache(); try ( Transaction tx = db.beginTx() ) { single( db.schema().getConstraints( label ).iterator() ).drop(); tx.success(); } // then try ( Transaction ignore = db.beginTx() ) { assertFalse( db.schema().getConstraints().iterator().hasNext() ); assertFalse( db.schema().getIndexes().iterator().hasNext() ); } }
@Test public void shouldReturnCountOneForExistingData() throws Exception { // given IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType(); processAll( updates ); // when try ( IndexReader reader = accessor.newReader() ) { for ( IndexEntryUpdate<IndexDescriptor> update : updates ) { long count = reader.countIndexedNodes( update.getEntityId(), valueCreatorUtil.indexDescriptor.properties(), update.values() ); // then assertEquals( 1, count ); } // and when Iterator<IndexEntryUpdate<IndexDescriptor>> generator = filter( skipExisting( updates ), valueCreatorUtil.randomUpdateGenerator( random ) ); long count = reader.countIndexedNodes( 123, valueCreatorUtil.indexDescriptor.properties(), generator.next().values()[0] ); // then assertEquals( 0, count ); } }
IndexDescriptor indexGetForSchema( SchemaDescriptor descriptor ) { IndexDescriptor indexDescriptor = storageReader.indexGetForSchema( descriptor ); Iterator<IndexDescriptor> indexes = iterator( indexDescriptor ); if ( ktx.hasTxStateWithChanges() ) { indexes = filter( SchemaDescriptor.equalTo( descriptor ), ktx.txState().indexDiffSetsBySchema( descriptor ).apply( indexes ) ); } return singleOrNull( indexes ); }