Search in sources :

Example 86 with AnyValue

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

the class DateTimeValue method truncate.

public static DateTimeValue truncate(TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone) {
    Pair<LocalDate, LocalTime> pair = getTruncatedDateAndTime(unit, input, "date time");
    LocalDate truncatedDate = pair.first();
    LocalTime truncatedTime = pair.other();
    ZoneId zoneId = input.supportsTimeZone() ? input.getZoneId(defaultZone) : defaultZone.get();
    ZonedDateTime truncatedZDT = ZonedDateTime.of(truncatedDate, truncatedTime, zoneId);
    if (fields.size() == 0) {
        return datetime(truncatedZDT);
    } else {
        // Timezone needs some special handling, since the builder will shift keeping the instant instead of the local time
        AnyValue timezone = fields.get("timezone");
        if (timezone != NO_VALUE) {
            truncatedZDT = truncatedZDT.withZoneSameLocal(timezoneOf(timezone));
        }
        return updateFieldMapWithConflictingSubseconds(fields, unit, truncatedZDT, (mapValue, zonedDateTime) -> {
            if (mapValue.size() == 0) {
                return datetime(zonedDateTime);
            } else {
                return build(mapValue.updatedWith("datetime", datetime(zonedDateTime)), defaultZone);
            }
        });
    }
}
Also used : LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) ZonedDateTime(java.time.ZonedDateTime) AnyValue(org.neo4j.values.AnyValue) LocalDate(java.time.LocalDate)

Example 87 with AnyValue

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

the class DateTimeValue method builder.

static DateTimeBuilder<DateTimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new DateTimeBuilder<>(defaultZone) {

        @Override
        protected boolean supportsTimeZone() {
            return true;
        }

        @Override
        protected boolean supportsEpoch() {
            return true;
        }

        private final ZonedDateTime defaultZonedDateTime = ZonedDateTime.of(TemporalFields.year.defaultValue, TemporalFields.month.defaultValue, TemporalFields.day.defaultValue, TemporalFields.hour.defaultValue, TemporalFields.minute.defaultValue, TemporalFields.second.defaultValue, TemporalFields.nanosecond.defaultValue, timezone());

        @Override
        public DateTimeValue buildInternal() {
            boolean selectingDate = fields.containsKey(TemporalFields.date);
            boolean selectingTime = fields.containsKey(TemporalFields.time);
            boolean selectingDateTime = fields.containsKey(TemporalFields.datetime);
            boolean selectingEpoch = fields.containsKey(TemporalFields.epochSeconds) || fields.containsKey(TemporalFields.epochMillis);
            boolean selectingTimeZone;
            ZonedDateTime result;
            if (selectingDateTime) {
                AnyValue dtField = fields.get(TemporalFields.datetime);
                if (!(dtField instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", dtField));
                }
                TemporalValue dt = (TemporalValue) dtField;
                LocalTime timePart = dt.getTimePart(defaultZone).toLocalTime();
                ZoneId zoneId = dt.getZoneId(defaultZone);
                result = ZonedDateTime.of(dt.getDatePart(), timePart, zoneId);
                selectingTimeZone = dt.supportsTimeZone();
            } else if (selectingEpoch) {
                if (fields.containsKey(TemporalFields.epochSeconds)) {
                    AnyValue epochField = fields.get(TemporalFields.epochSeconds);
                    if (!(epochField instanceof IntegralValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
                    }
                    IntegralValue epochSeconds = (IntegralValue) epochField;
                    result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochSeconds.longValue() * 1000), timezone()));
                } else {
                    AnyValue epochField = fields.get(TemporalFields.epochMillis);
                    if (!(epochField instanceof IntegralValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date time from: %s", epochField));
                    }
                    IntegralValue epochMillis = (IntegralValue) epochField;
                    result = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis.longValue()), timezone()));
                }
                selectingTimeZone = false;
            } else if (selectingTime || selectingDate) {
                LocalTime time;
                ZoneId zoneId;
                if (selectingTime) {
                    AnyValue timeField = fields.get(TemporalFields.time);
                    if (!(timeField instanceof TemporalValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct time from: %s", timeField));
                    }
                    TemporalValue t = (TemporalValue) timeField;
                    time = t.getTimePart(defaultZone).toLocalTime();
                    zoneId = t.getZoneId(defaultZone);
                    selectingTimeZone = t.supportsTimeZone();
                } else {
                    time = LocalTimeValue.DEFAULT_LOCAL_TIME;
                    zoneId = timezone();
                    selectingTimeZone = false;
                }
                LocalDate date;
                if (selectingDate) {
                    AnyValue dateField = fields.get(TemporalFields.date);
                    if (!(dateField instanceof TemporalValue)) {
                        throw new InvalidArgumentException(String.format("Cannot construct date from: %s", dateField));
                    }
                    TemporalValue t = (TemporalValue) dateField;
                    date = t.getDatePart();
                } else {
                    date = DateValue.DEFAULT_CALENDER_DATE;
                }
                result = ZonedDateTime.of(date, time, zoneId);
            } else {
                result = defaultZonedDateTime;
                selectingTimeZone = false;
            }
            if (fields.containsKey(TemporalFields.week) && !selectingDate && !selectingDateTime && !selectingEpoch) {
                // Be sure to be in the start of the week based year (which can be later than 1st Jan)
                result = result.with(IsoFields.WEEK_BASED_YEAR, safeCastIntegral(TemporalFields.year.name(), fields.get(TemporalFields.year), TemporalFields.year.defaultValue)).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
            }
            result = assignAllFields(result);
            if (timezone != null) {
                if (((selectingTime || selectingDateTime) && selectingTimeZone) || selectingEpoch) {
                    try {
                        result = result.withZoneSameInstant(timezone());
                    } catch (DateTimeParseException e) {
                        throw new TemporalParseException(e.getMessage(), e.getParsedString(), e.getErrorIndex(), e);
                    }
                } else {
                    result = result.withZoneSameLocal(timezone());
                }
            }
            return datetime(result);
        }

        @Override
        protected DateTimeValue selectDateTime(AnyValue datetime) {
            if (datetime instanceof DateTimeValue) {
                DateTimeValue value = (DateTimeValue) datetime;
                ZoneId zone = optionalTimezone();
                return zone == null ? value : new DateTimeValue(ZonedDateTime.of(value.temporal().toLocalDateTime(), zone));
            }
            if (datetime instanceof LocalDateTimeValue) {
                return new DateTimeValue(ZonedDateTime.of(((LocalDateTimeValue) datetime).temporal(), timezone()));
            }
            throw new UnsupportedTemporalUnitException("Cannot select datetime from: " + datetime);
        }
    };
}
Also used : LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) UnsupportedTemporalUnitException(org.neo4j.exceptions.UnsupportedTemporalUnitException) LocalDate(java.time.LocalDate) TemporalParseException(org.neo4j.exceptions.TemporalParseException) DateTimeParseException(java.time.format.DateTimeParseException) InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZonedDateTime(java.time.ZonedDateTime) AnyValue(org.neo4j.values.AnyValue)

Example 88 with AnyValue

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

the class BoltRequestMessageV3Test method shouldSerializeRelationship.

@Test
void shouldSerializeRelationship() throws Throwable {
    RelationshipValue rel = relationshipValue(12L, nodeValue(1L, stringArray(), VirtualValues.EMPTY_MAP), nodeValue(2L, stringArray(), VirtualValues.EMPTY_MAP), stringValue("KNOWS"), map(new String[] { "name", "age" }, new AnyValue[] { stringValue("Bob"), intValue(14) }));
    assertThat(serialized(rel)).isEqualTo("B1 71 91 B5 52 0C 01 02 85 4B 4E 4F 57 53 A2 84" + lineSeparator() + "6E 61 6D 65 83 42 6F 62 83 61 67 65 0E");
}
Also used : AnyValue(org.neo4j.values.AnyValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) Test(org.junit.jupiter.api.Test)

Example 89 with AnyValue

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

the class GenericIndexKeyValidator method worstCaseLength.

private static int worstCaseLength(AnyValue value) {
    if (value.isSequenceValue()) {
        SequenceValue sequenceValue = (SequenceValue) value;
        if (sequenceValue instanceof TextArray) {
            TextArray textArray = (TextArray) sequenceValue;
            int length = 0;
            for (int i = 0; i < textArray.length(); i++) {
                length += stringWorstCaseLength(textArray.stringValue(i).length());
            }
            return length;
        }
        return sequenceValue.length() * BIGGEST_STATIC_SIZE;
    } else {
        if (((Value) value).valueGroup().category() == ValueCategory.TEXT) {
            // For text, which is very dynamic in its nature do a worst-case off of number of characters in it
            return stringWorstCaseLength(((TextValue) value).length());
        }
        // For all else then use the biggest possible value for a non-dynamic, non-array value a state can occupy
        return BIGGEST_STATIC_SIZE;
    }
}
Also used : SequenceValue(org.neo4j.values.SequenceValue) AnyValue(org.neo4j.values.AnyValue) SequenceValue(org.neo4j.values.SequenceValue) Value(org.neo4j.values.storable.Value) TextValue(org.neo4j.values.storable.TextValue) TextArray(org.neo4j.values.storable.TextArray)

Example 90 with AnyValue

use of org.neo4j.values.AnyValue 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

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