Skip to content

Commit

Permalink
Use random values in NativeIndexAccessorTest
Browse files Browse the repository at this point in the history
Spatial is special snowflake as usual and continue to use fixed values for some tests
because lack of post filtering on this level.
  • Loading branch information
burqen committed Oct 3, 2018
1 parent 681276e commit fee561b
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 20 deletions.
Expand Up @@ -30,6 +30,7 @@
import org.neo4j.values.storable.DateValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;

public class DateLayoutTestUtil extends LayoutTestUtil<DateIndexKey,NativeIndexValue>
Expand Down Expand Up @@ -61,6 +62,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates()
return someUpdatesWithDuplicateValues();
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.DATE );
}

@Override
int compareIndexedPropertyValue( DateIndexKey key1, DateIndexKey key2 )
{
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.neo4j.values.storable.DateTimeValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;

import static java.time.ZoneOffset.UTC;
Expand Down Expand Up @@ -64,6 +65,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates()
return someUpdatesWithDuplicateValues();
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.ZONED_DATE_TIME );
}

@Override
int compareIndexedPropertyValue( ZonedDateTimeIndexKey key1, ZonedDateTimeIndexKey key2 )
{
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.neo4j.values.storable.DurationValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;

public class DurationLayoutTestUtil extends LayoutTestUtil<DurationIndexKey,NativeIndexValue>
Expand Down Expand Up @@ -62,6 +63,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates()
return someUpdatesWithDuplicateValues();
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.DURATION );
}

@Override
int compareIndexedPropertyValue( DurationIndexKey key1, DurationIndexKey key2 )
{
Expand Down
Expand Up @@ -60,6 +60,8 @@ abstract class LayoutTestUtil<KEY extends NativeIndexKey<KEY>, VALUE extends Nat

abstract IndexEntryUpdate<IndexDescriptor>[] someUpdates();

abstract RandomValues.Type[] supportedTypes();

protected double fractionDuplicates()
{
return 0.1;
Expand Down Expand Up @@ -136,11 +138,18 @@ Value[] extractValuesFromUpdates( IndexEntryUpdate<IndexDescriptor>[] updates )

IndexEntryUpdate<IndexDescriptor>[] generateAddUpdatesFor( Object[] values )
{
@SuppressWarnings( "unchecked" )
return generateAddUpdatesFor( Arrays.stream( values )
.map( Values::of )
.toArray( Value[]::new ) );
}

IndexEntryUpdate<IndexDescriptor>[] generateAddUpdatesFor( Value[] values )
{
//noinspection unchecked
IndexEntryUpdate<IndexDescriptor>[] indexEntryUpdates = new IndexEntryUpdate[values.length];
for ( int i = 0; i < indexEntryUpdates.length; i++ )
{
indexEntryUpdates[i] = add( i, Values.of( values[i] ) );
indexEntryUpdates[i] = add( i, values[i] );
}
return indexEntryUpdates;
}
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.neo4j.values.storable.LocalDateTimeValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;

public class LocalDateTimeLayoutTestUtil extends LayoutTestUtil<LocalDateTimeIndexKey,NativeIndexValue>
Expand Down Expand Up @@ -61,6 +62,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates()
return someUpdatesWithDuplicateValues();
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.LOCAL_DATE_TIME );
}

@Override
int compareIndexedPropertyValue( LocalDateTimeIndexKey key1, LocalDateTimeIndexKey key2 )
{
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.neo4j.values.storable.LocalTimeValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values;

public class LocalTimeLayoutTestUtil extends LayoutTestUtil<LocalTimeIndexKey,NativeIndexValue>
Expand Down Expand Up @@ -61,6 +62,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates()
return someUpdatesWithDuplicateValues();
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.LOCAL_TIME );
}

@Override
int compareIndexedPropertyValue( LocalTimeIndexKey key1, LocalTimeIndexKey key2 )
{
Expand Down
Expand Up @@ -54,6 +54,7 @@
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.RandomValues;
import org.neo4j.values.storable.Value;

import static java.lang.String.format;
Expand Down Expand Up @@ -83,6 +84,7 @@
public abstract class NativeIndexAccessorTest<KEY extends NativeIndexKey<KEY>, VALUE extends NativeIndexValue>
extends NativeIndexTestUtil<KEY,VALUE>
{
private static final int N_VALUES = 10;
NativeIndexAccessor<KEY,VALUE> accessor;

@Rule
Expand Down Expand Up @@ -135,7 +137,7 @@ public void processMustThrowAfterClose() throws Exception
public void shouldIndexAdd() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
try ( IndexUpdater updater = accessor.newUpdater( ONLINE ) )
{
// when
Expand All @@ -151,7 +153,7 @@ public void shouldIndexAdd() throws Exception
public void shouldIndexChange() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );
Iterator<IndexEntryUpdate<IndexDescriptor>> generator = filter( skipExisting( updates ), layoutUtil.randomUpdateGenerator( random ) );

Expand All @@ -174,7 +176,7 @@ public void shouldIndexChange() throws Exception
public void shouldIndexRemove() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

for ( int i = 0; i < updates.length; i++ )
Expand Down Expand Up @@ -237,7 +239,7 @@ public void shouldReturnZeroCountForEmptyIndex()
public void shouldReturnCountOneForExistingData() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand All @@ -264,7 +266,7 @@ public void shouldReturnCountOneForExistingData() throws Exception
public void shouldReturnCountZeroForMismatchingData() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand All @@ -288,7 +290,7 @@ public void shouldReturnCountZeroForMismatchingData() throws Exception
public void shouldReturnAllEntriesForExistsPredicate() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand All @@ -315,7 +317,7 @@ public void shouldReturnNoEntriesForExistsPredicateForEmptyIndex() throws Except
public void shouldReturnMatchingEntriesForExactPredicate() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand All @@ -332,7 +334,7 @@ public void shouldReturnMatchingEntriesForExactPredicate() throws Exception
public void shouldReturnNoEntriesForMismatchingExactPredicate() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand Down Expand Up @@ -421,7 +423,12 @@ public void shouldReturnNoEntriesForRangePredicateOutsideAnyMatch() throws Excep
public void mustHandleNestedQueries() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
mustHandleNestedQueries( updates );
}

void mustHandleNestedQueries( IndexEntryUpdate<IndexDescriptor>[] updates ) throws IndexEntryConflictException, IndexNotApplicableKernelException
{
processAll( updates );
layoutUtil.sort( updates );

Expand Down Expand Up @@ -449,7 +456,13 @@ public void mustHandleNestedQueries() throws Exception
public void mustHandleMultipleNestedQueries() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
mustHandleMultipleNestedQueries( updates );
}

void mustHandleMultipleNestedQueries( IndexEntryUpdate<IndexDescriptor>[] updates )
throws IndexEntryConflictException, IndexNotApplicableKernelException
{
processAll( updates );
layoutUtil.sort( updates );

Expand Down Expand Up @@ -498,7 +511,7 @@ private long entityIdOf( IndexEntryUpdate<IndexDescriptor> update )
public void shouldHandleMultipleConsecutiveUpdaters() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();

// when
for ( IndexEntryUpdate<IndexDescriptor> update : updates )
Expand Down Expand Up @@ -545,7 +558,7 @@ public void dropShouldDeleteAndCloseIndex()
public void forceShouldCheckpointTree() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] data = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] data = someUpdatesSingleType();
processAll( data );

// when
Expand All @@ -561,7 +574,7 @@ public void forceShouldCheckpointTree() throws Exception
public void closeShouldCloseTreeWithoutCheckpoint() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] data = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] data = someUpdatesSingleType();
processAll( data );

// when
Expand All @@ -587,7 +600,7 @@ public void snapshotFilesShouldReturnIndexFile()
public void shouldSampleIndex() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );
try ( IndexReader reader = accessor.newReader() )
{
Expand Down Expand Up @@ -659,7 +672,7 @@ public void writingAfterCloseShouldThrow()
public void shouldSeeAllEntriesInAllEntriesReader() throws Exception
{
// given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates();
IndexEntryUpdate<IndexDescriptor>[] updates = someUpdatesSingleType();
processAll( updates );

// when
Expand Down Expand Up @@ -730,7 +743,7 @@ private Value valueOf( IndexEntryUpdate<IndexDescriptor> update )
return update.values()[0];
}

private IndexProgressor.NodeValueClient filterClient( final NodeValueIterator iter, final IndexQuery.ExactPredicate filter )
private IndexProgressor.NodeValueClient filterClient( final NodeValueIterator iter, final IndexQuery filter )
{
return new IndexProgressor.NodeValueClient()
{
Expand Down Expand Up @@ -892,7 +905,7 @@ final void processAll( IndexEntryUpdate<IndexDescriptor>... updates )
}
}

private void forceAndCloseAccessor() throws IOException
private void forceAndCloseAccessor()
{
accessor.force( IOLimiter.UNLIMITED );
closeAccessor();
Expand All @@ -912,6 +925,17 @@ private IndexEntryUpdate<IndexDescriptor> simpleUpdate()
return IndexEntryUpdate.add( 0, indexDescriptor, of( 0 ) );
}

private IndexEntryUpdate<IndexDescriptor>[] someUpdatesSingleType()
{
RandomValues.Type type = randomValues.among( layoutUtil.supportedTypes() );
Value[] values = new Value[N_VALUES];
for ( int i = 0; i < N_VALUES; i++ )
{
values[i] = randomValues.nextValueOfTypes( type );
}
return layoutUtil.generateAddUpdatesFor( values );
}

// TODO: multiple query predicates... actually Lucene SimpleIndexReader only supports single predicate
// so perhaps we should wait with this until we know exactly how this works and which combinations
// that should be supported/optimized for.
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.test.rule.RandomRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.ValueGroup;

import static java.lang.String.format;
Expand Down Expand Up @@ -73,6 +74,7 @@ public abstract class NativeIndexTestUtil<KEY extends NativeIndexKey<KEY>,VALUE
private File indexFile;
PageCache pageCache;
IndexProvider.Monitor monitor = IndexProvider.Monitor.EMPTY;
RandomValues randomValues;

@Before
public void setup()
Expand All @@ -82,6 +84,7 @@ public void setup()
layout = layoutUtil.createLayout();
indexFile = directory.file( "index" );
pageCache = pageCacheRule.getPageCache( fs );
randomValues = random.randomValues();
}

public File getIndexFile()
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.neo4j.values.storable.NumberValue;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueGroup;

abstract class NumberLayoutTestUtil extends LayoutTestUtil<NumberIndexKey,NativeIndexValue>
{
Expand Down Expand Up @@ -59,6 +60,12 @@ abstract class NumberLayoutTestUtil extends LayoutTestUtil<NumberIndexKey,Native
super( indexDescriptor.withId( 0 ) );
}

@Override
RandomValues.Type[] supportedTypes()
{
return RandomValues.typesOfGroup( ValueGroup.NUMBER );
}

@Override
int compareIndexedPropertyValue( NumberIndexKey key1, NumberIndexKey key2 )
{
Expand Down

0 comments on commit fee561b

Please sign in to comment.