Search in sources :

Example 6 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class GraphDatabaseFacade method allNodesWithLabel.

private ResourceIterator<Node> allNodesWithLabel(final Label myLabel) {
    Statement statement = spi.currentStatement();
    int labelId = statement.readOperations().labelGetForName(myLabel.name());
    if (labelId == KeyReadOperations.NO_SUCH_LABEL) {
        statement.close();
        return emptyIterator();
    }
    final PrimitiveLongIterator nodeIds = statement.readOperations().nodesGetForLabel(labelId);
    return ResourceClosingIterator.newResourceIterator(statement, map(nodeId -> new NodeProxy(nodeActions, nodeId), nodeIds));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) InternalIndexState(org.neo4j.kernel.api.index.InternalIndexState) StoreId(org.neo4j.kernel.impl.store.StoreId) RELATIONSHIP_AUTO_INDEX(org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.RELATIONSHIP_AUTO_INDEX) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Suppliers(org.neo4j.function.Suppliers) ResourceClosingIterator(org.neo4j.helpers.collection.ResourceClosingIterator) URL(java.net.URL) ResourceIterable(org.neo4j.graphdb.ResourceIterable) Status(org.neo4j.kernel.api.exceptions.Status) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) Statement(org.neo4j.kernel.api.Statement) Property(org.neo4j.kernel.api.properties.Property) RelationshipProxy(org.neo4j.kernel.impl.core.RelationshipProxy) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) RelationshipAutoIndexerFacade(org.neo4j.kernel.impl.coreapi.RelationshipAutoIndexerFacade) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) NODE_AUTO_INDEX(org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.NODE_AUTO_INDEX) AutoIndexerFacade(org.neo4j.kernel.impl.coreapi.AutoIndexerFacade) Map(java.util.Map) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) Transaction(org.neo4j.graphdb.Transaction) TransactionalContextFactory(org.neo4j.kernel.impl.query.TransactionalContextFactory) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) PrefetchingResourceIterator(org.neo4j.helpers.collection.PrefetchingResourceIterator) KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) BidirectionalTraversalDescriptionImpl(org.neo4j.kernel.impl.traversal.BidirectionalTraversalDescriptionImpl) Result(org.neo4j.graphdb.Result) URLAccessValidationError(org.neo4j.graphdb.security.URLAccessValidationError) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) IndexProviderImpl(org.neo4j.kernel.impl.coreapi.IndexProviderImpl) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) StandardRelationshipActions(org.neo4j.kernel.impl.coreapi.StandardRelationshipActions) String.format(java.lang.String.format) Schema(org.neo4j.graphdb.schema.Schema) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) NO_SUCH_LABEL(org.neo4j.kernel.impl.api.operations.KeyReadOperations.NO_SUCH_LABEL) TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) IndexManager(org.neo4j.graphdb.index.IndexManager) Neo4jTransactionalContextFactory(org.neo4j.kernel.impl.query.Neo4jTransactionalContextFactory) Optional(java.util.Optional) DependencyResolver(org.neo4j.graphdb.DependencyResolver) RelationshipType(org.neo4j.graphdb.RelationshipType) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Guard(org.neo4j.kernel.guard.Guard) Label(org.neo4j.graphdb.Label) PlaceboTransaction(org.neo4j.kernel.impl.coreapi.PlaceboTransaction) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) SchemaImpl(org.neo4j.kernel.impl.coreapi.schema.SchemaImpl) EntityType(org.neo4j.storageengine.api.EntityType) StandardNodeActions(org.neo4j.kernel.impl.coreapi.StandardNodeActions) SchemaKernelException(org.neo4j.kernel.api.exceptions.schema.SchemaKernelException) MonoDirectionalTraversalDescription(org.neo4j.kernel.impl.traversal.MonoDirectionalTraversalDescription) NotFoundException(org.neo4j.graphdb.NotFoundException) Supplier(java.util.function.Supplier) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) Node(org.neo4j.graphdb.Node) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) IndexManagerImpl(org.neo4j.kernel.impl.coreapi.IndexManagerImpl) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReadOnlyRelationshipIndexFacade(org.neo4j.kernel.impl.coreapi.ReadOnlyRelationshipIndexFacade) ThrowingAction(org.neo4j.function.ThrowingAction) NO_SUCH_PROPERTY_KEY(org.neo4j.kernel.impl.api.operations.KeyReadOperations.NO_SUCH_PROPERTY_KEY) TokenAccess(org.neo4j.kernel.impl.api.TokenAccess) AutoIndexing(org.neo4j.kernel.api.legacyindex.AutoIndexing) Config(org.neo4j.kernel.configuration.Config) ReadOnlyIndexFacade(org.neo4j.kernel.impl.coreapi.ReadOnlyIndexFacade) PrimitiveLongCollections(org.neo4j.collection.primitive.PrimitiveLongCollections) ReadOperations(org.neo4j.kernel.api.ReadOperations) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) PrimitiveLongCollections.map(org.neo4j.collection.primitive.PrimitiveLongCollections.map) Iterators.emptyIterator(org.neo4j.helpers.collection.Iterators.emptyIterator) NodeProxy(org.neo4j.kernel.impl.core.NodeProxy) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) File(java.io.File) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) TimeUnit(java.util.concurrent.TimeUnit) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) AUTH_DISABLED(org.neo4j.kernel.api.security.SecurityContext.AUTH_DISABLED) KeyReadOperations(org.neo4j.kernel.impl.api.operations.KeyReadOperations) MultipleFoundException(org.neo4j.graphdb.MultipleFoundException) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ClientConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) Collections(java.util.Collections) Statement(org.neo4j.kernel.api.Statement) NodeProxy(org.neo4j.kernel.impl.core.NodeProxy)

Example 7 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class NeoStoreFileListing method gatherLegacyIndexFiles.

private Resource gatherLegacyIndexFiles(Collection<StoreFileMetadata> files) throws IOException {
    final Collection<ResourceIterator<File>> snapshots = new ArrayList<>();
    for (IndexImplementation indexProvider : legacyIndexProviders.all()) {
        ResourceIterator<File> snapshot = indexProvider.listStoreFiles();
        snapshots.add(snapshot);
        snapshot.stream().map(toNotAStoreTypeFile).collect(Collectors.toCollection(() -> files));
    }
    // the targetFiles list.
    return new MultiResource(snapshots);
}
Also used : IndexImplementation(org.neo4j.kernel.spi.legacyindex.IndexImplementation) ArrayList(java.util.ArrayList) ResourceIterator(org.neo4j.graphdb.ResourceIterator) File(java.io.File)

Example 8 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project elasticsearch-river-neo4j by sksamuel.

the class SimpleIndexingStrategyTest method thatNodePropertiesAreUsedAsFieldValues.

@Test
public void thatNodePropertiesAreUsedAsFieldValues() throws IOException {
    long id = new Random().nextInt(50000) + 1;
    Node node = mock(Node.class);
    when(node.getId()).thenReturn(id);
    when(node.getPropertyKeys()).thenReturn(Arrays.asList("name", "location", "band"));
    when(node.getProperty("name")).thenReturn("chris martin");
    when(node.getProperty("location")).thenReturn("hampstead");
    when(node.getProperty("band")).thenReturn("coldplay");
    when(node.hasLabel(DynamicLabel.label("User"))).thenReturn(true);
    when(node.getLabels()).thenReturn(new ResourceIterable<Label>() {

        @Override
        public ResourceIterator<Label> iterator() {
            return new ResourceIterator<Label>() {

                private List<Label> labels = Arrays.asList(DynamicLabel.label("User"), DynamicLabel.label("Swedish"));

                private int position = 0;

                @Override
                public void close() {
                }

                @Override
                public boolean hasNext() {
                    boolean result = false;
                    if (position < labels.size()) {
                        result = true;
                    }
                    return result;
                }

                @Override
                public Label next() {
                    Label label = labels.get(position);
                    position++;
                    return label;
                }

                @Override
                public void remove() {
                }
            };
        }
    });
    IndexRequest req = s.build("neo4j-index", "node", node, 12);
    assertEquals("neo4j-index", req.index());
    assertEquals("node", req.type());
    assertEquals(12, req.sourceAsMap().get("version"));
    assertEquals(String.valueOf(id), req.id());
    assertEquals(new ArrayList<String>() {

        {
            add("User");
            add("Swedish");
        }
    }, req.sourceAsMap().get("labels"));
    assertEquals("coldplay", req.sourceAsMap().get("band"));
    assertEquals("hampstead", req.sourceAsMap().get("location"));
    assertEquals("chris martin", req.sourceAsMap().get("name"));
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) DynamicLabel(org.neo4j.graphdb.DynamicLabel) IndexRequest(org.elasticsearch.action.index.IndexRequest) Random(java.util.Random) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Test(org.junit.Test)

Example 9 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class AbstractLuceneIndex method snapshot.

/**
     * Snapshot of all file in all index partitions.
     *
     * @return iterator over all index files.
     * @throws IOException
     * @see WritableIndexSnapshotFileIterator
     */
public ResourceIterator<File> snapshot() throws IOException {
    ensureOpen();
    List<ResourceIterator<File>> snapshotIterators = null;
    try {
        List<AbstractIndexPartition> partitions = getPartitions();
        snapshotIterators = new ArrayList<>(partitions.size());
        for (AbstractIndexPartition partition : partitions) {
            snapshotIterators.add(partition.snapshot());
        }
        return Iterators.concatResourceIterators(snapshotIterators.iterator());
    } catch (Exception e) {
        if (snapshotIterators != null) {
            try {
                IOUtils.closeAll(snapshotIterators);
            } catch (IOException ex) {
                throw Exceptions.withCause(ex, e);
            }
        }
        throw e;
    }
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ResourceIterator(org.neo4j.graphdb.ResourceIterator) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) AbstractIndexPartition(org.neo4j.kernel.api.impl.index.partition.AbstractIndexPartition)

Example 10 with ResourceIterator

use of org.neo4j.graphdb.ResourceIterator in project neo4j by neo4j.

the class CommunityLockAcquisitionTimeoutIT method timeoutOnAcquiringSharedLock.

@Test(timeout = TEST_TIMEOUT)
public void timeoutOnAcquiringSharedLock() throws Exception {
    expectedException.expect(new RootCauseMatcher<>(LockAcquisitionTimeoutException.class, "The transaction has been terminated. " + "Retry your operation in a new transaction, and you should see a successful result. " + "Unable to acquire lock within configured timeout. " + "Unable to acquire lock for resource: SCHEMA with id: 0 within 2000 millis."));
    try (Transaction ignored = database.beginTx()) {
        database.schema().indexFor(marker).on(TEST_PROPERTY_NAME).create();
        Future<Void> propertySetFuture = secondTransactionExecutor.executeDontWait(state -> {
            try (Transaction nestedTransaction = database.beginTx()) {
                ResourceIterator<Node> nodes = database.findNodes(marker);
                Node node = nodes.next();
                node.addLabel(Label.label("anotherLabel"));
                nestedTransaction.success();
            }
            return null;
        });
        secondTransactionExecutor.waitUntilWaiting(sharedLockWaitingPredicate());
        clockExecutor.execute((OtherThreadExecutor.WorkerCommand<Void, Void>) state -> {
            fakeClock.forward(3, TimeUnit.SECONDS);
            return null;
        });
        propertySetFuture.get();
        fail("Should throw termination exception.");
    }
}
Also used : ResourceIterator(org.neo4j.graphdb.ResourceIterator) GraphDatabaseFactoryState(org.neo4j.graphdb.factory.GraphDatabaseFactoryState) Label(org.neo4j.graphdb.Label) BeforeClass(org.junit.BeforeClass) SystemNanoClock(org.neo4j.time.SystemNanoClock) RootCauseMatcher(org.neo4j.test.mockito.matcher.RootCauseMatcher) CommunityEditionModule(org.neo4j.kernel.impl.factory.CommunityEditionModule) GraphDatabaseFacadeFactory(org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) GraphDatabaseDependencies(org.neo4j.kernel.GraphDatabaseDependencies) GraphDatabaseBuilder(org.neo4j.graphdb.factory.GraphDatabaseBuilder) Future(java.util.concurrent.Future) LockAcquisitionTimeoutException(org.neo4j.kernel.impl.locking.LockAcquisitionTimeoutException) FakeClock(org.neo4j.time.FakeClock) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) After(org.junit.After) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) Transaction(org.neo4j.graphdb.Transaction) ExpectedException(org.junit.rules.ExpectedException) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) AfterClass(org.junit.AfterClass) Config(org.neo4j.kernel.configuration.Config) DatabaseInfo(org.neo4j.kernel.impl.factory.DatabaseInfo) Predicate(java.util.function.Predicate) TestDirectory(org.neo4j.test.rule.TestDirectory) Test(org.junit.Test) CommunityLockClient(org.neo4j.kernel.impl.locking.community.CommunityLockClient) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) PlatformModule(org.neo4j.kernel.impl.factory.PlatformModule) Clock(java.time.Clock) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Clocks(org.neo4j.time.Clocks) LockAcquisitionTimeoutException(org.neo4j.kernel.impl.locking.LockAcquisitionTimeoutException) Transaction(org.neo4j.graphdb.Transaction) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Aggregations

ResourceIterator (org.neo4j.graphdb.ResourceIterator)11 File (java.io.File)5 Test (org.junit.Test)5 Map (java.util.Map)4 Label (org.neo4j.graphdb.Label)4 Node (org.neo4j.graphdb.Node)4 ArrayList (java.util.ArrayList)3 TimeUnit (java.util.concurrent.TimeUnit)3 Transaction (org.neo4j.graphdb.Transaction)3 GraphDatabaseSettings (org.neo4j.graphdb.factory.GraphDatabaseSettings)3 Config (org.neo4j.kernel.configuration.Config)3 GraphDatabaseFacade (org.neo4j.kernel.impl.factory.GraphDatabaseFacade)3 Clock (java.time.Clock)2 Future (java.util.concurrent.Future)2 Predicate (java.util.function.Predicate)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 After (org.junit.After)2 AfterClass (org.junit.AfterClass)2 Assert.fail (org.junit.Assert.fail)2 BeforeClass (org.junit.BeforeClass)2