Search in sources :

Example 6 with Bits

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

the class StorePropertyPayloadCursor method valueAsBits.

private Bits valueAsBits() {
    Bits bits = Bits.bits(MAX_BYTES_IN_SHORT_STRING_OR_SHORT_ARRAY);
    int blocksUsed = currentBlocksUsed();
    for (int i = 0; i < blocksUsed; i++) {
        bits.put(data[position + i]);
    }
    return bits;
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Example 7 with Bits

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

the class DynamicArrayStore method getRightArray.

public static Object getRightArray(Pair<byte[], byte[]> data) {
    byte[] header = data.first();
    byte[] bArray = data.other();
    byte typeId = header[0];
    if (typeId == PropertyType.STRING.intValue()) {
        ByteBuffer headerBuffer = ByteBuffer.wrap(header, 1, /*skip the type*/
        header.length - 1);
        int arrayLength = headerBuffer.getInt();
        String[] result = new String[arrayLength];
        ByteBuffer dataBuffer = ByteBuffer.wrap(bArray);
        for (int i = 0; i < arrayLength; i++) {
            int byteLength = dataBuffer.getInt();
            byte[] stringByteArray = new byte[byteLength];
            dataBuffer.get(stringByteArray);
            result[i] = PropertyStore.decodeString(stringByteArray);
        }
        return result;
    } else {
        ShortArray type = ShortArray.typeOf(typeId);
        int bitsUsedInLastByte = header[1];
        int requiredBits = header[2];
        if (requiredBits == 0) {
            return type.createEmptyArray();
        }
        Object result;
        if (type == ShortArray.BYTE && requiredBits == Byte.SIZE) {
            // Optimization for byte arrays (probably large ones)
            result = bArray;
        } else {
            // Fallback to the generic approach, which is a slower
            Bits bits = Bits.bitsFromBytes(bArray);
            int length = (bArray.length * 8 - (8 - bitsUsedInLastByte)) / requiredBits;
            result = type.createArray(length, bits, requiredBits);
        }
        return result;
    }
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits) ByteBuffer(java.nio.ByteBuffer)

Example 8 with Bits

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

the class DynamicArrayStore method allocateFromNumbers.

public static void allocateFromNumbers(Collection<DynamicRecord> target, Object array, DynamicRecordAllocator recordAllocator) {
    Class<?> componentType = array.getClass().getComponentType();
    boolean isPrimitiveByteArray = componentType.equals(Byte.TYPE);
    boolean isByteArray = componentType.equals(Byte.class) || isPrimitiveByteArray;
    ShortArray type = ShortArray.typeOf(array);
    if (type == null) {
        throw new IllegalArgumentException(array + " not a valid array type.");
    }
    int arrayLength = Array.getLength(array);
    int requiredBits = isByteArray ? Byte.SIZE : type.calculateRequiredBitsForArray(array, arrayLength);
    int totalBits = requiredBits * arrayLength;
    int numberOfBytes = (totalBits - 1) / 8 + 1;
    int bitsUsedInLastByte = totalBits % 8;
    bitsUsedInLastByte = bitsUsedInLastByte == 0 ? 8 : bitsUsedInLastByte;
    // type + rest + requiredBits header. TODO no need to use full bytes
    numberOfBytes += NUMBER_HEADER_SIZE;
    byte[] bytes;
    if (isByteArray) {
        bytes = new byte[NUMBER_HEADER_SIZE + arrayLength];
        bytes[0] = (byte) type.intValue();
        bytes[1] = (byte) bitsUsedInLastByte;
        bytes[2] = (byte) requiredBits;
        if (isPrimitiveByteArray) {
            arraycopy(array, 0, bytes, NUMBER_HEADER_SIZE, arrayLength);
        } else {
            Byte[] source = (Byte[]) array;
            for (int i = 0; i < source.length; i++) {
                bytes[NUMBER_HEADER_SIZE + i] = source[i];
            }
        }
    } else {
        Bits bits = Bits.bits(numberOfBytes);
        bits.put((byte) type.intValue());
        bits.put((byte) bitsUsedInLastByte);
        bits.put((byte) requiredBits);
        type.writeAll(array, arrayLength, requiredBits, bits);
        bytes = bits.asBytes();
    }
    allocateRecordsFromBytes(target, bytes, recordAllocator);
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Example 9 with Bits

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

the class InlineNodeLabels method parseInlined.

public static long[] parseInlined(long labelField) {
    byte numberOfLabels = labelCount(labelField);
    if (numberOfLabels == 0) {
        return EMPTY_LONG_ARRAY;
    }
    long existingLabelsField = parseLabelsBody(labelField);
    byte bitsPerLabel = (byte) (LABEL_BITS / numberOfLabels);
    Bits bits = bitsFromLongs(new long[] { existingLabelsField });
    long[] result = new long[numberOfLabels];
    for (int i = 0; i < result.length; i++) {
        result[i] = bits.getLong(bitsPerLabel);
    }
    return result;
}
Also used : Bits(org.neo4j.kernel.impl.util.Bits)

Example 10 with Bits

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

the class InlineNodeLabels method tryInlineInNodeRecord.

static boolean tryInlineInNodeRecord(NodeRecord node, long[] ids, Collection<DynamicRecord> changedDynamicRecords) {
    // i.e. the 0-valued high header bit can allow for 0-7 in-lined labels in the bit-packed format.
    if (ids.length > 7) {
        return false;
    }
    byte bitsPerLabel = (byte) (ids.length > 0 ? (LABEL_BITS / ids.length) : LABEL_BITS);
    Bits bits = bits(5);
    if (!inlineValues(ids, bitsPerLabel, bits)) {
        return false;
    }
    node.setLabelField(combineLabelCountAndLabelStorage((byte) ids.length, bits.getLongs()[0]), changedDynamicRecords);
    return true;
}
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