Search in sources :

Example 6 with WriteBuffer

use of org.gridgain.internal.h2.mvstore.WriteBuffer in project gridgain by gridgain.

the class ValueDataType method write.

@Override
public void write(WriteBuffer buff, Object obj) {
    if (obj instanceof SpatialKey) {
        buff.put((byte) SPATIAL_KEY_2D);
        getSpatialDataType().write(buff, obj);
        return;
    }
    Value x = (Value) obj;
    writeValue(buff, x);
}
Also used : SpatialKey(org.gridgain.internal.h2.mvstore.rtree.SpatialKey) Value(org.gridgain.internal.h2.value.Value)

Example 7 with WriteBuffer

use of org.gridgain.internal.h2.mvstore.WriteBuffer in project gridgain by gridgain.

the class ValueDataType method writeValue.

private void writeValue(WriteBuffer buff, Value v) {
    if (v == ValueNull.INSTANCE) {
        buff.put((byte) 0);
        return;
    }
    int type = v.getValueType();
    switch(type) {
        case Value.BOOLEAN:
            buff.put(v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE);
            break;
        case Value.BYTE:
            buff.put(BYTE).put(v.getByte());
            break;
        case Value.SHORT:
            buff.put(SHORT).putShort(v.getShort());
            break;
        case Value.ENUM:
        case Value.INT:
            {
                int x = v.getInt();
                if (x < 0) {
                    buff.put(INT_NEG).putVarInt(-x);
                } else if (x < 16) {
                    buff.put((byte) (INT_0_15 + x));
                } else {
                    buff.put(type == Value.INT ? INT : ENUM).putVarInt(x);
                }
                break;
            }
        case Value.LONG:
            {
                long x = v.getLong();
                if (x < 0) {
                    buff.put(LONG_NEG).putVarLong(-x);
                } else if (x < 8) {
                    buff.put((byte) (LONG_0_7 + x));
                } else {
                    buff.put(LONG).putVarLong(x);
                }
                break;
            }
        case Value.DECIMAL:
            {
                BigDecimal x = v.getBigDecimal();
                if (BigDecimal.ZERO.equals(x)) {
                    buff.put(DECIMAL_0_1);
                } else if (BigDecimal.ONE.equals(x)) {
                    buff.put((byte) (DECIMAL_0_1 + 1));
                } else {
                    int scale = x.scale();
                    BigInteger b = x.unscaledValue();
                    int bits = b.bitLength();
                    if (bits <= 63) {
                        if (scale == 0) {
                            buff.put(DECIMAL_SMALL_0).putVarLong(b.longValue());
                        } else {
                            buff.put(DECIMAL_SMALL).putVarInt(scale).putVarLong(b.longValue());
                        }
                    } else {
                        byte[] bytes = b.toByteArray();
                        buff.put(DECIMAL).putVarInt(scale).putVarInt(bytes.length).put(bytes);
                    }
                }
                break;
            }
        case Value.TIME:
            {
                ValueTime t = (ValueTime) v;
                long nanos = t.getNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put(TIME).putVarLong(millis).putVarLong(nanos);
                break;
            }
        case Value.DATE:
            {
                long x = ((ValueDate) v).getDateValue();
                buff.put(DATE).putVarLong(x);
                break;
            }
        case Value.TIMESTAMP:
            {
                ValueTimestamp ts = (ValueTimestamp) v;
                long dateValue = ts.getDateValue();
                long nanos = ts.getTimeNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put(TIMESTAMP).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos);
                break;
            }
        case Value.TIMESTAMP_TZ:
            {
                ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
                long dateValue = ts.getDateValue();
                long nanos = ts.getTimeNanos();
                long millis = nanos / 1000000;
                nanos -= millis * 1000000;
                buff.put(TIMESTAMP_TZ).putVarLong(dateValue).putVarLong(millis).putVarLong(nanos).putVarInt(ts.getTimeZoneOffsetMins());
                break;
            }
        case Value.JAVA_OBJECT:
            {
                byte[] b = v.getBytesNoCopy();
                buff.put(JAVA_OBJECT).putVarInt(b.length).put(b);
                break;
            }
        case Value.BYTES:
            {
                byte[] b = v.getBytesNoCopy();
                int len = b.length;
                if (len < 32) {
                    buff.put((byte) (BYTES_0_31 + len)).put(b);
                } else {
                    buff.put(BYTES).putVarInt(b.length).put(b);
                }
                break;
            }
        case Value.UUID:
            {
                ValueUuid uuid = (ValueUuid) v;
                buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
                break;
            }
        case Value.STRING:
            {
                String s = v.getString();
                int len = s.length();
                if (len < 32) {
                    buff.put((byte) (STRING_0_31 + len)).putStringData(s, len);
                } else {
                    buff.put(STRING);
                    writeString(buff, s);
                }
                break;
            }
        case Value.STRING_IGNORECASE:
            buff.put(STRING_IGNORECASE);
            writeString(buff, v.getString());
            break;
        case Value.STRING_FIXED:
            buff.put(STRING_FIXED);
            writeString(buff, v.getString());
            break;
        case Value.DOUBLE:
            {
                double x = v.getDouble();
                if (x == 1.0d) {
                    buff.put((byte) (DOUBLE_0_1 + 1));
                } else {
                    long d = Double.doubleToLongBits(x);
                    if (d == ValueDouble.ZERO_BITS) {
                        buff.put(DOUBLE_0_1);
                    } else {
                        buff.put(DOUBLE).putVarLong(Long.reverse(d));
                    }
                }
                break;
            }
        case Value.FLOAT:
            {
                float x = v.getFloat();
                if (x == 1.0f) {
                    buff.put((byte) (FLOAT_0_1 + 1));
                } else {
                    int f = Float.floatToIntBits(x);
                    if (f == ValueFloat.ZERO_BITS) {
                        buff.put(FLOAT_0_1);
                    } else {
                        buff.put(FLOAT).putVarInt(Integer.reverse(f));
                    }
                }
                break;
            }
        case Value.BLOB:
        case Value.CLOB:
            {
                buff.put(type == Value.BLOB ? BLOB : CLOB);
                ValueLobDb lob = (ValueLobDb) v;
                byte[] small = lob.getSmall();
                if (small == null) {
                    buff.putVarInt(-3).putVarInt(lob.getTableId()).putVarLong(lob.getLobId()).putVarLong(lob.getType().getPrecision());
                } else {
                    buff.putVarInt(small.length).put(small);
                }
                break;
            }
        case Value.ARRAY:
        case Value.ROW:
            {
                Value[] list = ((ValueCollectionBase) v).getList();
                buff.put(type == Value.ARRAY ? ARRAY : ROW).putVarInt(list.length);
                for (Value x : list) {
                    writeValue(buff, x);
                }
                break;
            }
        case Value.RESULT_SET:
            {
                buff.put(RESULT_SET);
                ResultInterface result = ((ValueResultSet) v).getResult();
                int columnCount = result.getVisibleColumnCount();
                buff.putVarInt(columnCount);
                for (int i = 0; i < columnCount; i++) {
                    writeString(buff, result.getAlias(i));
                    writeString(buff, result.getColumnName(i));
                    TypeInfo columnType = result.getColumnType(i);
                    buff.putVarInt(columnType.getValueType()).putVarLong(columnType.getPrecision()).putVarInt(columnType.getScale());
                }
                while (result.next()) {
                    buff.put((byte) 1);
                    Value[] row = result.currentRow();
                    for (int i = 0; i < columnCount; i++) {
                        writeValue(buff, row[i]);
                    }
                }
                buff.put((byte) 0);
                break;
            }
        case Value.GEOMETRY:
            {
                byte[] b = v.getBytes();
                int len = b.length;
                buff.put(GEOMETRY).putVarInt(len).put(b);
                break;
            }
        case Value.INTERVAL_YEAR:
        case Value.INTERVAL_MONTH:
        case Value.INTERVAL_DAY:
        case Value.INTERVAL_HOUR:
        case Value.INTERVAL_MINUTE:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) ordinal).putVarLong(interval.getLeading());
                break;
            }
        case Value.INTERVAL_SECOND:
        case Value.INTERVAL_YEAR_TO_MONTH:
        case Value.INTERVAL_DAY_TO_HOUR:
        case Value.INTERVAL_DAY_TO_MINUTE:
        case Value.INTERVAL_DAY_TO_SECOND:
        case Value.INTERVAL_HOUR_TO_MINUTE:
        case Value.INTERVAL_HOUR_TO_SECOND:
        case Value.INTERVAL_MINUTE_TO_SECOND:
            {
                ValueInterval interval = (ValueInterval) v;
                int ordinal = type - Value.INTERVAL_YEAR;
                if (interval.isNegative()) {
                    ordinal = ~ordinal;
                }
                buff.put(INTERVAL).put((byte) (ordinal)).putVarLong(interval.getLeading()).putVarLong(interval.getRemaining());
                break;
            }
        default:
            if (JdbcUtils.customDataTypesHandler != null) {
                byte[] b = v.getBytesNoCopy();
                buff.put((byte) CUSTOM_DATA_TYPE).putVarInt(type).putVarInt(b.length).put(b);
                break;
            }
            DbException.throwInternalError("type=" + v.getValueType());
    }
}
Also used : ValueUuid(org.gridgain.internal.h2.value.ValueUuid) ValueLobDb(org.gridgain.internal.h2.value.ValueLobDb) ResultInterface(org.gridgain.internal.h2.result.ResultInterface) ValueTimestampTimeZone(org.gridgain.internal.h2.value.ValueTimestampTimeZone) ValueString(org.gridgain.internal.h2.value.ValueString) ValueInterval(org.gridgain.internal.h2.value.ValueInterval) TypeInfo(org.gridgain.internal.h2.value.TypeInfo) BigDecimal(java.math.BigDecimal) ValueTime(org.gridgain.internal.h2.value.ValueTime) ValueTimestamp(org.gridgain.internal.h2.value.ValueTimestamp) Value(org.gridgain.internal.h2.value.Value) BigInteger(java.math.BigInteger)

Example 8 with WriteBuffer

use of org.gridgain.internal.h2.mvstore.WriteBuffer in project gridgain by gridgain.

the class VersionedValueType method write.

@Override
public void write(WriteBuffer buff, Object[] obj, int len, boolean key) {
    boolean fastPath = true;
    for (int i = 0; i < len; i++) {
        VersionedValue v = (VersionedValue) obj[i];
        if (v.getOperationId() != 0 || v.getCurrentValue() == null) {
            fastPath = false;
        }
    }
    if (fastPath) {
        buff.put((byte) 0);
        for (int i = 0; i < len; i++) {
            VersionedValue v = (VersionedValue) obj[i];
            valueType.write(buff, v.getCurrentValue());
        }
    } else {
        // slow path:
        // store op ids, and some entries may be null
        buff.put((byte) 1);
        for (int i = 0; i < len; i++) {
            write(buff, obj[i]);
        }
    }
}
Also used : VersionedValue(org.gridgain.internal.h2.value.VersionedValue)

Example 9 with WriteBuffer

use of org.gridgain.internal.h2.mvstore.WriteBuffer in project gridgain by gridgain.

the class VersionedValueType method write.

@Override
public void write(WriteBuffer buff, Object obj) {
    VersionedValue v = (VersionedValue) obj;
    long operationId = v.getOperationId();
    buff.putVarLong(operationId);
    if (operationId == 0) {
        valueType.write(buff, v.getCurrentValue());
    } else {
        Object committedValue = v.getCommittedValue();
        int flags = (v.getCurrentValue() == null ? 0 : 1) | (committedValue == null ? 0 : 2);
        buff.put((byte) flags);
        if (v.getCurrentValue() != null) {
            valueType.write(buff, v.getCurrentValue());
        }
        if (committedValue != null) {
            valueType.write(buff, committedValue);
        }
    }
}
Also used : VersionedValue(org.gridgain.internal.h2.value.VersionedValue)

Example 10 with WriteBuffer

use of org.gridgain.internal.h2.mvstore.WriteBuffer in project 468H2Project by lukeunderwood42.

the class TestMVStoreConcurrent method testConcurrentDataType.

private void testConcurrentDataType() throws InterruptedException {
    final ObjectDataType type = new ObjectDataType();
    final Object[] data = new Object[] { null, -1, 1, 10, "Hello", new Object[] { new byte[] { (byte) -1, (byte) 1 }, null }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 10 }, new Object[] { new byte[] { (byte) -1, (byte) 1 }, 20L }, new Object[] { new byte[] { (byte) 1, (byte) -1 }, 5 } };
    Arrays.sort(data, type::compare);
    Task[] tasks = new Task[2];
    for (int i = 0; i < tasks.length; i++) {
        tasks[i] = new Task() {

            @Override
            public void call() {
                Random r = new Random();
                WriteBuffer buff = new WriteBuffer();
                while (!stop) {
                    int a = r.nextInt(data.length);
                    int b = r.nextInt(data.length);
                    int comp;
                    if (r.nextBoolean()) {
                        comp = type.compare(a, b);
                    } else {
                        comp = -type.compare(b, a);
                    }
                    buff.clear();
                    type.write(buff, a);
                    buff.clear();
                    type.write(buff, b);
                    if (a == b) {
                        assertEquals(0, comp);
                    } else {
                        assertEquals(a > b ? 1 : -1, comp);
                    }
                }
            }
        };
        tasks[i].execute();
    }
    try {
        Thread.sleep(100);
    } finally {
        for (Task t : tasks) {
            t.get();
        }
    }
}
Also used : Task(org.h2.util.Task) WriteBuffer(org.h2.mvstore.WriteBuffer) Random(java.util.Random) ObjectDataType(org.h2.mvstore.type.ObjectDataType)

Aggregations

WriteBuffer (org.h2.mvstore.WriteBuffer)11 ObjectDataType (org.h2.mvstore.type.ObjectDataType)7 ByteBuffer (java.nio.ByteBuffer)5 Random (java.util.Random)5 Task (org.h2.util.Task)4 WriteBuffer (org.gridgain.internal.h2.mvstore.WriteBuffer)3 ObjectDataType (org.gridgain.internal.h2.mvstore.type.ObjectDataType)2 Value (org.gridgain.internal.h2.value.Value)2 VersionedValue (org.gridgain.internal.h2.value.VersionedValue)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 Broadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.Broadcaster)1 InMemoryBroadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.InMemoryBroadcaster)1 TCPBroadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.TCPBroadcaster)1 UDPBroadcaster (org.apache.jackrabbit.oak.plugins.document.persistentCache.broadcast.UDPBroadcaster)1 Compressor (org.gridgain.internal.h2.compress.Compressor)1 SpatialKey (org.gridgain.internal.h2.mvstore.rtree.SpatialKey)1 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)1 Task (org.gridgain.internal.h2.util.Task)1