From 46ea5b61eb4ae0dc137a6747a1882070958c736f Mon Sep 17 00:00:00 2001 From: Anton Persson Date: Thu, 27 Sep 2018 15:47:39 +0200 Subject: [PATCH] Gather all implementations of NativeIndexPopulatorTest in one parameterized test Except for Spatial for now. Expecting it to need some special care when moving to randomized values. --- .../DateTimeUniqueIndexPopulatorTest.java | 41 ---- .../schema/DateUniqueIndexPopulatorTest.java | 40 ---- .../DurationUniqueIndexPopulatorTest.java | 40 ---- ...orTest.java => LayoutTestUtilFactory.java} | 15 +- ...LocalDateTimeUniqueIndexPopulatorTest.java | 41 ---- .../LocalTimeUniqueIndexPopulatorTest.java | 40 ---- .../index/schema/NativeIndexAccessorTest.java | 78 +++++--- .../schema/NativeIndexAccessorTests.java | 2 +- ...st.java => NativeIndexPopulatorTests.java} | 167 +++++++++++++++- .../index/schema/NativeIndexTestUtil.java | 2 + .../NativeNonUniqueIndexPopulatorTest.java | 2 +- .../NativeUniqueIndexPopulatorTest.java | 179 +++++++++--------- .../SpatialUniqueIndexPopulatorTest.java | 2 +- .../StringUniqueIndexPopulatorTest.java | 35 ---- .../schema/TimeUniqueIndexPopulatorTest.java | 39 ---- .../index/schema/UniqueLayoutTestUtil.java | 2 +- 16 files changed, 311 insertions(+), 414 deletions(-) delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateTimeUniqueIndexPopulatorTest.java delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateUniqueIndexPopulatorTest.java delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DurationUniqueIndexPopulatorTest.java rename community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/{NumberUniqueIndexPopulatorTest.java => LayoutTestUtilFactory.java} (62%) delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalDateTimeUniqueIndexPopulatorTest.java delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalTimeUniqueIndexPopulatorTest.java rename community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/{NativeIndexPopulatorTest.java => NativeIndexPopulatorTests.java} (73%) delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/StringUniqueIndexPopulatorTest.java delete mode 100644 community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/TimeUniqueIndexPopulatorTest.java diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateTimeUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateTimeUniqueIndexPopulatorTest.java deleted file mode 100644 index 8884a3e7e4557..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateTimeUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class DateTimeUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = - new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.ZONED_DATE_TIME ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( - new DateTimeLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateUniqueIndexPopulatorTest.java deleted file mode 100644 index f7efe800bbde3..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DateUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class DateUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.DATE ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( - new DateLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DurationUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DurationUniqueIndexPopulatorTest.java deleted file mode 100644 index 039a8eb693398..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/DurationUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class DurationUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.DURATION ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( - new DurationLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NumberUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LayoutTestUtilFactory.java similarity index 62% rename from community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NumberUniqueIndexPopulatorTest.java rename to community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LayoutTestUtilFactory.java index 7c0fef0b7842f..1a05dec56bfdf 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NumberUniqueIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LayoutTestUtilFactory.java @@ -19,17 +19,8 @@ */ package org.neo4j.kernel.impl.index.schema; -public class NumberUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest +@FunctionalInterface +interface LayoutTestUtilFactory, VALUE extends NativeIndexValue> { - @Override - NativeIndexPopulator createPopulator() - { - return new NumberIndexPopulator( pageCache, fs, getIndexFile(), layout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new NumberUniqueLayoutTestUtil(); - } + LayoutTestUtil create(); } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalDateTimeUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalDateTimeUniqueIndexPopulatorTest.java deleted file mode 100644 index ff184f762efdd..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalDateTimeUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class LocalDateTimeUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = - new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.LOCAL_DATE_TIME ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( - new LocalDateTimeLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalTimeUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalTimeUniqueIndexPopulatorTest.java deleted file mode 100644 index 62facb9887d93..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/LocalTimeUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class LocalTimeUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = - new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.LOCAL_TIME ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( new LocalTimeLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTest.java index 954d7054d9d45..cd3c079e5a73b 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTest.java @@ -44,37 +44,63 @@ @RunWith( Parameterized.class ) public class NativeIndexAccessorTest, VALUE extends NativeIndexValue> extends NativeIndexAccessorTests { - @Parameterized.Parameters(name = "{index}: {0}") + @Parameterized.Parameters( name = "{index}: {0}" ) public static Collection data() { IndexDescriptor descriptor = TestIndexDescriptorFactory.forLabel( 42, 666 ); return Arrays.asList( new Object[][]{ - {"Number", numberAccessorFactory(), (LayoutUtilFactory) NumberNonUniqueLayoutTestUtil::new}, - {"String", stringAccessorFactory(), (LayoutUtilFactory) StringNonUniqueLayoutTestUtil::new}, - {"Date", temporalAccessorFactory( ValueGroup.DATE ), (LayoutUtilFactory) () -> new DateLayoutTestUtil( descriptor )}, - {"DateTime", temporalAccessorFactory( ValueGroup.ZONED_DATE_TIME ), (LayoutUtilFactory) () -> new DateTimeLayoutTestUtil( descriptor )}, - {"Duration", temporalAccessorFactory( ValueGroup.DURATION ), (LayoutUtilFactory) () -> new DurationLayoutTestUtil( descriptor )}, - {"LocalDateTime", temporalAccessorFactory( ValueGroup.LOCAL_DATE_TIME ), - (LayoutUtilFactory) () -> new LocalDateTimeLayoutTestUtil( descriptor )}, - {"LocalTime", temporalAccessorFactory( ValueGroup.LOCAL_TIME ), - (LayoutUtilFactory) () -> new LocalTimeLayoutTestUtil( descriptor )}, - {"LocalDateTime", temporalAccessorFactory( ValueGroup.LOCAL_DATE_TIME ), - (LayoutUtilFactory) () -> new LocalDateTimeLayoutTestUtil( descriptor )}, - {"Time", temporalAccessorFactory( ValueGroup.ZONED_TIME ), - (LayoutUtilFactory) () -> new TimeLayoutTestUtil( descriptor )}, - {"Generic", genericAccessorFactory(), (LayoutUtilFactory) () -> new GenericLayoutTestUtil( descriptor )}, + {"Number", + numberAccessorFactory(), + (LayoutTestUtilFactory) NumberNonUniqueLayoutTestUtil::new + }, + {"String", + stringAccessorFactory(), + (LayoutTestUtilFactory) StringNonUniqueLayoutTestUtil::new + }, + {"Date", + temporalAccessorFactory( ValueGroup.DATE ), + (LayoutTestUtilFactory) () -> new DateLayoutTestUtil( descriptor ) + }, + {"DateTime", + temporalAccessorFactory( ValueGroup.ZONED_DATE_TIME ), + (LayoutTestUtilFactory) () -> new DateTimeLayoutTestUtil( descriptor ) + }, + {"Duration", + temporalAccessorFactory( ValueGroup.DURATION ), + (LayoutTestUtilFactory) () -> new DurationLayoutTestUtil( descriptor ) + }, + {"LocalDateTime", + temporalAccessorFactory( ValueGroup.LOCAL_DATE_TIME ), + (LayoutTestUtilFactory) () -> new LocalDateTimeLayoutTestUtil( descriptor ) + }, + {"LocalTime", + temporalAccessorFactory( ValueGroup.LOCAL_TIME ), + (LayoutTestUtilFactory) () -> new LocalTimeLayoutTestUtil( descriptor ) + }, + {"LocalDateTime", + temporalAccessorFactory( ValueGroup.LOCAL_DATE_TIME ), + (LayoutTestUtilFactory) () -> new LocalDateTimeLayoutTestUtil( descriptor ) + }, + {"Time", + temporalAccessorFactory( ValueGroup.ZONED_TIME ), + (LayoutTestUtilFactory) () -> new TimeLayoutTestUtil( descriptor ) + }, + {"Generic", + genericAccessorFactory(), + (LayoutTestUtilFactory) () -> new GenericLayoutTestUtil( descriptor ) + }, //{ Spatial has it's own subclass because it need to override some of the test methods } } ); } private final AccessorFactory accessorFactory; - private final LayoutUtilFactory layoutUtilFactory; + private final LayoutTestUtilFactory layoutTestUtilFactory; @SuppressWarnings( "unused" ) - public NativeIndexAccessorTest( String name, AccessorFactory accessorFactory, LayoutUtilFactory layoutUtilFactory ) + public NativeIndexAccessorTest( String name, AccessorFactory accessorFactory, LayoutTestUtilFactory layoutTestUtilFactory ) { this.accessorFactory = accessorFactory; - this.layoutUtilFactory = layoutUtilFactory; + this.layoutTestUtilFactory = layoutTestUtilFactory; } @Override @@ -86,7 +112,7 @@ NativeIndexAccessor makeAccessor() throws IOException @Override LayoutTestUtil createLayoutTestUtil() { - return layoutUtilFactory.create(); + return layoutTestUtilFactory.create(); } /* Helpers */ @@ -100,11 +126,11 @@ private static AccessorFactory stringAccessorFa return StringIndexAccessor::new; } - private static AccessorFactory temporalAccessorFactory( ValueGroup temporalValueGroup ) + private static > AccessorFactory temporalAccessorFactory( ValueGroup temporalValueGroup ) { return ( pageCache, fs, storeFile, layout, cleanup, monitor, descriptor ) -> { - TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( storeFile, layout, temporalValueGroup ); + TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( storeFile, layout, temporalValueGroup ); return new TemporalIndexAccessor.PartAccessor<>( pageCache, fs, fileLayout, cleanup, monitor, descriptor ); }; } @@ -122,13 +148,7 @@ private static AccessorFactory genericAcce private interface AccessorFactory, VALUE extends NativeIndexValue> { NativeIndexAccessor create( PageCache pageCache, FileSystemAbstraction fs, - File storeFile, IndexLayout layout, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, IndexProvider.Monitor monitor, - StoreIndexDescriptor descriptor ) throws IOException; - } - - @FunctionalInterface - private interface LayoutUtilFactory, VALUE extends NativeIndexValue> - { - LayoutTestUtil create(); + File storeFile, IndexLayout layout, RecoveryCleanupWorkCollector recoveryCleanupWorkCollector, + IndexProvider.Monitor monitor, StoreIndexDescriptor descriptor ) throws IOException; } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTests.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTests.java index d03d18503d7de..3769f0b16a2da 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTests.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexAccessorTests.java @@ -1023,7 +1023,7 @@ IndexEntryUpdate[] someUpdates() private IndexEntryUpdate[] someUpdatesOfTypes( boolean allowDuplicates, RandomValues.Type... types ) { - if ( types.length < 1) + if ( types.length < 1 ) { throw new IllegalArgumentException( "Need at least one type, was " + Arrays.toString( types ) ); } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java similarity index 73% rename from community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTest.java rename to community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java index 2e6c2a2ff0c98..d4e5630a4e083 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexPopulatorTests.java @@ -34,6 +34,7 @@ import java.util.Optional; import java.util.Random; +import org.neo4j.helpers.Exceptions; import org.neo4j.index.internal.gbptree.GBPTree; import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector; import org.neo4j.io.fs.OpenMode; @@ -44,8 +45,10 @@ import org.neo4j.kernel.api.index.IndexUpdater; import org.neo4j.kernel.api.index.NodePropertyAccessor; import org.neo4j.storageengine.api.schema.IndexDescriptor; +import org.neo4j.storageengine.api.schema.IndexSample; import org.neo4j.values.storable.Values; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -54,10 +57,11 @@ import static org.junit.Assert.fail; import static org.neo4j.index.internal.gbptree.GBPTree.NO_HEADER_READER; import static org.neo4j.index.internal.gbptree.GBPTree.NO_HEADER_WRITER; +import static org.neo4j.kernel.impl.index.schema.LayoutTestUtil.countUniqueValues; import static org.neo4j.kernel.impl.index.schema.NativeIndexPopulator.BYTE_FAILED; import static org.neo4j.kernel.impl.index.schema.NativeIndexPopulator.BYTE_ONLINE; -public abstract class NativeIndexPopulatorTest,VALUE extends NativeIndexValue> +public abstract class NativeIndexPopulatorTests,VALUE extends NativeIndexValue> extends NativeIndexTestUtil { private static final int LARGE_AMOUNT_OF_UPDATES = 1_000; @@ -480,6 +484,167 @@ public void unsuccessfulCloseMustThrowAfterDrop() } } + public abstract static class Unique, V extends NativeIndexValue> extends NativeIndexPopulatorTests + { + @Test + public void addShouldThrowOnDuplicateValues() + { + // given + populator.create(); + IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); + + // when + try + { + populator.add( Arrays.asList( updates ) ); + fail( "Updates should have conflicted" ); + } + catch ( IndexEntryConflictException e ) + { + // then good + } + finally + { + populator.close( true ); + } + } + + @Test + public void updaterShouldThrowOnDuplicateValues() throws Exception + { + // given + populator.create(); + IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); + IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ); + + // when + for ( IndexEntryUpdate update : updates ) + { + updater.process( update ); + } + try + { + updater.close(); + fail( "Updates should have conflicted" ); + } + catch ( Exception e ) + { + // then + assertTrue( e.getMessage(), Exceptions.contains( e, IndexEntryConflictException.class ) ); + } + finally + { + populator.close( true ); + } + } + + @Test + public void shouldSampleUpdates() throws Exception + { + // GIVEN + populator.create(); + IndexEntryUpdate[] updates = layoutUtil.someUpdates(); + + // WHEN + populator.add( asList( updates ) ); + for ( IndexEntryUpdate update : updates ) + { + populator.includeSample( update ); + } + IndexSample sample = populator.sampleResult(); + + // THEN + assertEquals( updates.length, sample.sampleSize() ); + assertEquals( updates.length, sample.uniqueValues() ); + assertEquals( updates.length, sample.indexSize() ); + populator.close( true ); + } + } + + public abstract static class NonUnique, V extends NativeIndexValue> extends NativeIndexPopulatorTests + { + @Test + public void addShouldApplyDuplicateValues() throws Exception + { + // given + populator.create(); + IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); + + // when + populator.add( Arrays.asList( updates ) ); + + // then + populator.close( true ); + verifyUpdates( updates ); + } + + @Test + public void updaterShouldApplyDuplicateValues() throws Exception + { + // given + populator.create(); + IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); + try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) ) + { + // when + for ( IndexEntryUpdate update : updates ) + { + updater.process( update ); + } + } + + // then + populator.close( true ); + verifyUpdates( updates ); + } + + @Test + public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception + { + // GIVEN + populator.create(); + IndexEntryUpdate[] scanUpdates = layoutUtil.someUpdates(); + populator.add( Arrays.asList( scanUpdates ) ); + Iterator> generator = layoutUtil.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; + for ( Object value : updates ) + { + IndexEntryUpdate update = layoutUtil.add( nodeId++, Values.of( value ) ); + updater.process( update ); + } + } + + // 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 ); + } + + private Object[] asValues( IndexEntryUpdate[] updates ) + { + Object[] values = new Object[updates.length]; + for ( int i = 0; i < updates.length; i++ ) + { + values[i] = updates[i].values()[0].asObject(); + } + return values; + } + } + private int interleaveLargeAmountOfUpdates( Random updaterRandom, Iterator> updates ) throws IndexEntryConflictException { diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexTestUtil.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexTestUtil.java index 7ff530a8a22c2..cf4321dc6b0cd 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexTestUtil.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeIndexTestUtil.java @@ -36,6 +36,7 @@ import org.neo4j.index.internal.gbptree.Layout; import org.neo4j.index.internal.gbptree.RecoveryCleanupWorkCollector; import org.neo4j.io.pagecache.PageCache; +import org.neo4j.kernel.api.index.IndexDirectoryStructure; import org.neo4j.kernel.api.index.IndexEntryUpdate; import org.neo4j.kernel.api.index.IndexProvider; import org.neo4j.storageengine.api.schema.IndexDescriptor; @@ -71,6 +72,7 @@ public abstract class NativeIndexTestUtil,VALUE StoreIndexDescriptor indexDescriptor; LayoutTestUtil layoutUtil; IndexLayout layout; + private IndexDirectoryStructure indexDirectoryStructure; private File indexFile; PageCache pageCache; IndexProvider.Monitor monitor = IndexProvider.Monitor.EMPTY; diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeNonUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeNonUniqueIndexPopulatorTest.java index 6cba4d560ed21..221bcdafe4e3b 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeNonUniqueIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeNonUniqueIndexPopulatorTest.java @@ -34,7 +34,7 @@ import static org.neo4j.kernel.impl.index.schema.LayoutTestUtil.countUniqueValues; public abstract class NativeNonUniqueIndexPopulatorTest,VALUE extends NativeIndexValue> - extends NativeIndexPopulatorTest + extends NativeIndexPopulatorTests { @Test public void addShouldApplyDuplicateValues() throws Exception diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeUniqueIndexPopulatorTest.java index a423f4c68e9e1..688180624b87c 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeUniqueIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/NativeUniqueIndexPopulatorTest.java @@ -1,114 +1,109 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ package org.neo4j.kernel.impl.index.schema; -import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import java.io.File; +import java.io.IOException; import java.util.Arrays; +import java.util.Collection; -import org.neo4j.helpers.Exceptions; -import org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException; -import org.neo4j.kernel.api.index.IndexEntryUpdate; -import org.neo4j.kernel.api.index.IndexUpdater; +import org.neo4j.io.fs.FileSystemAbstraction; +import org.neo4j.io.pagecache.PageCache; +import org.neo4j.kernel.api.index.IndexProvider; +import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; import org.neo4j.storageengine.api.schema.IndexDescriptor; -import org.neo4j.storageengine.api.schema.IndexSample; +import org.neo4j.storageengine.api.schema.StoreIndexDescriptor; +import org.neo4j.values.storable.ValueGroup; -import static java.util.Arrays.asList; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -public abstract class NativeUniqueIndexPopulatorTest,VALUE extends NativeIndexValue> - extends NativeIndexPopulatorTest +@RunWith( Parameterized.class ) +public class NativeUniqueIndexPopulatorTest, VALUE extends NativeIndexValue> extends NativeIndexPopulatorTests.Unique { - @Test - public void addShouldThrowOnDuplicateValues() throws Exception + @Parameterized.Parameters(name = "{index}: {0}") + public static Collection data() { - // given - populator.create(); - IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); + IndexDescriptor descriptor = TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ); + return Arrays.asList( new Object[][]{ + {"Number", + numberPopulatorFactory(), + (LayoutTestUtilFactory) NumberUniqueLayoutTestUtil::new + }, + {"String", + (PopulatorFactory) StringIndexPopulator::new, + (LayoutTestUtilFactory) StringUniqueLayoutTestUtil::new + }, + {"Date", + temporalPopulatorFactory( ValueGroup.DATE ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new DateLayoutTestUtil( descriptor ) ) + }, + {"DateTime", + temporalPopulatorFactory( ValueGroup.ZONED_DATE_TIME ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new DateTimeLayoutTestUtil( descriptor ) ) + }, + {"Duration", + temporalPopulatorFactory( ValueGroup.DURATION ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new DurationLayoutTestUtil( descriptor ) ) + }, + {"LocalDateTime", + temporalPopulatorFactory( ValueGroup.LOCAL_DATE_TIME ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new LocalDateTimeLayoutTestUtil( descriptor ) ) + }, + {"LocalTime", + temporalPopulatorFactory( ValueGroup.LOCAL_TIME ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new LocalTimeLayoutTestUtil( descriptor ) ) + }, + {"LocalDateTime", + temporalPopulatorFactory( ValueGroup.LOCAL_DATE_TIME ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new LocalDateTimeLayoutTestUtil( descriptor ) ) + }, + {"Time", + temporalPopulatorFactory( ValueGroup.ZONED_TIME ), + (LayoutTestUtilFactory) () -> new UniqueLayoutTestUtil<>( new TimeLayoutTestUtil( descriptor ) ) + }, + // todo { Spatial has it's own subclass because it need to override some of the test methods } + } ); + } - // when - try - { - populator.add( Arrays.asList( updates ) ); - fail( "Updates should have conflicted" ); - } - catch ( IndexEntryConflictException e ) - { - // then good - } - finally - { - populator.close( true ); - } + private final PopulatorFactory populatorFactory; + private final LayoutTestUtilFactory layoutTestUtilFactory; + + @SuppressWarnings( "unused" ) + public NativeUniqueIndexPopulatorTest( String name, PopulatorFactory populatorFactory, LayoutTestUtilFactory layoutTestUtilFactory ) + { + this.populatorFactory = populatorFactory; + this.layoutTestUtilFactory = layoutTestUtilFactory; } - @Test - public void updaterShouldThrowOnDuplicateValues() throws Exception + @Override + NativeIndexPopulator createPopulator() throws IOException { - // given - populator.create(); - IndexEntryUpdate[] updates = layoutUtil.someUpdatesWithDuplicateValues(); - IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ); + return populatorFactory.create( pageCache, fs, getIndexFile(), layout, monitor, indexDescriptor ); + } - // when - for ( IndexEntryUpdate update : updates ) - { - updater.process( update ); - } - try - { - updater.close(); - fail( "Updates should have conflicted" ); - } - catch ( Exception e ) - { - // then - assertTrue( e.getMessage(), Exceptions.contains( e, IndexEntryConflictException.class ) ); - } - finally - { - populator.close( true ); - } + @Override + LayoutTestUtil createLayoutTestUtil() + { + return layoutTestUtilFactory.create(); } - @Test - public void shouldSampleUpdates() throws Exception + private static PopulatorFactory numberPopulatorFactory() { - // GIVEN - populator.create(); - IndexEntryUpdate[] updates = layoutUtil.someUpdates(); + return NumberIndexPopulator::new; + } - // WHEN - populator.add( asList( updates ) ); - for ( IndexEntryUpdate update : updates ) + private static > PopulatorFactory temporalPopulatorFactory( ValueGroup temporalValueGroup ) + { + return ( pageCache, fs, storeFile, layout, monitor, descriptor ) -> { - populator.includeSample( update ); - } - IndexSample sample = populator.sampleResult(); + TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( storeFile, layout, temporalValueGroup ); + return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, descriptor ); + }; + } - // THEN - assertEquals( updates.length, sample.sampleSize() ); - assertEquals( updates.length, sample.uniqueValues() ); - assertEquals( updates.length, sample.indexSize() ); - populator.close( true ); + @FunctionalInterface + private interface PopulatorFactory, VALUE extends NativeIndexValue> + { + NativeIndexPopulator create( PageCache pageCache, FileSystemAbstraction fs, File storeFile, IndexLayout layout, + IndexProvider.Monitor monitor, StoreIndexDescriptor descriptor ) throws IOException; } } diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/SpatialUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/SpatialUniqueIndexPopulatorTest.java index 22eb0248c15b6..606cf7ef01b2d 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/SpatialUniqueIndexPopulatorTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/SpatialUniqueIndexPopulatorTest.java @@ -27,7 +27,7 @@ import org.neo4j.kernel.impl.index.schema.config.ConfiguredSpaceFillingCurveSettingsCache; import org.neo4j.values.storable.CoordinateReferenceSystem; -public class SpatialUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest +public class SpatialUniqueIndexPopulatorTest extends NativeIndexPopulatorTests.Unique { private static final CoordinateReferenceSystem crs = CoordinateReferenceSystem.WGS84; private static final ConfiguredSpaceFillingCurveSettingsCache configuredSettings = new ConfiguredSpaceFillingCurveSettingsCache( Config.defaults() ); diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/StringUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/StringUniqueIndexPopulatorTest.java deleted file mode 100644 index c9354965619cf..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/StringUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -public class StringUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - return new StringIndexPopulator( pageCache, fs, getIndexFile(), layout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new StringUniqueLayoutTestUtil(); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/TimeUniqueIndexPopulatorTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/TimeUniqueIndexPopulatorTest.java deleted file mode 100644 index 71b59e7897ccf..0000000000000 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/TimeUniqueIndexPopulatorTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.kernel.impl.index.schema; - -import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; -import org.neo4j.values.storable.ValueGroup; - -public class TimeUniqueIndexPopulatorTest extends NativeUniqueIndexPopulatorTest -{ - @Override - NativeIndexPopulator createPopulator() - { - TemporalIndexFiles.FileLayout fileLayout = new TemporalIndexFiles.FileLayout<>( getIndexFile(), layout, ValueGroup.ZONED_TIME ); - return new TemporalIndexPopulator.PartPopulator<>( pageCache, fs, fileLayout, monitor, indexDescriptor ); - } - - @Override - protected LayoutTestUtil createLayoutTestUtil() - { - return new UniqueLayoutTestUtil<>( new TimeLayoutTestUtil( TestIndexDescriptorFactory.uniqueForLabel( 42, 666 ) ) ); - } -} diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/UniqueLayoutTestUtil.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/UniqueLayoutTestUtil.java index b34a2af8963c6..03a8c55af0cb2 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/UniqueLayoutTestUtil.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/index/schema/UniqueLayoutTestUtil.java @@ -27,7 +27,7 @@ import org.neo4j.values.storable.RandomValues; import org.neo4j.values.storable.Value; -class UniqueLayoutTestUtil, VALUE extends NativeIndexValue> extends LayoutTestUtil +class UniqueLayoutTestUtil, VALUE extends NativeIndexValue> extends LayoutTestUtil { private final LayoutTestUtil delegate;