Search in sources :

Example 1 with BinaryOffheapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream in project ignite by apache.

the class BinaryObjectOffheapImpl method typeId.

/** {@inheritDoc} */
@Override
public int typeId() {
    int typeId = BinaryPrimitives.readInt(ptr, start + GridBinaryMarshaller.TYPE_ID_POS);
    if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) {
        int off = start + GridBinaryMarshaller.DFLT_HDR_LEN;
        String clsName = BinaryUtils.doReadClassName(new BinaryOffheapInputStream(ptr + off, size));
        typeId = ctx.typeId(clsName);
    }
    return typeId;
}
Also used : BinaryOffheapInputStream(org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream)

Example 2 with BinaryOffheapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method unmarshal.

/**
     * @param ptr Off-heap pointer.
     * @param forceHeap If {@code true} creates heap-based object.
     * @return Object.
     * @throws BinaryObjectException If failed.
     */
public Object unmarshal(long ptr, boolean forceHeap) throws BinaryObjectException {
    assert ptr > 0 : ptr;
    int size = GridUnsafe.getInt(ptr);
    ptr += 4;
    byte type = GridUnsafe.getByte(ptr++);
    if (type != CacheObject.TYPE_BYTE_ARR) {
        assert size > 0 : size;
        BinaryInputStream in = new BinaryOffheapInputStream(ptr, size, forceHeap);
        return binaryMarsh.unmarshal(in);
    } else
        return U.copyMemory(ptr, size);
}
Also used : BinaryOffheapInputStream(org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream)

Example 3 with BinaryOffheapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream in project ignite by apache.

the class BinaryObjectOffheapImpl method fieldByOrder.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Nullable
@Override
public <F> F fieldByOrder(int order) {
    if (order == BinarySchema.ORDER_NOT_FOUND)
        return null;
    Object val;
    // Calculate field position.
    int schemaOff = BinaryPrimitives.readInt(ptr, start + GridBinaryMarshaller.SCHEMA_OR_RAW_OFF_POS);
    short flags = BinaryPrimitives.readShort(ptr, start + GridBinaryMarshaller.FLAGS_POS);
    int fieldIdLen = BinaryUtils.isCompactFooter(flags) ? 0 : BinaryUtils.FIELD_ID_LEN;
    int fieldOffLen = BinaryUtils.fieldOffsetLength(flags);
    int fieldOffsetPos = start + schemaOff + order * (fieldIdLen + fieldOffLen) + fieldIdLen;
    int fieldPos;
    if (fieldOffLen == BinaryUtils.OFFSET_1)
        fieldPos = start + ((int) BinaryPrimitives.readByte(ptr, fieldOffsetPos) & 0xFF);
    else if (fieldOffLen == BinaryUtils.OFFSET_2)
        fieldPos = start + ((int) BinaryPrimitives.readShort(ptr, fieldOffsetPos) & 0xFFFF);
    else
        fieldPos = start + BinaryPrimitives.readInt(ptr, fieldOffsetPos);
    // Read header and try performing fast lookup for well-known types (the most common types go first).
    byte hdr = BinaryPrimitives.readByte(ptr, fieldPos);
    switch(hdr) {
        case GridBinaryMarshaller.INT:
            val = BinaryPrimitives.readInt(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.LONG:
            val = BinaryPrimitives.readLong(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.BOOLEAN:
            val = BinaryPrimitives.readBoolean(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.SHORT:
            val = BinaryPrimitives.readShort(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.BYTE:
            val = BinaryPrimitives.readByte(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.CHAR:
            val = BinaryPrimitives.readChar(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.FLOAT:
            val = BinaryPrimitives.readFloat(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.DOUBLE:
            val = BinaryPrimitives.readDouble(ptr, fieldPos + 1);
            break;
        case GridBinaryMarshaller.STRING:
            {
                int dataLen = BinaryPrimitives.readInt(ptr, fieldPos + 1);
                byte[] data = BinaryPrimitives.readByteArray(ptr, fieldPos + 5, dataLen);
                val = new String(data, UTF_8);
                break;
            }
        case GridBinaryMarshaller.DATE:
            {
                long time = BinaryPrimitives.readLong(ptr, fieldPos + 1);
                val = new Date(time);
                break;
            }
        case GridBinaryMarshaller.TIMESTAMP:
            {
                long time = BinaryPrimitives.readLong(ptr, fieldPos + 1);
                int nanos = BinaryPrimitives.readInt(ptr, fieldPos + 1 + 8);
                Timestamp ts = new Timestamp(time);
                ts.setNanos(ts.getNanos() + nanos);
                val = ts;
                break;
            }
        case GridBinaryMarshaller.TIME:
            {
                long time = BinaryPrimitives.readLong(ptr, fieldPos + 1);
                val = new Time(time);
                break;
            }
        case GridBinaryMarshaller.UUID:
            {
                long most = BinaryPrimitives.readLong(ptr, fieldPos + 1);
                long least = BinaryPrimitives.readLong(ptr, fieldPos + 1 + 8);
                val = new UUID(most, least);
                break;
            }
        case GridBinaryMarshaller.DECIMAL:
            {
                int scale = BinaryPrimitives.readInt(ptr, fieldPos + 1);
                int dataLen = BinaryPrimitives.readInt(ptr, fieldPos + 5);
                byte[] data = BinaryPrimitives.readByteArray(ptr, fieldPos + 9, dataLen);
                boolean negative = data[0] < 0;
                if (negative)
                    data[0] &= 0x7F;
                BigInteger intVal = new BigInteger(data);
                if (negative)
                    intVal = intVal.negate();
                val = new BigDecimal(intVal, scale);
                break;
            }
        case GridBinaryMarshaller.NULL:
            val = null;
            break;
        default:
            BinaryOffheapInputStream stream = new BinaryOffheapInputStream(ptr, size, false);
            stream.position(fieldPos);
            val = BinaryUtils.unmarshal(stream, ctx, null);
            break;
    }
    return (F) val;
}
Also used : Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.util.Date) BigDecimal(java.math.BigDecimal) BinaryOffheapInputStream(org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream) BigInteger(java.math.BigInteger) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) UUID(java.util.UUID) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with BinaryOffheapInputStream

use of org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream in project ignite by apache.

the class BinaryObjectOffheapImpl method reader.

/**
     * Create new reader for this object.
     *
     * @param rCtx Reader context.
     * @param forUnmarshal {@code True} if reader is needed to unmarshal object.
     * @return Reader.
     */
private BinaryReaderExImpl reader(@Nullable BinaryReaderHandles rCtx, boolean forUnmarshal) {
    BinaryOffheapInputStream stream = new BinaryOffheapInputStream(ptr, size, false);
    stream.position(start);
    return new BinaryReaderExImpl(ctx, stream, ctx.configuration().getClassLoader(), rCtx, forUnmarshal);
}
Also used : BinaryOffheapInputStream(org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream)

Aggregations

BinaryOffheapInputStream (org.apache.ignite.internal.binary.streams.BinaryOffheapInputStream)4 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 UUID (java.util.UUID)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 BinaryInputStream (org.apache.ignite.internal.binary.streams.BinaryInputStream)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 Nullable (org.jetbrains.annotations.Nullable)1