Search in sources :

Example 1 with Context

use of org.neo4j.kernel.api.proc.Context in project neo4j by neo4j.

the class UserFunctionsTest method shouldMakeContextAvailable.

@Test
public void shouldMakeContextAvailable() throws Throwable {
    // Given
    Key<String> someKey = key("someKey", String.class);
    procs.register(new CallableUserFunction.BasicUserFunction(signature) {

        @Override
        public Object apply(Context ctx, Object[] input) throws ProcedureException {
            return ctx.get(someKey);
        }
    });
    BasicContext ctx = new BasicContext();
    ctx.put(someKey, "hello, world");
    // When
    Object result = procs.callFunction(ctx, signature.name(), new Object[0]);
    // Then
    assertThat(result, equalTo("hello, world"));
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) Context(org.neo4j.kernel.api.proc.Context) CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) BasicContext(org.neo4j.kernel.api.proc.BasicContext) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Test(org.junit.Test)

Example 2 with Context

use of org.neo4j.kernel.api.proc.Context in project neo4j by neo4j.

the class ProceduresTest method shouldMakeContextAvailable.

@Test
public void shouldMakeContextAvailable() throws Throwable {
    // Given
    Key<String> someKey = key("someKey", String.class);
    procs.register(new CallableProcedure.BasicProcedure(signature) {

        @Override
        public RawIterator<Object[], ProcedureException> apply(Context ctx, Object[] input) throws ProcedureException {
            return RawIterator.<Object[], ProcedureException>of(new Object[] { ctx.get(someKey) });
        }
    });
    BasicContext ctx = new BasicContext();
    ctx.put(someKey, "hello, world");
    // When
    RawIterator<Object[], ProcedureException> result = procs.callProcedure(ctx, signature.name(), new Object[0]);
    // Then
    assertThat(asList(result), contains(equalTo(new Object[] { "hello, world" })));
}
Also used : BasicContext(org.neo4j.kernel.api.proc.BasicContext) Context(org.neo4j.kernel.api.proc.Context) BasicContext(org.neo4j.kernel.api.proc.BasicContext) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) NTString(org.neo4j.kernel.api.proc.Neo4jTypes.NTString) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) RawIterator(org.neo4j.collection.RawIterator) Test(org.junit.Test)

Example 3 with Context

use of org.neo4j.kernel.api.proc.Context 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)

Aggregations

ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)3 Context (org.neo4j.kernel.api.proc.Context)3 Test (org.junit.Test)2 RawIterator (org.neo4j.collection.RawIterator)2 BasicContext (org.neo4j.kernel.api.proc.BasicContext)2 CallableProcedure (org.neo4j.kernel.api.proc.CallableProcedure)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Comparator.comparing (java.util.Comparator.comparing)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)1 NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)1 ClientConnectorAddresses (org.neo4j.causalclustering.discovery.ClientConnectorAddresses)1 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)1 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)1 ReadReplicaInfo (org.neo4j.causalclustering.discovery.ReadReplicaInfo)1