Search in sources :

Example 1 with SequenceValue

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

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

the class CypherFunctions method last.

@CalledFromGeneratedCode
public static AnyValue last(AnyValue container) {
    assert container != NO_VALUE : "NO_VALUE checks need to happen outside this call";
    if (container instanceof SequenceValue) {
        SequenceValue sequence = (SequenceValue) container;
        int length = sequence.length();
        if (length == 0) {
            return NO_VALUE;
        }
        return sequence.value(length - 1);
    } else {
        throw new CypherTypeException(format("Invalid input for function 'last()': Expected %s to be a list", container));
    }
}
Also used : SequenceValue(org.neo4j.values.SequenceValue) CypherTypeException(org.neo4j.exceptions.CypherTypeException) CalledFromGeneratedCode(org.neo4j.util.CalledFromGeneratedCode)

Example 3 with SequenceValue

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

Aggregations

SequenceValue (org.neo4j.values.SequenceValue)3 AnyValue (org.neo4j.values.AnyValue)2 CypherTypeException (org.neo4j.exceptions.CypherTypeException)1 Point (org.neo4j.graphdb.spatial.Point)1 CalledFromGeneratedCode (org.neo4j.util.CalledFromGeneratedCode)1 ByteArray (org.neo4j.values.storable.ByteArray)1 TextArray (org.neo4j.values.storable.TextArray)1 TextValue (org.neo4j.values.storable.TextValue)1 Value (org.neo4j.values.storable.Value)1