diff --git a/community/kernel/src/main/java/org/neo4j/kernel/builtinprocs/IndexProcedures.java b/community/kernel/src/main/java/org/neo4j/kernel/builtinprocs/IndexProcedures.java index f6c958b1bf96c..1dec036b9a4b6 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/builtinprocs/IndexProcedures.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/builtinprocs/IndexProcedures.java @@ -65,7 +65,14 @@ public void resampleIndex( String indexSpecification ) throws ProcedureException int labelId = getLabelId( index.label() ); int propertyKeyId = getPropertyKeyId( index.property() ); //TODO: Support composite indexes - triggerSampling( getIndex( labelId, propertyKeyId, index ) ); + try + { + triggerSampling( getIndex( labelId, propertyKeyId, index ) ); + } + catch ( IndexNotFoundKernelException e ) + { + throw new ProcedureException( e.status(), e.getMessage(), e ); + } } public void resampleOutdatedIndexes() @@ -157,7 +164,7 @@ private InternalIndexState getState( IndexSpecifier indexDescription, IndexDescr } } - private void triggerSampling( IndexDescriptor index ) + private void triggerSampling( IndexDescriptor index ) throws IndexNotFoundKernelException { indexingService.triggerIndexSampling( index, IndexSamplingMode.TRIGGER_REBUILD_ALL ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsAccessor.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsAccessor.java index a7d6b85603435..6a46fb19d23e2 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsAccessor.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsAccessor.java @@ -40,13 +40,13 @@ public interface CountsAccessor extends CountsVisitor.Visitable * @param target a register to store the read values in * @return the input register for convenience */ - DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister target ); + DoubleLongRegister indexUpdatesAndSize( long indexId, DoubleLongRegister target ); /** * @param target a register to store the read values in * @return the input register for convenience */ - DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister target ); + DoubleLongRegister indexSample( long indexId, DoubleLongRegister target ); interface Updater extends AutoCloseable { @@ -60,11 +60,11 @@ interface Updater extends AutoCloseable interface IndexStatsUpdater extends AutoCloseable { - void replaceIndexUpdateAndSize( IndexDescriptor descriptor, long updates, long size ); + void replaceIndexUpdateAndSize( long indexId, long updates, long size ); - void replaceIndexSample( IndexDescriptor descriptor, long unique, long size ); + void replaceIndexSample( long indexId, long unique, long size ); - void incrementIndexUpdates( IndexDescriptor descriptor, long delta ); + void incrementIndexUpdates( long indexId, long delta ); @Override void close(); @@ -94,15 +94,15 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { - stats.replaceIndexUpdateAndSize( descriptor, updates, size ); + stats.replaceIndexUpdateAndSize( indexId, updates, size ); } @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { - stats.replaceIndexSample( descriptor, unique, size ); + stats.replaceIndexSample( indexId, unique, size ); } } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsRecordState.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsRecordState.java index 66cc2c4eebc8c..45f77d4496f56 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsRecordState.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsRecordState.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; -import org.neo4j.kernel.api.schema.IndexDescriptor; import org.neo4j.kernel.impl.store.counts.keys.CountsKey; import org.neo4j.kernel.impl.transaction.command.Command; import org.neo4j.kernel.impl.transaction.state.RecordState; @@ -68,9 +67,9 @@ public DoubleLongRegister relationshipCount( int startLabelId, int typeId, int e } @Override - public DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister target ) + public DoubleLongRegister indexSample( long indexId, DoubleLongRegister target ) { - counts( indexSampleKey( descriptor ) ).copyTo( target ); + counts( indexSampleKey( indexId ) ).copyTo( target ); return target; } @@ -84,28 +83,28 @@ public void incrementRelationshipCount( int startLabelId, int typeId, int endLab } @Override - public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister target ) + public DoubleLongRegister indexUpdatesAndSize( long indexId, DoubleLongRegister target ) { - counts( indexStatisticsKey( descriptor ) ).copyTo( target ); + counts( indexStatisticsKey( indexId ) ).copyTo( target ); return target; } @Override - public void replaceIndexUpdateAndSize( IndexDescriptor descriptor, long updates, long size ) + public void replaceIndexUpdateAndSize( long indexId, long updates, long size ) { - counts( indexStatisticsKey( descriptor ) ).write( updates, size ); + counts( indexStatisticsKey( indexId ) ).write( updates, size ); } @Override - public void incrementIndexUpdates( IndexDescriptor descriptor, long delta ) + public void incrementIndexUpdates( long indexId, long delta ) { - counts( indexStatisticsKey( descriptor ) ).increment( delta, 0L ); + counts( indexStatisticsKey( indexId ) ).increment( delta, 0L ); } @Override - public void replaceIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void replaceIndexSample( long indexId, long unique, long size ) { - counts( indexSampleKey( descriptor ) ).write( unique, size ); + counts( indexSampleKey( indexId ) ).write( unique, size ); } @Override @@ -286,15 +285,15 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId verify( relationshipKey( startLabelId, typeId, endLabelId ), 0, count ); } @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { - verify( indexStatisticsKey( descriptor ), updates, size ); + verify( indexStatisticsKey( indexId ), updates, size ); } @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { - verify( indexSampleKey( descriptor ), unique, size ); + verify( indexSampleKey( indexId ), unique, size ); } private void verify( CountsKey key, long actualFirst, long actualSecond ) diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsVisitor.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsVisitor.java index 03cd0ecb862d9..780fd31ae0caa 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsVisitor.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/CountsVisitor.java @@ -19,8 +19,6 @@ */ package org.neo4j.kernel.impl.api; -import org.neo4j.kernel.api.schema.IndexDescriptor; - public interface CountsVisitor { interface Visitable @@ -32,9 +30,9 @@ interface Visitable void visitRelationshipCount( int startLabelId, int typeId, int endLabelId, long count ); - void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ); + void visitIndexStatistics( long indexId, long updates, long size ); - void visitIndexSample( IndexDescriptor descriptor, long unique, long size ); + void visitIndexSample( long indexId, long unique, long size ); public static class Adapter implements CountsVisitor { @@ -51,13 +49,13 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { // override in subclasses } @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { // override in subclasses } @@ -85,20 +83,20 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { for ( CountsVisitor visitor : visitors ) { - visitor.visitIndexStatistics( descriptor, updates, size ); + visitor.visitIndexStatistics( indexId, updates, size ); } } @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { for ( CountsVisitor visitor : visitors ) { - visitor.visitIndexSample( descriptor, unique, size ); + visitor.visitIndexSample( indexId, unique, size ); } } }; diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/StateHandlingStatementOperations.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/StateHandlingStatementOperations.java index 493f23940b6b7..838b78759bc1d 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/StateHandlingStatementOperations.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/StateHandlingStatementOperations.java @@ -1160,13 +1160,14 @@ public double indexUniqueValuesPercentage( KernelStatement statement, IndexDescr @Override public DoubleLongRegister indexUpdatesAndSize( KernelStatement statement, IndexDescriptor index, - DoubleLongRegister target ) + DoubleLongRegister target ) throws IndexNotFoundKernelException { return storeLayer.indexUpdatesAndSize( index, target ); } @Override public DoubleLongRegister indexSample( KernelStatement statement, IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException { return storeLayer.indexSample( index, target ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulator.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulator.java index 703af076a52ea..7392e9b9fbc75 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulator.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulator.java @@ -121,11 +121,11 @@ public StoreScan indexAllNodes() } @Override - protected IndexPopulation createPopulation( IndexPopulator populator, + protected IndexPopulation createPopulation( IndexPopulator populator, long indexId, IndexDescriptor descriptor, IndexConfiguration config, SchemaIndexProvider.Descriptor providerDescriptor, FlippableIndexProxy flipper, FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription ) { - return new BatchingIndexPopulation( populator, descriptor, config, providerDescriptor, flipper, + return new BatchingIndexPopulation( populator, indexId, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, indexUserDescription ); } @@ -339,11 +339,12 @@ private int getNumberOfPopulationWorkers() */ private class BatchingIndexPopulation extends IndexPopulation { - BatchingIndexPopulation( IndexPopulator populator, IndexDescriptor descriptor, IndexConfiguration config, - SchemaIndexProvider.Descriptor providerDescriptor, FlippableIndexProxy flipper, - FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription ) + BatchingIndexPopulation( IndexPopulator populator, long indexId, IndexDescriptor descriptor, + IndexConfiguration config, SchemaIndexProvider.Descriptor providerDescriptor, + FlippableIndexProxy flipper, FailedIndexProxyFactory failedIndexProxyFactory, + String indexUserDescription ) { - super( populator, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, + super( populator, indexId, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, indexUserDescription ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexCountsRemover.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexCountsRemover.java index d3fe087c121df..ce05eee0204a2 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexCountsRemover.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexCountsRemover.java @@ -24,16 +24,16 @@ public class IndexCountsRemover { private final IndexStoreView storeView; - private final IndexDescriptor descriptor; + private final long indexId; - public IndexCountsRemover( final IndexStoreView storeView, final IndexDescriptor descriptor ) + public IndexCountsRemover( final IndexStoreView storeView, final long indexId ) { this.storeView = storeView; - this.descriptor = descriptor; + this.indexId = indexId; } public void remove() { - storeView.replaceIndexCounts( descriptor, 0, 0, 0 ); + storeView.replaceIndexCounts( indexId, 0, 0, 0 ); } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMap.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMap.java index 7875cbc2b7838..9793c9bb484fa 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMap.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMap.java @@ -36,16 +36,18 @@ public final class IndexMap implements Cloneable { private final Map indexesById; private final Map indexesByDescriptor; + private final Map indexIdsByDescriptor; public IndexMap() { - this( new HashMap<>(), new HashMap<>() ); + this( new HashMap<>(), new HashMap<>(), new HashMap<>() ); } - private IndexMap( Map indexesById, Map indexesByDescriptor ) + private IndexMap( Map indexesById, Map indexesByDescriptor, Map indexIdsByDescriptor ) { this.indexesById = indexesById; this.indexesByDescriptor = indexesByDescriptor; + this.indexIdsByDescriptor = indexIdsByDescriptor; } public IndexProxy getIndexProxy( long indexId ) @@ -58,10 +60,16 @@ public IndexProxy getIndexProxy( IndexDescriptor descriptor ) return indexesByDescriptor.get( descriptor ); } + public long getIndexId( IndexDescriptor descriptor ) + { + return indexIdsByDescriptor.get( descriptor ); + } + public void putIndexProxy( long indexId, IndexProxy indexProxy ) { indexesById.put( indexId, indexProxy ); indexesByDescriptor.put( indexProxy.getDescriptor(), indexProxy ); + indexIdsByDescriptor.put( indexProxy.getDescriptor(), indexId ); } public IndexProxy removeIndexProxy( long indexId ) @@ -90,7 +98,8 @@ public Iterable getAllIndexProxies() @Override public IndexMap clone() { - return new IndexMap( cloneMap( indexesById ), cloneMap( indexesByDescriptor ) ); + return new IndexMap( cloneMap( indexesById ), cloneMap( indexesByDescriptor ), + cloneMap( indexIdsByDescriptor ) ); } private Map cloneMap( Map map ) @@ -105,6 +114,11 @@ public Iterator descriptors() return indexesByDescriptor.keySet().iterator(); } + public Iterator indexIds() + { + return indexesById.keySet().iterator(); + } + public int size() { return indexesById.size(); diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMapReference.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMapReference.java index 82f94696cb826..1d82a04946f0a 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMapReference.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexMapReference.java @@ -52,16 +52,26 @@ public IndexProxy getIndexProxy( IndexDescriptor descriptor ) throws IndexNotFou return proxy; } - public IndexProxy getOnlineIndexProxy( IndexDescriptor descriptor ) throws IndexNotFoundKernelException + public long getIndexId( IndexDescriptor descriptor ) throws IndexNotFoundKernelException + { + IndexProxy proxy = indexMap.getIndexProxy( descriptor ); + if ( proxy == null ) + { + throw new IndexNotFoundKernelException( "No index for " + descriptor + " exists." ); + } + return indexMap.getIndexId( descriptor ); + } + + public long getOnlineIndexId( IndexDescriptor descriptor ) throws IndexNotFoundKernelException { IndexProxy proxy = getIndexProxy( descriptor ); switch ( proxy.getState() ) { - case ONLINE: - return proxy; + case ONLINE: + return indexMap.getIndexId( descriptor ); - default: - throw new IndexNotFoundKernelException( "Expected index on " + descriptor + " to be online."); + default: + throw new IndexNotFoundKernelException( "Expected index on " + descriptor + " to be online."); } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexPopulationJob.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexPopulationJob.java index b257f86dcdc49..ae81903c40aaf 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexPopulationJob.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexPopulationJob.java @@ -75,6 +75,7 @@ public IndexPopulationJob( IndexStoreView storeView, * @param failedIndexProxyFactory {@link FailedIndexProxyFactory} to use after an unsuccessful population. */ public void addPopulator( IndexPopulator populator, + long indexId, IndexDescriptor descriptor, IndexConfiguration config, SchemaIndexProvider.Descriptor providerDescriptor, @@ -83,7 +84,7 @@ public void addPopulator( IndexPopulator populator, FailedIndexProxyFactory failedIndexProxyFactory ) { assert storeScan == null : "Population have already started, too late to add populators at this point"; - this.multiPopulator.addPopulator( populator, descriptor, providerDescriptor, config, flipper, + this.multiPopulator.addPopulator( populator, indexId, descriptor, providerDescriptor, config, flipper, failedIndexProxyFactory, indexUserDescription ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexProxyCreator.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexProxyCreator.java index 0b3793363218c..229886257c686 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexProxyCreator.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexProxyCreator.java @@ -77,14 +77,14 @@ public IndexProxy createPopulatingIndexProxy( final long ruleId, providerDescriptor, populator, indexUserDescription, - new IndexCountsRemover( storeView, descriptor ), + new IndexCountsRemover( storeView, ruleId ), logProvider ); PopulatingIndexProxy populatingIndex = new PopulatingIndexProxy( descriptor, config, providerDescriptor, populationJob ); - populationJob.addPopulator( populator, descriptor, config, providerDescriptor, indexUserDescription, + populationJob.addPopulator( populator, ruleId, descriptor, config, providerDescriptor, indexUserDescription, flipper, failureDelegateFactory ); flipper.flipTo( populatingIndex ); @@ -92,7 +92,7 @@ public IndexProxy createPopulatingIndexProxy( final long ruleId, // Prepare for flipping to online mode flipper.setFlipTarget( () -> { monitor.populationCompleteOn( descriptor ); - OnlineIndexProxy onlineProxy = new OnlineIndexProxy( + OnlineIndexProxy onlineProxy = new OnlineIndexProxy(ruleId, descriptor, config, onlineAccessorFromProvider( providerDescriptor, ruleId, config, samplingConfig ), storeView, providerDescriptor, true ); @@ -127,7 +127,7 @@ public IndexProxy createOnlineIndexProxy( long ruleId, IndexAccessor onlineAccessor = onlineAccessorFromProvider( providerDescriptor, ruleId, config, samplingConfig ); IndexProxy proxy; - proxy = new OnlineIndexProxy( descriptor, config, onlineAccessor, storeView, providerDescriptor, false ); + proxy = new OnlineIndexProxy( ruleId, descriptor, config, onlineAccessor, storeView, providerDescriptor, false ); proxy = new ContractCheckingIndexProxy( proxy, true ); return proxy; } @@ -158,7 +158,7 @@ public IndexProxy createFailedIndexProxy( long ruleId, indexUserDescription, indexPopulator, populationFailure, - new IndexCountsRemover( storeView, descriptor ), + new IndexCountsRemover( storeView, ruleId ), logProvider ); proxy = new ContractCheckingIndexProxy( proxy, true ); diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexStoreView.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexStoreView.java index a9b1ad0f932c4..1e4459e557382 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexStoreView.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexStoreView.java @@ -57,13 +57,13 @@ StoreScan visitNodes( */ void nodeAsUpdates( long nodeId, Collection target ); - DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister output ); + DoubleLongRegister indexUpdatesAndSize( long indexId, DoubleLongRegister output ); - DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister output ); + DoubleLongRegister indexSample( long indexId, DoubleLongRegister output ); - void replaceIndexCounts( IndexDescriptor descriptor, long uniqueElements, long maxUniqueElements, long indexSize ); + void replaceIndexCounts( long indexId, long uniqueElements, long maxUniqueElements, long indexSize ); - void incrementIndexUpdates( IndexDescriptor descriptor, long updatesDelta ); + void incrementIndexUpdates( long indexId, long updatesDelta ); StoreScan EMPTY_SCAN = new StoreScan() { @@ -114,7 +114,7 @@ public StoreScan visitNodes( int[] labelIds } @Override - public void replaceIndexCounts( IndexDescriptor descriptor, long uniqueElements, long maxUniqueElements, + public void replaceIndexCounts( long indexId, long uniqueElements, long maxUniqueElements, long indexSize ) { } @@ -125,19 +125,19 @@ public void nodeAsUpdates( long nodeId, Collection target ) } @Override - public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister output ) + public DoubleLongRegister indexUpdatesAndSize( long indexId, DoubleLongRegister output ) { return output; } @Override - public DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister output ) + public DoubleLongRegister indexSample( long indexId, DoubleLongRegister output ) { return output; } @Override - public void incrementIndexUpdates( IndexDescriptor descriptor, long updatesDelta ) + public void incrementIndexUpdates( long indexId, long updatesDelta ) { } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexingService.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexingService.java index 8452f71a89b9e..e55f84a212299 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexingService.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/IndexingService.java @@ -371,17 +371,17 @@ public void shutdown() public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor ) throws IndexNotFoundKernelException { - final IndexProxy indexProxy = indexMapRef.getOnlineIndexProxy( descriptor ); + final long indexId = indexMapRef.getOnlineIndexId( descriptor ); final DoubleLongRegister output = Registers.newDoubleLongRegister(); - storeView.indexUpdatesAndSize( indexProxy.getDescriptor(), output ); + storeView.indexUpdatesAndSize( indexId, output ); return output; } public double indexUniqueValuesPercentage( IndexDescriptor descriptor ) throws IndexNotFoundKernelException { - final IndexProxy indexProxy = indexMapRef.getOnlineIndexProxy( descriptor ); + final long indexId = indexMapRef.getOnlineIndexId( descriptor ); final DoubleLongRegister output = Registers.newDoubleLongRegister(); - storeView.indexSample( indexProxy.getDescriptor(), output ); + storeView.indexSample( indexId, output ); long unique = output.readFirst(); long size = output.readSecond(); if ( size == 0 ) @@ -624,10 +624,11 @@ public void triggerIndexSampling( IndexSamplingMode mode ) } public void triggerIndexSampling( IndexDescriptor descriptor, IndexSamplingMode mode ) + throws IndexNotFoundKernelException { String description = descriptor.userDescription( tokenNameLookup ); log.info( "Manual trigger for sampling index " + description + " [" + mode + "]" ); - samplingController.sampleIndex( descriptor, mode ); + samplingController.sampleIndex( indexMapRef.getIndexId( descriptor ), mode ); } private void awaitIndexFuture( Future future ) throws Exception @@ -682,6 +683,11 @@ public IndexProxy getIndexProxy( IndexDescriptor descriptor ) throws IndexNotFou return indexMapRef.getIndexProxy( descriptor ); } + public long getIndexId( IndexDescriptor descriptor ) throws IndexNotFoundKernelException + { + return indexMapRef.getIndexId( descriptor ); + } + public void validateIndex( long indexId ) throws IndexNotFoundKernelException, ConstraintVerificationFailedKernelException, IndexPopulationFailedKernelException { getIndexProxy( indexId ).validate(); diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulator.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulator.java index 21732ad44b5ec..c44752735144a 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulator.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulator.java @@ -72,7 +72,7 @@ * Usage of this class should be something like: *
    *
  1. Instantiation.
  2. - *
  3. One or more calls to {@link #addPopulator(IndexPopulator, IndexDescriptor, Descriptor, IndexConfiguration, + *
  4. One or more calls to {@link #addPopulator(IndexPopulator, long, IndexDescriptor, Descriptor, IndexConfiguration, * FlippableIndexProxy, FailedIndexProxyFactory, String)}.
  5. *
  6. Call to {@link #create()} to create data structures and files to start accepting updates.
  7. *
  8. Call to {@link #indexAllNodes()} (blocking call).
  9. @@ -109,23 +109,24 @@ public MultipleIndexPopulator( IndexStoreView storeView, LogProvider logProvider public IndexPopulation addPopulator( IndexPopulator populator, + long indexId, IndexDescriptor descriptor, Descriptor providerDescriptor, IndexConfiguration config, FlippableIndexProxy flipper, FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription ) { - IndexPopulation population = createPopulation( populator, descriptor, config, providerDescriptor, flipper, + IndexPopulation population = createPopulation( populator, indexId, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, indexUserDescription ); populations.add( population ); return population; } - protected IndexPopulation createPopulation( IndexPopulator populator, IndexDescriptor descriptor, + protected IndexPopulation createPopulation( IndexPopulator populator, long indexId, IndexDescriptor descriptor, IndexConfiguration config, Descriptor providerDescriptor, FlippableIndexProxy flipper, FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription ) { - return new IndexPopulation( populator, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, + return new IndexPopulation( populator, indexId, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, indexUserDescription ); } @@ -289,7 +290,7 @@ public IndexSample sampleResult() public void replaceIndexCounts( long uniqueElements, long maxUniqueElements, long indexSize ) { forEachPopulation( population -> - storeView.replaceIndexCounts( population.descriptor, uniqueElements, maxUniqueElements, indexSize ) ); + storeView.replaceIndexCounts( population.indexId, uniqueElements, maxUniqueElements, indexSize ) ); } public void flipAfterPopulation() @@ -453,6 +454,7 @@ public void close() public class IndexPopulation { public final IndexPopulator populator; + final long indexId; final IndexDescriptor descriptor; final IndexConfiguration config; final SchemaIndexProvider.Descriptor providerDescriptor; @@ -464,6 +466,7 @@ public class IndexPopulation IndexPopulation( IndexPopulator populator, + long indexId, IndexDescriptor descriptor, IndexConfiguration config, Descriptor providerDescriptor, @@ -472,13 +475,14 @@ public class IndexPopulation String indexUserDescription ) { this.populator = populator; + this.indexId = indexId; this.descriptor = descriptor; this.config = config; this.providerDescriptor = providerDescriptor; this.flipper = flipper; this.failedIndexProxyFactory = failedIndexProxyFactory; this.indexUserDescription = indexUserDescription; - this.indexCountsRemover = new IndexCountsRemover( storeView, descriptor ); + this.indexCountsRemover = new IndexCountsRemover( storeView, indexId ); } private void flipToFailed( Throwable t ) @@ -523,7 +527,7 @@ private void flip() throws FlipFailedKernelException flipper.flip( () -> { populateFromQueueIfAvailable( Long.MAX_VALUE ); IndexSample sample = populator.sampleResult(); - storeView.replaceIndexCounts( descriptor, sample.uniqueValues(), sample.sampleSize(), + storeView.replaceIndexCounts( indexId, sample.uniqueValues(), sample.sampleSize(), sample.indexSize() ); populator.close( true ); return null; diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxy.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxy.java index 8ead57f0ceed0..8f8f26d0a821b 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxy.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxy.java @@ -39,6 +39,7 @@ public class OnlineIndexProxy implements IndexProxy { + private final long indexId; private final IndexDescriptor descriptor; final IndexAccessor accessor; private final IndexStoreView storeView; @@ -72,17 +73,18 @@ public class OnlineIndexProxy implements IndexProxy // slightly more costly, but shouldn't make that big of a difference hopefully. private final boolean forcedIdempotentMode; - public OnlineIndexProxy( IndexDescriptor descriptor, IndexConfiguration configuration, IndexAccessor accessor, - IndexStoreView storeView, SchemaIndexProvider.Descriptor providerDescriptor, - boolean forcedIdempotentMode ) + public OnlineIndexProxy( long indexId, IndexDescriptor descriptor, IndexConfiguration configuration, + IndexAccessor accessor, IndexStoreView storeView, SchemaIndexProvider.Descriptor providerDescriptor, + boolean forcedIdempotentMode ) { + this.indexId = indexId; this.descriptor = descriptor; this.storeView = storeView; this.providerDescriptor = providerDescriptor; this.accessor = accessor; this.configuration = configuration; this.forcedIdempotentMode = forcedIdempotentMode; - this.indexCountsRemover = new IndexCountsRemover( storeView, descriptor ); + this.indexCountsRemover = new IndexCountsRemover( storeView, indexId ); } @Override @@ -98,7 +100,7 @@ public IndexUpdater newUpdater( final IndexUpdateMode mode ) private IndexUpdater updateCountingUpdater( final IndexUpdater indexUpdater ) { - return new UpdateCountingIndexUpdater( storeView, descriptor, indexUpdater ); + return new UpdateCountingIndexUpdater( storeView, indexId, indexUpdater ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingController.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingController.java index fc1e3d46f1536..cfdf83814acd7 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingController.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingController.java @@ -40,11 +40,11 @@ public class IndexSamplingController { private final IndexSamplingJobFactory jobFactory; - private final IndexSamplingJobQueue jobQueue; + private final IndexSamplingJobQueue jobQueue; private final IndexSamplingJobTracker jobTracker; private final IndexMapSnapshotProvider indexMapSnapshotProvider; private final JobScheduler scheduler; - private final Predicate indexRecoveryCondition; + private final RecoveryCondition indexRecoveryCondition; private final boolean backgroundSampling; private final Lock samplingLock = new ReentrantLock( true ); @@ -53,11 +53,11 @@ public class IndexSamplingController // use IndexSamplingControllerFactory.create do not instantiate directly IndexSamplingController( IndexSamplingConfig config, IndexSamplingJobFactory jobFactory, - IndexSamplingJobQueue jobQueue, + IndexSamplingJobQueue jobQueue, IndexSamplingJobTracker jobTracker, IndexMapSnapshotProvider indexMapSnapshotProvider, JobScheduler scheduler, - Predicate indexRecoveryCondition ) + RecoveryCondition indexRecoveryCondition ) { this.backgroundSampling = config.backgroundSampling(); this.jobFactory = jobFactory; @@ -71,14 +71,14 @@ public class IndexSamplingController public void sampleIndexes( IndexSamplingMode mode ) { IndexMap indexMap = indexMapSnapshotProvider.indexMapSnapshot(); - jobQueue.addAll( !mode.sampleOnlyIfUpdated, indexMap.descriptors() ); + jobQueue.addAll( !mode.sampleOnlyIfUpdated, indexMap.indexIds() ); scheduleSampling( mode, indexMap ); } - public void sampleIndex( IndexDescriptor descriptor, IndexSamplingMode mode ) + public void sampleIndex( long indexId, IndexSamplingMode mode ) { IndexMap indexMap = indexMapSnapshotProvider.indexMapSnapshot(); - jobQueue.add( !mode.sampleOnlyIfUpdated, descriptor ); + jobQueue.add( !mode.sampleOnlyIfUpdated, indexId ); scheduleSampling( mode, indexMap ); } @@ -88,13 +88,13 @@ public void recoverIndexSamples() try { IndexMap indexMap = indexMapSnapshotProvider.indexMapSnapshot(); - Iterator descriptors = indexMap.descriptors(); - while ( descriptors.hasNext() ) + Iterator indexIds = indexMap.indexIds(); + while ( indexIds.hasNext() ) { - IndexDescriptor descriptor = descriptors.next(); - if ( indexRecoveryCondition.test( descriptor ) ) + long indexId = indexIds.next(); + if ( indexRecoveryCondition.test( indexId, indexMap.getIndexProxy( indexId ).getDescriptor() ) ) { - sampleIndexOnCurrentThread( indexMap, descriptor ); + sampleIndexOnCurrentThread( indexMap, indexId ); } } } @@ -104,6 +104,11 @@ public void recoverIndexSamples() } } + public interface RecoveryCondition + { + boolean test(long indexId, IndexDescriptor descriptor); + } + private void scheduleSampling( IndexSamplingMode mode, IndexMap indexMap ) { if ( mode.blockUntilAllScheduled ) @@ -126,13 +131,13 @@ private void tryScheduleSampling( IndexMap indexMap ) { while ( jobTracker.canExecuteMoreSamplingJobs() ) { - IndexDescriptor descriptor = jobQueue.poll(); - if ( descriptor == null ) + Long indexId = jobQueue.poll(); + if ( indexId == null ) { return; } - sampleIndexOnTracker( indexMap, descriptor ); + sampleIndexOnTracker( indexMap, indexId ); } } finally @@ -160,12 +165,12 @@ private void scheduleAllSampling( IndexMap indexMap ) samplingLock.lock(); try { - Iterable descriptors = jobQueue.pollAll(); + Iterable indexIds = jobQueue.pollAll(); - for ( IndexDescriptor descriptor : descriptors ) + for ( Long indexId : indexIds ) { jobTracker.waitUntilCanExecuteMoreSamplingJobs(); - sampleIndexOnTracker( indexMap, descriptor ); + sampleIndexOnTracker( indexMap, indexId ); } } finally @@ -174,32 +179,32 @@ private void scheduleAllSampling( IndexMap indexMap ) } } - private void sampleIndexOnTracker( IndexMap indexMap, IndexDescriptor descriptor ) + private void sampleIndexOnTracker( IndexMap indexMap, long indexId ) { - IndexSamplingJob job = createSamplingJob( indexMap, descriptor ); + IndexSamplingJob job = createSamplingJob( indexMap, indexId ); if ( job != null ) { jobTracker.scheduleSamplingJob( job ); } } - private void sampleIndexOnCurrentThread( IndexMap indexMap, IndexDescriptor descriptor ) + private void sampleIndexOnCurrentThread( IndexMap indexMap, long indexId ) { - IndexSamplingJob job = createSamplingJob( indexMap, descriptor ); + IndexSamplingJob job = createSamplingJob( indexMap, indexId ); if ( job != null ) { job.run(); } } - private IndexSamplingJob createSamplingJob( IndexMap indexMap, IndexDescriptor descriptor ) + private IndexSamplingJob createSamplingJob( IndexMap indexMap, long indexId ) { - IndexProxy proxy = indexMap.getIndexProxy( descriptor ); + IndexProxy proxy = indexMap.getIndexProxy( indexId ); if ( proxy == null || proxy.getState() != InternalIndexState.ONLINE ) { return null; } - return jobFactory.create( proxy ); + return jobFactory.create( indexId, proxy ); } public void start() diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerFactory.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerFactory.java index 4d742716b86e6..e07f94c3d8a95 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerFactory.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerFactory.java @@ -55,25 +55,26 @@ public IndexSamplingController create( IndexMapSnapshotProvider snapshotProvider { OnlineIndexSamplingJobFactory jobFactory = new OnlineIndexSamplingJobFactory( storeView, tokenNameLookup, logProvider ); - Predicate samplingUpdatePredicate = createSamplingPredicate(); - IndexSamplingJobQueue jobQueue = new IndexSamplingJobQueue<>( samplingUpdatePredicate ); + Predicate samplingUpdatePredicate = createSamplingPredicate(); + IndexSamplingJobQueue jobQueue = new IndexSamplingJobQueue<>( samplingUpdatePredicate ); IndexSamplingJobTracker jobTracker = new IndexSamplingJobTracker( config, scheduler ); - Predicate indexRecoveryCondition = createIndexRecoveryCondition( logProvider, tokenNameLookup ); + IndexSamplingController.RecoveryCondition + indexRecoveryCondition = createIndexRecoveryCondition( logProvider, tokenNameLookup ); return new IndexSamplingController( config, jobFactory, jobQueue, jobTracker, snapshotProvider, scheduler, indexRecoveryCondition ); } - private Predicate createSamplingPredicate() + private Predicate createSamplingPredicate() { - return new Predicate() + return new Predicate() { private final DoubleLongRegister output = newDoubleLongRegister(); @Override - public boolean test( IndexDescriptor descriptor ) + public boolean test( Long indexId ) { - storeView.indexUpdatesAndSize( descriptor, output ); + storeView.indexUpdatesAndSize( indexId, output ); long updates = output.readFirst(); long size = output.readSecond(); long threshold = Math.round( config.updateRatio() * size ); @@ -82,18 +83,18 @@ public boolean test( IndexDescriptor descriptor ) }; } - private Predicate createIndexRecoveryCondition( final LogProvider logProvider, + private IndexSamplingController.RecoveryCondition createIndexRecoveryCondition( final LogProvider logProvider, final TokenNameLookup tokenNameLookup ) { - return new Predicate() + return new IndexSamplingController.RecoveryCondition() { private final Log log = logProvider.getLog( IndexSamplingController.class ); private final DoubleLongRegister register = newDoubleLongRegister(); @Override - public boolean test( IndexDescriptor descriptor ) + public boolean test( long indexId, IndexDescriptor descriptor ) { - boolean result = storeView.indexSample( descriptor, register ).readSecond() == 0; + boolean result = storeView.indexSample( indexId, register ).readSecond() == 0; if ( result ) { log.warn( "Recovering index sampling for index %s", descriptor.userDescription( tokenNameLookup ) ); diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJob.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJob.java index 4392db19a2715..077309a1d58b5 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJob.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJob.java @@ -19,9 +19,7 @@ */ package org.neo4j.kernel.impl.api.index.sampling; -import org.neo4j.kernel.api.schema.IndexDescriptor; - public interface IndexSamplingJob extends Runnable { - IndexDescriptor descriptor(); + long indexId(); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobFactory.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobFactory.java index 61b8ee8ca7c9d..66a254dba124d 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobFactory.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobFactory.java @@ -23,5 +23,5 @@ public interface IndexSamplingJobFactory { - IndexSamplingJob create( IndexProxy indexProxy ); + IndexSamplingJob create( long indexId, IndexProxy indexProxy); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTracker.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTracker.java index 82627a7d00fd5..a770ab05dd470 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTracker.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTracker.java @@ -26,14 +26,13 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import org.neo4j.kernel.api.schema.IndexDescriptor; import org.neo4j.kernel.impl.util.JobScheduler; public class IndexSamplingJobTracker { private final JobScheduler jobScheduler; private final int jobLimit; - private final Set executingJobDescriptors; + private final Set executingJobs; private final Lock lock = new ReentrantLock( true ); private final Condition canSchedule = lock.newCondition(); private final Condition allJobsFinished = lock.newCondition(); @@ -44,7 +43,7 @@ public IndexSamplingJobTracker( IndexSamplingConfig config, JobScheduler jobSche { this.jobScheduler = jobScheduler; this.jobLimit = config.jobLimit(); - this.executingJobDescriptors = new HashSet<>(); + this.executingJobs = new HashSet<>(); } public boolean canExecuteMoreSamplingJobs() @@ -52,7 +51,7 @@ public boolean canExecuteMoreSamplingJobs() lock.lock(); try { - return !stopped && executingJobDescriptors.size() < jobLimit; + return !stopped && executingJobs.size() < jobLimit; } finally { @@ -70,13 +69,13 @@ public void scheduleSamplingJob( final IndexSamplingJob samplingJob ) return; } - IndexDescriptor descriptor = samplingJob.descriptor(); - if ( executingJobDescriptors.contains( descriptor ) ) + long indexId = samplingJob.indexId(); + if ( executingJobs.contains( indexId ) ) { return; } - executingJobDescriptors.add( descriptor ); + executingJobs.add( indexId ); jobScheduler.schedule( JobScheduler.Groups.indexSampling, new Runnable() { @Override @@ -104,7 +103,7 @@ private void samplingJobCompleted( IndexSamplingJob samplingJob ) lock.lock(); try { - executingJobDescriptors.remove( samplingJob.descriptor() ); + executingJobs.remove( samplingJob.indexId() ); canSchedule.signalAll(); allJobsFinished.signalAll(); } @@ -145,7 +144,7 @@ public void awaitAllJobs(long time, TimeUnit unit) throws InterruptedException return; } - while ( !executingJobDescriptors.isEmpty() ) + while ( !executingJobs.isEmpty() ) { allJobsFinished.await( time, unit ); } @@ -163,7 +162,7 @@ public void stopAndAwaitAllJobs() { stopped = true; - while ( !executingJobDescriptors.isEmpty() ) + while ( !executingJobs.isEmpty() ) { allJobsFinished.awaitUninterruptibly(); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJob.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJob.java index 25ddaed37f373..e1b568cecb20e 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJob.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJob.java @@ -35,18 +35,18 @@ class OnlineIndexSamplingJob implements IndexSamplingJob { - private final IndexDescriptor indexDescriptor; + private final long indexId; private final IndexProxy indexProxy; private final IndexStoreView storeView; private final Log log; private final String indexUserDescription; - public OnlineIndexSamplingJob( IndexProxy indexProxy, + public OnlineIndexSamplingJob( long indexId, IndexProxy indexProxy, IndexStoreView storeView, String indexUserDescription, LogProvider logProvider ) { - this.indexDescriptor = indexProxy.getDescriptor(); + this.indexId = indexId; this.indexProxy = indexProxy; this.storeView = storeView; this.log = logProvider.getLog( getClass() ); @@ -54,9 +54,9 @@ public OnlineIndexSamplingJob( IndexProxy indexProxy, } @Override - public IndexDescriptor descriptor() + public long indexId() { - return indexDescriptor; + return indexId; } @Override @@ -74,7 +74,7 @@ public void run() // check again if the index is online before saving the counts in the store if ( indexProxy.getState() == ONLINE ) { - storeView.replaceIndexCounts( indexDescriptor, sample.uniqueValues(), sample.sampleSize(), + storeView.replaceIndexCounts( indexId, sample.uniqueValues(), sample.sampleSize(), sample.indexSize() ); durationLogger.markAsFinished(); log.info( diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobFactory.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobFactory.java index 3a5c36d3e416e..3de7545f4a7e9 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobFactory.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobFactory.java @@ -38,9 +38,9 @@ public OnlineIndexSamplingJobFactory( IndexStoreView storeView, TokenNameLookup } @Override - public IndexSamplingJob create( IndexProxy indexProxy ) + public IndexSamplingJob create( long indexId, IndexProxy indexProxy ) { final String indexUserDescription = indexProxy.getDescriptor().userDescription( nameLookup ); - return new OnlineIndexSamplingJob( indexProxy, storeView, indexUserDescription, logProvider ); + return new OnlineIndexSamplingJob( indexId, indexProxy, storeView, indexUserDescription, logProvider ); } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/updater/UpdateCountingIndexUpdater.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/updater/UpdateCountingIndexUpdater.java index 8f8aabf2e6d31..3727adda1238e 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/updater/UpdateCountingIndexUpdater.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/index/updater/UpdateCountingIndexUpdater.java @@ -31,14 +31,14 @@ public class UpdateCountingIndexUpdater implements IndexUpdater { private final IndexStoreView storeView; - private final IndexDescriptor descriptor; + private final long indexId; private final IndexUpdater delegate; private long updates; - public UpdateCountingIndexUpdater( IndexStoreView storeView, IndexDescriptor descriptor, IndexUpdater delegate ) + public UpdateCountingIndexUpdater( IndexStoreView storeView, long indexId, IndexUpdater delegate ) { this.storeView = storeView; - this.descriptor = descriptor; + this.indexId = indexId; this.delegate = delegate; } @@ -53,7 +53,7 @@ public void process( NodePropertyUpdate update ) throws IOException, IndexEntryC public void close() throws IOException, IndexEntryConflictException { delegate.close(); - storeView.incrementIndexUpdates( descriptor, updates ); + storeView.incrementIndexUpdates( indexId, updates ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/operations/CountsOperations.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/operations/CountsOperations.java index 7d24cc2c67384..6c7e520c62ced 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/operations/CountsOperations.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/operations/CountsOperations.java @@ -19,6 +19,7 @@ */ package org.neo4j.kernel.impl.api.operations; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.schema.IndexDescriptor; import org.neo4j.kernel.impl.api.KernelStatement; import org.neo4j.register.Register.DoubleLongRegister; @@ -38,7 +39,8 @@ public interface CountsOperations long countsForRelationshipWithoutTxState( KernelStatement statement, int startLabelId, int typeId, int endLabelId ); DoubleLongRegister indexUpdatesAndSize( KernelStatement statement, IndexDescriptor index, - DoubleLongRegister target ); + DoubleLongRegister target ) throws IndexNotFoundKernelException; - DoubleLongRegister indexSample( KernelStatement statement, IndexDescriptor index, DoubleLongRegister target ); + DoubleLongRegister indexSample( KernelStatement statement, IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException; } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/CacheLayer.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/CacheLayer.java index e560fe99b841b..683d50d0fc67c 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/CacheLayer.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/CacheLayer.java @@ -411,12 +411,14 @@ public int relationshipTypeCount() @Override public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException { return diskLayer.indexUpdatesAndSize( index, target ); } @Override public DoubleLongRegister indexSample( IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException { return diskLayer.indexSample( index, target ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/DiskLayer.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/DiskLayer.java index 6782511926d84..c36a4b16ded69 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/DiskLayer.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/api/store/DiskLayer.java @@ -502,14 +502,21 @@ public int relationshipTypeCount() @Override public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister target ) + throws IndexNotFoundKernelException { - return counts.indexUpdatesAndSize( descriptor, target ); + return counts.indexUpdatesAndSize( tryGetIndexId( descriptor ), target ); } @Override public DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister target ) + throws IndexNotFoundKernelException + { + return counts.indexSample( tryGetIndexId( descriptor ), target ); + } + + private long tryGetIndexId(IndexDescriptor descriptor) throws IndexNotFoundKernelException { - return counts.indexSample( descriptor, target ); + return indexService.getIndexId( descriptor ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotDeserializer.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotDeserializer.java index 2114f72d97c12..f4e13e042c3b8 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotDeserializer.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotDeserializer.java @@ -69,15 +69,13 @@ public static CountsSnapshot deserialize( ReadableClosableChannel channel ) thro break; case INDEX_SAMPLE: - descriptor = IndexDescriptorFactory.of( readNodePropertyDescriptor( channel ) ); - key = indexSampleKey( descriptor ); + key = indexSampleKey( channel.getLong() ); value = new long[]{channel.getLong(), channel.getLong()}; map.put( key, value ); break; case INDEX_STATISTICS: - descriptor = IndexDescriptorFactory.of( readNodePropertyDescriptor( channel ) ); - key = indexStatisticsKey( descriptor ); + key = indexStatisticsKey( channel.getLong() ); value = new long[]{channel.getLong(), channel.getLong()}; map.put( key, value ); break; @@ -91,23 +89,4 @@ public static CountsSnapshot deserialize( ReadableClosableChannel channel ) thro } return new CountsSnapshot( txid, map ); } - - private static NodePropertyDescriptor readNodePropertyDescriptor( ReadableClosableChannel channel ) throws IOException - { - int labelId = channel.getInt(); - short length = channel.getShort(); - if ( length > 1 ) - { - int[] propertyKeyIds = new int[length]; - for ( int i = 0; i < length; i++ ) - { - propertyKeyIds[i] = channel.getInt(); - } - return new NodeMultiPropertyDescriptor( labelId, propertyKeyIds ); - } - else - { - return new NodePropertyDescriptor( labelId, channel.getInt() ); - } - } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotSerializer.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotSerializer.java index c939b9cba242e..6a0802305e027 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotSerializer.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/countStore/CountsSnapshotSerializer.java @@ -85,7 +85,7 @@ public static void serialize( FlushableChannel channel, CountsSnapshot countsSna } IndexSampleKey indexSampleKey = (IndexSampleKey) key; channel.put( INDEX_SAMPLE.code ); - writeIndexDescription( channel, indexSampleKey.descriptor() ); + channel.putLong( indexSampleKey.indexId() ); channel.putLong( value[0] ); channel.putLong( value[1] ); break; @@ -98,7 +98,7 @@ public static void serialize( FlushableChannel channel, CountsSnapshot countsSna } IndexStatisticsKey indexStatisticsKey = (IndexStatisticsKey) key; channel.put( INDEX_STATISTICS.code ); - writeIndexDescription( channel, indexStatisticsKey.descriptor() ); + channel.putLong( indexStatisticsKey.indexId() ); channel.putLong( value[0] ); channel.putLong( value[1] ); break; @@ -111,23 +111,4 @@ public static void serialize( FlushableChannel channel, CountsSnapshot countsSna } } } - - private static void writeIndexDescription( FlushableChannel channel, IndexDescriptor descriptor ) throws IOException - { - channel.putInt( descriptor.getLabelId() ); - if ( descriptor.isComposite() ) - { - int[] propertyKeyIds = descriptor.getPropertyKeyIds(); - channel.putShort( (short) propertyKeyIds.length ); - for ( int prop : propertyKeyIds ) - { - channel.putInt( prop ); - } - } - else - { - channel.putShort( (short) 1 ); - channel.putInt( descriptor.getPropertyKeyId() ); - } - } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsTracker.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsTracker.java index a20ea857d7dd9..be41199ad6bbe 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsTracker.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsTracker.java @@ -77,7 +77,7 @@ public class CountsTracker extends AbstractKeyValueStore { /** The format specifier for the current version of the store file format. */ private static final byte[] FORMAT = {'N', 'e', 'o', 'C', 'o', 'u', 'n', 't', - 'S', 't', 'o', 'r', 'e', /**/0, 1, 'V'}; + 'S', 't', 'o', 'r', 'e', /**/0, 2, 'V'}; @SuppressWarnings("unchecked") private static final HeaderField[] HEADER_FIELDS = new HeaderField[]{FileVersion.FILE_VERSION}; public static final String LEFT = ".a", RIGHT = ".b"; @@ -184,16 +184,16 @@ public Register.DoubleLongRegister relationshipCount( int startLabelId, int type } @Override - public Register.DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, + public Register.DoubleLongRegister indexUpdatesAndSize( long indexId, Register.DoubleLongRegister target ) { - return get( indexStatisticsKey( descriptor ), target ); + return get( indexStatisticsKey( indexId ), target ); } @Override - public Register.DoubleLongRegister indexSample( IndexDescriptor descriptor, Register.DoubleLongRegister target ) + public Register.DoubleLongRegister indexSample( long indexId, Register.DoubleLongRegister target ) { - return get( indexSampleKey( descriptor ), target ); + return get( indexSampleKey( indexId ), target ); } public Optional apply( long txId ) diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsUpdater.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsUpdater.java index bef67d204ab2b..91d760716cf78 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsUpdater.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/CountsUpdater.java @@ -98,11 +98,11 @@ public void incrementRelationshipCount( int startLabelId, int typeId, int endLab * For key format, see {@link KeyFormat#visitIndexStatistics(IndexDescriptor, long, long)} */ @Override - public void replaceIndexUpdateAndSize( IndexDescriptor descriptor, long updates, long size ) + public void replaceIndexUpdateAndSize( long indexId, long updates, long size ) { try { - updater.apply( indexStatisticsKey( descriptor ), new Write( updates, size ) ); + updater.apply( indexStatisticsKey( indexId ), new Write( updates, size ) ); } catch ( IOException e ) { @@ -121,11 +121,11 @@ public void replaceIndexUpdateAndSize( IndexDescriptor descriptor, long updates, * For key format, see {@link KeyFormat#visitIndexSample(IndexDescriptor, long, long)} */ @Override - public void replaceIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void replaceIndexSample( long indexId, long unique, long size ) { try { - updater.apply( indexSampleKey( descriptor ), new Write( unique, size ) ); + updater.apply( indexSampleKey( indexId ), new Write( unique, size ) ); } catch ( IOException e ) { @@ -135,14 +135,14 @@ public void replaceIndexSample( IndexDescriptor descriptor, long unique, long si /** * For key format, see {@link KeyFormat#visitIndexStatistics(IndexDescriptor, long, long)} - * For value format, see {@link CountsUpdater#replaceIndexUpdateAndSize(IndexDescriptor, long, long)} + * For value format, see {@link CountsUpdater#replaceIndexUpdateAndSize(long, long, long)} */ @Override - public void incrementIndexUpdates( IndexDescriptor descriptor, long delta ) + public void incrementIndexUpdates( long indexId, long delta ) { try { - updater.apply( indexStatisticsKey( descriptor ), incrementFirstBy( delta ) ); + updater.apply( indexStatisticsKey( indexId ), incrementFirstBy( delta ) ); } catch ( IOException e ) { diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/KeyFormat.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/KeyFormat.java index 0e770dce6d205..767de96e3a022 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/KeyFormat.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/KeyFormat.java @@ -83,43 +83,40 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId * Key format: *
          *  0 1 2 3 4 5 6 7   8 9 A B C D E F
    -     * [t,0,0,0,l,l,l,l ; p,p,p,p,0,0,0,k]
    +     * [t,0,0,0,i,i,i,i ; 0,0,0,0,0,0,0,k]
          *  t - index entry marker - "{@link #INDEX}"
          *  k - entry (sub)type - "{@link #INDEX_STATS}"
    -     *  l - label id
    -     *  p - property key id
    +     *  i - index id
          * 
    - * For value format, see {@link org.neo4j.kernel.impl.store.counts.CountsUpdater#replaceIndexUpdateAndSize(IndexDescriptor, long, long)}. + * For value format, see {@link org.neo4j.kernel.impl.store.counts.CountsUpdater#replaceIndexUpdateAndSize(long, long, long)}. */ @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { - indexKey( INDEX_STATS, descriptor ); + indexKey( INDEX_STATS, indexId ); } /** * Key format: *
          *  0 1 2 3 4 5 6 7   8 9 A B C D E F
    -     * [t,0,0,0,l,l,l,l ; p,p,p,p,0,0,0,k]
    +     * [t,0,0,0,i,i,i,i ; 0,0,0,0,0,0,0,k]
          *  t - index entry marker - "{@link #INDEX}"
    -     *  k - entry (sub)type - "{@link #INDEX_SAMPLE}"
    -     *  l - label id
    -     *  p - property key id
    +     *  k - entry (sub)type - "{@link #INDEX_STATS}"
    +     *  i - index id
          * 
    - * For value format, see {@link org.neo4j.kernel.impl.store.counts.CountsUpdater#replaceIndexSample(IndexDescriptor , long, long)}. + * For value format, see {@link org.neo4j.kernel.impl.store.counts.CountsUpdater#replaceIndexSample(long , long, long)}. */ @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { - indexKey( INDEX_SAMPLE, descriptor ); + indexKey( INDEX_SAMPLE, indexId ); } - private void indexKey( byte indexKey, IndexDescriptor descriptor ) + private void indexKey( byte indexKey, long indexId ) { buffer.putByte( 0, INDEX ) - .putInt( 4, descriptor.getLabelId() ) - .putInt( 8, descriptor.getPropertyKeyId() ) + .putInt( 4, (int) indexId ) .putByte( 15, indexKey ); } @@ -133,13 +130,13 @@ public static CountsKey readKey( ReadableBuffer key ) throws UnknownKey return CountsKeyFactory.relationshipKey( key.getInt( 4 ), key.getInt( 8 ), key.getInt( 12 ) ); case KeyFormat.INDEX: byte indexKeyByte = key.getByte( 15 ); - IndexDescriptor descriptor = IndexDescriptorFactory.of( key.getInt( 4 ), key.getInt( 8 ) ); + long indexId = key.getInt( 4 ); switch ( indexKeyByte ) { case KeyFormat.INDEX_STATS: - return indexStatisticsKey( descriptor ); + return indexStatisticsKey( indexId ); case KeyFormat.INDEX_SAMPLE: - return CountsKeyFactory.indexSampleKey( descriptor ); + return CountsKeyFactory.indexSampleKey( indexId ); default: throw new IllegalStateException( "Unknown index key: " + indexKeyByte ); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/CountsKeyFactory.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/CountsKeyFactory.java index 43f7c24f60517..b1cf5c9b1a06b 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/CountsKeyFactory.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/CountsKeyFactory.java @@ -19,8 +19,6 @@ */ package org.neo4j.kernel.impl.store.counts.keys; -import org.neo4j.kernel.api.schema.IndexDescriptor; - public class CountsKeyFactory { public static NodeKey nodeKey( int labelId ) @@ -33,13 +31,13 @@ public static RelationshipKey relationshipKey( int startLabelId, int typeId, int return new RelationshipKey( startLabelId, typeId, endLabelId ); } - public static IndexStatisticsKey indexStatisticsKey( IndexDescriptor descriptor ) + public static IndexStatisticsKey indexStatisticsKey( long indexId ) { - return new IndexStatisticsKey( descriptor ); + return new IndexStatisticsKey( indexId ); } - public static IndexSampleKey indexSampleKey( IndexDescriptor descriptor ) + public static IndexSampleKey indexSampleKey( long indexId ) { - return new IndexSampleKey( descriptor ); + return new IndexSampleKey( indexId ); } } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexKey.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexKey.java index 48e79bf795e8c..d4c95f17b84b9 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexKey.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexKey.java @@ -24,25 +24,24 @@ abstract class IndexKey implements CountsKey { - private final IndexDescriptor descriptor; + private final long indexId; private final CountsKeyType type; - IndexKey( IndexDescriptor descriptor, CountsKeyType type ) + IndexKey( long indexId, CountsKeyType type ) { - this.descriptor = descriptor; + this.indexId = indexId; this.type = type; } - public IndexDescriptor descriptor() + public long indexId() { - return descriptor; + return indexId; } @Override public String toString() { - String propertyText = descriptor.descriptor().propertyIdText(); - return String.format( "IndexKey[%s (%s {%s})]", type.name(), label( descriptor.getLabelId() ), propertyText ); + return String.format( "IndexKey[%s:%d]", type.name(), indexId ); } @Override @@ -54,7 +53,7 @@ public CountsKeyType recordType() @Override public int hashCode() { - return 31 * descriptor.hashCode() + type.hashCode(); + return 31 * (int) indexId + type.hashCode(); } @Override @@ -68,9 +67,7 @@ public boolean equals( Object other ) { return false; } - - IndexKey indexKey = (IndexKey) other; - return indexKey.descriptor.equals( descriptor ); + return ((IndexKey) other).indexId() == indexId; } @Override @@ -78,8 +75,7 @@ public int compareTo( CountsKey other ) { if ( other instanceof IndexKey ) { - IndexKey that = (IndexKey) other; - return this.descriptor.descriptor().compareTo( that.descriptor.descriptor() ); + return (int) (indexId - ((IndexKey) other).indexId()); } return recordType().ordinal() - other.recordType().ordinal(); } diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexSampleKey.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexSampleKey.java index 76000b6e7a88c..45c04c1c4f78f 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexSampleKey.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexSampleKey.java @@ -24,15 +24,15 @@ public final class IndexSampleKey extends IndexKey { - IndexSampleKey( IndexDescriptor descriptor ) + IndexSampleKey( long indexId ) { - super( descriptor, CountsKeyType.INDEX_SAMPLE ); + super( indexId, CountsKeyType.INDEX_SAMPLE ); } @Override public void accept( CountsVisitor visitor, long unique, long size ) { - visitor.visitIndexSample( descriptor(), unique, size ); + visitor.visitIndexSample( indexId(), unique, size ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexStatisticsKey.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexStatisticsKey.java index aec11053a06a1..496bd5b8da005 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexStatisticsKey.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/counts/keys/IndexStatisticsKey.java @@ -24,15 +24,15 @@ public final class IndexStatisticsKey extends IndexKey { - IndexStatisticsKey( IndexDescriptor descriptor ) + IndexStatisticsKey( long indexId ) { - super( descriptor, CountsKeyType.INDEX_STATISTICS ); + super( indexId, CountsKeyType.INDEX_STATISTICS ); } @Override public void accept( CountsVisitor visitor, long updates, long size ) { - visitor.visitIndexStatistics( descriptor(), updates, size ); + visitor.visitIndexStatistics( indexId(), updates, size ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/transaction/state/storeview/NeoStoreIndexStoreView.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/transaction/state/storeview/NeoStoreIndexStoreView.java index b9017c153e636..654a13091d63f 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/transaction/state/storeview/NeoStoreIndexStoreView.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/transaction/state/storeview/NeoStoreIndexStoreView.java @@ -24,7 +24,6 @@ import org.neo4j.helpers.collection.Visitor; import org.neo4j.kernel.api.exceptions.EntityNotFoundException; -import org.neo4j.kernel.api.schema.IndexDescriptor; import org.neo4j.kernel.api.index.NodePropertyUpdate; import org.neo4j.kernel.api.labelscan.NodeLabelUpdate; import org.neo4j.kernel.api.properties.Property; @@ -67,35 +66,34 @@ public NeoStoreIndexStoreView( LockService locks, NeoStores neoStores ) } @Override - public DoubleLongRegister indexUpdatesAndSize( IndexDescriptor descriptor, DoubleLongRegister output ) + public DoubleLongRegister indexUpdatesAndSize( long indexId, DoubleLongRegister output ) { - return counts.indexUpdatesAndSize( descriptor, output ); + return counts.indexUpdatesAndSize( indexId, output ); } @Override - public void replaceIndexCounts( IndexDescriptor descriptor, - long uniqueElements, long maxUniqueElements, long indexSize ) + public void replaceIndexCounts( long indexId, long uniqueElements, long maxUniqueElements, long indexSize ) { try ( CountsAccessor.IndexStatsUpdater updater = counts.updateIndexCounts() ) { - updater.replaceIndexSample( descriptor, uniqueElements, maxUniqueElements ); - updater.replaceIndexUpdateAndSize( descriptor, 0L, indexSize ); + updater.replaceIndexSample( indexId, uniqueElements, maxUniqueElements ); + updater.replaceIndexUpdateAndSize( indexId, 0L, indexSize ); } } @Override - public void incrementIndexUpdates( IndexDescriptor descriptor, long updatesDelta ) + public void incrementIndexUpdates( long indexId, long updatesDelta ) { try ( CountsAccessor.IndexStatsUpdater updater = counts.updateIndexCounts() ) { - updater.incrementIndexUpdates( descriptor, updatesDelta ); + updater.incrementIndexUpdates( indexId, updatesDelta ); } } @Override - public DoubleLongRegister indexSample( IndexDescriptor descriptor, DoubleLongRegister output ) + public DoubleLongRegister indexSample( long indexId, DoubleLongRegister output ) { - return counts.indexSample( descriptor, output ); + return counts.indexSample( indexId, output ); } @Override diff --git a/community/kernel/src/main/java/org/neo4j/storageengine/api/StoreReadLayer.java b/community/kernel/src/main/java/org/neo4j/storageengine/api/StoreReadLayer.java index fe384d3469cf2..61d13f6498e63 100644 --- a/community/kernel/src/main/java/org/neo4j/storageengine/api/StoreReadLayer.java +++ b/community/kernel/src/main/java/org/neo4j/storageengine/api/StoreReadLayer.java @@ -373,9 +373,11 @@ void relationshipVisit( long relationshipId, int relationshipTypeCount(); - DoubleLongRegister indexUpdatesAndSize( IndexDescriptor index, DoubleLongRegister target ); + DoubleLongRegister indexUpdatesAndSize( IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException; - DoubleLongRegister indexSample( IndexDescriptor index, DoubleLongRegister target ); + DoubleLongRegister indexSample( IndexDescriptor index, DoubleLongRegister target ) + throws IndexNotFoundKernelException; boolean nodeExists( long id ); } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/builtinprocs/ResampleIndexProcedureTest.java b/community/kernel/src/test/java/org/neo4j/kernel/builtinprocs/ResampleIndexProcedureTest.java index df4781d8b48e2..b5b3bb3cf6ab7 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/builtinprocs/ResampleIndexProcedureTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/builtinprocs/ResampleIndexProcedureTest.java @@ -119,7 +119,8 @@ public void shouldThrowAnExceptionIfTheIndexDoesNotExist() } @Test - public void shouldTriggerResampling() throws SchemaRuleNotFoundException, ProcedureException + public void shouldTriggerResampling() + throws SchemaRuleNotFoundException, ProcedureException, IndexNotFoundKernelException { IndexDescriptor index = IndexDescriptorFactory.of( 123, 456 ); when( operations.indexGetForLabelAndPropertyKey( anyObject() ) ).thenReturn( index ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulatorTest.java index e332ebabf2596..b283d55158c6d 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/BatchingMultipleIndexPopulatorTest.java @@ -292,14 +292,18 @@ private static IndexPopulator addPopulator( BatchingMultipleIndexPopulator batch { IndexPopulator populator = mock( IndexPopulator.class ); IndexDescriptor descriptor = IndexDescriptorFactory.of( id, id ); + long indexId = id; IndexProxyFactory indexProxyFactory = mock( IndexProxyFactory.class ); FailedIndexProxyFactory failedIndexProxyFactory = mock( FailedIndexProxyFactory.class ); FlippableIndexProxy flipper = new FlippableIndexProxy(); flipper.setFlipTarget( indexProxyFactory ); - batchingPopulator.addPopulator( populator, descriptor, new SchemaIndexProvider.Descriptor( "foo", "1" ), - IndexConfiguration.NON_UNIQUE, flipper, failedIndexProxyFactory, "testIndex" ); + batchingPopulator.addPopulator( + populator, indexId, descriptor, + new SchemaIndexProvider.Descriptor( "foo", "1" ), + IndexConfiguration.NON_UNIQUE, flipper, + failedIndexProxyFactory, "testIndex" ); return populator; } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobTest.java index 5b2a3aebcabf2..758ea4e2a63a2 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexPopulationJobTest.java @@ -39,13 +39,12 @@ import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Transaction; -import org.neo4j.graphdb.schema.IndexDefinition; import org.neo4j.helpers.collection.MapUtil; import org.neo4j.helpers.collection.Pair; import org.neo4j.helpers.collection.Visitor; import org.neo4j.kernel.api.KernelAPI; import org.neo4j.kernel.api.KernelTransaction; -import org.neo4j.kernel.api.schema.NodePropertyDescriptor; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.exceptions.KernelException; import org.neo4j.kernel.api.exceptions.TransactionFailureException; @@ -65,16 +64,13 @@ import org.neo4j.kernel.impl.api.KernelSchemaStateStore; import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProvider; import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig; -import org.neo4j.kernel.impl.coreapi.schema.IndexDefinitionImpl; import org.neo4j.kernel.impl.coreapi.schema.InternalSchemaActions; -import org.neo4j.kernel.impl.coreapi.schema.PropertyNameUtils; import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.logging.AssertableLogProvider; import org.neo4j.logging.AssertableLogProvider.LogMatcherBuilder; import org.neo4j.logging.LogProvider; import org.neo4j.logging.NullLogProvider; import org.neo4j.register.Register.DoubleLongRegister; -import org.neo4j.register.Registers; import org.neo4j.storageengine.api.schema.PopulationProgress; import org.neo4j.test.DoubleLatch; import org.neo4j.test.OtherThreadExecutor; @@ -106,6 +102,7 @@ import static org.neo4j.kernel.impl.api.index.IndexingService.NO_MONITOR; import static org.neo4j.kernel.impl.api.index.TestSchemaIndexProviderDescriptor.PROVIDER_DESCRIPTOR; import static org.neo4j.logging.AssertableLogProvider.inLog; +import static org.neo4j.register.Registers.newDoubleLongRegister; public class IndexPopulationJobTest { @@ -156,7 +153,7 @@ public void shouldPopulateIndexWithOneNode() throws Exception String value = "Taylor"; long nodeId = createNode( map( name, value ), FIRST ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, new FlippableIndexProxy(), false ); + IndexPopulationJob job = newIndexPopulationJob( populator, new FlippableIndexProxy(), false ); // WHEN job.run(); @@ -183,7 +180,7 @@ public void shouldFlushSchemaStateAfterPopulation() throws Exception createNode( map( name, value ), FIRST ); stateHolder.apply( MapUtil.stringMap( "key", "original_value" ) ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, new FlippableIndexProxy(), false ); + IndexPopulationJob job = newIndexPopulationJob( populator, new FlippableIndexProxy(), false ); // WHEN job.run(); @@ -203,7 +200,7 @@ public void shouldPopulateIndexWithASmallDataset() throws Exception createNode( map( age, 31 ), FIRST ); long node4 = createNode( map( age, 35, name, value ), FIRST ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, new FlippableIndexProxy(), false ); + IndexPopulationJob job = newIndexPopulationJob( populator, new FlippableIndexProxy(), false ); // WHEN job.run(); @@ -237,7 +234,7 @@ public void shouldIndexConcurrentUpdatesWhilePopulating() throws Exception int propertyKeyId = getPropertyKeyForName( name ); NodeChangingWriter populator = new NodeChangingWriter( changeNode, propertyKeyId, value1, changedValue, labelId ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, new FlippableIndexProxy(), false ); + IndexPopulationJob job = newIndexPopulationJob( populator, new FlippableIndexProxy(), false ); populator.setJob( job ); // WHEN @@ -262,7 +259,7 @@ public void shouldRemoveViaConcurrentIndexUpdatesWhilePopulating() throws Except long node3 = createNode( map( name, value3 ), FIRST ); int propertyKeyId = getPropertyKeyForName( name ); NodeDeletingWriter populator = new NodeDeletingWriter( node2, propertyKeyId, value2, labelId ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, new FlippableIndexProxy(), false ); + IndexPopulationJob job = newIndexPopulationJob( populator, new FlippableIndexProxy(), false ); populator.setJob( job ); // WHEN @@ -285,7 +282,7 @@ public void shouldTransitionToFailedStateIfPopulationJobCrashes() throws Excepti FlippableIndexProxy index = new FlippableIndexProxy(); createNode( map( name, "Taylor" ), FIRST ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, failingPopulator, index, false ); + IndexPopulationJob job = newIndexPopulationJob( failingPopulator, index, false ); // WHEN job.run(); @@ -308,7 +305,7 @@ public void shouldBeAbleToCancelPopulationJob() throws Exception Matchers.>any()) ) .thenReturn(storeScan ); - final IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, index, storeView, + final IndexPopulationJob job = newIndexPopulationJob( populator, index, storeView, NullLogProvider.getInstance(), false ); OtherThreadExecutor populationJobRunner = cleanup.add( new OtherThreadExecutor<>( @@ -345,7 +342,7 @@ public void shouldLogJobProgress() throws Exception AssertableLogProvider logProvider = new AssertableLogProvider(); FlippableIndexProxy index = mock( FlippableIndexProxy.class ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, index, indexStoreView, logProvider, false ); + IndexPopulationJob job = newIndexPopulationJob( populator, index, indexStoreView, logProvider, false ); // When job.run(); @@ -366,7 +363,7 @@ public void shouldLogJobFailure() throws Exception AssertableLogProvider logProvider = new AssertableLogProvider(); FlippableIndexProxy index = mock( FlippableIndexProxy.class ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, index, indexStoreView, logProvider, false ); + IndexPopulationJob job = newIndexPopulationJob( populator, index, indexStoreView, logProvider, false ); Throwable failure = new IllegalStateException( "not successful" ); doThrow( failure ).when( populator ).create(); @@ -388,7 +385,7 @@ public void shouldFlipToFailedUsingFailedIndexProxyFactory() throws Exception FailedIndexProxyFactory failureDelegateFactory = mock( FailedIndexProxyFactory.class ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); IndexPopulationJob job = - newIndexPopulationJob( FIRST, name, failureDelegateFactory, populator, + newIndexPopulationJob( failureDelegateFactory, populator, new FlippableIndexProxy(), indexStoreView, NullLogProvider.getInstance(), false ); IllegalStateException failure = new IllegalStateException( "not successful" ); @@ -414,7 +411,7 @@ public void shouldCloseAndFailOnFailure() throws Exception LogProvider logProvider = NullLogProvider.getInstance(); FlippableIndexProxy index = mock( FlippableIndexProxy.class ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, index, indexStoreView, logProvider, false ); + IndexPopulationJob job = newIndexPopulationJob( populator, index, indexStoreView, logProvider, false ); String failureMessage = "not successful"; IllegalStateException failure = new IllegalStateException( failureMessage ); @@ -440,7 +437,7 @@ public void shouldFailIfDeferredConstraintViolated() throws Exception LogProvider logProvider = NullLogProvider.getInstance(); FlippableIndexProxy index = new FlippableIndexProxy( mock( IndexProxy.class ) ); IndexPopulator populator = spy( inMemoryPopulator( false ) ); - IndexPopulationJob job = newIndexPopulationJob( FIRST, name, populator, index, indexStoreView, logProvider, true ); + IndexPopulationJob job = newIndexPopulationJob( populator, index, indexStoreView, logProvider, true ); IndexEntryConflictException failure = new PreexistingIndexEntryConflictException( "duplicate value", 0, 1 ); doThrow( failure ).when( populator ).verifyDeferredConstraints( indexStoreView ); @@ -644,38 +641,37 @@ private IndexPopulator inMemoryPopulator( boolean constraint ) throws Transactio return new InMemoryIndexProvider().getPopulator( 21, descriptor, indexConfig, samplingConfig ); } - private IndexPopulationJob newIndexPopulationJob( Label label, String propertyKey, IndexPopulator populator, + private IndexPopulationJob newIndexPopulationJob( IndexPopulator populator, FlippableIndexProxy flipper, boolean constraint ) throws TransactionFailureException { - return newIndexPopulationJob( label, propertyKey, populator, flipper, indexStoreView, + return newIndexPopulationJob( populator, flipper, indexStoreView, NullLogProvider.getInstance(), constraint ); } - private IndexPopulationJob newIndexPopulationJob( Label label, String propertyKey, - IndexPopulator populator, + private IndexPopulationJob newIndexPopulationJob( IndexPopulator populator, FlippableIndexProxy flipper, IndexStoreView storeView, LogProvider logProvider, boolean constraint ) throws TransactionFailureException { - return newIndexPopulationJob( label, propertyKey, + return newIndexPopulationJob( mock( FailedIndexProxyFactory.class ), populator, flipper, storeView, logProvider, constraint ); } - private IndexPopulationJob newIndexPopulationJob( Label label, String propertyKey, - FailedIndexProxyFactory failureDelegateFactory, + private IndexPopulationJob newIndexPopulationJob( FailedIndexProxyFactory failureDelegateFactory, IndexPopulator populator, FlippableIndexProxy flipper, IndexStoreView storeView, LogProvider logProvider, boolean constraint ) throws TransactionFailureException { - IndexDescriptor descriptor = indexDescriptor( label, propertyKey ); + IndexDescriptor descriptor = indexDescriptor( FIRST, name ); + long indexId = 0; flipper.setFlipTarget( mock( IndexProxyFactory.class ) ); MultipleIndexPopulator multiPopulator = new MultipleIndexPopulator( storeView, logProvider ); IndexPopulationJob job = new IndexPopulationJob( storeView, multiPopulator, NO_MONITOR, stateHolder::clear ); - job.addPopulator( populator, descriptor, IndexConfiguration.of( constraint ), PROVIDER_DESCRIPTOR, - format( ":%s(%s)", label.name(), propertyKey ), flipper, failureDelegateFactory ); + job.addPopulator( populator, indexId, descriptor, IndexConfiguration.of( constraint ), PROVIDER_DESCRIPTOR, + format( ":%s(%s)", FIRST.name(), name ), flipper, failureDelegateFactory ); return job; } @@ -684,12 +680,11 @@ private IndexDescriptor indexDescriptor( Label label, String propertyKey ) throw try ( KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AnonymousContext.read() ); Statement statement = tx.acquireStatement() ) { - IndexDefinition indexDefinition = new IndexDefinitionImpl( actions, label, propertyKey, false ); - NodePropertyDescriptor descriptor = - IndexDescriptorFactory.getTokens( statement.readOperations(), indexDefinition ); - IndexDescriptor index = IndexDescriptorFactory.of( descriptor ); + int labelId = statement.readOperations().labelGetForName( label.name() ); + int propertyKeyId = statement.readOperations().propertyKeyGetForName( propertyKey ); + IndexDescriptor descriptor = IndexDescriptorFactory.of( labelId, propertyKeyId ); tx.success(); - return index; + return descriptor; } } @@ -700,9 +695,17 @@ private DoubleLongRegister indexUpdatesAndSize( Label label, String propertyKey { int labelId = statement.readOperations().labelGetForName( label.name() ); int propertyKeyId = statement.readOperations().propertyKeyGetForName( propertyKey ); - DoubleLongRegister result = - statement.readOperations().indexUpdatesAndSize( IndexDescriptorFactory.of( labelId, propertyKeyId ), - Registers.newDoubleLongRegister() ); + DoubleLongRegister result = newDoubleLongRegister(0, 0); + try + { + result = statement.readOperations() + .indexUpdatesAndSize( IndexDescriptorFactory.of( labelId, propertyKeyId ), + newDoubleLongRegister() ); + } + catch (IndexNotFoundKernelException e) + { + // Tests ignore this + } tx.success(); return result; } @@ -713,10 +716,18 @@ private DoubleLongRegister indexSample( Label label, String propertyKey ) throws try ( KernelTransaction tx = kernel.newTransaction( KernelTransaction.Type.implicit, AnonymousContext.read() ); Statement statement = tx.acquireStatement() ) { - DoubleLongRegister result = Registers.newDoubleLongRegister(); int labelId = statement.readOperations().labelGetForName( label.name() ); int propertyKeyId = statement.readOperations().propertyKeyGetForName( propertyKey ); - statement.readOperations().indexSample( IndexDescriptorFactory.of( labelId, propertyKeyId ), result ); + DoubleLongRegister result = newDoubleLongRegister(0, 0); + try + { + result = statement.readOperations() + .indexSample( IndexDescriptorFactory.of( labelId, propertyKeyId ), newDoubleLongRegister() ); + } + catch (IndexNotFoundKernelException e) + { + // Tests ignore this + } tx.success(); return result; } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsIT.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsIT.java index 9bfec390d17e8..590cdd59daf7c 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsIT.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsIT.java @@ -24,6 +24,7 @@ import org.junit.Rule; import org.junit.Test; +import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.neo4j.graphdb.GraphDatabaseService; @@ -48,6 +49,7 @@ import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.logging.AssertableLogProvider; import org.neo4j.register.Register.DoubleLongRegister; +import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.test.TestGraphDatabaseFactory; import org.neo4j.test.rule.fs.EphemeralFileSystemRule; @@ -98,9 +100,10 @@ public void shouldRecoverIndexCountsBySamplingThemOnStartup() // where ALIEN and SPECIMEN are both the first ids of their kind IndexDescriptor index = IndexDescriptorFactory.of( labelId( ALIEN ), pkId( SPECIMEN ) ); + long indexId = indexId( index ); // for which we don't have index counts - resetIndexCounts( index ); + resetIndexCounts( indexId ); // when we shutdown the database and restart it restart(); @@ -110,11 +113,11 @@ public void shouldRecoverIndexCountsBySamplingThemOnStartup() assertEqualRegisters( "Unexpected updates and size for the index", newDoubleLongRegister( 0, 32 ), - tracker.indexUpdatesAndSize( index, newDoubleLongRegister() ) ); + tracker.indexUpdatesAndSize( indexId, newDoubleLongRegister() ) ); assertEqualRegisters( "Unexpected sampling result", newDoubleLongRegister( 16, 32 ), - tracker.indexSample( index, newDoubleLongRegister() ) + tracker.indexSample( indexId, newDoubleLongRegister() ) ); // and also @@ -189,12 +192,24 @@ private IndexDefinition indexAliensBySpecimen() } } - private void resetIndexCounts( IndexDescriptor index ) + private long indexId(IndexDescriptor index) + { + for ( SchemaRule rule : neoStores().getSchemaStore() ) + { + if ( rule.descriptor().equals( index.descriptor() ) ) + { + return rule.getId(); + } + } + return -1; + } + + private void resetIndexCounts( long indexId ) { try ( CountsAccessor.IndexStatsUpdater updater = neoStores().getCounts().updateIndexCounts() ) { - updater.replaceIndexSample( index, 0, 0 ); - updater.replaceIndexUpdateAndSize( index, 0, 0 ); + updater.replaceIndexSample( indexId, 0, 0 ); + updater.replaceIndexUpdateAndSize( indexId, 0, 0 ); } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsTest.java index fa86dd8f5fbfe..26bc94c0873bb 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexStatisticsTest.java @@ -54,11 +54,13 @@ import org.neo4j.kernel.api.properties.Property; import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge; import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine; +import org.neo4j.kernel.impl.store.NeoStores; import org.neo4j.kernel.impl.store.counts.CountsTracker; import org.neo4j.kernel.internal.GraphDatabaseAPI; import org.neo4j.kernel.monitoring.Monitors; import org.neo4j.register.Register.DoubleLongRegister; import org.neo4j.register.Registers; +import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.test.rule.DatabaseRule; import org.neo4j.test.rule.EmbeddedDatabaseRule; @@ -174,6 +176,7 @@ public void shouldRemoveIndexStatisticsAfterIndexIsDeleted() throws KernelExcept // given createSomePersons(); IndexDescriptor index = awaitOnline( createIndex( "Person", "name" ) ); + long indexId = indexId( index ); // when dropIndex( index ); @@ -186,12 +189,12 @@ public void shouldRemoveIndexStatisticsAfterIndexIsDeleted() throws KernelExcept } catch ( IndexNotFoundKernelException e ) { - DoubleLongRegister actual = getTracker().indexSample( index, Registers.newDoubleLongRegister() ); + DoubleLongRegister actual = getTracker().indexSample( indexId, Registers.newDoubleLongRegister() ); assertDoubleLongEquals( 0L, 0L, actual ); } // and then index size and index updates are zero on disk - DoubleLongRegister actual = getTracker().indexUpdatesAndSize( index, Registers.newDoubleLongRegister() ); + DoubleLongRegister actual = getTracker().indexUpdatesAndSize( indexId, Registers.newDoubleLongRegister() ); assertDoubleLongEquals( 0L, 0L, actual ); } @@ -496,6 +499,24 @@ private IndexDescriptor createIndex( String labelName, String propertyKeyName ) } } + private long indexId(IndexDescriptor index) + { + for ( SchemaRule rule : neoStores().getSchemaStore() ) + { + if ( rule.descriptor().equals( index.descriptor() ) ) + { + return rule.getId(); + } + } + return -1; + } + + private NeoStores neoStores() + { + return ( (GraphDatabaseAPI) db ).getDependencyResolver().resolveDependency( RecordStorageEngine.class ) + .testAccessNeoStores(); + } + private IndexDescriptor awaitOnline( IndexDescriptor index ) throws KernelException { long start = System.currentTimeMillis(); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexingServiceTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexingServiceTest.java index 8e0533e014376..98528d68e1fa8 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexingServiceTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/IndexingServiceTest.java @@ -158,7 +158,7 @@ public class IndexingServiceTest public void setUp() { when( populator.sampleResult() ).thenReturn( new IndexSample() ); - when( storeView.indexSample( any( IndexDescriptor.class ), any( DoubleLongRegister.class ) ) ) + when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ) .thenAnswer( invocation -> invocation.getArguments()[1] ); } @@ -385,7 +385,7 @@ public void shouldLogIndexStateOnStart() throws Exception when(mockLookup.labelGetName( 2 )).thenReturn( "LabelTwo" ); when(mockLookup.propertyKeyGetName( 1 )).thenReturn( "propertyOne" ); when(mockLookup.propertyKeyGetName( 2 )).thenReturn( "propertyTwo" ); - when( storeView.indexSample( any( IndexDescriptor.class ), any( DoubleLongRegister.class ) ) ).thenReturn( newDoubleLongRegister( 32L, 32L ) ); + when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ).thenReturn( newDoubleLongRegister( 32L, 32L ) ); logProvider.clear(); @@ -444,7 +444,7 @@ public void shouldSnapshotOnlineIndexes() throws Exception when( indexAccessor.snapshotFiles()).thenAnswer( newResourceIterator( theFile ) ); when( indexProvider.getInitialState( indexId ) ).thenReturn( ONLINE ); when( indexProvider.getInitialState( indexId2 ) ).thenReturn( ONLINE ); - when( storeView.indexSample( any( IndexDescriptor.class ), any( DoubleLongRegister.class ) ) ) + when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ) .thenReturn( newDoubleLongRegister( 32L, 32L ) ); life.start(); @@ -476,7 +476,7 @@ public void shouldNotSnapshotPopulatingIndexes() throws Exception when( indexAccessor.snapshotFiles() ).thenAnswer( newResourceIterator( theFile ) ); when( indexProvider.getInitialState( indexId ) ).thenReturn( POPULATING ); when( indexProvider.getInitialState( indexId2 ) ).thenReturn( ONLINE ); - when( storeView.indexSample( any( IndexDescriptor.class ), any( DoubleLongRegister.class ) ) ).thenReturn( newDoubleLongRegister( 32L, 32L ) ); + when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ).thenReturn( newDoubleLongRegister( 32L, 32L ) ); life.start(); // WHEN @@ -521,9 +521,13 @@ public void shouldLogTriggerSamplingOnAllIndexes() throws Exception public void shouldLogTriggerSamplingOnAnIndexes() throws Exception { // given - IndexingService indexingService = newIndexingServiceWithMockedDependencies( populator, accessor, withData() ); + long indexId = 0; IndexSamplingMode mode = TRIGGER_REBUILD_ALL; IndexDescriptor descriptor = IndexDescriptorFactory.of( 0, 1 ); + IndexingService indexingService = newIndexingServiceWithMockedDependencies( populator, accessor, withData(), + indexRule( indexId, descriptor.descriptor(), PROVIDER_DESCRIPTOR ) ); + life.init(); + life.start(); // when indexingService.triggerIndexSampling( descriptor, mode ); @@ -754,7 +758,7 @@ public void shouldNotLoseIndexDescriptorDueToOtherSimilarIndexDuringRecovery() t .nodeAsUpdates( eq( nodeId ), any( Collection.class ) ); DoubleLongRegister register = mock( DoubleLongRegister.class ); when( register.readSecond() ).thenReturn( 42L ); - when( storeView.indexSample( any( IndexDescriptor.class ), any( DoubleLongRegister.class ) ) ) + when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ) .thenReturn( register ); // For some reason the usual accessor returned null from newUpdater, even when told to return the updater // so spying on a real object instead. @@ -1004,6 +1008,7 @@ private IndexingService newIndexingServiceWithMockedDependencies( IndexPopulator IndexingService.Monitor monitor, IndexRule... rules ) throws IOException { + when( indexProvider.getInitialState( anyLong() ) ).thenReturn( ONLINE ); when( indexProvider.getProviderDescriptor() ).thenReturn( PROVIDER_DESCRIPTOR ); when( indexProvider.getPopulator( anyLong(), any( IndexDescriptor.class ), any( IndexConfiguration.class ), any( IndexSamplingConfig.class ) ) ).thenReturn( populator ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorTest.java index a584fb73164f1..624aeb36bdaac 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorTest.java @@ -159,14 +159,11 @@ public void testFailPopulator() throws IOException @Test public void testFailByPopulation() throws IOException { - IndexDescriptor descriptor1 = IndexDescriptorFactory.of( 1, 1 ); - IndexDescriptor descriptor2 = IndexDescriptorFactory.of( 2, 2 ); - IndexPopulator populator1 = createIndexPopulator(); IndexPopulator populator2 = createIndexPopulator(); - addPopulator( populator1, descriptor1 ); - IndexPopulation population2 = addPopulator( populator2, descriptor2 ); + addPopulator( populator1, 1 ); + IndexPopulation population2 = addPopulator( populator2, 2 ); multipleIndexPopulator.fail( population2, getPopulatorException() ); @@ -177,14 +174,11 @@ public void testFailByPopulation() throws IOException @Test public void testFailByPopulationRemovesPopulator() throws IOException { - IndexDescriptor descriptor1 = IndexDescriptorFactory.of( 1, 1 ); - IndexDescriptor descriptor2 = IndexDescriptorFactory.of( 2, 2 ); - IndexPopulator populator1 = createIndexPopulator(); IndexPopulator populator2 = createIndexPopulator(); - IndexPopulation population1 = addPopulator( populator1, descriptor1 ); - IndexPopulation population2 = addPopulator( populator2, descriptor2 ); + IndexPopulation population1 = addPopulator( populator1, 1 ); + IndexPopulation population2 = addPopulator( populator2, 2 ); multipleIndexPopulator.fail( population1, getPopulatorException() ); multipleIndexPopulator.fail( population2, getPopulatorException() ); @@ -197,12 +191,11 @@ public void testFailByPopulationRemovesPopulator() throws IOException @Test public void testFailByNonExistingPopulation() throws IOException { - IndexDescriptor descriptor = IndexDescriptorFactory.of( 1, 1 ); IndexPopulation nonExistingPopulation = mock( IndexPopulation.class ); IndexPopulator populator = createIndexPopulator(); - addPopulator( populator, descriptor ); + addPopulator( populator, 1 ); multipleIndexPopulator.fail( nonExistingPopulation, getPopulatorException() ); @@ -254,8 +247,8 @@ public void testFlipAfterPopulation() throws Exception IndexPopulator indexPopulator1 = createIndexPopulator(); IndexPopulator indexPopulator2 = createIndexPopulator(); - FlippableIndexProxy flipper1 = addPopulator( indexPopulator1, 1 ); - FlippableIndexProxy flipper2 = addPopulator( indexPopulator2, 2 ); + FlippableIndexProxy flipper1 = addPopulator( indexPopulator1, 1 ).flipper; + FlippableIndexProxy flipper2 = addPopulator( indexPopulator2, 2 ).flipper; multipleIndexPopulator.flipAfterPopulation(); @@ -291,7 +284,7 @@ public void testCancelPopulation() throws IOException multipleIndexPopulator.cancel(); verify( indexStoreView, times( 2 ) ) - .replaceIndexCounts( any( IndexDescriptor.class ), eq( 0L ), eq( 0L ), eq( 0L ) ); + .replaceIndexCounts( anyLong(), eq( 0L ), eq( 0L ), eq( 0L ) ); verify( indexPopulator1 ).close( false ); verify( indexPopulator2 ).close( false ); } @@ -319,7 +312,7 @@ public void testIndexFlip() throws IOException verify( indexPopulator2 ).close( true ); verify( indexPopulator2 ).sampleResult(); - verify( indexStoreView ).replaceIndexCounts( any( IndexDescriptor.class ), anyLong(), anyLong(), anyLong() ); + verify( indexStoreView ).replaceIndexCounts( anyLong(), anyLong(), anyLong(), anyLong() ); } @Test @@ -445,47 +438,38 @@ private void checkPopulatorFailure( IndexPopulator populator ) verify( populator ).close( false ); } - private void addPopulator( IndexPopulator indexPopulator, int id, FlippableIndexProxy flippableIndexProxy, + private IndexPopulation addPopulator( IndexPopulator indexPopulator, int id, FlippableIndexProxy flippableIndexProxy, FailedIndexProxyFactory failedIndexProxyFactory ) { - addPopulator(multipleIndexPopulator, indexPopulator, id, flippableIndexProxy, failedIndexProxyFactory ); + return addPopulator(multipleIndexPopulator, indexPopulator, id, flippableIndexProxy, failedIndexProxyFactory ); } - private void addPopulator( MultipleIndexPopulator multipleIndexPopulator, IndexPopulator indexPopulator, int id, + private IndexPopulation addPopulator( MultipleIndexPopulator multipleIndexPopulator, IndexPopulator indexPopulator, int id, FlippableIndexProxy flippableIndexProxy, FailedIndexProxyFactory failedIndexProxyFactory ) { - IndexDescriptor descriptor = mock( IndexDescriptor.class ); - when( descriptor.getLabelId() ).thenReturn( id ); - when( descriptor.getPropertyKeyId() ).thenReturn( id ); - addPopulator( multipleIndexPopulator, descriptor, indexPopulator, flippableIndexProxy, - failedIndexProxyFactory ); - } - - private IndexPopulation addPopulator(IndexPopulator indexPopulator, IndexDescriptor descriptor ) - { - return addPopulator( multipleIndexPopulator, indexPopulator, descriptor ); + return addPopulator( multipleIndexPopulator, id, IndexDescriptorFactory.of( id, id ), indexPopulator, + flippableIndexProxy, failedIndexProxyFactory ); } private IndexPopulation addPopulator( MultipleIndexPopulator multipleIndexPopulator, IndexPopulator indexPopulator, - IndexDescriptor descriptor ) + long indexId, IndexDescriptor descriptor ) { - return addPopulator( multipleIndexPopulator, descriptor, indexPopulator, mock( FlippableIndexProxy.class ), - mock( FailedIndexProxyFactory.class ) ); + return addPopulator( multipleIndexPopulator, indexId, descriptor, indexPopulator, + mock( FlippableIndexProxy.class ), mock( FailedIndexProxyFactory.class ) ); } - private IndexPopulation addPopulator(MultipleIndexPopulator multipleIndexPopulator, IndexDescriptor descriptor, - IndexPopulator indexPopulator, + private IndexPopulation addPopulator(MultipleIndexPopulator multipleIndexPopulator, long indexId, + IndexDescriptor descriptor, IndexPopulator indexPopulator, FlippableIndexProxy flippableIndexProxy, FailedIndexProxyFactory failedIndexProxyFactory ) { - return multipleIndexPopulator.addPopulator( indexPopulator, descriptor, + return multipleIndexPopulator.addPopulator( indexPopulator, indexId, descriptor, mock( SchemaIndexProvider.Descriptor.class ), IndexConfiguration.NON_UNIQUE, flippableIndexProxy, failedIndexProxyFactory, "userIndexDescription" ); } - private FlippableIndexProxy addPopulator( IndexPopulator indexPopulator, int id ) + private IndexPopulation addPopulator( IndexPopulator indexPopulator, int id ) { FlippableIndexProxy indexProxy = mock( FlippableIndexProxy.class ); - addPopulator( indexPopulator, id, indexProxy, mock( FailedIndexProxyFactory.class ) ); - return indexProxy; + return addPopulator( indexPopulator, id, indexProxy, mock( FailedIndexProxyFactory.class ) ); } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorUpdatesTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorUpdatesTest.java index 747b8d496b667..f36242e2d9e6e 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorUpdatesTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/MultipleIndexPopulatorUpdatesTest.java @@ -112,8 +112,7 @@ public void updateForHigherNodeIgnoredWhenUsingFullNodeStoreScan() IndexUpdater indexUpdater = mock( IndexUpdater.class ); when( populator.newPopulatingUpdater( storeView ) ).thenReturn( indexUpdater ); - IndexDescriptor descriptor = IndexDescriptorFactory.of( 1, 1 ); - addPopulator( indexPopulator, populator, descriptor ); + addPopulator( indexPopulator, populator, 1, IndexDescriptorFactory.of( 1, 1 ) ); indexPopulator.create(); StoreScan storeScan = indexPopulator.indexAllNodes(); @@ -148,18 +147,19 @@ private IndexPopulator createIndexPopulator() return populator; } - private MultipleIndexPopulator.IndexPopulation addPopulator( MultipleIndexPopulator multipleIndexPopulator, IndexPopulator indexPopulator, - IndexDescriptor descriptor ) + private MultipleIndexPopulator.IndexPopulation addPopulator( MultipleIndexPopulator multipleIndexPopulator, + IndexPopulator indexPopulator, long indexId, IndexDescriptor descriptor ) { - return addPopulator( multipleIndexPopulator, descriptor, indexPopulator, mock( FlippableIndexProxy.class ), + return addPopulator( multipleIndexPopulator, indexId, descriptor, indexPopulator, mock( FlippableIndexProxy.class ), mock( FailedIndexProxyFactory.class ) ); } - private MultipleIndexPopulator.IndexPopulation addPopulator(MultipleIndexPopulator multipleIndexPopulator, IndexDescriptor descriptor, + private MultipleIndexPopulator.IndexPopulation addPopulator( MultipleIndexPopulator multipleIndexPopulator, + long indexId, IndexDescriptor descriptor, IndexPopulator indexPopulator, FlippableIndexProxy flippableIndexProxy, FailedIndexProxyFactory failedIndexProxyFactory ) { - return multipleIndexPopulator.addPopulator( indexPopulator, descriptor, + return multipleIndexPopulator.addPopulator( indexPopulator, indexId, descriptor, mock( SchemaIndexProvider.Descriptor.class ), IndexConfiguration.NON_UNIQUE, flippableIndexProxy, failedIndexProxyFactory, "userIndexDescription" ); } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxyTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxyTest.java index 1530b2c159920..0953e84bf237c 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxyTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/OnlineIndexProxyTest.java @@ -35,6 +35,7 @@ public class OnlineIndexProxyTest { + private final long indexId = 1; private final IndexDescriptor descriptor = IndexDescriptorFactory.of( 1, 2 ); private final IndexConfiguration config = IndexConfiguration.NON_UNIQUE; private final SchemaIndexProvider.Descriptor providerDescriptor = mock( SchemaIndexProvider.Descriptor.class ); @@ -45,7 +46,7 @@ public class OnlineIndexProxyTest public void shouldRemoveIndexCountsWhenTheIndexItselfIsDropped() throws IOException { // given - OnlineIndexProxy index = new OnlineIndexProxy( descriptor, config, accessor, + OnlineIndexProxy index = new OnlineIndexProxy( indexId, descriptor, config, accessor, storeView, providerDescriptor, false ); // when @@ -53,7 +54,7 @@ public void shouldRemoveIndexCountsWhenTheIndexItselfIsDropped() throws IOExcept // then verify( accessor ).drop(); - verify( storeView ).replaceIndexCounts( descriptor, 0L, 0L, 0L ); + verify( storeView ).replaceIndexCounts( indexId, 0L, 0L, 0L ); verifyNoMoreInteractions( accessor, storeView ); } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerTest.java index 25482850ff0e5..de1f3e1c904b3 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingControllerTest.java @@ -53,7 +53,7 @@ public class IndexSamplingControllerTest public void shouldStartASamplingJobForEachIndexInTheDB() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); @@ -61,7 +61,7 @@ public void shouldStartASamplingJobForEachIndexInTheDB() controller.sampleIndexes( BACKGROUND_REBUILD_UPDATED ); // then - verify( jobFactory ).create( indexProxy ); + verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); verify( tracker, times( 2 ) ).canExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); @@ -71,7 +71,7 @@ public void shouldStartASamplingJobForEachIndexInTheDB() public void shouldNotStartAJobIfTheIndexIsNotOnline() throws InterruptedException { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( POPULATING ); @@ -87,7 +87,7 @@ public void shouldNotStartAJobIfTheIndexIsNotOnline() throws InterruptedExceptio public void shouldNotStartAJobIfTheTrackerCannotHandleIt() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( false ); when( indexProxy.getState() ).thenReturn( ONLINE ); @@ -115,7 +115,7 @@ protected Boolean initialValue() } }; - IndexSamplingJobFactory jobFactory = proxy -> { + IndexSamplingJobFactory jobFactory = (indexId, proxy) -> { // make sure we execute this once per thread if ( hasRun.get() ) { @@ -140,7 +140,7 @@ protected Boolean initialValue() }; final IndexSamplingController controller = new IndexSamplingController( - samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, FALSE + samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); @@ -171,19 +171,19 @@ protected Boolean initialValue() public void shouldSampleAllTheIndexes() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); when( anotherIndexProxy.getState() ).thenReturn( ONLINE ); - indexMap.putIndexProxy( 3, anotherIndexProxy ); + indexMap.putIndexProxy( anotherIndexId, anotherIndexProxy ); // when controller.sampleIndexes( TRIGGER_REBUILD_UPDATED ); // then - verify( jobFactory ).create( indexProxy ); + verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); - verify( jobFactory ).create( anotherIndexProxy ); + verify( jobFactory ).create( anotherIndexId, anotherIndexProxy ); verify( tracker ).scheduleSamplingJob( anotherJob ); verify( tracker, times( 2 ) ).waitUntilCanExecuteMoreSamplingJobs(); @@ -194,17 +194,17 @@ public void shouldSampleAllTheIndexes() public void shouldSampleAllTheOnlineIndexes() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); when( anotherIndexProxy.getState() ).thenReturn( POPULATING ); - indexMap.putIndexProxy( 3, anotherIndexProxy ); + indexMap.putIndexProxy( anotherIndexId, anotherIndexProxy ); // when controller.sampleIndexes( TRIGGER_REBUILD_UPDATED ); // then - verify( jobFactory ).create( indexProxy ); + verify( jobFactory ).create( indexId, indexProxy ); verify( tracker ).scheduleSamplingJob( job ); verify( tracker, times( 2 ) ).waitUntilCanExecuteMoreSamplingJobs(); @@ -220,7 +220,7 @@ public void shouldNotStartOtherSamplingWhenSamplingAllTheIndexes() final DoubleLatch jobLatch = new DoubleLatch(); final DoubleLatch testLatch = new DoubleLatch(); - IndexSamplingJobFactory jobFactory = proxy -> { + IndexSamplingJobFactory jobFactory = (indexId, proxy) -> { if ( ! concurrentCount.compareAndSet( 0, 1 ) ) { throw new IllegalStateException( "count !== 0 on create" ); @@ -235,7 +235,7 @@ public void shouldNotStartOtherSamplingWhenSamplingAllTheIndexes() }; final IndexSamplingController controller = new IndexSamplingController( - samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, TRUE + samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, always( true ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); @@ -265,14 +265,14 @@ public void shouldNotStartOtherSamplingWhenSamplingAllTheIndexes() public void shouldRecoverOnlineIndex() { // given - IndexSamplingController controller = newSamplingController( TRUE ); + IndexSamplingController controller = newSamplingController( always( true ) ); when( indexProxy.getState() ).thenReturn( ONLINE ); // when controller.recoverIndexSamples(); // then - verify( jobFactory ).create( indexProxy ); + verify( jobFactory ).create( indexId, indexProxy ); verify( job ).run(); verifyNoMoreInteractions( jobFactory, job, tracker ); } @@ -281,7 +281,7 @@ public void shouldRecoverOnlineIndex() public void shouldNotRecoverOfflineIndex() { // given - IndexSamplingController controller = newSamplingController( TRUE ); + IndexSamplingController controller = newSamplingController( always( true ) ); when( indexProxy.getState() ).thenReturn( FAILED ); // when @@ -295,7 +295,7 @@ public void shouldNotRecoverOfflineIndex() public void shouldNotRecoverOnlineIndexIfNotNeeded() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( indexProxy.getState() ).thenReturn( ONLINE ); // when @@ -309,19 +309,19 @@ public void shouldNotRecoverOnlineIndexIfNotNeeded() public void shouldSampleIndex() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( true ); when( indexProxy.getState() ).thenReturn( ONLINE ); when( anotherIndexProxy.getState() ).thenReturn( ONLINE ); - indexMap.putIndexProxy( 3, anotherIndexProxy ); + indexMap.putIndexProxy( anotherIndexId, anotherIndexProxy ); // when - controller.sampleIndex( indexProxy.getDescriptor(), TRIGGER_REBUILD_UPDATED ); + controller.sampleIndex( indexId, TRIGGER_REBUILD_UPDATED ); // then - verify( jobFactory, times(1) ).create( indexProxy ); + verify( jobFactory, times(1) ).create( indexId, indexProxy ); verify( tracker, times(1) ).scheduleSamplingJob( job ); - verify( jobFactory, never() ).create( anotherIndexProxy ); + verify( jobFactory, never() ).create( anotherIndexId, anotherIndexProxy ); verify( tracker, never() ).scheduleSamplingJob( anotherJob ); verify( tracker, times( 1 ) ).waitUntilCanExecuteMoreSamplingJobs(); @@ -332,28 +332,43 @@ public void shouldSampleIndex() public void shouldNotStartForSingleIndexAJobIfTheTrackerCannotHandleIt() { // given - IndexSamplingController controller = newSamplingController( FALSE ); + IndexSamplingController controller = newSamplingController( always( false ) ); when( tracker.canExecuteMoreSamplingJobs() ).thenReturn( false ); when( indexProxy.getState() ).thenReturn( ONLINE ); // when - controller.sampleIndex( indexProxy.getDescriptor(), BACKGROUND_REBUILD_UPDATED ); + controller.sampleIndex( indexId, BACKGROUND_REBUILD_UPDATED ); // then verify( tracker, times( 1 ) ).canExecuteMoreSamplingJobs(); verifyNoMoreInteractions( jobFactory, tracker ); } - private static final Predicate TRUE = Predicates.alwaysTrue(); - private static final Predicate FALSE = Predicates.alwaysFalse(); + private static class Always implements IndexSamplingController.RecoveryCondition + { + private final boolean ans; + + Always( boolean ans ) + { + this.ans = ans; + } + + @Override + public boolean test( long indexId, IndexDescriptor descriptor ) + { + return ans; + } + } private final IndexSamplingConfig samplingConfig = mock( IndexSamplingConfig.class ); private final IndexSamplingJobFactory jobFactory = mock( IndexSamplingJobFactory.class ); - private final IndexSamplingJobQueue jobQueue = new IndexSamplingJobQueue<>( TRUE ); + private final IndexSamplingJobQueue jobQueue = new IndexSamplingJobQueue<>( Predicates.alwaysTrue() ); private final IndexSamplingJobTracker tracker = mock( IndexSamplingJobTracker.class ); private final JobScheduler scheduler = mock( JobScheduler.class ); private final IndexMapSnapshotProvider snapshotProvider = mock( IndexMapSnapshotProvider.class ); private final IndexMap indexMap = new IndexMap(); + private final long indexId = 2; + private final long anotherIndexId = 3; private final IndexProxy indexProxy = mock( IndexProxy.class ); private final IndexProxy anotherIndexProxy = mock( IndexProxy.class ); private final NodePropertyDescriptor descriptor = new NodePropertyDescriptor( 3, 4 ); @@ -367,12 +382,17 @@ public void shouldNotStartForSingleIndexAJobIfTheTrackerCannotHandleIt() when( indexProxy.getDescriptor() ).thenReturn( IndexDescriptorFactory.of( descriptor ) ); when( anotherIndexProxy.getDescriptor() ).thenReturn( IndexDescriptorFactory.of( anotherDescriptor ) ); when( snapshotProvider.indexMapSnapshot() ).thenReturn( indexMap ); - when( jobFactory.create( indexProxy ) ).thenReturn( job ); - when( jobFactory.create( anotherIndexProxy ) ).thenReturn( anotherJob ); - indexMap.putIndexProxy( 2, indexProxy ); + when( jobFactory.create( indexId, indexProxy ) ).thenReturn( job ); + when( jobFactory.create( anotherIndexId, anotherIndexProxy ) ).thenReturn( anotherJob ); + indexMap.putIndexProxy( indexId, indexProxy ); + } + + private IndexSamplingController.RecoveryCondition always(boolean ans) + { + return new Always(ans); } - private IndexSamplingController newSamplingController( Predicate recoveryPredicate ) + private IndexSamplingController newSamplingController( IndexSamplingController.RecoveryCondition recoveryPredicate ) { return new IndexSamplingController( samplingConfig, jobFactory, jobQueue, tracker, snapshotProvider, scheduler, recoveryPredicate diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTrackerTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTrackerTest.java index d113cc8ffdb20..926d7bceefaf6 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTrackerTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/IndexSamplingJobTrackerTest.java @@ -53,6 +53,9 @@ public class IndexSamplingJobTrackerTest IndexDescriptor index11 = IndexDescriptorFactory.of( descriptor11 ); IndexDescriptor index12 = IndexDescriptorFactory.of( descriptor12 ); IndexDescriptor index22 = IndexDescriptorFactory.of( descriptor22 ); + long indexId11 = 0; + long indexId12 = 1; + long indexId22 = 2; @Test public void shouldNotRunASampleJobWhichIsAlreadyRunning() throws Throwable @@ -70,8 +73,6 @@ public void shouldNotRunASampleJobWhichIsAlreadyRunning() throws Throwable assertTrue( jobTracker.canExecuteMoreSamplingJobs() ); IndexSamplingJob job = new IndexSamplingJob() { - private final IndexDescriptor descriptor = index12; - @Override public void run() { @@ -82,9 +83,9 @@ public void run() } @Override - public IndexDescriptor descriptor() + public long indexId() { - return descriptor; + return indexId12; } }; @@ -114,8 +115,6 @@ public void shouldNotAcceptMoreJobsThanAllowed() throws Throwable jobTracker.scheduleSamplingJob( new IndexSamplingJob() { - private final IndexDescriptor descriptor = index12; - @Override public void run() { @@ -124,9 +123,9 @@ public void run() } @Override - public IndexDescriptor descriptor() + public long indexId() { - return descriptor; + return indexId12; } } ); @@ -179,9 +178,9 @@ public void shouldAcceptNewJobWhenRunningJobFinishes() throws Throwable jobTracker.scheduleSamplingJob( new IndexSamplingJob() { @Override - public IndexDescriptor descriptor() + public long indexId() { - return IndexDescriptorFactory.of( descriptor11 ); + return indexId11; } @Override @@ -198,9 +197,9 @@ public void run() jobTracker.scheduleSamplingJob( new IndexSamplingJob() { @Override - public IndexDescriptor descriptor() + public long indexId() { - return index22; + return indexId22; } @Override @@ -261,8 +260,8 @@ public void shouldStopAndWaitForAllJobsToFinish() throws Throwable final CountDownLatch latch1 = new CountDownLatch( 1 ); final CountDownLatch latch2 = new CountDownLatch( 1 ); - WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob( index11, latch1 ); - WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob( index22, latch1 ); + WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob( indexId11, latch1 ); + WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob( indexId22, latch1 ); jobTracker.scheduleSamplingJob( job1 ); jobTracker.scheduleSamplingJob( job2 ); @@ -299,8 +298,8 @@ public void shouldWaitForAllJobsToFinish() throws Throwable final CountDownLatch latch1 = new CountDownLatch( 1 ); final CountDownLatch latch2 = new CountDownLatch( 1 ); - WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob( index11, latch1 ); - WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob( index22, latch1 ); + WaitingIndexSamplingJob job1 = new WaitingIndexSamplingJob( indexId11, latch1 ); + WaitingIndexSamplingJob job2 = new WaitingIndexSamplingJob( indexId22, latch1 ); jobTracker.scheduleSamplingJob( job1 ); jobTracker.scheduleSamplingJob( job2 ); @@ -331,21 +330,21 @@ public void shouldWaitForAllJobsToFinish() throws Throwable private static class WaitingIndexSamplingJob implements IndexSamplingJob { - final IndexDescriptor descriptor; + final long indexId; final CountDownLatch latch; volatile boolean executed; - WaitingIndexSamplingJob( IndexDescriptor descriptor, CountDownLatch latch ) + WaitingIndexSamplingJob( long indexId, CountDownLatch latch ) { - this.descriptor = descriptor; + this.indexId = indexId; this.latch = latch; } @Override - public IndexDescriptor descriptor() + public long indexId() { - return descriptor; + return indexId; } @Override diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobTest.java index f855f42abd736..32da8b6fedf7f 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/api/index/sampling/OnlineIndexSamplingJobTest.java @@ -48,14 +48,14 @@ public class OnlineIndexSamplingJobTest public void shouldSampleTheIndexAndStoreTheValueWhenTheIndexIsOnline() { // given - OnlineIndexSamplingJob job = new OnlineIndexSamplingJob( indexProxy, indexStoreView, "Foo", logProvider ); + OnlineIndexSamplingJob job = new OnlineIndexSamplingJob( indexId, indexProxy, indexStoreView, "Foo", logProvider ); when( indexProxy.getState() ).thenReturn( ONLINE ); // when job.run(); // then - verify( indexStoreView ).replaceIndexCounts( indexDescriptor, indexUniqueValues, indexSize, indexSize ); + verify( indexStoreView ).replaceIndexCounts( indexId, indexUniqueValues, indexSize, indexSize ); verifyNoMoreInteractions( indexStoreView ); } @@ -63,7 +63,7 @@ public void shouldSampleTheIndexAndStoreTheValueWhenTheIndexIsOnline() public void shouldSampleTheIndexButDoNotStoreTheValuesIfTheIndexIsNotOnline() { // given - OnlineIndexSamplingJob job = new OnlineIndexSamplingJob( indexProxy, indexStoreView, "Foo", logProvider ); + OnlineIndexSamplingJob job = new OnlineIndexSamplingJob( indexId, indexProxy, indexStoreView, "Foo", logProvider ); when( indexProxy.getState() ).thenReturn( FAILED ); // when @@ -74,6 +74,7 @@ public void shouldSampleTheIndexButDoNotStoreTheValuesIfTheIndexIsNotOnline() } private final LogProvider logProvider = NullLogProvider.getInstance(); + private final long indexId = 1; private final IndexProxy indexProxy = mock( IndexProxy.class ); private final IndexStoreView indexStoreView = mock( IndexStoreView.class ); private final IndexDescriptor indexDescriptor = IndexDescriptorFactory.of( 1, 2 ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/CountsOracle.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/CountsOracle.java index 7e5031e1ebf25..9056626136e4f 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/CountsOracle.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/CountsOracle.java @@ -56,14 +56,14 @@ public void relationship( Node start, int type, Node end ) state.addRelationship( start.labels, type, end.labels ); } - public void indexUpdatesAndSize( IndexDescriptor descriptor, long updates, long size ) + public void indexUpdatesAndSize( long indexId, long updates, long size ) { - state.replaceIndexUpdateAndSize( descriptor, updates, size ); + state.replaceIndexUpdateAndSize( indexId, updates, size ); } - public void indexSampling( IndexDescriptor descriptor, long unique, long size ) + public void indexSampling( long indexId, long unique, long size ) { - state.replaceIndexSample( descriptor, unique, size ); + state.replaceIndexSample( indexId, unique, size ); } public void update( CountsTracker target, long txId ) @@ -110,19 +110,19 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor descriptor, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { Register.DoubleLongRegister output = - tracker.indexUpdatesAndSize( descriptor, newDoubleLongRegister() ); + tracker.indexUpdatesAndSize( indexId, newDoubleLongRegister() ); assertEquals( "Should be able to read visited state.", output.readFirst(), updates ); assertEquals( "Should be able to read visited state.", output.readSecond(), size ); } @Override - public void visitIndexSample( IndexDescriptor descriptor, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { Register.DoubleLongRegister output = - tracker.indexSample( descriptor, newDoubleLongRegister() ); + tracker.indexSample( indexId, newDoubleLongRegister() ); assertEquals( "Should be able to read visited state.", output.readFirst(), unique ); assertEquals( "Should be able to read visited state.", output.readSecond(), size ); } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/CountsStoreMapGenerator.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/CountsStoreMapGenerator.java index 9a168de67c17a..241c786bb72c0 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/CountsStoreMapGenerator.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/CountsStoreMapGenerator.java @@ -62,8 +62,7 @@ private static void addIndexSampleKeys( int num, Map map ) { for ( int i = 0; i < num; i++ ) { - IndexDescriptor descriptor = IndexDescriptorFactory.of( i, i ); - map.put( CountsKeyFactory.indexSampleKey( descriptor ), new long[]{i, i} ); + map.put( CountsKeyFactory.indexSampleKey( i ), new long[]{i, i} ); } } @@ -71,8 +70,7 @@ private static void addIndexStatisticsKeys( int num, Map map ) { for ( int i = 0; i < num; i++ ) { - IndexDescriptor descriptor = IndexDescriptorFactory.of( i, i ); - map.put( CountsKeyFactory.indexStatisticsKey( descriptor ), new long[]{i, i} ); + map.put( CountsKeyFactory.indexStatisticsKey( i ), new long[]{i, i} ); } } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreCountsSnapshotSerializerTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreCountsSnapshotSerializerTest.java index 0564641dbd269..22b49a4a4adec 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreCountsSnapshotSerializerTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreCountsSnapshotSerializerTest.java @@ -148,15 +148,14 @@ public void correctlySerializesEntityRelationship() throws IOException public void correctlySerializesIndexSample() throws IOException { //GIVEN + long indexId = 1; int serializedLength = Long.BYTES + Integer.BYTES //Serialization Prefix - + Byte.BYTES + Short.BYTES + (2 * Integer.BYTES) + + + Byte.BYTES + Long.BYTES + (2 * Long.BYTES); //A single INDEX_SAMPLE from count store. - writeAndSerializeIndexSample( 1, 1, 1 ); + writeAndSerializeIndexSample( indexId, 1 ); initializeBuffers( serializedLength ); expectedBytes.put( INDEX_SAMPLE.code ); - expectedBytes.putInt( 1 ); - expectedBytes.putShort( (short)1 ); - expectedBytes.putInt( 1 ); + expectedBytes.putLong( indexId ); expectedBytes.putLong( 1 ); expectedBytes.putLong( 1 ); expectedBytes.position( 0 ); @@ -171,15 +170,14 @@ public void correctlySerializesIndexSample() throws IOException public void correctlySerializesIndexStatistics() throws IOException { //GIVEN + long indexId = 1; int serializedLength = Long.BYTES + Integer.BYTES //Serialization Prefix - + Byte.BYTES + Short.BYTES + (2 * Integer.BYTES) + + + Byte.BYTES + Long.BYTES + (2 * Long.BYTES); //A single INDEX_STATISTICS from count store. - writeAndSerializeIndexStatistics( 1, 1, 1 ); + writeAndSerializeIndexStatistics( indexId, 1 ); initializeBuffers( serializedLength ); expectedBytes.put( INDEX_STATISTICS.code ); - expectedBytes.putInt( 1 ); - expectedBytes.putShort( (short)1 ); - expectedBytes.putInt( 1 ); + expectedBytes.putLong( indexId ); expectedBytes.putLong( 1 ); expectedBytes.putLong( 1 ); expectedBytes.position( 0 ); @@ -213,7 +211,7 @@ public void throwsExceptionOnWrongValueLengthForEntityRelationship() throws IOEx public void throwsExceptionOnWrongValueLengthForIndexSample() throws IOException { Map brokenMap = new ConcurrentHashMap<>(); - brokenMap.put( indexSampleKey( 1, 1 ), new long[]{1} ); + brokenMap.put( indexSampleKey( 1 ), new long[]{1} ); CountsSnapshot brokenSnapshot = new CountsSnapshot( 1, brokenMap ); serialize( logChannel, brokenSnapshot ); } @@ -222,7 +220,7 @@ public void throwsExceptionOnWrongValueLengthForIndexSample() throws IOException public void throwsExceptionOnWrongValueLengthForIndexStatistics() throws IOException { Map brokenMap = new ConcurrentHashMap<>(); - brokenMap.put( indexStatisticsKey( 1, 1 ), new long[]{1} ); + brokenMap.put( indexStatisticsKey( 1 ), new long[]{1} ); CountsSnapshot brokenSnapshot = new CountsSnapshot( 1, brokenMap ); serialize( logChannel, brokenSnapshot ); } @@ -234,21 +232,14 @@ public void onlyHandleExpectedRecordTypes() new CountsKeyType[]{EMPTY, ENTITY_NODE, ENTITY_RELATIONSHIP, INDEX_STATISTICS, INDEX_SAMPLE} ); } - public static IndexSampleKey indexSampleKey( int labelId, int propertyKeyId ) - { - //TODO: Consider enhancing stests for composite indexes - return CountsKeyFactory.indexSampleKey( indexFor( labelId, propertyKeyId ) ); - } - - public static IndexStatisticsKey indexStatisticsKey( int labelId, int propertyKeyId ) + public static IndexSampleKey indexSampleKey( long indexId ) { - //TODO: Consider enhancing stests for composite indexes - return CountsKeyFactory.indexStatisticsKey( indexFor( labelId, propertyKeyId ) ); + return CountsKeyFactory.indexSampleKey( indexId ); } - private static IndexDescriptor indexFor( int labelId, int propertyKeyId ) + public static IndexStatisticsKey indexStatisticsKey( long indexId) { - return IndexDescriptorFactory.of( labelId, propertyKeyId ); + return CountsKeyFactory.indexStatisticsKey( indexId ); } private void initializeBuffers( int serializedLength ) @@ -282,19 +273,19 @@ private void writeAndSerializeEntityRelationship( int startId, int type, int end serialize( logChannel, countsSnapshot ); } - private void writeAndSerializeIndexSample( int labelId, int propertyKeyId, long count ) + private void writeAndSerializeIndexSample( long indexId, long count ) throws IOException { countsSnapshot.getMap() - .put( indexSampleKey( labelId, propertyKeyId ), new long[]{count, count} ); + .put( indexSampleKey( indexId ), new long[]{count, count} ); serialize( logChannel, countsSnapshot ); } - private void writeAndSerializeIndexStatistics( int labelId, int propertyKeyId, long count ) + private void writeAndSerializeIndexStatistics( long indexId, long count ) throws IOException { countsSnapshot.getMap() - .put( indexStatisticsKey( labelId, propertyKeyId ), new long[]{count, count} ); + .put( indexStatisticsKey( indexId ), new long[]{count, count} ); serialize( logChannel, countsSnapshot ); } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreSnapshotDeserializerTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreSnapshotDeserializerTest.java index 7e3d4bd309f3c..4c47c84826ee2 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreSnapshotDeserializerTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/countStore/InMemoryCountsStoreSnapshotDeserializerTest.java @@ -155,19 +155,17 @@ public void correctlyDeserializeEntityRelationship() throws IOException public void correctlyDeserializeIndexSample() throws IOException { //GIVEN + long indexId = 1; serializedBytes = ByteBuffer.allocate( 1000 ); InMemoryClosableChannel logChannel = new InMemoryClosableChannel( serializedBytes.array(), false ); writeSimpleHeader( logChannel ); logChannel.put( INDEX_SAMPLE.code ); - logChannel.putInt( 1 ); - logChannel.putShort( (short) 1 ); - logChannel.putInt( 1 ); + logChannel.putLong( indexId ); logChannel.putLong( 1 ); logChannel.putLong( 1 ); - IndexDescriptor index = IndexDescriptorFactory.of( 1, 1 ); //WHEN - IndexSampleKey expectedNode = CountsKeyFactory.indexSampleKey( index ); + IndexSampleKey expectedNode = CountsKeyFactory.indexSampleKey( indexId ); CountsSnapshot countsSnapshot = deserialize( logChannel ); //THEN @@ -179,19 +177,17 @@ public void correctlyDeserializeIndexSample() throws IOException public void correctlyDeserializeIndexStatistics() throws IOException { //GIVEN + long indexId = 1; serializedBytes = ByteBuffer.allocate( 1000 ); InMemoryClosableChannel logChannel = new InMemoryClosableChannel( serializedBytes.array(), false ); writeSimpleHeader( logChannel ); logChannel.put( INDEX_STATISTICS.code ); - logChannel.putInt( 1 ); - logChannel.putShort( (short) 1 ); - logChannel.putInt( 1 ); + logChannel.putLong( indexId ); logChannel.putLong( 1 ); logChannel.putLong( 1 ); - IndexDescriptor index = IndexDescriptorFactory.of( 1, 1 ); //WHEN - IndexStatisticsKey expectedNode = CountsKeyFactory.indexStatisticsKey( index ); + IndexStatisticsKey expectedNode = CountsKeyFactory.indexStatisticsKey( indexId ); CountsSnapshot countsSnapshot = deserialize( logChannel ); //THEN diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsRotationTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsRotationTest.java index a8cb035cb2b9d..7195cabee6fc8 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsRotationTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsRotationTest.java @@ -426,13 +426,13 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor index, long updates, long size) { - records.add( Pair.of( CountsKeyFactory.indexStatisticsKey( index ), size ) ); + public void visitIndexStatistics( long indexId, long updates, long size) { + records.add( Pair.of( CountsKeyFactory.indexStatisticsKey( indexId ), size ) ); } @Override - public void visitIndexSample( IndexDescriptor index, long unique, long size) { - records.add( Pair.of( CountsKeyFactory.indexSampleKey( index ), size ) ); + public void visitIndexSample( long indexId, long unique, long size) { + records.add( Pair.of( CountsKeyFactory.indexSampleKey( indexId ), size ) ); } } ); return records; diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsTrackerTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsTrackerTest.java index ac6f2fb24acb2..a4a7e06286608 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsTrackerTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/counts/CountsTrackerTest.java @@ -78,14 +78,14 @@ public void shouldBeAbleToWriteDataToCountsTracker() throws Exception { // given CountsTracker tracker = resourceManager.managed( newTracker() ); - IndexDescriptor index = IndexDescriptorFactory.of( 1, 1 ); + long indexId = 0; CountsOracle oracle = new CountsOracle(); { CountsOracle.Node a = oracle.node( 1 ); CountsOracle.Node b = oracle.node( 1 ); oracle.relationship( a, 1, b ); - oracle.indexSampling( index, 2, 2 ); - oracle.indexUpdatesAndSize( index, 10, 2 ); + oracle.indexSampling( indexId, 2, 2 ); + oracle.indexUpdatesAndSize( indexId, 10, 2 ); } // when @@ -103,11 +103,11 @@ public void shouldBeAbleToWriteDataToCountsTracker() throws Exception // when try ( CountsAccessor.IndexStatsUpdater updater = tracker.updateIndexCounts() ) { - updater.incrementIndexUpdates( index, 2 ); + updater.incrementIndexUpdates( indexId, 2 ); } // then - oracle.indexUpdatesAndSize( index, 12, 2 ); + oracle.indexUpdatesAndSize( indexId, 12, 2 ); oracle.verify( tracker ); // when @@ -274,7 +274,7 @@ public void shouldRotateOnDataChangesEvenIfTransactionIsUnchanged() throws Excep File before = tracker.currentFile(); try ( CountsAccessor.IndexStatsUpdater updater = tracker.updateIndexCounts() ) { - updater.incrementIndexUpdates( IndexDescriptorFactory.of( 7, 8 ), 100 ); + updater.incrementIndexUpdates( 7, 100 ); } // when @@ -367,9 +367,9 @@ private CountsOracle someData() oracle.relationship( n1, 1, n3 ); oracle.relationship( n1, 1, n2 ); oracle.relationship( n0, 1, n3 ); - IndexDescriptor index = IndexDescriptorFactory.of( 1, 2 ); - oracle.indexUpdatesAndSize( index, 0L, 50L ); - oracle.indexSampling( index, 25L, 50L ); + long indexId = 2; + oracle.indexUpdatesAndSize( indexId, 0L, 50L ); + oracle.indexSampling( indexId, 25L, 50L ); return oracle; } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/transaction/state/storeview/LabelScanViewNodeStoreScanTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/transaction/state/storeview/LabelScanViewNodeStoreScanTest.java index 3149e78e79a1a..71d108406ffea 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/transaction/state/storeview/LabelScanViewNodeStoreScanTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/transaction/state/storeview/LabelScanViewNodeStoreScanTest.java @@ -158,7 +158,7 @@ private void populateWithConcurrentUpdates( LabelScanViewNodeStoreScan getLabelScanViewStoreScan( int[] labelIds ) @@ -176,13 +176,13 @@ public LabelScanTestMultipleIndexPopulator( IndexStoreView storeView, } @Override - public IndexPopulation createPopulation( IndexPopulator populator, + public IndexPopulation createPopulation( IndexPopulator populator, long indexId, IndexDescriptor descriptor, IndexConfiguration config, SchemaIndexProvider.Descriptor providerDescriptor, FlippableIndexProxy flipper, FailedIndexProxyFactory failedIndexProxyFactory, String indexUserDescription ) { - return super.createPopulation( populator, descriptor, config, providerDescriptor, flipper, + return super.createPopulation( populator, indexId, descriptor, config, providerDescriptor, flipper, failedIndexProxyFactory, indexUserDescription ); } diff --git a/community/neo4j/src/test/java/org/neo4j/index/IndexSamplingIntegrationTest.java b/community/neo4j/src/test/java/org/neo4j/index/IndexSamplingIntegrationTest.java index a7df1407cf4e2..6a336ee0e18a4 100644 --- a/community/neo4j/src/test/java/org/neo4j/index/IndexSamplingIntegrationTest.java +++ b/community/neo4j/src/test/java/org/neo4j/index/IndexSamplingIntegrationTest.java @@ -30,8 +30,11 @@ import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.schema.IndexDefinition; import org.neo4j.io.fs.FileUtils; -import org.neo4j.kernel.api.schema.IndexDescriptor; +import org.neo4j.kernel.api.ReadOperations; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.schema.IndexDescriptorFactory; +import org.neo4j.kernel.impl.api.index.IndexingService; +import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge; import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine; import org.neo4j.kernel.impl.store.MetaDataStore; import org.neo4j.kernel.impl.store.StoreFactory; @@ -181,7 +184,24 @@ public void shouldSampleUniqueIndex() throws Throwable assertEquals( nodes - deletedNodes, indexSizeRegister.readSecond() ); } - private DoubleLongRegister fetchIndexSamplingValues( GraphDatabaseService db ) + private long indexId( GraphDatabaseAPI api ) throws IndexNotFoundKernelException + { + try ( Transaction tx = api.beginTx() ) + { + IndexingService indexingService = + api.getDependencyResolver().resolveDependency( IndexingService.class ); + ReadOperations readOperations = + api.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ).get() + .readOperations(); + int labelId = readOperations.labelGetForName( label.name() ); + int propertyKeyId = readOperations.propertyKeyGetForName( property ); + long indexId = indexingService.getIndexId( IndexDescriptorFactory.of( labelId, propertyKeyId ) ); + tx.success(); + return indexId; + } + } + + private DoubleLongRegister fetchIndexSamplingValues( GraphDatabaseService db ) throws IndexNotFoundKernelException { try { @@ -191,8 +211,7 @@ private DoubleLongRegister fetchIndexSamplingValues( GraphDatabaseService db ) GraphDatabaseAPI api = (GraphDatabaseAPI) db; CountsTracker countsTracker = api.getDependencyResolver().resolveDependency( RecordStorageEngine.class ) .testAccessNeoStores().getCounts(); - IndexDescriptor descriptor = IndexDescriptorFactory.of( 0, 0 ); - IndexSampleKey key = CountsKeyFactory.indexSampleKey( descriptor ); // cheating a bit... + IndexSampleKey key = CountsKeyFactory.indexSampleKey( indexId( api ) ); return countsTracker.get( key, Registers.newDoubleLongRegister() ); } finally @@ -204,7 +223,7 @@ private DoubleLongRegister fetchIndexSamplingValues( GraphDatabaseService db ) } } - private DoubleLongRegister fetchIndexSizeValues( GraphDatabaseService db ) + private DoubleLongRegister fetchIndexSizeValues( GraphDatabaseService db ) throws IndexNotFoundKernelException { try { @@ -214,8 +233,7 @@ private DoubleLongRegister fetchIndexSizeValues( GraphDatabaseService db ) GraphDatabaseAPI api = (GraphDatabaseAPI) db; CountsTracker countsTracker = api.getDependencyResolver().resolveDependency( RecordStorageEngine.class ) .testAccessNeoStores().getCounts(); - IndexDescriptor descriptor = IndexDescriptorFactory.of( 0, 0 ); - IndexStatisticsKey key = CountsKeyFactory.indexStatisticsKey( descriptor ); // cheating a bit... + IndexStatisticsKey key = CountsKeyFactory.indexStatisticsKey( indexId( api ) ); return countsTracker.get( key, Registers.newDoubleLongRegister() ); } finally diff --git a/community/shell/src/main/java/org/neo4j/shell/kernel/apps/Schema.java b/community/shell/src/main/java/org/neo4j/shell/kernel/apps/Schema.java index 62b240fad37ea..88f6900281474 100644 --- a/community/shell/src/main/java/org/neo4j/shell/kernel/apps/Schema.java +++ b/community/shell/src/main/java/org/neo4j/shell/kernel/apps/Schema.java @@ -29,6 +29,7 @@ import org.neo4j.graphdb.schema.ConstraintType; import org.neo4j.graphdb.schema.IndexDefinition; import org.neo4j.graphdb.schema.Schema.IndexState; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.schema.NodePropertyDescriptor; import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.schema.IndexDescriptorFactory; @@ -194,7 +195,14 @@ private void sampleIndexes( Label[] labels, String property, boolean sampleAll, throw new ShellException( "No property associated with '" + property + "' was found" ); } - indexingService.triggerIndexSampling( IndexDescriptorFactory.of( labelKey, propertyKey ), samplingMode ); + try + { + indexingService.triggerIndexSampling( IndexDescriptorFactory.of( labelKey, propertyKey ), samplingMode ); + } + catch ( IndexNotFoundKernelException e ) + { + throw new ShellException( e.getMessage() ); + } } private IndexSamplingMode getSamplingMode( boolean forceSample ) diff --git a/enterprise/ha/src/test/java/org/neo4j/kernel/ha/HaCountsIT.java b/enterprise/ha/src/test/java/org/neo4j/kernel/ha/HaCountsIT.java index 2803bb34754b6..7b8220466fd47 100644 --- a/enterprise/ha/src/test/java/org/neo4j/kernel/ha/HaCountsIT.java +++ b/enterprise/ha/src/test/java/org/neo4j/kernel/ha/HaCountsIT.java @@ -32,6 +32,7 @@ import org.neo4j.kernel.api.Statement; import org.neo4j.kernel.api.exceptions.KernelException; import org.neo4j.kernel.api.schema.IndexDescriptor; +import org.neo4j.kernel.impl.api.index.IndexingService; import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge; import org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster; import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine; @@ -130,18 +131,18 @@ public void shouldUpdateCountsOnSlavesWhenCreatingAnIndexOnMaster() throws Excep // when creating a node on the master createANode( master, LABEL, PROPERTY_VALUE, PROPERTY_NAME ); IndexDescriptor indexDescriptor = createAnIndex( master, LABEL, PROPERTY_NAME ); - awaitOnline( master, indexDescriptor ); + long indexId = awaitOnline( master, indexDescriptor ); // and the slaves got the updates cluster.sync( master ); - awaitOnline( slave1, indexDescriptor ); - awaitOnline( slave2, indexDescriptor ); + long index1 = awaitOnline( slave1, indexDescriptor ); + long index2 = awaitOnline( slave2, indexDescriptor ); // then the slaves has updated counts - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, master ); - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, slave1 ); - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, slave2 ); + assertOnIndexCounts( 0, 1, 1, 1, indexId, master ); + assertOnIndexCounts( 0, 1, 1, 1, index1, slave1 ); + assertOnIndexCounts( 0, 1, 1, 1, index2, slave2 ); } @Test @@ -150,18 +151,18 @@ public void shouldUpdateCountsOnClusterWhenCreatingANodeOnSlaveAndAnIndexOnMaste // when creating a node on the master createANode( slave1, LABEL, PROPERTY_VALUE, PROPERTY_NAME ); IndexDescriptor indexDescriptor = createAnIndex( master, LABEL, PROPERTY_NAME ); - awaitOnline( master, indexDescriptor ); + long indexId = awaitOnline( master, indexDescriptor ); // and the updates are propagate in the cluster cluster.sync(); - awaitOnline( slave1, indexDescriptor ); - awaitOnline( slave2, indexDescriptor ); + long index1 = awaitOnline( slave1, indexDescriptor ); + long index2 = awaitOnline( slave2, indexDescriptor ); // then the slaves has updated counts - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, master ); - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, slave1 ); - assertOnIndexCounts( 0, 1, 1, 1, indexDescriptor, slave2 ); + assertOnIndexCounts( 0, 1, 1, 1, indexId, master ); + assertOnIndexCounts( 0, 1, 1, 1, index1, slave1 ); + assertOnIndexCounts( 0, 1, 1, 1, index2, slave2 ); } private void createANode( HighlyAvailableGraphDatabase db, Label label, String value, String property ) @@ -203,13 +204,13 @@ private void assertOnNodeCounts( int expectedTotalNodes, int expectedLabelledNod private void assertOnIndexCounts( int expectedIndexUpdates, int expectedIndexSize, int expectedUniqueValues, int expectedSampleSize, - IndexDescriptor descriptor, HighlyAvailableGraphDatabase db ) + long indexId, HighlyAvailableGraphDatabase db ) { CountsTracker counts = counts( db ); assertDoubleLongEquals( expectedIndexUpdates, expectedIndexSize, - counts.indexUpdatesAndSize( descriptor, newDoubleLongRegister() ) ); + counts.indexUpdatesAndSize( indexId, newDoubleLongRegister() ) ); assertDoubleLongEquals( expectedUniqueValues, expectedSampleSize, - counts.indexSample( descriptor, newDoubleLongRegister() ) ); + counts.indexSample( indexId, newDoubleLongRegister() ) ); } private void assertDoubleLongEquals( int expectedFirst, int expectedSecond, DoubleLongRegister actualValues ) @@ -232,7 +233,12 @@ private Statement statement( HighlyAvailableGraphDatabase db ) .get(); } - private IndexDescriptor awaitOnline( HighlyAvailableGraphDatabase db, IndexDescriptor index ) + private IndexingService indexingService( HighlyAvailableGraphDatabase db ) + { + return db.getDependencyResolver().resolveDependency( IndexingService.class ); + } + + private long awaitOnline( HighlyAvailableGraphDatabase db, IndexDescriptor index ) throws KernelException { long start = System.currentTimeMillis(); @@ -244,7 +250,7 @@ private IndexDescriptor awaitOnline( HighlyAvailableGraphDatabase db, IndexDescr switch ( statement( db ).readOperations().indexGetState( index ) ) { case ONLINE: - return index; + return indexingService( db ).getIndexId( index ); case FAILED: throw new IllegalStateException( "Index failed instead of becoming ONLINE" ); diff --git a/enterprise/management/src/main/java/org/neo4j/management/impl/IndexSamplingManagerBean.java b/enterprise/management/src/main/java/org/neo4j/management/impl/IndexSamplingManagerBean.java index 8a220c1145e68..52a8115cd9520 100644 --- a/enterprise/management/src/main/java/org/neo4j/management/impl/IndexSamplingManagerBean.java +++ b/enterprise/management/src/main/java/org/neo4j/management/impl/IndexSamplingManagerBean.java @@ -26,6 +26,7 @@ import org.neo4j.jmx.impl.ManagementData; import org.neo4j.jmx.impl.Neo4jMBean; import org.neo4j.kernel.NeoStoreDataSource; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.schema.NodePropertyDescriptor; import org.neo4j.kernel.api.schema.IndexDescriptorFactory; import org.neo4j.kernel.impl.api.index.IndexingService; @@ -127,8 +128,15 @@ public void triggerIndexSampling( String labelKey, String propertyKey, boolean f throw new IllegalArgumentException( "No property or label key was found associated with " + propertyKey + " and " + labelKey ); } - state.indexingService.triggerIndexSampling( IndexDescriptorFactory.of( labelKeyId, propertyKeyId ), - getIndexSamplingMode( forceSample ) ); + try + { + state.indexingService.triggerIndexSampling( IndexDescriptorFactory.of( labelKeyId, propertyKeyId ), + getIndexSamplingMode( forceSample ) ); + } + catch ( IndexNotFoundKernelException e ) + { + throw new IllegalArgumentException( e.getMessage() ); + } } private IndexSamplingMode getIndexSamplingMode( boolean forceSample ) diff --git a/enterprise/management/src/test/java/org/neo4j/management/impl/IndexSamplingManagerBeanTest.java b/enterprise/management/src/test/java/org/neo4j/management/impl/IndexSamplingManagerBeanTest.java index 4d62360f35bfb..e70984ee8c3cc 100644 --- a/enterprise/management/src/test/java/org/neo4j/management/impl/IndexSamplingManagerBeanTest.java +++ b/enterprise/management/src/test/java/org/neo4j/management/impl/IndexSamplingManagerBeanTest.java @@ -24,6 +24,7 @@ import org.neo4j.graphdb.DependencyResolver; import org.neo4j.kernel.NeoStoreDataSource; +import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException; import org.neo4j.kernel.api.schema.IndexDescriptorFactory; import org.neo4j.kernel.impl.api.index.IndexingService; import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingMode; @@ -64,7 +65,7 @@ public void setup() } @Test - public void samplingTriggeredWhenIdsArePresent() + public void samplingTriggeredWhenIdsArePresent() throws IndexNotFoundKernelException { // Given IndexSamplingManagerBean.StoreAccess storeAccess = new IndexSamplingManagerBean.StoreAccess(); @@ -79,7 +80,7 @@ public void samplingTriggeredWhenIdsArePresent() } @Test - public void forceSamplingTriggeredWhenIdsArePresent() + public void forceSamplingTriggeredWhenIdsArePresent() throws IndexNotFoundKernelException { // Given IndexSamplingManagerBean.StoreAccess storeAccess = new IndexSamplingManagerBean.StoreAccess(); diff --git a/tools/src/main/java/org/neo4j/tools/dump/DumpCountsStore.java b/tools/src/main/java/org/neo4j/tools/dump/DumpCountsStore.java index af44b67f4db4a..cbf0c192a9c83 100644 --- a/tools/src/main/java/org/neo4j/tools/dump/DumpCountsStore.java +++ b/tools/src/main/java/org/neo4j/tools/dump/DumpCountsStore.java @@ -23,17 +23,22 @@ import java.io.IOException; import java.io.PrintStream; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.pagecache.PageCache; import org.neo4j.kernel.api.ReadOperations; import org.neo4j.kernel.api.schema.IndexDescriptor; +import org.neo4j.kernel.api.schema.IndexDescriptorFactory; +import org.neo4j.kernel.api.schema.NodePropertyDescriptor; import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.impl.api.CountsVisitor; import org.neo4j.kernel.impl.core.RelationshipTypeToken; import org.neo4j.kernel.impl.store.NeoStores; +import org.neo4j.kernel.impl.store.SchemaStore; import org.neo4j.kernel.impl.store.StoreFactory; import org.neo4j.kernel.impl.store.TokenStore; import org.neo4j.kernel.impl.store.counts.CountsTracker; @@ -45,7 +50,9 @@ import org.neo4j.kernel.lifecycle.Lifespan; import org.neo4j.logging.LogProvider; import org.neo4j.logging.NullLogProvider; +import org.neo4j.storageengine.api.EntityType; import org.neo4j.storageengine.api.Token; +import org.neo4j.storageengine.api.schema.SchemaRule; import static org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache; @@ -97,26 +104,29 @@ public static void dumpCountsStore( FileSystemAbstraction fs, File path, PrintSt DumpCountsStore( PrintStream out ) { - this( out, Collections.emptyList(), Collections.emptyList(), Collections.emptyList() ); + this( out, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList() ); } DumpCountsStore( PrintStream out, NeoStores neoStores ) { - this( out, + this( out, allSchemaRulesFrom( neoStores.getSchemaStore() ), allTokensFrom( neoStores.getLabelTokenStore() ), allTokensFrom( neoStores.getRelationshipTypeTokenStore() ), allTokensFrom( neoStores.getPropertyKeyTokenStore() ) ); } private final PrintStream out; + private final Map schemaRules; private final List labels; private final List relationshipTypes; private final List propertyKeys; - private DumpCountsStore( PrintStream out, List labels, List relationshipTypes, - List propertyKeys ) + private DumpCountsStore( PrintStream out, Map schemaRules, List labels, + List relationshipTypes, + List propertyKeys ) { this.out = out; + this.schemaRules = schemaRules; this.labels = labels; this.relationshipTypes = relationshipTypes; this.propertyKeys = propertyKeys; @@ -149,15 +159,17 @@ public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId } @Override - public void visitIndexStatistics( IndexDescriptor index, long updates, long size ) + public void visitIndexStatistics( long indexId, long updates, long size ) { + IndexDescriptor index = schemaRules.get( indexId ); out.printf( "\tIndexStatistics[(%s {%s})]:\tupdates=%d, size=%d%n", label( index.getLabelId() ), propertyKey( index.descriptor().getPropertyKeyId() ), updates, size ); } @Override - public void visitIndexSample( IndexDescriptor index, long unique, long size ) + public void visitIndexSample( long indexId, long unique, long size ) { + IndexDescriptor index = schemaRules.get( indexId ); out.printf( "\tIndexSample[(%s {%s})]:\tunique=%d, size=%d%n", label( index.getLabelId() ), propertyKey( index.descriptor().getPropertyKeyId() ), unique, size ); } @@ -230,6 +242,19 @@ private static List allTokensFrom( TokenStore allSchemaRulesFrom( SchemaStore store ) + { + HashMap rules = new HashMap<>(); + for ( SchemaRule rule : store ) + { + if ( rule.descriptor().entityType() == EntityType.NODE ) + { + rules.put( rule.getId(), IndexDescriptorFactory.of( (NodePropertyDescriptor) rule.descriptor() ) ); + } + } + return rules; + } + private static class VisitableCountsTracker extends CountsTracker { diff --git a/tools/src/test/java/org/neo4j/tools/dump/DumpCountsStoreTest.java b/tools/src/test/java/org/neo4j/tools/dump/DumpCountsStoreTest.java index 0a6fbc2889c66..51f5e72f0a8d9 100644 --- a/tools/src/test/java/org/neo4j/tools/dump/DumpCountsStoreTest.java +++ b/tools/src/test/java/org/neo4j/tools/dump/DumpCountsStoreTest.java @@ -23,10 +23,12 @@ import org.junit.Test; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.neo4j.kernel.api.index.SchemaIndexProvider; import org.neo4j.kernel.api.schema.IndexDescriptor; import org.neo4j.kernel.api.schema.IndexDescriptorFactory; import org.neo4j.kernel.impl.core.RelationshipTypeToken; @@ -34,12 +36,15 @@ import org.neo4j.kernel.impl.store.NeoStores; import org.neo4j.kernel.impl.store.PropertyKeyTokenStore; import org.neo4j.kernel.impl.store.RelationshipTypeTokenStore; +import org.neo4j.kernel.impl.store.SchemaStore; import org.neo4j.kernel.impl.store.kvstore.BigEndianByteArrayBuffer; import org.neo4j.kernel.impl.store.kvstore.HeaderField; import org.neo4j.kernel.impl.store.kvstore.Headers; import org.neo4j.kernel.impl.store.kvstore.ReadableBuffer; import org.neo4j.kernel.impl.store.kvstore.WritableBuffer; +import org.neo4j.kernel.impl.store.record.IndexRule; import org.neo4j.storageengine.api.Token; +import org.neo4j.storageengine.api.schema.SchemaRule; import org.neo4j.test.rule.SuppressOutput; import static org.hamcrest.Matchers.allOf; @@ -65,6 +70,7 @@ public class DumpCountsStoreTest private static final int INDEX_PROPERTY_KEY_ID = 1; private static final String INDEX_PROPERTY = "indexProperty"; + private static final long indexId = 0; private static final IndexDescriptor descriptor = IndexDescriptorFactory.of( INDEX_LABEL_ID, INDEX_PROPERTY_KEY_ID ); @@ -125,7 +131,7 @@ public void dumpUnknownKey() public void dumpIndexStatistic() { DumpCountsStore countsStore = getCountStore(); - countsStore.visitIndexStatistics( descriptor, 3, 4 ); + countsStore.visitIndexStatistics( indexId, 3, 4 ); assertThat( suppressOutput.getOutputVoice().toString(), containsString( "IndexStatistics[(:indexLabel [labelId=3] {indexProperty [keyId=1]})" + "]:\tupdates=3, size=4" ) ); @@ -135,7 +141,7 @@ public void dumpIndexStatistic() public void dumpIndexSample() { DumpCountsStore countsStore = getCountStore(); - countsStore.visitIndexSample( descriptor, 1, 2 ); + countsStore.visitIndexSample( indexId, 1, 2 ); assertThat( suppressOutput.getOutputVoice().toString(), containsString( "IndexSample[(:indexLabel [labelId=3] {indexProperty [keyId=1]})]:\tunique=1, size=2" )); } @@ -151,11 +157,18 @@ private NeoStores createNeoStores() LabelTokenStore labelTokenStore = mock( LabelTokenStore.class ); RelationshipTypeTokenStore typeTokenStore = mock( RelationshipTypeTokenStore.class ); PropertyKeyTokenStore propertyKeyTokenStore = mock( PropertyKeyTokenStore.class ); + SchemaStore schemaStore = mock( SchemaStore.class ); + SchemaIndexProvider.Descriptor providerDescriptor = new SchemaIndexProvider.Descriptor( "in-memory", "1.0" ); + IndexRule rule = IndexRule.indexRule( indexId, descriptor.descriptor(), providerDescriptor ); + ArrayList rules = new ArrayList<>(); + rules.add( rule ); when( labelTokenStore.getTokens( Integer.MAX_VALUE ) ).thenReturn( getLabelTokens() ); when( typeTokenStore.getTokens( Integer.MAX_VALUE ) ).thenReturn( getTypeTokes() ); when( propertyKeyTokenStore.getTokens( Integer.MAX_VALUE ) ).thenReturn( getPropertyTokens() ); + when( schemaStore.iterator() ).thenReturn( rules.iterator() ); + when( neoStores.getSchemaStore() ).thenReturn( schemaStore ); when( neoStores.getLabelTokenStore() ).thenReturn( labelTokenStore ); when( neoStores.getRelationshipTypeTokenStore() ).thenReturn( typeTokenStore ); when( neoStores.getPropertyKeyTokenStore() ).thenReturn( propertyKeyTokenStore );