private void assertResultsInOrder( List<Pair<Long,Value>> expected, NodeValueIndexCursor cursor ) { Comparator<Pair<Long,Value>> comparator = indexOrder == IndexOrder.ASCENDING ? ( a, b ) -> Values.COMPARATOR.compare( a.other(), b.other() ) : ( a, b ) -> Values.COMPARATOR.compare( b.other(), a.other() ); expected.sort( comparator ); Iterator<Pair<Long,Value>> expectedRows = expected.iterator(); while ( cursor.next() && expectedRows.hasNext() ) { Pair<Long, Value> expectedRow = expectedRows.next(); assertThat( cursor.nodeReference(), equalTo( expectedRow.first() ) ); for ( int i = 0; i < cursor.numberOfProperties(); i++ ) { Value value = cursor.propertyValue( i ); assertThat( value, equalTo( expectedRow.other() ) ); } } assertFalse( expectedRows.hasNext() ); assertFalse( cursor.next() ); }
@Test public void shouldPerformIndexScan() throws Exception { // given int label = token.nodeLabel( "Node" ); int prop = token.propertyKey( "prop" ); IndexReference index = schemaRead.index( label, prop ); IndexValueCapability wildcardCapability = index.valueCapability( ValueCategory.UNKNOWN ); try ( NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when read.nodeIndexScan( index, node, IndexOrder.NONE, indexProvidesAllValues() ); // then assertThat( node.numberOfProperties(), equalTo( 1 ) ); assertFoundNodesAndValue( node, TOTAL_NODE_COUNT, uniqueIds, wildcardCapability, indexProvidesAllValues() ); } }
@Test public void shouldPerformStringContainmentSearch() throws Exception { // given boolean needsValues = indexProvidesStringValues(); int label = token.nodeLabel( "Node" ); int prop = token.propertyKey( "prop" ); IndexReference index = schemaRead.index( label, prop ); IndexValueCapability stringCapability = index.valueCapability( ValueCategory.TEXT ); try ( NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when read.nodeIndexSeek( index, node, IndexOrder.NONE, needsValues, IndexQuery.stringContains( prop, stringValue( "o" ) ) ); // then assertThat( node.numberOfProperties(), equalTo( 1 ) ); assertFoundNodesAndValue( node, uniqueIds, stringCapability, needsValues, strOne, strTwo1, strTwo2 ); } }
@Test public void shouldPerformStringSuffixSearch() throws Exception { // given boolean needsValues = indexProvidesStringValues(); int label = token.nodeLabel( "Node" ); int prop = token.propertyKey( "prop" ); IndexReference index = schemaRead.index( label, prop ); IndexValueCapability stringCapability = index.valueCapability( ValueCategory.TEXT ); try ( NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when read.nodeIndexSeek( index, node, IndexOrder.NONE, needsValues, IndexQuery.stringSuffix( prop, stringValue( "e" ) ) ); // then assertThat( node.numberOfProperties(), equalTo( 1 ) ); assertFoundNodesAndValue( node, uniqueIds, stringCapability, needsValues, strOne, strThree1, strThree2, strThree3 ); } }
@Test public void shouldPerformStringPrefixSearch() throws Exception { // given boolean needsValues = indexProvidesStringValues(); int label = token.nodeLabel( "Node" ); int prop = token.propertyKey( "prop" ); IndexReference index = schemaRead.index( label, prop ); IndexValueCapability stringCapability = index.valueCapability( ValueCategory.TEXT ); try ( NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when read.nodeIndexSeek( index, node, IndexOrder.NONE, needsValues, IndexQuery.stringPrefix( prop, stringValue( "t" ) ) ); // then assertThat( node.numberOfProperties(), equalTo( 1 ) ); assertFoundNodesAndValue( node, uniqueIds, stringCapability, needsValues, strTwo1, strTwo2, strThree1, strThree2, strThree3 ); } }
@Test public void shouldPerformExactLookupInCompositeIndex() throws Exception { // given boolean needsValues = false; int label = token.nodeLabel( "Person" ); int firstName = token.propertyKey( "firstname" ); int surname = token.propertyKey( "surname" ); IndexReference index = schemaRead.index( label, firstName, surname ); try ( NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when IndexValueCapability valueCapability = index.valueCapability( ValueCategory.TEXT, ValueCategory.TEXT ); read.nodeIndexSeek( index, node, IndexOrder.NONE, needsValues, IndexQuery.exact( firstName, "Joe" ), IndexQuery.exact( surname, "Dalton" ) ); // then assertThat( node.numberOfProperties(), equalTo( 2 ) ); assertFoundNodesAndNoValue( node, 1, uniqueIds ); } }
@Test public void shouldNotFindDeletedNodeInIndexScan() throws Exception { // Given boolean needsValues = indexProvidesAllValues(); int label = token.nodeLabel( "Node" ); int prop = token.propertyKey( "prop" ); IndexReference index = schemaRead.index( label, prop ); IndexValueCapability wildcardCapability = index.valueCapability( ValueCategory.UNKNOWN ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeValueIndexCursor node = cursors.allocateNodeValueIndexCursor() ) { MutableLongSet uniqueIds = new LongHashSet(); // when tx.dataRead().nodeIndexScan( index, node, IndexOrder.NONE, needsValues ); assertThat( node.numberOfProperties(), equalTo( 1 ) ); assertFoundNodesAndValue( node, TOTAL_NODE_COUNT, uniqueIds, wildcardCapability, needsValues ); // then tx.dataWrite().nodeDelete( strOne ); tx.dataRead().nodeIndexScan( index, node, IndexOrder.NONE, needsValues ); assertFoundNodesAndValue( node, TOTAL_NODE_COUNT - 1, uniqueIds, wildcardCapability, needsValues ); } }