Search in sources :

Example 1 with Bits

use of org.neo4j.kernel.impl.util.Bits in project neo4j by neo4j.

the class TestArrayStore method assertBitPackedArrayGetsCorrectlySerializedAndDeserialized.

private void assertBitPackedArrayGetsCorrectlySerializedAndDeserialized(Object array, PropertyType type, int expectedBitsUsedPerItem) {
    Collection<DynamicRecord> records = storeArray(array);
    Pair<byte[], byte[]> asBytes = loadArray(records);
    assertArrayHeader(asBytes.first(), type, expectedBitsUsedPerItem);
    Bits bits = Bits.bitsFromBytes(asBytes.other());
    int length = Array.getLength(array);
    for (int i = 0; i < length; i++) {
        assertEquals(((Number) Array.get(array, i)).longValue(), bits.getLong(expectedBitsUsedPerItem));
    }
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) Bits(org.neo4j.kernel.impl.util.Bits)

Example 2 with Bits

use of org.neo4j.kernel.impl.util.Bits 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)

Example 3 with Bits

use of org.neo4j.kernel.impl.util.Bits in project neo4j by neo4j.

the class NodeStore method readOwnerFromDynamicLabelsRecord.

public static Long readOwnerFromDynamicLabelsRecord(DynamicRecord record) {
    byte[] data = record.getData();
    byte[] header = PropertyType.ARRAY.readDynamicRecordHeader(data);
    byte[] array = Arrays.copyOfRange(data, header.length, data.length);
    int requiredBits = header[2];
    if (requiredBits == 0) {
        return null;
    }
    Bits bits = Bits.bitsFromBytes(array);
    return bits.getLong(requiredBits);
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Example 4 with Bits

use of org.neo4j.kernel.impl.util.Bits in project neo4j by neo4j.

the class MetaDataStore method versionLongToString.

public static String versionLongToString(long storeVersion) {
    if (storeVersion == -1) {
        return CommonAbstractStore.UNKNOWN_VERSION;
    }
    Bits bits = Bits.bitsFromLongs(new long[] { storeVersion });
    int length = bits.getShort(8);
    if (length == 0 || length > 7) {
        throw new IllegalArgumentException(format("The read version string length %d is not proper.", length));
    }
    char[] result = new char[length];
    for (int i = 0; i < length; i++) {
        result[i] = (char) bits.getShort(8);
    }
    return new String(result);
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Example 5 with Bits

use of org.neo4j.kernel.impl.util.Bits in project neo4j by neo4j.

the class StorePropertyPayloadCursor method shortArrayValue.

Object shortArrayValue() {
    assertOfType(SHORT_ARRAY);
    Bits bits = valueAsBits();
    return ShortArray.decode(bits);
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Aggregations

Bits (org.neo4j.kernel.impl.util.Bits)15 ByteBuffer (java.nio.ByteBuffer)2 LongerShortString (org.neo4j.kernel.impl.store.LongerShortString)1 ShortArray (org.neo4j.kernel.impl.store.ShortArray)1 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)1