Skip to content

Commit

Permalink
Fix bugs introduced by rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Feb 27, 2017
1 parent 3764722 commit 3cdb074
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.neo4j.storageengine.api.schema.IndexSampler;

import static org.neo4j.collection.primitive.PrimitiveLongCollections.toPrimitiveIterator;
import static org.neo4j.kernel.api.schema_new.IndexQuery.IndexQueryType.exact;
import static org.neo4j.kernel.impl.api.PropertyValueComparison.COMPARE_VALUES;
import static org.neo4j.kernel.impl.api.PropertyValueComparison.SuperType.NUMBER;
import static org.neo4j.kernel.impl.api.PropertyValueComparison.SuperType.STRING;
Expand Down Expand Up @@ -97,8 +98,8 @@ private synchronized PrimitiveLongIterator rangeSeekByNumberInclusive( Number lo
return toPrimitiveIterator( nodeIds.iterator() );
}

private synchronized PrimitiveLongIterator rangeSeekByString( String lower, boolean includeLower,
String upper, boolean includeUpper )
private synchronized PrimitiveLongIterator rangeSeekByString( String lower, boolean includeLower, String upper,
boolean includeUpper )
{
Set<Long> nodeIds = new HashSet<>();
for ( Map.Entry<List<Object>,Set<Long>> entry : data.entrySet() )
Expand Down Expand Up @@ -249,12 +250,24 @@ public synchronized IndexSampler createSampler()
@Override
public PrimitiveLongIterator query( IndexQuery... predicates )
{
assert predicates.length == 1: "composite indexes not yet supported";
if ( predicates.length > 1 )
{
Object[] values = new Object[predicates.length];
for ( int i = 0; i < predicates.length; i++ )
{
assert predicates[i].type() == exact : "composite indexes only supported for seek";
values[i] = ((IndexQuery.ExactPredicate)predicates[i]).value();
}
return seek( values );
}
assert predicates.length == 1 : "composite indexes not yet supported, except for seek on all properties";
IndexQuery predicate = predicates[0];
switch ( predicate.type() )
{
case exists: return scan();
case exact: return seek( ((IndexQuery.ExactPredicate)predicate).value() );
case exists:
return scan();
case exact:
return seek( ((IndexQuery.ExactPredicate) predicate).value() );
case rangeNumeric:
IndexQuery.NumberRangePredicate np = (IndexQuery.NumberRangePredicate) predicate;
return rangeSeekByNumberInclusive( np.from(), np.to() );
Expand Down
Expand Up @@ -44,6 +44,7 @@
import org.neo4j.storageengine.api.schema.IndexSampler;

import static org.neo4j.kernel.api.impl.schema.LuceneDocumentStructure.NODE_ID_KEY;
import static org.neo4j.kernel.api.schema_new.IndexQuery.IndexQueryType.exact;
import static org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor.Type.UNIQUE;

/**
Expand Down Expand Up @@ -86,7 +87,17 @@ public IndexSampler createSampler()
@Override
public PrimitiveLongIterator query( IndexQuery... predicates )
{
assert predicates.length == 1: "not yet supporting composite queries";
if ( predicates.length > 1 )
{
Object[] values = new Object[predicates.length];
for ( int i = 0; i < predicates.length; i++ )
{
assert predicates[i].type() == exact : "composite indexes only supported for seek";
values[i] = ((IndexQuery.ExactPredicate)predicates[i]).value();
}
return seek( values );
}
assert predicates.length == 1 : "composite indexes not yet supported, except for seek on all properties";
IndexQuery predicate = predicates[0];
switch ( predicate.type() )
{
Expand Down Expand Up @@ -115,9 +126,9 @@ public PrimitiveLongIterator query( IndexQuery... predicates )
}
}

private PrimitiveLongIterator seek( Object value )
private PrimitiveLongIterator seek( Object... values )
{
return query( LuceneDocumentStructure.newSeekQuery( value ) );
return query( LuceneDocumentStructure.newSeekQuery( values ) );
}

private PrimitiveLongIterator rangeSeekByNumberInclusive( Number lower, Number upper )
Expand Down
Expand Up @@ -75,7 +75,7 @@ public class DatabaseCompositeIndexAccessorTest

private LuceneIndexAccessor accessor;
private final long nodeId = 1, nodeId2 = 2;
private final Object[] values = {"value1", "value2"}, value2 = {40, 42};
private final Object[] values = {"value1", "values2"}, values2 = {40, 42};
private DirectoryFactory.InMemoryDirectoryFactory dirFactory;
private static final NewIndexDescriptor indexDescriptor = NewIndexDescriptorFactory
.forLabel( 0, PROP_ID1, PROP_ID2 );
Expand Down Expand Up @@ -138,7 +138,7 @@ public void after() throws IOException
public void indexReaderShouldSupportScan() throws Exception
{
// GIVEN
updateAndCommit( asList( add( nodeId, value ), add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId, values ), add( nodeId2, values2 ) ) );
IndexReader reader = accessor.newReader();

// WHEN
Expand All @@ -147,16 +147,16 @@ public void indexReaderShouldSupportScan() throws Exception
// THEN
assertEquals( asSet( nodeId, nodeId2 ), PrimitiveLongCollections.toSet( results ) );
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
reader.close();
}

@Test
public void indexReaderShouldSupportSeekOnCompositeIndexUpdatedWithTwoTransactions() throws Exception
{
// GIVEN
updateAndCommit( asList( add( nodeId, value ), add( nodeId2, value ) ) );
updateAndCommit( asList( add( nodeId, value2 ), add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId, values ), add( nodeId2, values ) ) );
updateAndCommit( asList( add( nodeId, values2 ), add( nodeId2, values2 ) ) );
IndexReader reader = accessor.newReader();

// WHEN
Expand All @@ -165,28 +165,28 @@ public void indexReaderShouldSupportSeekOnCompositeIndexUpdatedWithTwoTransactio
// THEN
assertEquals( asSet( nodeId, nodeId2 ), PrimitiveLongCollections.toSet( results ) );
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
reader.close();
}

@Test
public void multipleIndexReadersFromDifferentPointsInTimeCanSeeDifferentResults() throws Exception
{
// WHEN
updateAndCommit( asList( add( nodeId, value ) ) );
updateAndCommit( asList( add( nodeId, values ) ) );
IndexReader firstReader = accessor.newReader();
updateAndCommit( asList( add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId2, values2 ) ) );
IndexReader secondReader = accessor.newReader();

// THEN
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( firstReader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
assertEquals( asSet(), PrimitiveLongCollections.toSet( firstReader
.query( IndexQuery.exact( PROP_ID1, value2[0] ), IndexQuery.exact( PROP_ID2, value2[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values2[0] ), IndexQuery.exact( PROP_ID2, values2[1] ) ) ) );
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( secondReader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
assertEquals( asSet( nodeId2 ), PrimitiveLongCollections.toSet( secondReader
.query( IndexQuery.exact( PROP_ID1, value2[0] ), IndexQuery.exact( PROP_ID2, value2[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values2[0] ), IndexQuery.exact( PROP_ID2, values2[1] ) ) ) );
firstReader.close();
secondReader.close();
}
Expand All @@ -195,56 +195,56 @@ public void multipleIndexReadersFromDifferentPointsInTimeCanSeeDifferentResults(
public void canAddNewData() throws Exception
{
// WHEN
updateAndCommit( asList( add( nodeId, value ), add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId, values ), add( nodeId2, values2 ) ) );
IndexReader reader = accessor.newReader();

// THEN
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
reader.close();
}

@Test
public void canChangeExistingData() throws Exception
{
// GIVEN
updateAndCommit( asList( add( nodeId, value ) ) );
updateAndCommit( asList( add( nodeId, values ) ) );

// WHEN
updateAndCommit( asList( change( nodeId, value, value2 ) ) );
updateAndCommit( asList( change( nodeId, values, values2 ) ) );
IndexReader reader = accessor.newReader();

// THEN
assertEquals( asSet( nodeId ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value2[0] ), IndexQuery.exact( PROP_ID2, value2[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values2[0] ), IndexQuery.exact( PROP_ID2, values2[1] ) ) ) );
assertEquals( emptySetOf( Long.class ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
reader.close();
}

@Test
public void canRemoveExistingData() throws Exception
{
// GIVEN
updateAndCommit( asList( add( nodeId, value ), add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId, values ), add( nodeId2, values2 ) ) );

// WHEN
updateAndCommit( asList( remove( nodeId, value ) ) );
updateAndCommit( asList( remove( nodeId, values ) ) );
IndexReader reader = accessor.newReader();

// THEN
assertEquals( asSet( nodeId2 ), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value2[0] ), IndexQuery.exact( PROP_ID2, value2[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values2[0] ), IndexQuery.exact( PROP_ID2, values2[1] ) ) ) );
assertEquals( asSet(), PrimitiveLongCollections.toSet( reader
.query( IndexQuery.exact( PROP_ID1, value[0] ), IndexQuery.exact( PROP_ID2, value[1] ) ) ) );
.query( IndexQuery.exact( PROP_ID1, values[0] ), IndexQuery.exact( PROP_ID2, values[1] ) ) ) );
reader.close();
}

@Test
public void shouldStopSamplingWhenIndexIsDropped() throws Exception
{
// given
updateAndCommit( asList( add( nodeId, value ), add( nodeId2, value2 ) ) );
updateAndCommit( asList( add( nodeId, values ), add( nodeId2, values2 ) ) );

// when
IndexReader indexReader = accessor.newReader(); // needs to be acquired before drop() is called
Expand Down

0 comments on commit 3cdb074

Please sign in to comment.