use of org.gridgain.internal.h2.mvstore.WriteBuffer in project h2database by h2database.
the class TestConcurrent 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, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return type.compare(o1, o2);
}
});
Task[] tasks = new Task[2];
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new Task() {
@Override
public void call() throws Exception {
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();
}
}
}
use of org.gridgain.internal.h2.mvstore.WriteBuffer in project h2database by h2database.
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));
}
}
use of org.gridgain.internal.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();
}
}
}
use of org.gridgain.internal.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]);
}
use of org.gridgain.internal.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));
}
}
Aggregations