use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class TestMVStoreStopCompact method testStopCompact.
private void testStopCompact(int retentionTime, int timeout) throws InterruptedException {
String fileName = getBaseDir() + "/testStopCompact.h3";
FileUtils.createDirectories(getBaseDir());
FileUtils.delete(fileName);
// store with a very small page size, to make sure
// there are many leaf pages
MVStore s = new MVStore.Builder().fileName(fileName).open();
s.setRetentionTime(retentionTime);
MVMap<Integer, String> map = s.openMap("data");
long start = System.currentTimeMillis();
Random r = new Random(1);
for (int i = 0; i < 4000000; i++) {
long time = System.currentTimeMillis() - start;
if (time > timeout) {
break;
}
int x = r.nextInt(10000000);
map.put(x, "Hello World " + i * 10);
}
s.setAutoCommitDelay(100);
long oldWriteCount = s.getFileStore().getWriteCount();
// expect background write to stop after 5 seconds
Thread.sleep(5000);
long newWriteCount = s.getFileStore().getWriteCount();
// expect that compaction didn't cause many writes
assertTrue(newWriteCount - oldWriteCount < 30);
s.close();
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class TestStreamStore method testDetectIllegalId.
private void testDetectIllegalId() throws IOException {
Map<Long, byte[]> map = new HashMap<>();
StreamStore store = new StreamStore(map);
try {
store.length(new byte[] { 3, 0, 0 });
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
store.remove(new byte[] { 3, 0, 0 });
fail();
} catch (IllegalArgumentException e) {
// expected
}
map.put(0L, new byte[] { 3, 0, 0 });
InputStream in = store.get(new byte[] { 2, 1, 0 });
try {
in.read();
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class TestStreamStore method testLoop.
private void testLoop() throws IOException {
Map<Long, byte[]> map = new HashMap<>();
StreamStore store = new StreamStore(map);
assertEquals(256 * 1024, store.getMaxBlockSize());
assertEquals(256, store.getMinBlockSize());
store.setNextKey(0);
assertEquals(0, store.getNextKey());
test(store, 10, 20, 1000);
for (int i = 0; i < 20; i++) {
test(store, 0, 128, i);
test(store, 10, 128, i);
}
for (int i = 20; i < 200; i += 10) {
test(store, 0, 128, i);
test(store, 10, 128, i);
}
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class TestStreamStore method testWithFullMap.
private void testWithFullMap() throws IOException {
final AtomicInteger tests = new AtomicInteger();
Map<Long, byte[]> map = new HashMap<Long, byte[]>() {
private static final long serialVersionUID = 1L;
@Override
public boolean containsKey(Object k) {
tests.incrementAndGet();
if (((Long) k) < Long.MAX_VALUE / 2) {
// simulate a *very* full map
return true;
}
return super.containsKey(k);
}
};
StreamStore store = new StreamStore(map);
store.setMinBlockSize(20);
store.setMaxBlockSize(100);
store.setNextKey(0);
store.put(new ByteArrayInputStream(new byte[100]));
assertEquals(1, map.size());
assertEquals(64, tests.get());
assertEquals(Long.MAX_VALUE / 2 + 1, store.getNextKey());
}
use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.
the class TestReorderWrites method testMVStore.
private void testMVStore() {
FilePathReorderWrites fs = FilePathReorderWrites.register();
String fileName = "reorder:memFS:test.mv";
try {
for (int i = 0; i < 1000; i++) {
log(i + " --------------------------------");
// this test is not interested in power off failures during
// initial creation
fs.setPowerOffCountdown(0, 0);
// release the static data this test generates
FileUtils.delete("memFS:test.mv");
FileUtils.delete("memFS:test.mv.copy");
MVStore store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
// store.setRetentionTime(10);
Map<Integer, byte[]> map = store.openMap("data");
map.put(-1, new byte[1]);
store.commit();
store.getFileStore().sync();
Random r = new Random(i);
int stop = 4 + r.nextInt(20);
log("countdown start");
fs.setPowerOffCountdown(stop, i);
try {
for (int j = 1; j < 100; j++) {
Map<Integer, Integer> newMap = store.openMap("d" + j);
newMap.put(j, j * 10);
int key = r.nextInt(10);
int len = 10 * r.nextInt(1000);
if (r.nextBoolean()) {
map.remove(key);
} else {
map.put(key, new byte[len]);
}
log("op " + j + ": ");
store.commit();
switch(r.nextInt(10)) {
case 0:
log("op compact");
store.compact(100, 10 * 1024);
break;
case 1:
log("op compactMoveChunks");
store.compactMoveChunks();
log("op compactMoveChunks done");
break;
}
}
// write has to fail at some point
fail();
} catch (IllegalStateException e) {
log("stop " + e + ", cause: " + e.getCause());
// expected
}
try {
store.close();
} catch (IllegalStateException e) {
// expected
store.closeImmediately();
}
log("verify");
fs.setPowerOffCountdown(100, 0);
if (LOG) {
MVStoreTool.dump(fileName, true);
}
store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
map = store.openMap("data");
if (!map.containsKey(-1)) {
fail("key not found, size=" + map.size() + " i=" + i);
} else {
assertEquals("i=" + i, 1, map.get(-1).length);
}
for (int j = 0; j < 100; j++) {
Map<Integer, Integer> newMap = store.openMap("d" + j);
newMap.get(j);
}
map.keySet();
store.close();
}
} finally {
// release the static data this test generates
FileUtils.delete("memFS:test.mv");
FileUtils.delete("memFS:test.mv.copy");
}
}
Aggregations