Search in sources :

Example 26 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class MyCoreAPI method makeNode.

public long makeNode(String label) throws ProcedureException {
    long result;
    try (Transaction tx = graph.beginTransaction(KernelTransaction.Type.explicit, AnonymousContext.write())) {
        Statement statement = this.txBridge.get();
        long nodeId = statement.dataWriteOperations().nodeCreate();
        int labelId = statement.tokenWriteOperations().labelGetOrCreateForName(label);
        statement.dataWriteOperations().nodeAddLabel(nodeId, labelId);
        result = nodeId;
        tx.success();
    } catch (Exception e) {
        log.error("Failed to create node: " + e.getMessage());
        throw new ProcedureException(Status.Procedure.ProcedureCallFailed, "Failed to create node: " + e.getMessage(), e);
    }
    return result;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Transaction(org.neo4j.graphdb.Transaction) Statement(org.neo4j.kernel.api.Statement) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Example 27 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ClusterOverviewProcedure method apply.

@Override
public RawIterator<Object[], ProcedureException> apply(Context ctx, Object[] input) throws ProcedureException {
    List<ReadWriteEndPoint> endpoints = new ArrayList<>();
    CoreTopology coreTopology = topologyService.coreServers();
    Set<MemberId> coreMembers = coreTopology.members().keySet();
    MemberId leader = null;
    try {
        leader = leaderLocator.getLeader();
    } catch (NoLeaderFoundException e) {
        log.debug("No write server found. This can happen during a leader switch.");
    }
    for (MemberId memberId : coreMembers) {
        Optional<CoreServerInfo> coreServerInfo = coreTopology.find(memberId);
        if (coreServerInfo.isPresent()) {
            Role role = memberId.equals(leader) ? Role.LEADER : Role.FOLLOWER;
            endpoints.add(new ReadWriteEndPoint(coreServerInfo.get().connectors(), role, memberId.getUuid(), asList(coreServerInfo.get().groups())));
        } else {
            log.debug("No Address found for " + memberId);
        }
    }
    for (Map.Entry<MemberId, ReadReplicaInfo> readReplica : topologyService.readReplicas().members().entrySet()) {
        ReadReplicaInfo readReplicaInfo = readReplica.getValue();
        endpoints.add(new ReadWriteEndPoint(readReplicaInfo.connectors(), Role.READ_REPLICA, readReplica.getKey().getUuid(), asList(readReplicaInfo.groups())));
    }
    endpoints.sort(comparing(o -> o.addresses().toString()));
    return map((endpoint) -> new Object[] { endpoint.memberId().toString(), endpoint.addresses().uriList().stream().map(URI::toString).toArray(), endpoint.role().name(), endpoint.groups() }, asRawIterator(endpoints.iterator()));
}
Also used : ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Neo4jTypes(org.neo4j.kernel.api.proc.Neo4jTypes) Log(org.neo4j.logging.Log) RawIterator(org.neo4j.collection.RawIterator) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ClientConnectorAddresses(org.neo4j.causalclustering.discovery.ClientConnectorAddresses) LogProvider(org.neo4j.logging.LogProvider) ArrayList(java.util.ArrayList) Iterators.asRawIterator(org.neo4j.helpers.collection.Iterators.asRawIterator) Map(java.util.Map) MemberId(org.neo4j.causalclustering.identity.MemberId) URI(java.net.URI) ReadReplicaInfo(org.neo4j.causalclustering.discovery.ReadReplicaInfo) Comparator.comparing(java.util.Comparator.comparing) ProcedureSignature.procedureSignature(org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Iterators.map(org.neo4j.helpers.collection.Iterators.map) TopologyService(org.neo4j.causalclustering.discovery.TopologyService) Set(java.util.Set) UUID(java.util.UUID) Iterables.asList(org.neo4j.helpers.collection.Iterables.asList) List(java.util.List) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) Optional(java.util.Optional) Context(org.neo4j.kernel.api.proc.Context) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) ReadReplicaInfo(org.neo4j.causalclustering.discovery.ReadReplicaInfo) ArrayList(java.util.ArrayList) URI(java.net.URI) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) Map(java.util.Map)

Example 28 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class ClusterOverviewIT method clusterOverview.

private List<MemberInfo> clusterOverview(GraphDatabaseFacade db) throws TransactionFailureException, ProcedureException {
    KernelAPI kernel = db.getDependencyResolver().resolveDependency(KernelAPI.class);
    KernelTransaction transaction = kernel.newTransaction(Type.implicit, AnonymousContext.read());
    List<MemberInfo> infos = new ArrayList<>();
    try (Statement statement = transaction.acquireStatement()) {
        RawIterator<Object[], ProcedureException> itr = statement.procedureCallOperations().procedureCallRead(procedureName("dbms", "cluster", ClusterOverviewProcedure.PROCEDURE_NAME), null);
        while (itr.hasNext()) {
            Object[] row = itr.next();
            Object[] addresses = (Object[]) row[1];
            infos.add(new MemberInfo(Arrays.copyOf(addresses, addresses.length, String[].class), Role.valueOf((String) row[2])));
        }
    }
    return infos;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) ArrayList(java.util.ArrayList) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) KernelAPI(org.neo4j.kernel.api.KernelAPI)

Example 29 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class BuiltInProcedures method listIndexes.

@Description("List all indexes in the database.")
@Procedure(name = "db.indexes", mode = READ)
public Stream<IndexResult> listIndexes() throws ProcedureException {
    try (Statement statement = tx.acquireStatement()) {
        ReadOperations operations = statement.readOperations();
        TokenNameLookup tokens = new StatementTokenNameLookup(operations);
        List<NewIndexDescriptor> indexes = asList(operations.indexesGetAll());
        Set<NewIndexDescriptor> uniqueIndexes = asSet(operations.uniqueIndexesGetAll());
        indexes.addAll(uniqueIndexes);
        indexes.sort(Comparator.comparing(a -> a.userDescription(tokens)));
        ArrayList<IndexResult> result = new ArrayList<>();
        for (NewIndexDescriptor index : indexes) {
            try {
                String type;
                if (uniqueIndexes.contains(index)) {
                    type = IndexType.NODE_UNIQUE_PROPERTY.typeName();
                } else {
                    type = IndexType.NODE_LABEL_PROPERTY.typeName();
                }
                result.add(new IndexResult("INDEX ON " + index.schema().userDescription(tokens), operations.indexGetState(index).toString(), type));
            } catch (IndexNotFoundKernelException e) {
                throw new ProcedureException(Status.Schema.IndexNotFound, e, "No index on ", index.userDescription(tokens));
            }
        }
        return result.stream();
    }
}
Also used : IndexingService(org.neo4j.kernel.impl.api.index.IndexingService) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Label(org.neo4j.graphdb.Label) Context(org.neo4j.procedure.Context) Status(org.neo4j.kernel.api.exceptions.Status) Iterators.asSet(org.neo4j.helpers.collection.Iterators.asSet) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ArrayList(java.util.ArrayList) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) Procedure(org.neo4j.procedure.Procedure) TokenAccess(org.neo4j.kernel.impl.api.TokenAccess) ReadOperations(org.neo4j.kernel.api.ReadOperations) Set(java.util.Set) READ(org.neo4j.procedure.Mode.READ) Description(org.neo4j.procedure.Description) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Iterators.asList(org.neo4j.helpers.collection.Iterators.asList) DependencyResolver(org.neo4j.graphdb.DependencyResolver) Name(org.neo4j.procedure.Name) RelationshipType(org.neo4j.graphdb.RelationshipType) Comparator(java.util.Comparator) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) ArrayList(java.util.ArrayList) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ReadOperations(org.neo4j.kernel.api.ReadOperations) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) TokenNameLookup(org.neo4j.kernel.api.TokenNameLookup) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 30 with ProcedureException

use of org.neo4j.kernel.api.exceptions.ProcedureException in project neo4j by neo4j.

the class IndexProcedures method resampleIndex.

public void resampleIndex(String indexSpecification) throws ProcedureException {
    IndexSpecifier index = parse(indexSpecification);
    int labelId = getLabelId(index.label());
    int propertyKeyId = getPropertyKeyId(index.property());
    //TODO: Support composite indexes
    try {
        triggerSampling(getIndex(labelId, propertyKeyId, index));
    } catch (IndexNotFoundKernelException e) {
        throw new ProcedureException(e.status(), e.getMessage(), e);
    }
}
Also used : IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException)

Aggregations

ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)49 Test (org.junit.Test)28 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)14 BasicContext (org.neo4j.kernel.api.proc.BasicContext)10 MethodHandle (java.lang.invoke.MethodHandle)8 ArrayList (java.util.ArrayList)8 CallableUserFunction (org.neo4j.kernel.api.proc.CallableUserFunction)8 FieldSignature (org.neo4j.kernel.api.proc.FieldSignature)8 QualifiedName (org.neo4j.kernel.api.proc.QualifiedName)8 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)7 UserFunctionSignature (org.neo4j.kernel.api.proc.UserFunctionSignature)7 RawIterator (org.neo4j.collection.RawIterator)6 Context (org.neo4j.kernel.api.proc.Context)6 Log (org.neo4j.logging.Log)6 List (java.util.List)5 Statement (org.neo4j.kernel.api.Statement)5 ProcedureSignature (org.neo4j.kernel.api.proc.ProcedureSignature)5 Procedure (org.neo4j.procedure.Procedure)5 Method (java.lang.reflect.Method)4 Comparator (java.util.Comparator)4