Search in sources :

Example 91 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class RoutingResultFormat method parseRows.

private static Map<Role, List<SocketAddress>> parseRows(ListValue rows) {
    Map<Role, List<SocketAddress>> endpoints = new HashMap<>();
    for (AnyValue single : rows) {
        MapValue row = (MapValue) single;
        Role role = Role.valueOf(((TextValue) row.get("role")).stringValue());
        List<SocketAddress> addresses = parseEndpoints((ListValue) row.get("addresses"));
        endpoints.put(role, addresses);
    }
    Arrays.stream(Role.values()).forEach(r -> endpoints.putIfAbsent(r, Collections.emptyList()));
    return endpoints;
}
Also used : HashMap(java.util.HashMap) AnyValue(org.neo4j.values.AnyValue) ArrayList(java.util.ArrayList) List(java.util.List) MapValue(org.neo4j.values.virtual.MapValue) SocketAddress(org.neo4j.configuration.helpers.SocketAddress)

Example 92 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class BoltConnectionIT method shouldHandleFailureDuringResultPublishing.

@Test
void shouldHandleFailureDuringResultPublishing() throws Throwable {
    // Given
    var machine = newStateMachineAfterAuth();
    var pullAllCallbackCalled = new CountDownLatch(1);
    var error = new AtomicReference<Neo4jError>();
    // When something fails while publishing the result stream
    machine.process(run(), nullResponseHandler());
    machine.process(pullAll(), new BoltResponseHandler() {

        @Override
        public boolean onPullRecords(BoltResult result, long size) throws Throwable {
            throw new RuntimeException("Ooopsies!");
        }

        @Override
        public boolean onDiscardRecords(BoltResult result, long size) throws Throwable {
            throw new RuntimeException("Not this one!");
        }

        @Override
        public void onMetadata(String key, AnyValue value) {
        }

        @Override
        public void markFailed(Neo4jError err) {
            error.set(err);
            pullAllCallbackCalled.countDown();
        }

        @Override
        public void markIgnored() {
        }

        @Override
        public void onFinish() {
        }
    });
    // Then
    assertTrue(pullAllCallbackCalled.await(30, TimeUnit.SECONDS));
    var err = error.get();
    assertThat(err.status()).isEqualTo(Status.General.UnknownError);
    assertThat(err.message()).contains("Ooopsies!");
}
Also used : Neo4jError(org.neo4j.bolt.runtime.Neo4jError) AnyValue(org.neo4j.values.AnyValue) BoltResponseHandler(org.neo4j.bolt.runtime.BoltResponseHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BoltResult(org.neo4j.bolt.runtime.BoltResult) Test(org.junit.jupiter.api.Test)

Example 93 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class PathValueBuilderTest method builder.

private static PathValueBuilder builder(AnyValue... values) {
    DbAccess dbAccess = mock(DbAccess.class);
    RelationshipScanCursor cursors = mock(RelationshipScanCursor.class);
    Map<Long, RelationshipValue> relMap = new HashMap<>();
    for (AnyValue value : values) {
        if (value instanceof NodeValue) {
            NodeValue nodeValue = (NodeValue) value;
            when(dbAccess.nodeById(nodeValue.id())).thenReturn(nodeValue);
        } else if (value instanceof RelationshipValue) {
            RelationshipValue relationshipValue = (RelationshipValue) value;
            relMap.put(relationshipValue.id(), relationshipValue);
        } else {
            throw new AssertionError("invalid input");
        }
        Mockito.doAnswer((Answer<Void>) invocationOnMock -> {
            long id = invocationOnMock.getArgument(0);
            RelationshipScanCursor cursor = invocationOnMock.getArgument(1);
            RelationshipValue rel = relMap.get(id);
            when(cursor.sourceNodeReference()).thenReturn(rel.startNode().id());
            when(cursor.targetNodeReference()).thenReturn(rel.endNode().id());
            return null;
        }).when(dbAccess).singleRelationship(anyLong(), any(RelationshipScanCursor.class));
    }
    return new PathValueBuilder(dbAccess, cursors);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EMPTY_MAP(org.neo4j.values.virtual.VirtualValues.EMPTY_MAP) AnyValue(org.neo4j.values.AnyValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) NO_VALUE(org.neo4j.values.storable.Values.NO_VALUE) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) NodeValue(org.neo4j.values.virtual.NodeValue) HashMap(java.util.HashMap) Mockito.when(org.mockito.Mockito.when) Values.stringValue(org.neo4j.values.storable.Values.stringValue) VirtualValues.list(org.neo4j.values.virtual.VirtualValues.list) EMPTY_LIST(org.neo4j.values.virtual.VirtualValues.EMPTY_LIST) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) EMPTY_TEXT_ARRAY(org.neo4j.values.storable.Values.EMPTY_TEXT_ARRAY) DbAccess(org.neo4j.cypher.internal.runtime.DbAccess) Map(java.util.Map) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) PathValue(org.neo4j.values.virtual.PathValue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) VirtualValues(org.neo4j.values.virtual.VirtualValues) Mockito.mock(org.mockito.Mockito.mock) NodeValue(org.neo4j.values.virtual.NodeValue) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) HashMap(java.util.HashMap) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AnyValue(org.neo4j.values.AnyValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) DbAccess(org.neo4j.cypher.internal.runtime.DbAccess)

Example 94 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class PathValueBuilder method addMultipleUndirected.

/**
 * Adds multiple undirected relationships to the path
 *
 * @param relationships the undirected relationships to add
 * @param target the final target node of the path
 */
@CalledFromGeneratedCode
public void addMultipleUndirected(ListValue relationships, NodeValue target) {
    if (relationships.isEmpty()) {
        // nothing to add
        return;
    }
    long previous = nodes.get(nodes.size() - 1).id();
    RelationshipValue first = (RelationshipValue) relationships.head();
    boolean correctDirection = startNode(first, dbAccess, cursor).id() == previous || endNode(first, dbAccess, cursor).id() == previous;
    int i;
    if (correctDirection) {
        for (i = 0; i < relationships.size() - 1; i++) {
            AnyValue value = relationships.value(i);
            if (notNoValue(value)) {
                // we know these relationships have already loaded start and end relationship
                // so we should not use CypherFunctions::[start,end]Node to look them up
                addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) value);
            }
        }
    } else {
        for (i = relationships.size() - 1; i > 0; i--) {
            AnyValue value = relationships.value(i);
            if (notNoValue(value)) {
                // we know these relationships have already loaded start and end relationship
                // so we should not use CypherFunctions::[start,end]Node to look them up
                addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) relationships.value(i));
            }
        }
    }
    AnyValue last = relationships.value(i);
    if (notNoValue(last)) {
        rels.add((RelationshipValue) last);
        nodes.add(target);
    }
}
Also used : AnyValue(org.neo4j.values.AnyValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) CalledFromGeneratedCode(org.neo4j.util.CalledFromGeneratedCode)

Example 95 with AnyValue

use of org.neo4j.values.AnyValue in project neo4j by neo4j.

the class PathValueBuilder method addMultipleUndirected.

/**
 * Adds multiple undirected relationships to the path
 *
 * @param relationships the undirected relationships to add
 */
@CalledFromGeneratedCode
public void addMultipleUndirected(ListValue relationships) {
    if (relationships.isEmpty()) {
        // nothing to add
        return;
    }
    long previous = nodes.get(nodes.size() - 1).id();
    RelationshipValue first = (RelationshipValue) relationships.head();
    boolean correctDirection = startNode(first, dbAccess, cursor).id() == previous || endNode(first, dbAccess, cursor).id() == previous;
    if (correctDirection) {
        for (AnyValue value : relationships) {
            if (notNoValue(value)) {
                addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) value);
            }
        }
    } else {
        ListValue reversed = relationships.reverse();
        for (AnyValue rel : reversed) {
            if (notNoValue(rel)) {
                addUndirectedWhenRelationshipsAreFullyLoaded((RelationshipValue) rel);
            }
        }
    }
}
Also used : ListValue(org.neo4j.values.virtual.ListValue) AnyValue(org.neo4j.values.AnyValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) CalledFromGeneratedCode(org.neo4j.util.CalledFromGeneratedCode)

Aggregations

AnyValue (org.neo4j.values.AnyValue)95 Test (org.junit.jupiter.api.Test)58 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)19 ListValue (org.neo4j.values.virtual.ListValue)14 CallableUserFunction (org.neo4j.kernel.api.procedure.CallableUserFunction)11 RelationshipValue (org.neo4j.values.virtual.RelationshipValue)11 CallableProcedure (org.neo4j.kernel.api.procedure.CallableProcedure)10 List (java.util.List)9 TextValue (org.neo4j.values.storable.TextValue)9 RawIterator (org.neo4j.collection.RawIterator)8 MapValue (org.neo4j.values.virtual.MapValue)8 Context (org.neo4j.kernel.api.procedure.Context)7 KernelIntegrationTest (org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)7 ArrayList (java.util.ArrayList)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Values.stringValue (org.neo4j.values.storable.Values.stringValue)6 LocalDate (java.time.LocalDate)5 LocalTime (java.time.LocalTime)5 ZonedDateTime (java.time.ZonedDateTime)5 Arrays (java.util.Arrays)5