Skip to content

Commit

Permalink
Testing of index distinct values on kernel-api level
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Feb 6, 2019
1 parent 98c31af commit affe1db
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
import org.eclipse.collections.api.set.primitive.MutableLongSet;
import org.eclipse.collections.impl.factory.primitive.LongLists;
import org.eclipse.collections.impl.set.mutable.primitive.LongHashSet;
import org.junit.Assume;
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
Expand All @@ -42,8 +47,10 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.neo4j.graphdb.Label.label;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian;
import static org.neo4j.values.storable.CoordinateReferenceSystem.Cartesian_3D;
Expand All @@ -69,6 +76,7 @@ public void createTestGraph( GraphDatabaseService graphDb )
try ( Transaction tx = graphDb.beginTx() )
{
graphDb.schema().indexFor( label( "Node" ) ).on( "prop" ).create();
graphDb.schema().indexFor( label( "Node" ) ).on( "prop2" ).create();
tx.success();
}
try ( Transaction tx = graphDb.beginTx() )
Expand Down Expand Up @@ -772,7 +780,7 @@ public void shouldRespectOrderCapabilitiesForWildcard() throws Exception
public void shouldProvideValuesForPoints() throws Exception
{
// given
Assume.assumeTrue( indexProvidesAllValues() );
assumeTrue( indexProvidesAllValues() );

int label = token.nodeLabel( "What" );
int prop = token.propertyKey( "ever" );
Expand All @@ -795,7 +803,7 @@ public void shouldProvideValuesForPoints() throws Exception
public void shouldProvideValuesForAllTypes() throws Exception
{
// given
Assume.assumeTrue( indexProvidesAllValues() );
assumeTrue( indexProvidesAllValues() );

int label = token.nodeLabel( "What" );
int prop = token.propertyKey( "ever" );
Expand Down Expand Up @@ -1427,6 +1435,63 @@ public void shouldFindSwappedNodeInCompositeIndex() throws Exception
}
}

@Test
public void shouldCountDistinctValues() throws Exception
{
// Given
int label = token.nodeLabel( "Node" );
int key = token.propertyKey( "prop2" );
IndexReference index = schemaRead.index( label, key );
Map<Value,Set<Long>> expected = new HashMap<>();
int expectedTotalCount = 100;
try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction() )
{
ThreadLocalRandom random = ThreadLocalRandom.current();
Write write = tx.dataWrite();
for ( int i = 0; i < expectedTotalCount; i++ )
{
long nodeId = write.nodeCreate();
write.nodeAddLabel( nodeId, label );
Value value = Values.of( random.nextBoolean() ? String.valueOf( i % 10 ) : (i % 10) );
write.nodeSetProperty( nodeId, key, value );
expected.computeIfAbsent( value, v -> new HashSet<>() ).add( nodeId );
}
tx.success();
}

// then
try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction();
NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() )
{
tx.dataRead().nodeIndexDistinctValues( index, node );
long totalCount = 0;
boolean hasValues = true;
while ( node.next() )
{
long count = node.nodeReference();
if ( node.hasValue() && node.propertyValue( 0 ) != null )
{
Value value = node.propertyValue( 0 );
Set<Long> expectedNodes = expected.remove( value );
assertNotNull( expectedNodes );
assertEquals( count, expectedNodes.size() );
}
else
{
// Some providers just can't serve the values for all types, which makes this test unable to do detailed checks for those values
// and the total count
hasValues = false;
}
totalCount += count;
}
if ( hasValues )
{
assertTrue( expected.toString(), expected.isEmpty() );
}
assertEquals( expectedTotalCount, totalCount );
}
}

private long nodeWithProp( GraphDatabaseService graphDb, Object value )
{
Node node = graphDb.createNode( label( "Node" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public void initialize( IndexDescriptor descriptor,
IndexOrder indexOrder,
boolean needsValues )
{
assert query != null && query.length > 0;
assert query != null;
super.initialize( progressor );
sortedMergeJoin.initialize( indexOrder );

this.indexOrder = indexOrder;
this.needsValues = needsValues;
this.query = query;

if ( read.hasTxStateWithChanges() )
if ( read.hasTxStateWithChanges() && query.length > 0 )
{
IndexQuery firstPredicate = query[0];
switch ( firstPredicate.type() )
Expand Down

0 comments on commit affe1db

Please sign in to comment.