From d3bf0543fc01c0d7eed60d7798a58e1b1d4b89c1 Mon Sep 17 00:00:00 2001 From: Anton Persson Date: Fri, 15 Feb 2019 11:02:00 +0100 Subject: [PATCH] Fix problem in shouldSampleUpdatesIfConfiguredForOnlineSampling Because we compared Values as Objects instead of as Values the expected sample results where wrong even though actual sample results where correct. --- .../schema/NativeIndexPopulatorTests.java | 63 ++++++++++--------- .../impl/index/schema/ValueCreatorUtil.java | 7 ++- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java index b08332dd39cf..09eb1a27de61 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java @@ -46,6 +46,7 @@ import org.neo4j.storageengine.api.NodePropertyAccessor; import org.neo4j.storageengine.api.schema.IndexDescriptor; import org.neo4j.storageengine.api.schema.IndexSample; +import org.neo4j.values.storable.Value; import org.neo4j.values.storable.Values; import static java.util.Arrays.asList; @@ -602,44 +603,50 @@ public void updaterShouldApplyDuplicateValues() throws Exception public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception { // GIVEN - populator.create(); - IndexEntryUpdate[] scanUpdates = valueCreatorUtil.someUpdates( random ); - populator.add( Arrays.asList( scanUpdates ) ); - Iterator> 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 ) ) + try { - long nodeId = 1000; - for ( Object value : updates ) + populator.create(); + IndexEntryUpdate[] scanUpdates = valueCreatorUtil.someUpdates( random ); + populator.add( Arrays.asList( scanUpdates ) ); + Iterator> 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 update = valueCreatorUtil.add( nodeId++, Values.of( value ) ); - updater.process( update ); + long nodeId = 1000; + for ( Value value : updates ) + { + IndexEntryUpdate update = valueCreatorUtil.add( nodeId++, value ); + updater.process( update ); + } } - } - // WHEN - IndexSample sample = populator.sampleResult(); + // WHEN + IndexSample sample = populator.sampleResult(); - // THEN - Object[] allValues = Arrays.copyOf( updates, updates.length + scanUpdates.length ); - System.arraycopy( asValues( scanUpdates ), 0, allValues, updates.length, scanUpdates.length ); - assertEquals( updates.length + scanUpdates.length, sample.sampleSize() ); - assertEquals( countUniqueValues( allValues ), sample.uniqueValues() ); - assertEquals( updates.length + scanUpdates.length, sample.indexSize() ); - populator.close( true ); + // THEN + Value[] allValues = Arrays.copyOf( updates, updates.length + scanUpdates.length ); + System.arraycopy( asValues( scanUpdates ), 0, allValues, updates.length, scanUpdates.length ); + assertEquals( updates.length + scanUpdates.length, sample.sampleSize() ); + assertEquals( countUniqueValues( allValues ), sample.uniqueValues() ); + assertEquals( updates.length + scanUpdates.length, sample.indexSize() ); + } + finally + { + populator.close( true ); + } } - private Object[] asValues( IndexEntryUpdate[] updates ) + private Value[] asValues( IndexEntryUpdate[] updates ) { - Object[] values = new Object[updates.length]; + Value[] values = new Value[updates.length]; for ( int i = 0; i < updates.length; i++ ) { - values[i] = updates[i].values()[0].asObject(); + values[i] = updates[i].values()[0]; } return values; } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/ValueCreatorUtil.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/ValueCreatorUtil.java index f9dddc7b966f..859448ce8ace 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/ValueCreatorUtil.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/ValueCreatorUtil.java @@ -28,6 +28,7 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -173,9 +174,11 @@ static int countUniqueValues( IndexEntryUpdate[] updates ) 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 set = new TreeSet<>( Values.COMPARATOR ); + set.addAll( Arrays.asList( updates ) ); + return set.size(); } void sort( IndexEntryUpdate[] updates )