public static <T> long count( Iterator<T> iterator ) { return count( iterator, Predicates.alwaysTrue() ); }
/** * Counts the number of filtered items in the {@code iterable} by looping through it. * * @param <T> the type of items in the iterator. * @param iterable the {@link Iterable} to count items in. * @param filter the filter to test items against * @return the number of found in {@code iterable}. */ public static <T> long count( Iterable<T> iterable, Predicate<T> filter ) { Iterator<T> iterator = iterable.iterator(); try { return Iterators.count( iterator, filter ); } finally { if ( iterator instanceof ResourceIterator ) { ((ResourceIterator<?>) iterator).close(); } } }
private long countHits( IndexHits<Node> query ) { try { return count( query ); } finally { query.close(); } }
@Test public void shouldAcquireSchemaReadLockLazilyBeforeGettingAllConstraints() { // given int labelId = 1; int relTypeId = 2; UniquenessConstraintDescriptor uniquenessConstraint = uniqueForLabel( labelId, 2, 3, 3 ); RelExistenceConstraintDescriptor existenceConstraint = existsForRelType( relTypeId, 3, 4, 5 ); when( storageReader.constraintsGetAll() ) .thenReturn( Iterators.iterator( uniquenessConstraint, existenceConstraint ) ); // when Iterator<ConstraintDescriptor> result = allStoreHolder.constraintsGetAll( ); Iterators.count( result ); // then assertThat( asList( result ), empty() ); order.verify( storageReader ).constraintsGetAll(); order.verify( locks ).acquireShared( LockTracer.NONE, ResourceTypes.LABEL, labelId ); order.verify( locks ).acquireShared( LockTracer.NONE, ResourceTypes.RELATIONSHIP_TYPE, relTypeId ); }
@Test public void shouldGraphDatabaseServiceBeAccessible() { // Given the rule in the beginning of this class // When I run this test // Then assertEquals( 2, Iterators.count( neo4j.getGraphDatabaseService().execute( "MATCH (n:User) RETURN n" ) ) ); }
@Override public void visitNodePropertyChanges( long id, Iterator<StorageProperty> added, Iterator<StorageProperty> changed, IntIterable removed ) { propertiesChecked.setTrue(); assertEquals( 1, id ); assertFalse( changed.hasNext() ); assertTrue( removed.isEmpty() ); assertEquals( 1, Iterators.count( added, Predicates.alwaysTrue() ) ); } } );
@Test public void createdNodeShouldShowUpWithinTransaction() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); // WHEN Transaction tx = beansAPI.beginTx(); Node firstNode = createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); long sizeBeforeDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); firstNode.delete(); long sizeAfterDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); tx.close(); // THEN assertThat( sizeBeforeDelete, equalTo(1L) ); assertThat( sizeAfterDelete, equalTo(0L) ); }
@Test public void deletedNodeShouldShowUpWithinTransaction() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); Node firstNode = createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); // WHEN Transaction tx = beansAPI.beginTx(); long sizeBeforeDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); firstNode.delete(); long sizeAfterDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); tx.close(); // THEN assertThat( sizeBeforeDelete, equalTo(1L) ); assertThat( sizeAfterDelete, equalTo(0L) ); }
@Test public void createdNodeShouldShowUpInIndexQuery() { // GIVEN GraphDatabaseService beansAPI = dbRule.getGraphDatabaseAPI(); Neo4jMatchers.createIndex( beansAPI, LABEL1, "name" ); createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); // WHEN Transaction tx = beansAPI.beginTx(); long sizeBeforeDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); createNode( beansAPI, map( "name", "Mattias" ), LABEL1 ); long sizeAfterDelete = count( beansAPI.findNodes( LABEL1, "name", "Mattias" ) ); tx.close(); // THEN assertThat( sizeBeforeDelete, equalTo(1L) ); assertThat( sizeAfterDelete, equalTo(2L) ); }
@Test public void testMaxDepthAndCustomPruneEvaluatorCombined() { Evaluator lessThanThreeRels = path -> count( path.endNode().getRelationships( Direction.OUTGOING ).iterator() ) < 3 ? Evaluation.INCLUDE_AND_PRUNE : Evaluation.INCLUDE_AND_CONTINUE; TraversalDescription description = getGraphDb().traversalDescription().evaluator( Evaluators.all() ) .evaluator( toDepth( 1 ) ).evaluator( lessThanThreeRels ); Set<String> expectedNodes = new HashSet<>( asList( "a", "b", "c", "d", "e" ) ); try ( Transaction tx = beginTx() ) { for ( Path position : description.traverse( node( "a" ) ) ) { String name = (String) position.endNode().getProperty( "name" ); assertTrue( name + " shouldn't have been returned", expectedNodes.remove( name ) ); } tx.success(); } assertTrue( expectedNodes.isEmpty() ); } }
@Test public void lookupWithinTransaction() { try ( Transaction tx = db.beginTx() ) { // when db.createNode( label( "Node" ) ).setProperty( "prop", store ); // then assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); // no need to actually commit this node } }
@Test public void lookupWithinTransactionWithCacheEviction() { try ( Transaction tx = db.beginTx() ) { // when db.createNode( label( "Node" ) ).setProperty( "prop", store ); // then assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); // no need to actually commit this node } }
@Test( timeout = TEST_TIMEOUT ) public void indexCreationDoNotBlockWritesOnOtherLabel() throws ExecutionException, InterruptedException { Label markerLabel = Label.label( "testLabel3" ); Label nodesLabel = Label.label( "testLabel4" ); try ( Transaction transaction = database.beginTx() ) { database.schema().indexFor( markerLabel ).on( "testProperty" ).create(); Future<?> creation = executorService.submit( createNodeWithLabel( nodesLabel ) ); creation.get(); transaction.success(); } try ( Transaction transaction = database.beginTx() ) { try ( ResourceIterator<Node> nodes = database.findNodes( nodesLabel ) ) { assertEquals( 1, Iterators.count( nodes ) ); } } }
@Test public void concurrentSchemaRuleAdd() throws Throwable { SchemaCache cache = newSchemaCache(); Race race = new Race(); int indexNumber = 10; for ( int i = 0; i < indexNumber; i++ ) { int id = i; race.addContestant( () -> cache.addSchemaRule( newIndexRule( id, id, id ) ) ); } race.go(); assertEquals( indexNumber, Iterables.count( cache.indexDescriptors() ) ); for ( int labelId = 0; labelId < indexNumber; labelId++ ) { assertEquals( 1, Iterators.count( cache.indexDescriptorsForLabel( labelId ) ) ); } for ( int propertyId = 0; propertyId < indexNumber; propertyId++ ) { assertEquals( 1, Iterators.count( cache.indexesByProperty( propertyId ) ) ); } }
@Test public void shouldNotIndexNodesWithWrongLabel() throws Exception { // Given File file = new File( dbRule.getDatabaseDirAbsolutePath() ); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); inserter.createNode( map("name", "Bob"), label( "User" ), label("Admin")); inserter.createDeferredSchemaIndex( label( "Banana" ) ).on( "name" ).create(); // When inserter.shutdown(); // Then GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { assertThat( count( db.findNodes( label( "Banana" ), "name", "Bob" ) ), equalTo( 0L ) ); } finally { db.shutdown(); } }
@Test public void shouldNotSeeDeletedRelationshipWhenQueryingWithStartAndEndNode() { // GIVEN RelationshipIndex index = relationshipIndex( EXACT_CONFIG ); Node start = graphDb.createNode(); Node end = graphDb.createNode(); RelationshipType type = withName( "REL" ); Relationship rel = start.createRelationshipTo( end, type ); index.add( rel, "Type", type.name() ); finishTx( true ); beginTx(); // WHEN IndexHits<Relationship> hits = index.get( "Type", type.name(), start, end ); assertEquals( 1, count( hits ) ); assertEquals( 1, hits.size() ); index.remove( rel ); // THEN hits = index.get( "Type", type.name(), start, end ); assertEquals( 0, count( hits ) ); assertEquals( 0, hits.size() ); }
@Test public void lookupWithoutTransaction() { // when Node node; try ( Transaction tx = db.beginTx() ) { (node = db.createNode( label( "Node" ) )).setProperty( "prop", store ); tx.success(); } // then try ( Transaction tx = db.beginTx() ) { assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); tx.success(); } deleteNode( node ); }
@Test public void lookupWithoutTransactionWithCacheEviction() { // when Node node; try ( Transaction tx = db.beginTx() ) { (node = db.createNode( label( "Node" ) )).setProperty( "prop", store ); tx.success(); } // then try ( Transaction tx = db.beginTx() ) { assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); tx.success(); } deleteNode( node ); } }
@Test( expected = IllegalArgumentException.class ) public void bothSidesMustHaveSameUniqueness() { createGraph( "A TO B" ); Traverser traverse = getGraphDb().bidirectionalTraversalDescription() .startSide( getGraphDb().traversalDescription().uniqueness( Uniqueness.NODE_GLOBAL ) ) .endSide( getGraphDb().traversalDescription().uniqueness( Uniqueness.RELATIONSHIP_GLOBAL ) ) .traverse( getNodeWithName( "A" ), getNodeWithName( "B" ) ); try ( ResourceIterator<Path> iterator = traverse.iterator() ) { Iterators.count( iterator ); } }
@Test void updateMultiplePartitionedIndex() throws IOException { try ( SchemaIndex index = LuceneSchemaIndexBuilder.create( descriptor, config ) .withFileSystem( fileSystem ) .withIndexRootFolder( testDir.directory( "partitionedIndexForUpdates" ) ) .build() ) { index.create(); index.open(); addDocumentToIndex( index, 45 ); index.getIndexWriter().updateDocument( LuceneDocumentStructure.newTermForChangeOrRemove( 100 ), LuceneDocumentStructure.documentRepresentingProperties( (long) 100, Values.intValue( 100 ) ) ); index.maybeRefreshBlocking(); long documentsInIndex = Iterators.count( index.allDocumentsReader().iterator() ); assertEquals( 46, documentsInIndex, "Index should contain 45 added and 1 updated document." ); } }