Search in sources :

Example 1 with ShortArray

use of org.neo4j.kernel.impl.store.ShortArray in project neo4j by neo4j.

the class StorePropertyPayloadCursor method readArrayFromBuffer.

private static Object readArrayFromBuffer(ByteBuffer buffer) {
    if (buffer.limit() <= 0) {
        throw new IllegalStateException("Given buffer is empty");
    }
    byte typeId = buffer.get();
    buffer.order(ByteOrder.BIG_ENDIAN);
    try {
        if (typeId == PropertyType.STRING.intValue()) {
            int arrayLength = buffer.getInt();
            String[] result = new String[arrayLength];
            for (int i = 0; i < arrayLength; i++) {
                int byteLength = buffer.getInt();
                result[i] = UTF8.decode(buffer.array(), buffer.position(), byteLength);
                buffer.position(buffer.position() + byteLength);
            }
            return result;
        } else {
            ShortArray type = ShortArray.typeOf(typeId);
            int bitsUsedInLastByte = buffer.get();
            int requiredBits = buffer.get();
            if (requiredBits == 0) {
                return type.createEmptyArray();
            }
            Object result;
            if (type == ShortArray.BYTE && requiredBits == Byte.SIZE) {
                // Optimization for byte arrays (probably large ones)
                byte[] byteArray = new byte[buffer.limit() - buffer.position()];
                buffer.get(byteArray);
                result = byteArray;
            } else {
                // Fallback to the generic approach, which is a slower
                Bits bits = Bits.bitsFromBytes(buffer.array(), buffer.position());
                int length = ((buffer.limit() - buffer.position()) * 8 - (8 - bitsUsedInLastByte)) / requiredBits;
                result = type.createArray(length, bits, requiredBits);
            }
            return result;
        }
    } finally {
        buffer.order(ByteOrder.LITTLE_ENDIAN);
    }
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits) LongerShortString(org.neo4j.kernel.impl.store.LongerShortString) ShortArray(org.neo4j.kernel.impl.store.ShortArray)

Aggregations

LongerShortString (org.neo4j.kernel.impl.store.LongerShortString)1 ShortArray (org.neo4j.kernel.impl.store.ShortArray)1 Bits (org.neo4j.kernel.impl.util.Bits)1