Search in sources :

Example 36 with AnyValue

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

the class ListValue method iterationAsArray.

private AnyValue[] iterationAsArray() {
    List<AnyValue> values = new ArrayList<>();
    int size = 0;
    for (AnyValue value : this) {
        values.add(value);
        size++;
    }
    return values.toArray(new AnyValue[size]);
}
Also used : AnyValue(org.neo4j.values.AnyValue) ArrayList(java.util.ArrayList)

Example 37 with AnyValue

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

the class ListValue method distinct.

public ListValue distinct() {
    long keptValuesHeapSize = 0;
    Set<AnyValue> seen = new HashSet<>();
    List<AnyValue> kept = new ArrayList<>();
    for (AnyValue value : this) {
        if (seen.add(value)) {
            kept.add(value);
            keptValuesHeapSize += value.estimatedHeapUsage();
        }
    }
    return new JavaListListValue(kept, keptValuesHeapSize);
}
Also used : AnyValue(org.neo4j.values.AnyValue) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 38 with AnyValue

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

the class BoltResponseMessageTest 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"), VirtualValues.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 39 with AnyValue

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

the class Neo4jPackV1Test method shouldPackUtf8.

@Test
void shouldPackUtf8() throws IOException {
    // Given
    String value = "\uD83D\uDE31";
    byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
    TextValue textValue = utf8Value(bytes, 0, bytes.length);
    PackedOutputArray output = new PackedOutputArray();
    Neo4jPack.Packer packer = neo4jPack.newPacker(output);
    packer.pack(textValue);
    // When
    AnyValue unpacked = unpacked(output.bytes());
    assertThat(unpacked).isInstanceOf(UTF8StringValue.class);
    // Then
    assertThat(unpacked).isEqualTo(textValue);
}
Also used : TextValue(org.neo4j.values.storable.TextValue) AnyValue(org.neo4j.values.AnyValue) Test(org.junit.jupiter.api.Test)

Example 40 with AnyValue

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

the class ValueUtils method of.

/**
 * Creates an AnyValue by doing type inspection. Do not use in production code where performance is important.
 *
 * @param object the object to turned into a AnyValue
 * @return the AnyValue corresponding to object.
 */
@SuppressWarnings("unchecked")
public static AnyValue of(Object object) {
    if (object instanceof AnyValue) {
        return (AnyValue) object;
    }
    Value value = Values.unsafeOf(object, true);
    if (value != null) {
        return value;
    } else {
        if (object instanceof Entity) {
            if (object instanceof Node) {
                return fromNodeEntity((Node) object);
            } else if (object instanceof Relationship) {
                return fromRelationshipEntity((Relationship) object);
            } else {
                throw new IllegalArgumentException("Unknown entity + " + object.getClass().getName());
            }
        } else if (object instanceof Iterable<?>) {
            if (object instanceof Path) {
                return fromPath((Path) object);
            } else if (object instanceof List<?>) {
                return asListValue((List<Object>) object);
            } else {
                return asListValue((Iterable<Object>) object);
            }
        } else if (object instanceof Map<?, ?>) {
            return asMapValue((Map<String, Object>) object);
        } else if (object instanceof Iterator<?>) {
            ListValueBuilder builder = ListValueBuilder.newListBuilder();
            Iterator<?> iterator = (Iterator<?>) object;
            while (iterator.hasNext()) {
                builder.add(ValueUtils.of(iterator.next()));
            }
            return builder.build();
        } else if (object instanceof Object[]) {
            Object[] array = (Object[]) object;
            if (array.length == 0) {
                return VirtualValues.EMPTY_LIST;
            }
            ListValueBuilder builder = ListValueBuilder.newListBuilder(array.length);
            for (Object o : array) {
                builder.add(ValueUtils.of(o));
            }
            return builder.build();
        } else if (object instanceof Stream<?>) {
            return asListValue(((Stream<Object>) object).collect(Collectors.toList()));
        } else if (object instanceof Geometry) {
            return asGeometryValue((Geometry) object);
        } else {
            ClassLoader classLoader = object.getClass().getClassLoader();
            throw new IllegalArgumentException(String.format("Cannot convert %s of type %s to AnyValue, classloader=%s, classloader-name=%s", object, object.getClass().getName(), classLoader != null ? classLoader.toString() : "null", classLoader != null ? classLoader.getName() : "null"));
        }
    }
}
Also used : Path(org.neo4j.graphdb.Path) Entity(org.neo4j.graphdb.Entity) Node(org.neo4j.graphdb.Node) Geometry(org.neo4j.graphdb.spatial.Geometry) Relationship(org.neo4j.graphdb.Relationship) AnyValue(org.neo4j.values.AnyValue) AnyValue(org.neo4j.values.AnyValue) DoubleValue(org.neo4j.values.storable.DoubleValue) NodeValue(org.neo4j.values.virtual.NodeValue) Value(org.neo4j.values.storable.Value) MapValue(org.neo4j.values.virtual.MapValue) LongValue(org.neo4j.values.storable.LongValue) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) BooleanValue(org.neo4j.values.storable.BooleanValue) IntValue(org.neo4j.values.storable.IntValue) ListValue(org.neo4j.values.virtual.ListValue) TextValue(org.neo4j.values.storable.TextValue) PointValue(org.neo4j.values.storable.PointValue) PathValue(org.neo4j.values.virtual.PathValue) Iterator(java.util.Iterator) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) 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