Search in sources :

Example 61 with AnyValue

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

the class TimeValue method builder.

private static TimeBuilder<TimeValue> builder(Supplier<ZoneId> defaultZone) {
    return new TimeBuilder<>(defaultZone) {

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

        @Override
        public TimeValue buildInternal() {
            boolean selectingTime = fields.containsKey(TemporalFields.time);
            boolean selectingTimeZone;
            OffsetTime result;
            if (selectingTime) {
                AnyValue time = fields.get(TemporalFields.time);
                if (!(time instanceof TemporalValue)) {
                    throw new InvalidArgumentException(String.format("Cannot construct time from: %s", time));
                }
                TemporalValue t = (TemporalValue) time;
                result = t.getTimePart(defaultZone);
                selectingTimeZone = t.supportsTimeZone();
            } else {
                ZoneId timezone = timezone();
                if (!(timezone instanceof ZoneOffset)) {
                    timezone = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                }
                result = defaultTime(timezone);
                selectingTimeZone = false;
            }
            result = assignAllFields(result);
            if (timezone != null) {
                ZoneOffset currentOffset = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                if (selectingTime && selectingTimeZone) {
                    result = result.withOffsetSameInstant(currentOffset);
                } else {
                    result = result.withOffsetSameLocal(currentOffset);
                }
            }
            return time(result);
        }

        @Override
        protected TimeValue selectTime(AnyValue temporal) {
            if (!(temporal instanceof TemporalValue)) {
                throw new InvalidArgumentException(String.format("Cannot construct time from: %s", temporal));
            }
            if (temporal instanceof TimeValue && timezone == null) {
                return (TimeValue) temporal;
            }
            TemporalValue v = (TemporalValue) temporal;
            OffsetTime time = v.getTimePart(defaultZone);
            if (timezone != null) {
                ZoneOffset currentOffset = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezone())).getOffset();
                time = time.withOffsetSameInstant(currentOffset);
            }
            return time(time);
        }
    };
}
Also used : InvalidArgumentException(org.neo4j.exceptions.InvalidArgumentException) ZoneId(java.time.ZoneId) OffsetTime(java.time.OffsetTime) AnyValue(org.neo4j.values.AnyValue) ZoneOffset(java.time.ZoneOffset)

Example 62 with AnyValue

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

the class TimeValue method truncate.

public static TimeValue truncate(TemporalUnit unit, TemporalValue input, MapValue fields, Supplier<ZoneId> defaultZone) {
    OffsetTime time = input.getTimePart(defaultZone);
    OffsetTime truncatedOT = assertValidUnit(() -> time.truncatedTo(unit));
    if (fields.size() == 0) {
        return time(truncatedOT);
    } 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) {
            ZonedDateTime currentDT = assertValidArgument(() -> ZonedDateTime.ofInstant(Instant.now(), timezoneOf(timezone)));
            ZoneOffset currentOffset = currentDT.getOffset();
            truncatedOT = truncatedOT.withOffsetSameLocal(currentOffset);
        }
        return updateFieldMapWithConflictingSubseconds(fields, unit, truncatedOT, (mapValue, offsetTime) -> {
            if (mapValue.size() == 0) {
                return time(offsetTime);
            } else {
                return build(mapValue.updatedWith("time", time(offsetTime)), defaultZone);
            }
        });
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) OffsetTime(java.time.OffsetTime) AnyValue(org.neo4j.values.AnyValue) ZoneOffset(java.time.ZoneOffset)

Example 63 with AnyValue

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

the class DbIndexesFailureMessageIT method listAllIndexesWithFailedIndex.

@Test
void listAllIndexesWithFailedIndex() throws Throwable {
    // Given
    KernelTransaction dataTransaction = newTransaction(AUTH_DISABLED);
    String labelName = "Fail";
    String propertyKey = "foo";
    int failedLabel = dataTransaction.tokenWrite().labelGetOrCreateForName(labelName);
    int propertyKeyId1 = dataTransaction.tokenWrite().propertyKeyGetOrCreateForName(propertyKey);
    this.transaction.createNode(Label.label(labelName)).setProperty(propertyKey, "some value");
    commit();
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    LabelSchemaDescriptor schema = forLabel(failedLabel, propertyKeyId1);
    IndexDescriptor index = transaction.schemaWrite().indexCreate(schema, "fail foo index");
    commit();
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        assertThrows(IllegalStateException.class, () -> tx.schema().awaitIndexesOnline(2, MINUTES));
    }
    // When
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "indexDetails")).id(), new TextValue[] { stringValue(index.getName()) }, ProcedureCallContext.EMPTY);
    assertTrue(stream.hasNext());
    AnyValue[] result = stream.next();
    assertFalse(stream.hasNext());
    // Commit procedure transaction
    commit();
    // Then
    assertEquals(longValue(index.getId()), result[0]);
    assertEquals(stringValue("fail foo index"), result[1]);
    assertEquals(stringValue("FAILED"), result[2]);
    assertEquals(doubleValue(0.0), result[3]);
    assertEquals(stringValue("NONUNIQUE"), result[4]);
    assertEquals(stringValue("BTREE"), result[5]);
    assertEquals(stringValue("NODE"), result[6]);
    assertEquals(VirtualValues.list(stringValue(labelName)), result[7]);
    assertEquals(VirtualValues.list(stringValue(propertyKey)), result[8]);
    assertEquals(stringValue(NATIVE_BTREE10.providerName()), result[9]);
    assertMapsEqual(index.getIndexConfig().asMap(), (MapValue) result[10]);
    assertThat(((TextValue) result[11]).stringValue()).contains("java.lang.RuntimeException: Fail on update during population");
    assertEquals(12, result.length);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TextValue(org.neo4j.values.storable.TextValue) AnyValue(org.neo4j.values.AnyValue) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) Test(org.junit.jupiter.api.Test)

Example 64 with AnyValue

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

the class HeapTrackingListValueBuilderTest method addAndIterateElements.

@Test
void addAndIterateElements() {
    int iterations = rnd.nextInt(10, 1000);
    for (int i = 0; i < iterations; i++) {
        listValueBuilder.add(Values.longValue(i));
    }
    // Validate builder size
    long memoryTrackerActualSize = meter.measureDeep(memoryTracker);
    long actualBuilderSize = meter.measureDeep(listValueBuilder) - memoryTrackerActualSize;
    long estimatedBuilderSize = memoryTracker.estimatedHeapMemory();
    assertEquals(actualBuilderSize, estimatedBuilderSize);
    // Validate value size
    ListValue listValue = listValueBuilder.build();
    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) AnyValue(org.neo4j.values.AnyValue) Test(org.junit.jupiter.api.Test)

Example 65 with AnyValue

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

the class UserFunctionTest method shouldGiveHelpfulErrorOnNullMessageException.

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

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