Skip to content

Commit

Permalink
Randomized index order test (non-composite)
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Sep 20, 2018
1 parent 10c7164 commit e5cac0a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
Expand Up @@ -1121,7 +1121,8 @@ private void shouldSeekInOrderExactWithRange( IndexOrder order, Object o0, Objec
SimpleNodeValueClient client = new SimpleNodeValueClient(); SimpleNodeValueClient client = new SimpleNodeValueClient();
try ( AutoCloseable ignored = query( client, order, exact, range ) ) try ( AutoCloseable ignored = query( client, order, exact, range ) )
{ {
assertOrder( client, order, 6 ); List<Long> seenIds = assertOrder( client, order );
assertThat( seenIds.size(), equalTo( 6 ) );
} }
} }


Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;


import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
Expand All @@ -46,7 +47,6 @@
import org.neo4j.values.storable.ValueGroup; import org.neo4j.values.storable.ValueGroup;
import org.neo4j.values.storable.Values; import org.neo4j.values.storable.Values;


import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;


Expand Down Expand Up @@ -133,14 +133,16 @@ protected AutoCloseable query( SimpleNodeValueClient client, IndexOrder order, I
return reader; return reader;
} }


void assertOrder( SimpleNodeValueClient client, IndexOrder order, int expectedCount ) List<Long> assertOrder( SimpleNodeValueClient client, IndexOrder order )
{ {
List<Long> seenIds = new ArrayList<>();
Value[] prevValues = null; Value[] prevValues = null;
Value[] values; Value[] values;
int count = 0; int count = 0;
while ( client.next() ) while ( client.next() )
{ {
count++; count++;
seenIds.add( client.reference );
values = client.values; values = client.values;
if ( order == IndexOrder.ASCENDING ) if ( order == IndexOrder.ASCENDING )
{ {
Expand All @@ -156,7 +158,7 @@ else if ( order == IndexOrder.DESCENDING )
} }
prevValues = values; prevValues = values;
} }
assertThat( "correct number of hits", count, equalTo( expectedCount ) ); return seenIds;
} }


IndexOrder[] orderCapability( IndexQuery... predicates ) IndexOrder[] orderCapability( IndexQuery... predicates )
Expand Down
Expand Up @@ -815,7 +815,8 @@ private void shouldRangeSeekInOrder( IndexOrder order, Object o0, Object o1, Obj
SimpleNodeValueClient client = new SimpleNodeValueClient(); SimpleNodeValueClient client = new SimpleNodeValueClient();
try ( AutoCloseable ignored = query( client, order, range ) ) try ( AutoCloseable ignored = query( client, order, range ) )
{ {
assertOrder( client, order, 6 ); List<Long> seenIds = assertOrder( client, order );
assertThat( seenIds.size(), equalTo( 6 ) );
} }
} }


Expand Down
Expand Up @@ -30,8 +30,10 @@
import java.util.TreeSet; import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;


import org.neo4j.internal.kernel.api.IndexOrder;
import org.neo4j.internal.kernel.api.IndexQuery; import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory; import org.neo4j.kernel.api.schema.index.TestIndexDescriptorFactory;
import org.neo4j.storageengine.api.schema.SimpleNodeValueClient;
import org.neo4j.values.storable.RandomValues; import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value; import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values; import org.neo4j.values.storable.Values;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void testExactMatchOnRandomValues() throws Exception
} }


@Test @Test
public void testRangeMatchOnRandomValues() throws Exception public void testRangeMatchInOrderOnRandomValues() throws Exception
{ {
Assume.assumeTrue( "Assume support for granular composite queries", testSuite.supportsGranularCompositeQueries() ); Assume.assumeTrue( "Assume support for granular composite queries", testSuite.supportsGranularCompositeQueries() );
// given // given
Expand All @@ -87,6 +89,7 @@ public void testRangeMatchOnRandomValues() throws Exception


for ( int i = 0; i < 100; i++ ) for ( int i = 0; i < 100; i++ )
{ {
// Construct a random range query of random value type
RandomValues.Type type = random.among( types ); RandomValues.Type type = random.among( types );
Value from = random.randomValues().nextValueOfType( type ); Value from = random.randomValues().nextValueOfType( type );
Value to = random.randomValues().nextValueOfType( type ); Value to = random.randomValues().nextValueOfType( type );
Expand All @@ -99,20 +102,43 @@ public void testRangeMatchOnRandomValues() throws Exception
boolean fromInclusive = random.nextBoolean(); boolean fromInclusive = random.nextBoolean();
boolean toInclusive = random.nextBoolean(); boolean toInclusive = random.nextBoolean();


// when // Expected result based on query
List<Long> expectedIds = sortedValues.subSet( add( 0, descriptor.schema(), from ), fromInclusive, add( 0, descriptor.schema(), to ), toInclusive ) IndexQuery.RangePredicate<?> predicate = IndexQuery.range( 0, from, fromInclusive, to, toInclusive );
.stream() List<Long> expectedIds = expectedIds( sortedValues, from, to, fromInclusive, toInclusive );
.map( IndexEntryUpdate::getEntityId )
.collect( Collectors.toList() );
List<Long> actualIds = query( IndexQuery.range( 0, from, fromInclusive, to, toInclusive ) );
expectedIds.sort( Long::compare );
actualIds.sort( Long::compare );


// then // Depending on order capabilities we verify ids or order and ids.
assertThat( actualIds, equalTo( expectedIds ) ); IndexOrder[] indexOrders = indexProvider.getCapability().orderCapability( predicate.valueGroup().category() );
for ( IndexOrder order : indexOrders )
{
List<Long> actualIds;
if ( order == IndexOrder.NONE )
{
actualIds = query( predicate );
}
else
{
SimpleNodeValueClient client = new SimpleNodeValueClient();
try ( AutoCloseable ignore = query( client, order, predicate ) )
{
actualIds = assertOrder( client, order );
}
}
actualIds.sort( Long::compare );
// then
assertThat( actualIds, equalTo( expectedIds ) );
}
} }
} }


private List<Long> expectedIds( TreeSet<IndexEntryUpdate> sortedValues, Value from, Value to, boolean fromInclusive, boolean toInclusive )
{
return sortedValues.subSet( add( 0, descriptor.schema(), from ), fromInclusive, add( 0, descriptor.schema(), to ), toInclusive )
.stream()
.map( IndexEntryUpdate::getEntityId )
.sorted( Long::compare )
.collect( Collectors.toList() );
}

private List<Value> generateValuesFromType( RandomValues.Type[] types ) private List<Value> generateValuesFromType( RandomValues.Type[] types )
{ {
List<Value> values = new ArrayList<>(); List<Value> values = new ArrayList<>();
Expand Down

0 comments on commit e5cac0a

Please sign in to comment.