Skip to content

Commit

Permalink
Various fusion index classes cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Mar 12, 2018
1 parent 74e1d71 commit 4d0a944
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 65 deletions.
Expand Up @@ -40,9 +40,7 @@
import org.neo4j.kernel.impl.index.schema.fusion.FusionIndexProvider.Selector;
import org.neo4j.storageengine.api.schema.IndexReader;

import static org.neo4j.helpers.collection.Iterators.array;
import static org.neo4j.helpers.collection.Iterators.concatResourceIterators;
import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexBase.forAll;

class FusionIndexAccessor extends FusionIndexBase<IndexAccessor> implements IndexAccessor
{
Expand Down Expand Up @@ -96,7 +94,7 @@ public void close() throws IOException
@Override
public IndexReader newReader()
{
return new FusionIndexReader( instancesAs( IndexReader.class, accessor -> accessor.newReader() ), selector, descriptor );
return new FusionIndexReader( instancesAs( IndexReader.class, IndexAccessor::newReader ), selector, descriptor );
}

@Override
Expand Down
Expand Up @@ -20,12 +20,9 @@
package org.neo4j.kernel.impl.index.schema.fusion;

import java.lang.reflect.Array;
import java.util.Arrays;
import javax.print.attribute.standard.MediaSize;

import org.neo4j.function.ThrowingConsumer;
import org.neo4j.function.ThrowingFunction;
import org.neo4j.storageengine.api.schema.IndexSample;

/**
* Acting as a simplifier for the multiplexing that is going in inside a fusion index. A fusion index consists of multiple parts,
Expand All @@ -36,14 +33,14 @@
*/
public abstract class FusionIndexBase<T>
{
static final int INSTANCE_COUNT = 5;
private static final int INSTANCE_COUNT = 5;
private static final String[] NAMES = { "string", "number", "spatial", "temporal", "lucene" };

static final int STRING = 0;
static final int NUMBER = 1;
static final int SPATIAL = 2;
static final int TEMPORAL = 3;
static final int LUCENE = 4;
static final String[] NAMES = { "string", "number", "spatial", "temporal", "lucene" };

final T[] instances;
final FusionIndexProvider.Selector selector;
Expand Down Expand Up @@ -95,6 +92,7 @@ static String nameOf( int slot )
* @param <E> the type of exception anticipated, inferred from the lambda
* @throws E if consumption fails with this exception
*/
@SafeVarargs
public static <T, E extends Exception> void forAll( ThrowingConsumer<T,E> consumer, T... subjects ) throws E
{
E exception = null;
Expand Down Expand Up @@ -124,7 +122,7 @@ public static <T, E extends Exception> void forAll( ThrowingConsumer<T,E> consum
}

/**
* See {@link #forAll(ThrowingConsumer, Iterable)}
* See {@link #forAll(ThrowingConsumer, Object[])}
* NOTE: duplicate of {@link #forAll(ThrowingConsumer, Object[])} to avoid having to wrap subjects of one form into another.
* There are some real use cases for passing in an Iterable instead of array out there...
*
Expand Down
Expand Up @@ -33,7 +33,6 @@
import org.neo4j.storageengine.api.schema.IndexSample;

import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexSampler.combineSamples;
import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexBase.forAll;

class FusionIndexPopulator extends FusionIndexBase<IndexPopulator> implements IndexPopulator
{
Expand Down Expand Up @@ -122,6 +121,6 @@ public void includeSample( IndexEntryUpdate<?> update )
@Override
public IndexSample sampleResult()
{
return combineSamples( instancesAs( IndexSample.class, populator -> populator.sampleResult() ) );
return combineSamples( instancesAs( IndexSample.class, IndexPopulator::sampleResult ) );
}
}
Expand Up @@ -23,7 +23,6 @@

import org.neo4j.collection.primitive.PrimitiveLongResourceCollections;
import org.neo4j.collection.primitive.PrimitiveLongResourceIterator;
import org.neo4j.function.ThrowingFunction;
import org.neo4j.graphdb.Resource;
import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexQuery;
Expand All @@ -39,16 +38,13 @@

import org.neo4j.storageengine.api.schema.IndexProgressor;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.storageengine.api.schema.IndexSampler;
import org.neo4j.values.storable.Value;

import static java.lang.String.format;
import static org.neo4j.helpers.collection.Iterators.array;
import static org.neo4j.internal.kernel.api.IndexQuery.StringContainsPredicate;
import static org.neo4j.internal.kernel.api.IndexQuery.StringPrefixPredicate;
import static org.neo4j.internal.kernel.api.IndexQuery.StringSuffixPredicate;
import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexBase.forAll;

class FusionIndexReader extends FusionIndexBase<IndexReader> implements IndexReader
{
Expand Down
Expand Up @@ -26,9 +26,6 @@
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.impl.index.schema.fusion.FusionIndexProvider.Selector;

import static org.neo4j.helpers.collection.Iterators.array;
import static org.neo4j.kernel.impl.index.schema.fusion.FusionIndexBase.forAll;

class FusionIndexUpdater extends FusionIndexBase<IndexUpdater> implements IndexUpdater
{
FusionIndexUpdater( IndexUpdater[] updaters, Selector selector )
Expand Down
Expand Up @@ -52,10 +52,6 @@

public class FusionIndexPopulatorTest
{
private IndexPopulator stringPopulator;
private IndexPopulator numberPopulator;
private IndexPopulator spatialPopulator;
private IndexPopulator temporalPopulator;
private IndexPopulator lucenePopulator;
private IndexPopulator[] allPopulators;
private FusionIndexPopulator fusionIndexPopulator;
Expand All @@ -65,10 +61,10 @@ public class FusionIndexPopulatorTest
@Before
public void mockComponents()
{
stringPopulator = mock( IndexPopulator.class );
numberPopulator = mock( IndexPopulator.class );
spatialPopulator = mock( IndexPopulator.class );
temporalPopulator = mock( IndexPopulator.class );
IndexPopulator stringPopulator = mock( IndexPopulator.class );
IndexPopulator numberPopulator = mock( IndexPopulator.class );
IndexPopulator spatialPopulator = mock( IndexPopulator.class );
IndexPopulator temporalPopulator = mock( IndexPopulator.class );
lucenePopulator = mock( IndexPopulator.class );
allPopulators = array( stringPopulator, numberPopulator, spatialPopulator, temporalPopulator, lucenePopulator );
fusionIndexPopulator = new FusionIndexPopulator( allPopulators, new FusionSelector(), indexId, dropAction );
Expand Down
Expand Up @@ -148,9 +148,9 @@ public void getPopulationFailureMustThrowIfNoFailure()
// when
// ... no failure
IllegalStateException failure = new IllegalStateException( "not failed" );
for ( int i = 0; i < providers.length; i++ )
for ( IndexProvider provider : providers )
{
when( providers[i].getPopulationFailure( anyLong(), any( SchemaIndexDescriptor.class ) ) ).thenThrow( failure );
when( provider.getPopulationFailure( anyLong(), any( SchemaIndexDescriptor.class ) ) ).thenThrow( failure );
}

// then
Expand Down Expand Up @@ -194,24 +194,21 @@ public void getPopulationFailureMustReportFailureWhenAnyFailed()
@Test
public void getPopulationFailureMustReportFailureWhenMultipleFail()
{
FusionIndexProvider fusionSchemaIndexProvider = fusionProvider();

// when
String[] failures = new String[providers.length];
for ( int i = 0; i < providers.length; i++ )
{
FusionIndexProvider fusionSchemaIndexProvider = fusionProvider();

// when
String[] failures = new String[providers.length];
for ( int j = 0; j < providers.length; j++ )
{
failures[j] = "FAILURE[" + j + "]";
when( providers[j].getPopulationFailure( anyLong(), any( SchemaIndexDescriptor.class ) ) ).thenReturn( failures[j] );
}
failures[i] = "FAILURE[" + i + "]";
when( providers[i].getPopulationFailure( anyLong(), any( SchemaIndexDescriptor.class ) ) ).thenReturn( failures[i] );
}

// then
String populationFailure = fusionSchemaIndexProvider.getPopulationFailure( 0, forLabel( 0, 0 ) );
for ( String failure : failures )
{
assertThat( populationFailure, containsString( failure ) );
}
// then
String populationFailure = fusionSchemaIndexProvider.getPopulationFailure( 0, forLabel( 0, 0 ) );
for ( String failure : failures )
{
assertThat( populationFailure, containsString( failure ) );
}
}

Expand Down
Expand Up @@ -114,13 +114,6 @@ public void closeIteratorMustCloseAll() throws Exception
}
}

private PrimitiveLongResourceIterator mockReaderForQuery( IndexReader reader ) throws IndexNotApplicableKernelException
{
PrimitiveLongResourceIterator mockIterator = mock( PrimitiveLongResourceIterator.class );
when( reader.query( any( IndexQuery.class ) ) ).thenReturn( mockIterator );
return mockIterator;
}

/* countIndexedNodes */

@Test
Expand Down
Expand Up @@ -42,7 +42,6 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;

class FusionIndexTestHelp
Expand Down
Expand Up @@ -24,11 +24,6 @@
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException;
Expand All @@ -48,10 +43,6 @@

public class FusionIndexUpdaterTest
{
private IndexUpdater stringUpdater;
private IndexUpdater numberUpdater;
private IndexUpdater spatialUpdater;
private IndexUpdater temporalUpdater;
private IndexUpdater luceneUpdater;
private IndexUpdater[] allUpdaters;
private FusionIndexUpdater fusionIndexUpdater;
Expand All @@ -62,10 +53,10 @@ public class FusionIndexUpdaterTest
@Before
public void mockComponents()
{
stringUpdater = mock( IndexUpdater.class );
numberUpdater = mock( IndexUpdater.class );
spatialUpdater = mock( IndexUpdater.class );
temporalUpdater = mock( IndexUpdater.class );
IndexUpdater stringUpdater = mock( IndexUpdater.class );
IndexUpdater numberUpdater = mock( IndexUpdater.class );
IndexUpdater spatialUpdater = mock( IndexUpdater.class );
IndexUpdater temporalUpdater = mock( IndexUpdater.class );
luceneUpdater = mock( IndexUpdater.class );
allUpdaters = array( stringUpdater, numberUpdater, spatialUpdater, temporalUpdater, luceneUpdater );
fusionIndexUpdater = new FusionIndexUpdater( allUpdaters, new FusionSelector() );
Expand Down Expand Up @@ -262,7 +253,8 @@ public void closeMustThrowIfAnyThrow() throws Exception
{
for ( int i = 0; i < allUpdaters.length; i++ )
{
FusionIndexTestHelp.verifyFusionCloseThrowOnSingleCloseThrow( allUpdaters[i], fusionIndexUpdater );
IndexUpdater updater = allUpdaters[i];
FusionIndexTestHelp.verifyFusionCloseThrowOnSingleCloseThrow( updater, fusionIndexUpdater );
mockComponents();
}
}
Expand Down

0 comments on commit 4d0a944

Please sign in to comment.