Skip to content

Commit

Permalink
NativeIndexAccessorTests delegate value generation to ValueCreatorUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Oct 3, 2018
1 parent 2e20cd6 commit 66b383d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 52 deletions.
Expand Up @@ -19,7 +19,6 @@
*/
package org.neo4j.kernel.impl.index.schema;

import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.collections.api.iterator.LongIterator;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -87,7 +86,7 @@ public abstract class NativeIndexAccessorTests<KEY extends NativeIndexKey<KEY>,
extends NativeIndexTestUtil<KEY,VALUE>
{
private static final int N_VALUES = 10;
NativeIndexAccessor<KEY,VALUE> accessor;
private NativeIndexAccessor<KEY,VALUE> accessor;

@Rule
public ExpectedException expected = ExpectedException.none();
Expand Down Expand Up @@ -215,6 +214,7 @@ public void shouldHandleRandomUpdates() throws Exception
applyUpdatesToExpectedData( expectedData, batch );
// verifyUpdates
forceAndCloseAccessor();
//noinspection unchecked
verifyUpdates( expectedData.toArray( new IndexEntryUpdate[0] ) );
setupAccessor();
}
Expand Down Expand Up @@ -961,7 +961,7 @@ private IndexEntryUpdate<IndexDescriptor> selectRandomItem( Set<IndexEntryUpdate
}

@SafeVarargs
final void processAll( IndexEntryUpdate<IndexDescriptor>... updates )
private final void processAll( IndexEntryUpdate<IndexDescriptor>... updates )
throws IndexEntryConflictException
{
try ( IndexUpdater updater = accessor.newUpdater( ONLINE ) )
Expand Down Expand Up @@ -996,7 +996,7 @@ private IndexEntryUpdate<IndexDescriptor> simpleUpdate()
private IndexEntryUpdate<IndexDescriptor>[] someUpdatesSingleType()
{
RandomValues.Type type = random.randomValues().among( valueCreatorUtil.supportedTypes() );
return someUpdatesOfTypes( true, type );
return valueCreatorUtil.someUpdates( random, new RandomValues.Type[]{type}, true );
}

private IndexEntryUpdate<IndexDescriptor>[] someUpdatesSingleTypeNoDuplicates()
Expand All @@ -1013,31 +1013,7 @@ private IndexEntryUpdate<IndexDescriptor>[] someUpdatesSingleTypeNoDuplicates( R
type = random.randomValues().among( types );
}
while ( type == RandomValues.Type.BOOLEAN );
return someUpdatesOfTypes( false, type );
}

IndexEntryUpdate<IndexDescriptor>[] someUpdates()
{
return someUpdatesOfTypes( true, valueCreatorUtil.supportedTypes() );
}

private IndexEntryUpdate<IndexDescriptor>[] someUpdatesOfTypes( boolean allowDuplicates, RandomValues.Type... types )
{
if ( types.length < 1 )
{
throw new IllegalArgumentException( "Need at least one type, was " + Arrays.toString( types ) );
}
Value[] values = new Value[N_VALUES];
int i = 0;
while ( i < N_VALUES )
{
Value value = random.randomValues().nextValueOfTypes( types );
if ( allowDuplicates || !ArrayUtils.contains( values, value ) )
{
values[i++] = value;
}
}
return valueCreatorUtil.generateAddUpdatesFor( values );
return valueCreatorUtil.someUpdates( random, new RandomValues.Type[]{type}, false );
}

private RandomValues.Type[] supportedTypesExcludingNonOrderable()
Expand Down
Expand Up @@ -51,18 +51,18 @@ class ValueCreatorUtil<KEY extends NativeIndexKey<KEY>, VALUE extends NativeInde

final StoreIndexDescriptor indexDescriptor;
private final RandomValues.Type[] supportedTypes;
private final double fractionDuplication;
private final double fractionDuplicates;

ValueCreatorUtil( ValueCreatorUtil delegate )
{
this( delegate.indexDescriptor, delegate.supportedTypes, delegate.fractionDuplication );
this( delegate.indexDescriptor, delegate.supportedTypes, delegate.fractionDuplicates );
}

ValueCreatorUtil( StoreIndexDescriptor indexDescriptor, RandomValues.Type[] supportedTypes, double fractionDuplication )
ValueCreatorUtil( StoreIndexDescriptor indexDescriptor, RandomValues.Type[] supportedTypes, double fractionDuplicates )
{
this.indexDescriptor = indexDescriptor;
this.supportedTypes = supportedTypes;
this.fractionDuplication = fractionDuplication;
this.fractionDuplicates = fractionDuplicates;
}

int compareIndexedPropertyValue( KEY key1, KEY key2 )
Expand All @@ -75,9 +75,9 @@ RandomValues.Type[] supportedTypes()
return supportedTypes;
}

double fractionDuplicates()
private double fractionDuplicates()
{
return fractionDuplication;
return fractionDuplicates;
}

IndexQuery rangeQuery( Value from, boolean fromInclusive, Value to, boolean toInclusive )
Expand All @@ -92,7 +92,19 @@ StoreIndexDescriptor indexDescriptor()

IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule randomRule )
{
Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator = randomUpdateGenerator( randomRule );
return someUpdates( randomRule, supportedTypes(), fractionDuplicates() );
}

IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule random, RandomValues.Type[] types, boolean allowDuplicates )
{
double fractionDuplicates = allowDuplicates ? FRACTION_DUPLICATE_NON_UNIQUE : FRACTION_DUPLICATE_UNIQUE;
return someUpdates( random, types, fractionDuplicates );
}

private IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule random, RandomValues.Type[] types, double fractionDuplicates )
{
RandomValueGenerator valueGenerator = new RandomValueGenerator( random.randomValues(), types, fractionDuplicates );
RandomUpdateGenerator randomUpdateGenerator = new RandomUpdateGenerator( valueGenerator );
//noinspection unchecked
IndexEntryUpdate<IndexDescriptor>[] result = new IndexEntryUpdate[N_VALUES];
for ( int i = 0; i < N_VALUES; i++ )
Expand All @@ -102,22 +114,9 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule randomRule )
return result;
}

Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator( RandomRule randomRule )
{
Iterator<Value> valueIterator = randomValueGenerator( randomRule );
return new RandomUpdateGenerator( valueIterator );
}

private Iterator<Value> randomValueGenerator( RandomRule randomRule )
{
RandomValues randomValues = randomRule.randomValues();
double fractionDuplicates = fractionDuplicates();
return new RandomValueGenerator( fractionDuplicates, randomValues );
}

IndexEntryUpdate<IndexDescriptor>[] someUpdatesWithDuplicateValues( RandomRule randomRule )
{
Iterator<Value> valueIterator = randomValueGenerator( randomRule );
Iterator<Value> valueIterator = new RandomValueGenerator( randomRule.randomValues(), supportedTypes(), fractionDuplicates() );
Value[] someValues = new Value[N_VALUES];
for ( int i = 0; i < N_VALUES; i++ )
{
Expand All @@ -126,6 +125,12 @@ IndexEntryUpdate<IndexDescriptor>[] someUpdatesWithDuplicateValues( RandomRule r
return generateAddUpdatesFor( ArrayUtils.addAll( someValues, someValues ) );
}

Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator( RandomRule randomRule )
{
Iterator<Value> valueIterator = new RandomValueGenerator( randomRule.randomValues(), supportedTypes(), fractionDuplicates() );
return new RandomUpdateGenerator( valueIterator );
}

IndexEntryUpdate<IndexDescriptor>[] generateAddUpdatesFor( Value[] values )
{
//noinspection unchecked
Expand Down Expand Up @@ -179,11 +184,13 @@ private class RandomValueGenerator extends PrefetchingIterator<Value>
{
private final Set<Value> uniqueCompareValues;
private final List<Value> uniqueValues;
private final RandomValues.Type[] types;
private final double fractionDuplicates;
private final RandomValues randomValues;

RandomValueGenerator( double fractionDuplicates, RandomValues randomValues )
RandomValueGenerator( RandomValues randomValues, RandomValues.Type[] types, double fractionDuplicates )
{
this.types = types;
this.fractionDuplicates = fractionDuplicates;
this.randomValues = randomValues;
this.uniqueCompareValues = new HashSet<>();
Expand Down Expand Up @@ -212,7 +219,7 @@ private Value newUniqueValue( RandomValues random, Set<Value> uniqueCompareValue
Value value;
do
{
value = random.nextValueOfTypes( supportedTypes() );
value = random.nextValueOfTypes( types );
}
while ( !uniqueCompareValues.add( value ) );
uniqueValues.add( value );
Expand Down

0 comments on commit 66b383d

Please sign in to comment.