use of org.h2.store.Page in project h2database by h2database.
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
*/
private int write(Chunk chunk, WriteBuffer buff) {
int start = buff.position();
int len = keys.length;
int type = children != null ? DataUtils.PAGE_TYPE_NODE : DataUtils.PAGE_TYPE_LEAF;
buff.putInt(0).putShort((byte) 0).putVarInt(map.getId()).putVarInt(len);
int typePos = buff.position();
buff.put((byte) type);
if (type == DataUtils.PAGE_TYPE_NODE) {
writeChildren(buff);
for (int i = 0; i <= len; i++) {
buff.putVarLong(children[i].count);
}
}
int compressStart = buff.position();
map.getKeyType().write(buff, keys, len, true);
if (type == DataUtils.PAGE_TYPE_LEAF) {
map.getValueType().write(buff, values, len, false);
}
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 (pos != 0) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_INTERNAL, "Page already stored");
}
pos = DataUtils.getPagePos(chunkId, start, pageLength, type);
store.cachePage(pos, this, getMemory());
if (type == DataUtils.PAGE_TYPE_NODE) {
// cache again - this will make sure nodes stays in the cache
// for a longer time
store.cachePage(pos, this, getMemory());
}
long 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);
}
return typePos + 1;
}
use of org.h2.store.Page in project h2database by h2database.
the class Page method setValue.
/**
* Replace the value at an index in this page.
*
* @param index the index
* @param value the new value
* @return the old value
*/
public Object setValue(int index, Object value) {
Object old = values[index];
// this is slightly slower:
// values = Arrays.copyOf(values, values.length);
values = values.clone();
DataType valueType = map.getValueType();
if (isPersistent()) {
addMemory(valueType.getMemory(value) - valueType.getMemory(old));
}
values[index] = value;
return old;
}
use of org.h2.store.Page in project h2database by h2database.
the class Page method setKey.
/**
* Replace the key at an index in this page.
*
* @param index the index
* @param key the new key
*/
public void setKey(int index, Object key) {
// this is slightly slower:
// keys = Arrays.copyOf(keys, keys.length);
keys = keys.clone();
if (isPersistent()) {
Object old = keys[index];
DataType keyType = map.getKeyType();
int mem = keyType.getMemory(key);
if (old != null) {
mem -= keyType.getMemory(old);
}
addMemory(mem);
}
keys[index] = key;
}
use of org.h2.store.Page in project h2database by h2database.
the class TestDataPage method testAll.
private void testAll() {
Data page = Data.create(this, 128);
char[] data = new char[0x10000];
for (int i = 0; i < data.length; i++) {
data[i] = (char) i;
}
String s = new String(data);
page.checkCapacity(s.length() * 4);
page.writeString(s);
int len = page.length();
assertEquals(len, Data.getStringLen(s));
page.reset();
assertEquals(s, page.readString());
page.reset();
page.writeString("H\u1111!");
page.writeString("John\tBrack's \"how are you\" M\u1111ller");
page.writeValue(ValueInt.get(10));
page.writeValue(ValueString.get("test"));
page.writeValue(ValueFloat.get(-2.25f));
page.writeValue(ValueDouble.get(10.40));
page.writeValue(ValueNull.INSTANCE);
trace(new String(page.getBytes()));
page.reset();
trace(page.readString());
trace(page.readString());
trace(page.readValue().getInt());
trace(page.readValue().getString());
trace("" + page.readValue().getFloat());
trace("" + page.readValue().getDouble());
trace(page.readValue().toString());
page.reset();
page.writeInt(0);
page.writeInt(Integer.MAX_VALUE);
page.writeInt(Integer.MIN_VALUE);
page.writeInt(1);
page.writeInt(-1);
page.writeInt(1234567890);
page.writeInt(54321);
trace(new String(page.getBytes()));
page.reset();
trace(page.readInt());
trace(page.readInt());
trace(page.readInt());
trace(page.readInt());
trace(page.readInt());
trace(page.readInt());
trace(page.readInt());
page = null;
}
use of org.h2.store.Page in project h2database by h2database.
the class TestAll method run.
private static void run(String... args) throws Exception {
SelfDestructor.startCountdown(4 * 60);
long time = System.nanoTime();
printSystemInfo();
// use lower values, to better test those cases,
// and (for delays) to speed up the tests
System.setProperty("h2.maxMemoryRows", "100");
System.setProperty("h2.check2", "true");
System.setProperty("h2.delayWrongPasswordMin", "0");
System.setProperty("h2.delayWrongPasswordMax", "0");
System.setProperty("h2.useThreadContextClassLoader", "true");
// System.setProperty("h2.modifyOnWrite", "true");
// speedup
// System.setProperty("h2.syncMethod", "");
/*
recovery tests with small freeList pages, page size 64
reopen org.h2.test.unit.TestPageStore
-Xmx1500m -D reopenOffset=3 -D reopenShift=1
power failure test
power failure test: MULTI_THREADED=TRUE
power failure test: larger binaries and additional index.
power failure test with randomly generating / dropping indexes and tables.
drop table test;
create table test(id identity, name varchar(100) default space(100));
@LOOP 10 insert into test select null, null from system_range(1, 100000);
delete from test;
documentation: review package and class level javadocs
documentation: rolling review at main.html
-------------
remove old TODO, move to roadmap
kill a test:
kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
*/
TestAll test = new TestAll();
if (args.length > 0) {
if ("travis".equals(args[0])) {
test.travis = true;
test.testAll();
} else if ("vmlens".equals(args[0])) {
test.vmlens = true;
test.testAll();
} else if ("reopen".equals(args[0])) {
System.setProperty("h2.delayWrongPasswordMin", "0");
System.setProperty("h2.check2", "false");
System.setProperty("h2.analyzeAuto", "100");
System.setProperty("h2.pageSize", "64");
System.setProperty("h2.reopenShift", "5");
FilePathRec.register();
test.reopen = true;
TestReopen reopen = new TestReopen();
reopen.init();
FilePathRec.setRecorder(reopen);
test.runTests();
} else if ("crash".equals(args[0])) {
test.endless = true;
new TestCrashAPI().runTest(test);
} else if ("synth".equals(args[0])) {
new TestSynth().runTest(test);
} else if ("kill".equals(args[0])) {
new TestKill().runTest(test);
} else if ("random".equals(args[0])) {
test.endless = true;
new TestRandomSQL().runTest(test);
} else if ("join".equals(args[0])) {
new TestJoin().runTest(test);
test.endless = true;
} else if ("btree".equals(args[0])) {
new TestBtreeIndex().runTest(test);
} else if ("all".equals(args[0])) {
test.testEverything();
} else if ("codeCoverage".equals(args[0])) {
test.codeCoverage = true;
test.runCoverage();
} else if ("multiThread".equals(args[0])) {
new TestMulti().runTest(test);
} else if ("halt".equals(args[0])) {
new TestHaltApp().runTest(test);
} else if ("timer".equals(args[0])) {
new TestTimer().runTest(test);
}
} else {
test.testAll();
}
System.out.println(TestBase.formatTime(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time)) + " total");
}
Aggregations