Search in sources :

Example 6 with WriteBuffer

use of org.h2.mvstore.WriteBuffer in project h2database by h2database.

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)

Example 7 with WriteBuffer

use of org.h2.mvstore.WriteBuffer in project SpringStudy by myounghaklee.

the class TestDataUtils method testWriteBuffer.

private static void testWriteBuffer() {
    WriteBuffer buff = new WriteBuffer();
    buff.put(new byte[1500000]);
    buff.put(new byte[1900000]);
}
Also used : WriteBuffer(org.h2.mvstore.WriteBuffer)

Example 8 with WriteBuffer

use of org.h2.mvstore.WriteBuffer in project SpringStudy by myounghaklee.

the class TestObjectDataType method test.

private void test(Object last, Object x) {
    ObjectDataType ot = new ObjectDataType();
    // switch to the last type before every operation,
    // to test switching types
    ot.getMemory(last);
    assertTrue(ot.getMemory(x) >= 0);
    ot.getMemory(last);
    assertTrue(ot.getMemory(x) >= 0);
    ot.getMemory(last);
    assertEquals(0, ot.compare(x, x));
    WriteBuffer buff = new WriteBuffer();
    ot.getMemory(last);
    ot.write(buff, x);
    buff.put((byte) 123);
    ByteBuffer bb = buff.getBuffer();
    bb.flip();
    ot.getMemory(last);
    Object y = ot.read(bb);
    assertEquals(123, bb.get());
    assertEquals(0, bb.remaining());
    assertEquals(x.getClass().getName(), y.getClass().getName());
    ot.getMemory(last);
    assertEquals(0, ot.compare(x, y));
    if (x.getClass().isArray()) {
        if (x instanceof byte[]) {
            assertTrue(Arrays.equals((byte[]) x, (byte[]) y));
        } else if (x instanceof boolean[]) {
            assertTrue(Arrays.equals((boolean[]) x, (boolean[]) y));
        } else if (x instanceof short[]) {
            assertTrue(Arrays.equals((short[]) x, (short[]) y));
        } else if (x instanceof float[]) {
            assertTrue(Arrays.equals((float[]) x, (float[]) y));
        } else if (x instanceof double[]) {
            assertTrue(Arrays.equals((double[]) x, (double[]) y));
        } else if (x instanceof char[]) {
            assertTrue(Arrays.equals((char[]) x, (char[]) y));
        } else if (x instanceof int[]) {
            assertTrue(Arrays.equals((int[]) x, (int[]) y));
        } else if (x instanceof long[]) {
            assertTrue(Arrays.equals((long[]) x, (long[]) y));
        } else {
            assertTrue(Arrays.equals((Object[]) x, (Object[]) y));
        }
    } else {
        assertEquals(x.hashCode(), y.hashCode());
        assertTrue(x.equals(y));
    }
}
Also used : WriteBuffer(org.h2.mvstore.WriteBuffer) ObjectDataType(org.h2.mvstore.type.ObjectDataType) ByteBuffer(java.nio.ByteBuffer)

Example 9 with WriteBuffer

use of org.h2.mvstore.WriteBuffer in project SpringStudy by myounghaklee.

the class ValueDataType method save.

@Override
public void save(WriteBuffer buff, MetaType<Database> metaType) {
    writeIntArray(buff, sortTypes);
    int columnCount = rowFactory == null ? 0 : rowFactory.getColumnCount();
    buff.putVarInt(columnCount);
    int[] indexes = rowFactory == null ? null : rowFactory.getIndexes();
    writeIntArray(buff, indexes);
    buff.put(rowFactory == null || rowFactory.getRowDataType().isStoreKeys() ? (byte) 1 : (byte) 0);
}
Also used : ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint)

Example 10 with WriteBuffer

use of org.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 ValueBigint (org.h2.value.ValueBigint)9 ValueSmallint (org.h2.value.ValueSmallint)9 ValueTinyint (org.h2.value.ValueTinyint)9 ByteBuffer (java.nio.ByteBuffer)8 ObjectDataType (org.h2.mvstore.type.ObjectDataType)7 Random (java.util.Random)5 Value (org.h2.value.Value)5 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 Compressor (org.h2.compress.Compressor)4 Task (org.h2.util.Task)4 ValueTimestamp (org.h2.value.ValueTimestamp)4 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)4 ValueUuid (org.h2.value.ValueUuid)4 WriteBuffer (org.gridgain.internal.h2.mvstore.WriteBuffer)3 DataUtils.readString (org.h2.mvstore.DataUtils.readString)3 ValueBlob (org.h2.value.ValueBlob)3 ValueClob (org.h2.value.ValueClob)3 ValueDecfloat (org.h2.value.ValueDecfloat)3