use of org.h2.mvstore.WriteBuffer in project SpringStudy by myounghaklee.
the class ValueDataType method write.
@Override
public void write(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.TINYINT:
buff.put(TINYINT).put(v.getByte());
break;
case Value.SMALLINT:
buff.put(SMALLINT).putShort(v.getShort());
break;
case Value.ENUM:
case Value.INTEGER:
{
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.INTEGER ? INTEGER : ENUM).putVarInt(x);
}
break;
}
case Value.BIGINT:
writeLong(buff, v.getLong());
break;
case Value.NUMERIC:
{
BigDecimal x = v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
buff.put(NUMERIC_0_1);
} else if (BigDecimal.ONE.equals(x)) {
buff.put((byte) (NUMERIC_0_1 + 1));
} else {
int scale = x.scale();
BigInteger b = x.unscaledValue();
int bits = b.bitLength();
if (bits <= 63) {
if (scale == 0) {
buff.put(NUMERIC_SMALL_0).putVarLong(b.longValue());
} else {
buff.put(NUMERIC_SMALL).putVarInt(scale).putVarLong(b.longValue());
}
} else {
byte[] bytes = b.toByteArray();
buff.put(NUMERIC).putVarInt(scale).putVarInt(bytes.length).put(bytes);
}
}
break;
}
case Value.DECFLOAT:
{
ValueDecfloat d = (ValueDecfloat) v;
buff.put((byte) DECFLOAT);
if (d.isFinite()) {
BigDecimal x = d.getBigDecimal();
byte[] bytes = x.unscaledValue().toByteArray();
buff.putVarInt(x.scale()).putVarInt(bytes.length).put(bytes);
} else {
int c;
if (d == ValueDecfloat.NEGATIVE_INFINITY) {
c = -3;
} else if (d == ValueDecfloat.POSITIVE_INFINITY) {
c = -2;
} else {
c = -1;
}
buff.putVarInt(0).putVarInt(c);
}
break;
}
case Value.TIME:
writeTimestampTime(buff.put(TIME), ((ValueTime) v).getNanos());
break;
case Value.TIME_TZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
long nanosOfDay = t.getNanos();
buff.put((byte) TIME_TZ).putVarInt((int) (nanosOfDay / DateTimeUtils.NANOS_PER_SECOND)).putVarInt((int) (nanosOfDay % DateTimeUtils.NANOS_PER_SECOND));
writeTimeZone(buff, t.getTimeZoneOffsetSeconds());
break;
}
case Value.DATE:
buff.put(DATE).putVarLong(((ValueDate) v).getDateValue());
break;
case Value.TIMESTAMP:
{
ValueTimestamp ts = (ValueTimestamp) v;
buff.put(TIMESTAMP).putVarLong(ts.getDateValue());
writeTimestampTime(buff, ts.getTimeNanos());
break;
}
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
buff.put((byte) TIMESTAMP_TZ).putVarLong(ts.getDateValue());
writeTimestampTime(buff, ts.getTimeNanos());
writeTimeZone(buff, ts.getTimeZoneOffsetSeconds());
break;
}
case Value.JAVA_OBJECT:
writeBinary(JAVA_OBJECT, buff, v);
break;
case Value.VARBINARY:
{
byte[] b = v.getBytesNoCopy();
int len = b.length;
if (len < 32) {
buff.put((byte) (VARBINARY_0_31 + len)).put(b);
} else {
buff.put(VARBINARY).putVarInt(len).put(b);
}
break;
}
case Value.BINARY:
writeBinary((byte) BINARY, buff, v);
break;
case Value.UUID:
{
ValueUuid uuid = (ValueUuid) v;
buff.put(UUID).putLong(uuid.getHigh()).putLong(uuid.getLow());
break;
}
case Value.VARCHAR:
{
String s = v.getString();
int len = s.length();
if (len < 32) {
buff.put((byte) (VARCHAR_0_31 + len)).putStringData(s, len);
} else {
writeString(buff.put(VARCHAR), s);
}
break;
}
case Value.VARCHAR_IGNORECASE:
writeString(buff.put(VARCHAR_IGNORECASE), v.getString());
break;
case Value.CHAR:
writeString(buff.put(CHAR), 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.REAL:
{
float x = v.getFloat();
if (x == 1.0f) {
buff.put((byte) (REAL_0_1 + 1));
} else {
int f = Float.floatToIntBits(x);
if (f == ValueReal.ZERO_BITS) {
buff.put(REAL_0_1);
} else {
buff.put(REAL).putVarInt(Integer.reverse(f));
}
}
break;
}
case Value.BLOB:
{
buff.put(BLOB);
ValueBlob lob = (ValueBlob) v;
LobData lobData = lob.getLobData();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength());
} else {
byte[] small = ((LobDataInMemory) lobData).getSmall();
buff.putVarInt(small.length).put(small);
}
break;
}
case Value.CLOB:
{
buff.put(CLOB);
ValueClob lob = (ValueClob) v;
LobData lobData = lob.getLobData();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
buff.putVarInt(-3).putVarInt(lobDataDatabase.getTableId()).putVarLong(lobDataDatabase.getLobId()).putVarLong(lob.octetLength()).putVarLong(lob.charLength());
} else {
byte[] small = ((LobDataInMemory) lobData).getSmall();
buff.putVarInt(small.length).put(small).putVarLong(lob.charLength());
}
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) {
write(buff, x);
}
break;
}
case Value.GEOMETRY:
writeBinary(GEOMETRY, buff, v);
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;
}
case Value.JSON:
writeBinary((byte) JSON, buff, v);
break;
default:
throw DbException.getInternalError("type=" + v.getValueType());
}
}
use of org.h2.mvstore.WriteBuffer in project SpringStudy by myounghaklee.
the class Page method write.
/**
* Store the page and update the position.
*
* @param chunk the chunk
* @param buff the target buffer
* @param toc prospective table of content
* @return the position of the buffer just after the type
*/
protected final int write(Chunk chunk, WriteBuffer buff, List<Long> toc) {
pageNo = toc.size();
int keyCount = getKeyCount();
int start = buff.position();
// placeholder for pageLength
buff.putInt(0).putShort(// placeholder for check
(byte) 0).putVarInt(pageNo).putVarInt(map.getId()).putVarInt(keyCount);
int typePos = buff.position();
int type = isLeaf() ? PAGE_TYPE_LEAF : DataUtils.PAGE_TYPE_NODE;
buff.put((byte) type);
int childrenPos = buff.position();
writeChildren(buff, true);
int compressStart = buff.position();
map.getKeyType().write(buff, keys, keyCount);
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 = store.getCompressorFast();
compressType = DataUtils.PAGE_COMPRESSED;
} else {
compressor = store.getCompressorHigh();
compressType = DataUtils.PAGE_COMPRESSED_HIGH;
}
byte[] comp = new byte[expLen * 2];
ByteBuffer byteBuffer = buff.getBuffer();
int pos = 0;
byte[] exp;
if (byteBuffer.hasArray()) {
exp = byteBuffer.array();
pos = byteBuffer.arrayOffset() + compressStart;
} else {
exp = Utils.newBytes(expLen);
buff.position(compressStart).get(exp);
}
int compLen = compressor.compress(exp, pos, expLen, comp, 0);
int plus = DataUtils.getVarIntLen(expLen - compLen);
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;
long tocElement = DataUtils.getTocElement(getMapId(), start, buff.position() - start, type);
toc.add(tocElement);
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.newMVStoreException(DataUtils.ERROR_INTERNAL, "Page already stored");
}
long pagePos = DataUtils.getPagePos(chunkId, tocElement);
boolean isDeleted = isRemoved();
while (!posUpdater.compareAndSet(this, isDeleted ? 1L : 0L, pagePos)) {
isDeleted = isRemoved();
}
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 pageLengthEncoded = DataUtils.getPageMaxLength(pos);
boolean singleWriter = map.isSingleWriter();
chunk.accountForWrittenPage(pageLengthEncoded, singleWriter);
if (isDeleted) {
store.accountForRemovedPage(pagePos, chunk.version + 1, singleWriter, pageNo);
}
diskSpaceUsed = pageLengthEncoded != DataUtils.PAGE_LARGE ? pageLengthEncoded : pageLength;
return childrenPos;
}
use of org.h2.mvstore.WriteBuffer in project gridgain by gridgain.
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.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.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]);
}
Aggregations