use of org.gridgain.internal.h2.mvstore.WriteBuffer in project gridgain by gridgain.
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() {
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 gridgain by gridgain.
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 gridgain by gridgain.
the class Page method write.
/**
* Store the page and update the position.
*
* @param chunk the chunk
* @param buff the target buffer
* @return the position of the buffer just after the type
*/
protected final int write(Chunk chunk, WriteBuffer buff) {
int start = buff.position();
int len = getKeyCount();
int type = isLeaf() ? PAGE_TYPE_LEAF : DataUtils.PAGE_TYPE_NODE;
buff.putInt(0).putShort((byte) 0).putVarInt(map.getId()).putVarInt(len);
int typePos = buff.position();
buff.put((byte) type);
writeChildren(buff, true);
int compressStart = buff.position();
map.getKeyType().write(buff, keys, len, true);
writeValues(buff);
MVStore store = map.getStore();
int expLen = buff.position() - compressStart;
if (expLen > 16) {
int compressionLevel = store.getCompressionLevel();
if (compressionLevel > 0) {
Compressor compressor;
int compressType;
if (compressionLevel == 1) {
compressor = map.getStore().getCompressorFast();
compressType = DataUtils.PAGE_COMPRESSED;
} else {
compressor = map.getStore().getCompressorHigh();
compressType = DataUtils.PAGE_COMPRESSED_HIGH;
}
byte[] exp = new byte[expLen];
buff.position(compressStart).get(exp);
byte[] comp = new byte[expLen * 2];
int compLen = compressor.compress(exp, expLen, comp, 0);
int plus = DataUtils.getVarIntLen(compLen - expLen);
if (compLen + plus < expLen) {
buff.position(typePos).put((byte) (type + compressType));
buff.position(compressStart).putVarInt(expLen - compLen).put(comp, 0, compLen);
}
}
}
int pageLength = buff.position() - start;
int chunkId = chunk.id;
int check = DataUtils.getCheckValue(chunkId) ^ DataUtils.getCheckValue(start) ^ DataUtils.getCheckValue(pageLength);
buff.putInt(start, pageLength).putShort(start + 4, (short) check);
if (isSaved()) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_INTERNAL, "Page already stored");
}
pos = DataUtils.getPagePos(chunkId, start, pageLength, type);
store.cachePage(this);
if (type == DataUtils.PAGE_TYPE_NODE) {
// cache again - this will make sure nodes stays in the cache
// for a longer time
store.cachePage(this);
}
int max = DataUtils.getPageMaxLength(pos);
chunk.maxLen += max;
chunk.maxLenLive += max;
chunk.pageCount++;
chunk.pageCountLive++;
if (removedInMemory) {
// if the page was removed _before_ the position was assigned, we
// need to mark it removed here, so the fields are updated
// when the next chunk is stored
map.removePage(pos, memory);
}
diskSpaceUsed = max != DataUtils.PAGE_LARGE ? max : pageLength;
return typePos + 1;
}
use of org.gridgain.internal.h2.mvstore.WriteBuffer in project 468H2Project by lukeunderwood42.
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