Skip to content

Commit

Permalink
Fix problem in shouldSampleUpdatesIfConfiguredForOnlineSampling
Browse files Browse the repository at this point in the history
Because we compared Values as Objects instead of as Values the expected
sample results where wrong even though actual sample results where correct.
  • Loading branch information
burqen committed Feb 18, 2019
1 parent 6e0a076 commit d3bf054
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
Expand Up @@ -46,6 +46,7 @@
import org.neo4j.storageengine.api.NodePropertyAccessor; import org.neo4j.storageengine.api.NodePropertyAccessor;
import org.neo4j.storageengine.api.schema.IndexDescriptor; import org.neo4j.storageengine.api.schema.IndexDescriptor;
import org.neo4j.storageengine.api.schema.IndexSample; import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values; import org.neo4j.values.storable.Values;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
Expand Down Expand Up @@ -602,44 +603,50 @@ public void updaterShouldApplyDuplicateValues() throws Exception
public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception
{ {
// GIVEN // GIVEN
populator.create(); try
IndexEntryUpdate<IndexDescriptor>[] scanUpdates = valueCreatorUtil.someUpdates( random );
populator.add( Arrays.asList( scanUpdates ) );
Iterator<IndexEntryUpdate<IndexDescriptor>> generator = valueCreatorUtil.randomUpdateGenerator( random );
Object[] updates = new Object[5];
updates[0] = generator.next().values()[0].asObject();
updates[1] = generator.next().values()[0].asObject();
updates[2] = updates[1];
updates[3] = generator.next().values()[0].asObject();
updates[4] = updates[3];
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{ {
long nodeId = 1000; populator.create();
for ( Object value : updates ) IndexEntryUpdate<IndexDescriptor>[] scanUpdates = valueCreatorUtil.someUpdates( random );
populator.add( Arrays.asList( scanUpdates ) );
Iterator<IndexEntryUpdate<IndexDescriptor>> generator = valueCreatorUtil.randomUpdateGenerator( random );
Value[] updates = new Value[5];
updates[0] = generator.next().values()[0];
updates[1] = generator.next().values()[0];
updates[2] = updates[1];
updates[3] = generator.next().values()[0];
updates[4] = updates[3];
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{ {
IndexEntryUpdate<IndexDescriptor> update = valueCreatorUtil.add( nodeId++, Values.of( value ) ); long nodeId = 1000;
updater.process( update ); for ( Value value : updates )
{
IndexEntryUpdate<IndexDescriptor> update = valueCreatorUtil.add( nodeId++, value );
updater.process( update );
}
} }
}


// WHEN // WHEN
IndexSample sample = populator.sampleResult(); IndexSample sample = populator.sampleResult();


// THEN // THEN
Object[] allValues = Arrays.copyOf( updates, updates.length + scanUpdates.length ); Value[] allValues = Arrays.copyOf( updates, updates.length + scanUpdates.length );
System.arraycopy( asValues( scanUpdates ), 0, allValues, updates.length, scanUpdates.length ); System.arraycopy( asValues( scanUpdates ), 0, allValues, updates.length, scanUpdates.length );
assertEquals( updates.length + scanUpdates.length, sample.sampleSize() ); assertEquals( updates.length + scanUpdates.length, sample.sampleSize() );
assertEquals( countUniqueValues( allValues ), sample.uniqueValues() ); assertEquals( countUniqueValues( allValues ), sample.uniqueValues() );
assertEquals( updates.length + scanUpdates.length, sample.indexSize() ); assertEquals( updates.length + scanUpdates.length, sample.indexSize() );
populator.close( true ); }
finally
{
populator.close( true );
}
} }


private Object[] asValues( IndexEntryUpdate<IndexDescriptor>[] updates ) private Value[] asValues( IndexEntryUpdate<IndexDescriptor>[] updates )
{ {
Object[] values = new Object[updates.length]; Value[] values = new Value[updates.length];
for ( int i = 0; i < updates.length; i++ ) for ( int i = 0; i < updates.length; i++ )
{ {
values[i] = updates[i].values()[0].asObject(); values[i] = updates[i].values()[0];
} }
return values; return values;
} }
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;


Expand Down Expand Up @@ -173,9 +174,11 @@ static int countUniqueValues( IndexEntryUpdate<IndexDescriptor>[] updates )
return Stream.of( updates ).map( update -> update.values()[0] ).collect( Collectors.toSet() ).size(); return Stream.of( updates ).map( update -> update.values()[0] ).collect( Collectors.toSet() ).size();
} }


static int countUniqueValues( Object[] updates ) static int countUniqueValues( Value[] updates )
{ {
return Stream.of( updates ).collect( Collectors.toSet() ).size(); Set<Value> set = new TreeSet<>( Values.COMPARATOR );
set.addAll( Arrays.asList( updates ) );
return set.size();
} }


void sort( IndexEntryUpdate<IndexDescriptor>[] updates ) void sort( IndexEntryUpdate<IndexDescriptor>[] updates )
Expand Down

0 comments on commit d3bf054

Please sign in to comment.