Skip to content

Commit

Permalink
String IndexPopulator tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint authored and burqen committed Feb 15, 2018
1 parent 4a6e91c commit 81e91ba
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 177 deletions.
Expand Up @@ -160,7 +160,7 @@ 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( Number[] updates ) static int countUniqueValues( Object[] updates )
{ {
return Stream.of( updates ).collect( Collectors.toSet() ).size(); return Stream.of( updates ).collect( Collectors.toSet() ).size();
} }
Expand Down
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.index.schema;

import org.junit.Test;

import java.util.Arrays;
import java.util.Iterator;

import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.values.storable.Values;

import static org.junit.Assert.assertEquals;

import static org.neo4j.kernel.impl.index.schema.LayoutTestUtil.countUniqueValues;

public abstract class NativeNonUniqueSchemaIndexPopulatorTest<KEY extends NativeSchemaKey,VALUE extends NativeSchemaValue>
extends NativeSchemaIndexPopulatorTest<KEY,VALUE>
{
@Test
public void addShouldApplyDuplicateValues() throws Exception
{
// given
populator.create();
IndexEntryUpdate<IndexDescriptor>[] 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<IndexDescriptor>[] updates = layoutUtil.someUpdatesWithDuplicateValues();
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{
// when
for ( IndexEntryUpdate<IndexDescriptor> update : updates )
{
updater.process( update );
}
}

// then
populator.close( true );
verifyUpdates( updates );
}

@Test
public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception
{
// GIVEN
populator.create();
IndexEntryUpdate<IndexDescriptor>[] scanUpdates = layoutUtil.someUpdates();
populator.add( Arrays.asList( scanUpdates ) );
Iterator<IndexEntryUpdate<IndexDescriptor>> 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<IndexDescriptor> update = layoutUtil.add( nodeId++, Values.of( value ) );
updater.process( update );
populator.includeSample( update );
}
}

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

// THEN
assertEquals( updates.length, sample.sampleSize() );
assertEquals( countUniqueValues( updates ), sample.uniqueValues() );
assertEquals( updates.length, sample.indexSize() );
populator.close( true );
}
}
Expand Up @@ -347,7 +347,7 @@ public void shouldReturnNoEntriesForMismatchingExactPredicate() throws Exception
public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndExclusiveEnd() throws Exception public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndExclusiveEnd() throws Exception
{ {
// given // given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates(); IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesNoDuplicateValues();
processAll( updates ); processAll( updates );
layoutUtil.sort( updates ); layoutUtil.sort( updates );


Expand All @@ -362,7 +362,7 @@ public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndExc
public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndInclusiveEnd() throws Exception public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndInclusiveEnd() throws Exception
{ {
// given // given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates(); IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesNoDuplicateValues();
processAll( updates ); processAll( updates );
layoutUtil.sort( updates ); layoutUtil.sort( updates );


Expand All @@ -377,7 +377,7 @@ public void shouldReturnMatchingEntriesForRangePredicateWithInclusiveStartAndInc
public void shouldReturnMatchingEntriesForRangePredicateWithExclusiveStartAndExclusiveEnd() throws Exception public void shouldReturnMatchingEntriesForRangePredicateWithExclusiveStartAndExclusiveEnd() throws Exception
{ {
// given // given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates(); IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesNoDuplicateValues();
processAll( updates ); processAll( updates );
layoutUtil.sort( updates ); layoutUtil.sort( updates );


Expand All @@ -392,7 +392,7 @@ public void shouldReturnMatchingEntriesForRangePredicateWithExclusiveStartAndExc
public void shouldReturnMatchingEntriesForRangePredicateWithExclusiveStartAndInclusiveEnd() throws Exception public void shouldReturnMatchingEntriesForRangePredicateWithExclusiveStartAndInclusiveEnd() throws Exception
{ {
// given // given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates(); IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesNoDuplicateValues();
processAll( updates ); processAll( updates );
layoutUtil.sort( updates ); layoutUtil.sort( updates );


Expand Down Expand Up @@ -688,7 +688,7 @@ public void shouldSeeNoEntriesInAllEntriesReaderOnEmptyIndex()
public void shouldNotSeeFilteredEntries() throws Exception public void shouldNotSeeFilteredEntries() throws Exception
{ {
// given // given
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdates(); IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesNoDuplicateValues();
processAll( updates ); processAll( updates );
layoutUtil.sort( updates ); layoutUtil.sort( updates );
IndexReader reader = accessor.newReader(); IndexReader reader = accessor.newReader();
Expand Down
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.index.schema;

import org.junit.Test;

import java.util.Arrays;

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.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.storageengine.api.schema.IndexSample;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import static java.util.Arrays.asList;

public abstract class NativeUniqueSchemaIndexPopulatorTest<KEY extends NativeSchemaKey,VALUE extends NativeSchemaValue>
extends NativeSchemaIndexPopulatorTest<KEY,VALUE>
{
@Test
public void addShouldThrowOnDuplicateValues() throws Exception
{
// given
populator.create();
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesWithDuplicateValues();

// when
try
{
populator.add( Arrays.asList( updates ) );
fail( "Updates should have conflicted" );
}
catch ( Exception e )
{
// then
assertTrue( Exceptions.contains( e, IndexEntryConflictException.class ) );
}
finally
{
populator.close( true );
}
}

@Test
public void updaterShouldThrowOnDuplicateValues() throws Exception
{
// given
populator.create();
IndexEntryUpdate<IndexDescriptor>[] updates = layoutUtil.someUpdatesWithDuplicateValues();
IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor );

// when
for ( IndexEntryUpdate<IndexDescriptor> 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<IndexDescriptor>[] updates = layoutUtil.someUpdates();

// WHEN
populator.add( asList( updates ) );
for ( IndexEntryUpdate<IndexDescriptor> 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 );
}
}
Expand Up @@ -19,26 +19,13 @@
*/ */
package org.neo4j.kernel.impl.index.schema; package org.neo4j.kernel.impl.index.schema;


import org.junit.Test;

import java.io.File; import java.io.File;
import java.util.Arrays;

import org.neo4j.index.internal.gbptree.Layout; import org.neo4j.index.internal.gbptree.Layout;
import org.neo4j.io.fs.FileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache; import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.schema.index.IndexDescriptor;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig; import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.values.storable.Values;

import static org.junit.Assert.assertEquals;
import static org.neo4j.helpers.ArrayUtil.array;
import static org.neo4j.kernel.impl.index.schema.LayoutTestUtil.countUniqueValues;


public class NumberNonUniqueSchemaIndexPopulatorTest extends NativeSchemaIndexPopulatorTest<NumberSchemaKey,NativeSchemaValue> public class NumberNonUniqueSchemaIndexPopulatorTest extends NativeNonUniqueSchemaIndexPopulatorTest<NumberSchemaKey,NativeSchemaValue>
{ {
@Override @Override
NativeSchemaIndexPopulator<NumberSchemaKey,NativeSchemaValue> createPopulator( PageCache pageCache, FileSystemAbstraction fs, NativeSchemaIndexPopulator<NumberSchemaKey,NativeSchemaValue> createPopulator( PageCache pageCache, FileSystemAbstraction fs,
Expand All @@ -48,70 +35,6 @@ NativeSchemaIndexPopulator<NumberSchemaKey,NativeSchemaValue> createPopulator( P
indexId ); indexId );
} }


@Test
public void addShouldApplyDuplicateValues() throws Exception
{
// given
populator.create();
IndexEntryUpdate<IndexDescriptor>[] 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<IndexDescriptor>[] updates = layoutUtil.someUpdatesWithDuplicateValues();
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{
// when
for ( IndexEntryUpdate<IndexDescriptor> update : updates )
{
updater.process( update );
}
}

// then
populator.close( true );
verifyUpdates( updates );
}

@Test
public void shouldSampleUpdatesIfConfiguredForOnlineSampling() throws Exception
{
// GIVEN
populator.create();
IndexEntryUpdate<IndexDescriptor>[] scanUpdates = layoutUtil.someUpdates();
populator.add( Arrays.asList( scanUpdates ) );
Number[] updates = array( 101, 102, 102, 103, 103 );
try ( IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ) )
{
long nodeId = 1000;
for ( Number number : updates )
{
IndexEntryUpdate<IndexDescriptor> update = layoutUtil.add( nodeId++, Values.of( number ) );
updater.process( update );
populator.includeSample( update );
}
}

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

// THEN
assertEquals( updates.length, sample.sampleSize() );
assertEquals( countUniqueValues( updates ), sample.uniqueValues() );
assertEquals( updates.length, sample.indexSize() );
populator.close( true );
}

@Override @Override
protected LayoutTestUtil<NumberSchemaKey,NativeSchemaValue> createLayoutTestUtil() protected LayoutTestUtil<NumberSchemaKey,NativeSchemaValue> createLayoutTestUtil()
{ {
Expand Down

0 comments on commit 81e91ba

Please sign in to comment.