Search in sources :

Example 11 with AnyValue

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

the class UserAggregationFunctionTest method shouldGiveHelpfulErrorOnNullMessageException.

@Test
void shouldGiveHelpfulErrorOnNullMessageException() throws Throwable {
    // Given
    CallableUserAggregationFunction method = compile(FunctionThatThrowsNullMsgExceptionAtInvocation.class).get(0);
    ProcedureException exception = assertThrows(ProcedureException.class, () -> method.create(prepareContext()).update(new AnyValue[] {}));
    assertThat(exception.getMessage()).isEqualTo("Failed to invoke function `org.neo4j.procedure.impl.test`: Caused by: java.lang.IndexOutOfBoundsException");
}
Also used : CallableUserAggregationFunction(org.neo4j.kernel.api.procedure.CallableUserAggregationFunction) AnyValue(org.neo4j.values.AnyValue) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Example 12 with AnyValue

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

the class ProcedureCompilation method toByteArray.

/**
 * Byte arrays needs special treatment since it is not a proper Cypher type
 * @param input either a ByteArray or ListValue of bytes
 * @return input value converted to a byte[]
 */
public static byte[] toByteArray(AnyValue input) {
    if (input instanceof ByteArray) {
        return ((ByteArray) input).asObjectCopy();
    }
    if (input instanceof SequenceValue) {
        SequenceValue list = (SequenceValue) input;
        if (list.iterationPreference() == RANDOM_ACCESS) {
            byte[] bytes = new byte[list.length()];
            for (int a = 0; a < bytes.length; a++) {
                bytes[a] = asByte(list.value(a));
            }
            return bytes;
        } else {
            // list.length may have linear complexity, still worth doing it upfront
            byte[] bytes = new byte[list.length()];
            int i = 0;
            for (AnyValue anyValue : list) {
                bytes[i++] = asByte(anyValue);
            }
            return bytes;
        }
    } else {
        throw new IllegalArgumentException("Cannot convert " + input.getClass().getSimpleName() + " to byte[] for input to procedure");
    }
}
Also used : SequenceValue(org.neo4j.values.SequenceValue) AnyValue(org.neo4j.values.AnyValue) ByteArray(org.neo4j.values.storable.ByteArray) Point(org.neo4j.graphdb.spatial.Point)

Example 13 with AnyValue

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

the class ProcedureCompilation method toAnyValue.

/**
 * Takes an expression evaluating to one of the supported java values and turns
 * it into the corresponding AnyValue
 *
 * @param expression the expression to evaluate
 * @param userType the type of the expression to map
 * @return an expression properly mapped to AnyValue
 */
private static Expression toAnyValue(Expression expression, Class<?> userType, Expression context) {
    if (AnyValue.class.isAssignableFrom(userType)) {
        return nullCheck(expression, cast(userType, expression));
    }
    String type = userType.getCanonicalName();
    if (type.equals(LONG)) {
        return invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), expression);
    } else if (type.equals(BOXED_LONG)) {
        return nullCheck(expression, invoke(methodReference(Values.class, LongValue.class, "longValue", long.class), unbox(expression)));
    } else if (type.equals(DOUBLE)) {
        return invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), expression);
    } else if (type.equals(BOXED_DOUBLE)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DoubleValue.class, "doubleValue", double.class), unbox(expression)));
    } else if (type.equals(NUMBER)) {
        return nullCheck(expression, invoke(methodReference(Values.class, NumberValue.class, "numberValue", Number.class), expression));
    } else if (type.equals(BOOLEAN)) {
        return invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), expression);
    } else if (type.equals(BOXED_BOOLEAN)) {
        return nullCheck(expression, invoke(methodReference(Values.class, BooleanValue.class, "booleanValue", boolean.class), unbox(expression)));
    } else if (type.equals(STRING)) {
        return invoke(methodReference(Values.class, Value.class, "stringOrNoValue", String.class), expression);
    } else if (type.equals(BYTE_ARRAY)) {
        return nullCheck(expression, invoke(methodReference(Values.class, ByteArray.class, "byteArray", byte[].class), expression));
    } else if (type.equals(LIST)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, ListValue.class, "asListValue", Iterable.class), expression));
    } else if (type.equals(MAP)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, MapValue.class, "asMapValue", Map.class), expression));
    } else if (type.equals(ZONED_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(DateTimeValue.class, DateTimeValue.class, "datetime", ZonedDateTime.class), expression));
    } else if (type.equals(OFFSET_TIME)) {
        return nullCheck(expression, invoke(methodReference(TimeValue.class, TimeValue.class, "time", OffsetTime.class), expression));
    } else if (type.equals(LOCAL_DATE)) {
        return nullCheck(expression, invoke(methodReference(DateValue.class, DateValue.class, "date", LocalDate.class), expression));
    } else if (type.equals(LOCAL_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalTimeValue.class, LocalTimeValue.class, "localTime", LocalTime.class), expression));
    } else if (type.equals(LOCAL_DATE_TIME)) {
        return nullCheck(expression, invoke(methodReference(LocalDateTimeValue.class, LocalDateTimeValue.class, "localDateTime", LocalDateTime.class), expression));
    } else if (type.equals(TEMPORAL_AMOUNT)) {
        return nullCheck(expression, invoke(methodReference(Values.class, DurationValue.class, "durationValue", TemporalAmount.class), expression));
    } else if (type.equals(NODE)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getNode = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, NodeValue.class, "fromNodeEntity", Node.class), getNode));
    } else if (type.equals(RELATIONSHIP)) {
        Expression internalTransaction = invoke(context, methodReference(Context.class, InternalTransaction.class, "internalTransactionOrNull"));
        Expression getRelationship = invoke(internalTransaction, methodReference(InternalTransaction.class, Entity.class, "validateSameDB", Entity.class), expression);
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, RelationshipValue.class, "fromRelationshipEntity", Relationship.class), getRelationship));
    } else if (type.equals(PATH)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PathValue.class, "fromPath", Path.class), expression));
    } else if (type.equals(POINT)) {
        return nullCheck(expression, invoke(methodReference(ValueUtils.class, PointValue.class, "asPointValue", Point.class), expression));
    } else {
        return invoke(methodReference(ValueUtils.class, AnyValue.class, "of", Object.class), expression);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Context(org.neo4j.kernel.api.procedure.Context) Path(org.neo4j.graphdb.Path) NodeValue(org.neo4j.values.virtual.NodeValue) Entity(org.neo4j.graphdb.Entity) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) DateTimeValue(org.neo4j.values.storable.DateTimeValue) PathValue(org.neo4j.values.virtual.PathValue) ListValue(org.neo4j.values.virtual.ListValue) Node(org.neo4j.graphdb.Node) Values(org.neo4j.values.storable.Values) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) LocalDate(java.time.LocalDate) LocalDateTimeValue(org.neo4j.values.storable.LocalDateTimeValue) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) DoubleValue(org.neo4j.values.storable.DoubleValue) ZonedDateTime(java.time.ZonedDateTime) Expression(org.neo4j.codegen.Expression) DateValue(org.neo4j.values.storable.DateValue) LongValue(org.neo4j.values.storable.LongValue) AnyValue(org.neo4j.values.AnyValue)

Example 14 with AnyValue

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

the class RoutingResultFormatTest method shouldSerializeToAndFromRecordFormat.

@Test
void shouldSerializeToAndFromRecordFormat() {
    // given
    List<SocketAddress> writers = asList(new SocketAddress("write", 1), new SocketAddress("write", 2), new SocketAddress("write", 3));
    List<SocketAddress> readers = asList(new SocketAddress("read", 4), new SocketAddress("read", 5), new SocketAddress("read", 6), new SocketAddress("read", 7));
    List<SocketAddress> routers = singletonList(new SocketAddress("route", 8));
    long ttlSeconds = 5;
    RoutingResult original = new RoutingResult(routers, writers, readers, ttlSeconds * 1000);
    // when
    AnyValue[] record = RoutingResultFormat.build(original);
    // then
    RoutingResult parsed = RoutingResultFormat.parse(record);
    assertEquals(original, parsed);
}
Also used : AnyValue(org.neo4j.values.AnyValue) SocketAddress(org.neo4j.configuration.helpers.SocketAddress) Test(org.junit.jupiter.api.Test)

Example 15 with AnyValue

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

the class BuiltInDbmsProceduresIT method listAllCapabilitiesShouldNotReturnBlocked.

@Test
void listAllCapabilitiesShouldNotReturnBlocked() throws KernelException {
    // set blocked capabilities
    Config config = dependencyResolver.resolveDependency(Config.class);
    config.set(CapabilitiesSettings.dbms_capabilities_blocked, List.of("my.custom.**"));
    QualifiedName procedureName = procedureName("dbms", "listAllCapabilities");
    int procedureId = procs().procedureGet(procedureName).id();
    RawIterator<AnyValue[], ProcedureException> callResult = procs().procedureCallDbms(procedureId, new AnyValue[] {}, ProcedureCallContext.EMPTY);
    List<AnyValue[]> capabilities = asList(callResult);
    List<String> capabilityNames = capabilities.stream().map(c -> ((TextValue) c[0]).stringValue()).collect(Collectors.toList());
    assertThat(capabilityNames).containsExactlyInAnyOrder(DBMSCapabilities.dbms_instance_version.name().fullName(), DBMSCapabilities.dbms_instance_kernel_version.name().fullName(), DBMSCapabilities.dbms_instance_edition.name().fullName(), DBMSCapabilities.dbms_instance_operational_mode.name().fullName(), TestCapabilities.my_dynamic_capability.name().fullName(), TestCapabilities.my_internal_capability.name().fullName());
}
Also used : AnyValue(org.neo4j.values.AnyValue) Arrays(java.util.Arrays) Label(org.neo4j.graphdb.Label) RawIterator(org.neo4j.collection.RawIterator) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) Iterators.asList(org.neo4j.internal.helpers.collection.Iterators.asList) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Config(org.neo4j.configuration.Config) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) ProcedureSignature.procedureName(org.neo4j.internal.kernel.api.procs.ProcedureSignature.procedureName) Public(org.neo4j.annotations.Public) Values(org.neo4j.values.storable.Values) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) CapabilitiesRegistry(org.neo4j.capabilities.CapabilitiesRegistry) Capability(org.neo4j.capabilities.Capability) CapabilitiesSettings(org.neo4j.capabilities.CapabilitiesSettings) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FALSE(org.neo4j.configuration.SettingValueParsers.FALSE) CapabilityProviderContext(org.neo4j.capabilities.CapabilityProviderContext) INTEGER(org.neo4j.capabilities.Type.INTEGER) DOUBLE(org.neo4j.capabilities.Type.DOUBLE) CapabilityProvider(org.neo4j.capabilities.CapabilityProvider) Name(org.neo4j.capabilities.Name) DBMSCapabilities(org.neo4j.capabilities.DBMSCapabilities) BooleanValue(org.neo4j.values.storable.BooleanValue) BOOLEAN(org.neo4j.capabilities.Type.BOOLEAN) TextValue(org.neo4j.values.storable.TextValue) Label.label(org.neo4j.graphdb.Label.label) Values.stringValue(org.neo4j.values.storable.Values.stringValue) Collectors(java.util.stream.Collectors) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) List(java.util.List) ProcedureCallContext(org.neo4j.internal.kernel.api.procs.ProcedureCallContext) ArrayUtils.toArray(org.apache.commons.lang3.ArrayUtils.toArray) Description(org.neo4j.configuration.Description) KernelException(org.neo4j.exceptions.KernelException) Matchers.contains(org.hamcrest.Matchers.contains) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CapabilityDeclaration(org.neo4j.capabilities.CapabilityDeclaration) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest) Config(org.neo4j.configuration.Config) TextValue(org.neo4j.values.storable.TextValue) QualifiedName(org.neo4j.internal.kernel.api.procs.QualifiedName) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test) KernelIntegrationTest(org.neo4j.kernel.impl.api.integrationtest.KernelIntegrationTest)

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