Search in sources :

Example 1 with LongValue

use of org.neo4j.values.storable.LongValue 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 2 with LongValue

use of org.neo4j.values.storable.LongValue in project neo4j by neo4j.

the class HeapTrackingListValueBuilderTest method streamAndCollectElements.

@Test
void streamAndCollectElements() {
    int iterations = rnd.nextInt(10, 1000);
    ArrayList<LongValue> list = new ArrayList<>(iterations);
    for (int i = 0; i < iterations; i++) {
        list.add(Values.longValue(i));
    }
    var collector = HeapTrackingListValueBuilder.collector(memoryTracker);
    ListValue listValue = list.stream().collect(collector);
    // Validate value size
    long memoryTrackerActualSize = meter.measureDeep(memoryTracker);
    long actualValueSize = meter.measureDeep(listValue) - memoryTrackerActualSize;
    long estimatedValueSize = listValue.estimatedHeapUsage();
    assertEquals(actualValueSize, estimatedValueSize);
    // Validate items
    Iterator<AnyValue> iterator = listValue.iterator();
    for (int i = 0; i < iterations; i++) {
        assertTrue(iterator.hasNext());
        assertEquals(i, ((LongValue) iterator.next()).longValue());
    }
    assertFalse(iterator.hasNext());
}
Also used : ListValue(org.neo4j.values.virtual.ListValue) LongValue(org.neo4j.values.storable.LongValue) ArrayList(java.util.ArrayList) AnyValue(org.neo4j.values.AnyValue) Test(org.junit.jupiter.api.Test)

Example 3 with LongValue

use of org.neo4j.values.storable.LongValue in project neo4j by neo4j.

the class PrimitiveOnlyValueWriterTest method shouldConvertLongValueToLong.

@Test
void shouldConvertLongValueToLong() {
    PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
    LongValue value = longValue(42);
    assertEquals(42L, writer.valueAsObject(value));
}
Also used : LongValue(org.neo4j.values.storable.LongValue) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with LongValue

use of org.neo4j.values.storable.LongValue in project neo4j by neo4j.

the class PrimitiveOnlyValueWriterTest method shouldConvertMultipleValues.

@Test
void shouldConvertMultipleValues() {
    PrimitiveOnlyValueWriter writer = new PrimitiveOnlyValueWriter();
    TextValue value1 = stringValue("Hello");
    TextValue value2 = stringValue(" ");
    TextValue value3 = stringValue("World!");
    LongValue value4 = longValue(42);
    assertEquals("Hello", writer.valueAsObject(value1));
    assertEquals(" ", writer.valueAsObject(value2));
    assertEquals("World!", writer.valueAsObject(value3));
    assertEquals(42L, writer.valueAsObject(value4));
}
Also used : TextValue(org.neo4j.values.storable.TextValue) LongValue(org.neo4j.values.storable.LongValue) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with LongValue

use of org.neo4j.values.storable.LongValue in project neo4j by neo4j.

the class RoutingResultFormat method build.

public static AnyValue[] build(RoutingResult result) {
    ListValue routers = asValues(result.routeEndpoints());
    ListValue readers = asValues(result.readEndpoints());
    ListValue writers = asValues(result.writeEndpoints());
    ListValueBuilder servers = ListValueBuilder.newListBuilder();
    if (writers.size() > 0) {
        MapValueBuilder builder = new MapValueBuilder();
        builder.add(ROLE_KEY, WRTE_NAME);
        builder.add(ADDRESSES_KEY, writers);
        servers.add(builder.build());
    }
    if (readers.size() > 0) {
        MapValueBuilder builder = new MapValueBuilder();
        builder.add(ROLE_KEY, READ_NAME);
        builder.add(ADDRESSES_KEY, readers);
        servers.add(builder.build());
    }
    if (routers.size() > 0) {
        MapValueBuilder builder = new MapValueBuilder();
        builder.add(ROLE_KEY, ROUTE_NAME);
        builder.add(ADDRESSES_KEY, routers);
        servers.add(builder.build());
    }
    LongValue timeToLiveSeconds = longValue(MILLISECONDS.toSeconds(result.ttlMillis()));
    return new AnyValue[] { timeToLiveSeconds, servers.build() };
}
Also used : MapValueBuilder(org.neo4j.values.virtual.MapValueBuilder) ListValue(org.neo4j.values.virtual.ListValue) LongValue(org.neo4j.values.storable.LongValue) AnyValue(org.neo4j.values.AnyValue) ListValueBuilder(org.neo4j.values.virtual.ListValueBuilder)

Aggregations

LongValue (org.neo4j.values.storable.LongValue)6 ListValue (org.neo4j.values.virtual.ListValue)4 Test (org.junit.jupiter.api.Test)3 AnyValue (org.neo4j.values.AnyValue)3 ArrayList (java.util.ArrayList)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 Expression (org.neo4j.codegen.Expression)1 Entity (org.neo4j.graphdb.Entity)1 Node (org.neo4j.graphdb.Node)1 Path (org.neo4j.graphdb.Path)1 Context (org.neo4j.kernel.api.procedure.Context)1 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)1 ValueUtils (org.neo4j.kernel.impl.util.ValueUtils)1 DateTimeValue (org.neo4j.values.storable.DateTimeValue)1 DateValue (org.neo4j.values.storable.DateValue)1 DoubleValue (org.neo4j.values.storable.DoubleValue)1