use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testRandom.
private void testRandom() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName);
MVMap<Integer, Integer> m = s.openMap("data");
TreeMap<Integer, Integer> map = new TreeMap<>();
Random r = new Random(1);
int operationCount = 1000;
int maxValue = 30;
Integer expected, got;
for (int i = 0; i < operationCount; i++) {
int k = r.nextInt(maxValue);
int v = r.nextInt();
boolean compareAll;
switch(r.nextInt(3)) {
case 0:
log(i + ": put " + k + " = " + v);
expected = map.put(k, v);
got = m.put(k, v);
if (expected == null) {
assertNull(got);
} else {
assertEquals(expected, got);
}
compareAll = true;
break;
case 1:
log(i + ": remove " + k);
expected = map.remove(k);
got = m.remove(k);
if (expected == null) {
assertNull(got);
} else {
assertEquals(expected, got);
}
compareAll = true;
break;
default:
Integer a = map.get(k);
Integer b = m.get(k);
if (a == null || b == null) {
assertTrue(a == b);
} else {
assertEquals(a.intValue(), b.intValue());
}
compareAll = false;
break;
}
if (compareAll) {
Iterator<Integer> it = m.keyIterator(null);
Iterator<Integer> itExpected = map.keySet().iterator();
while (itExpected.hasNext()) {
assertTrue(it.hasNext());
expected = itExpected.next();
got = it.next();
assertEquals(expected, got);
}
assertFalse(it.hasNext());
}
}
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testCompressEmptyPage.
private void testCompressEmptyPage() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore store = new MVStore.Builder().cacheSize(100).fileName(fileName).compress().autoCommitBufferSize(10 * 1024).open();
MVMap<String, String> map = store.openMap("test");
store.removeMap(map);
store.commit();
store.close();
store = new MVStore.Builder().compress().open();
store.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testIndexSkip.
private void testIndexSkip() {
MVStore s = openStore(null, 4);
MVMap<Integer, Integer> map = s.openMap("test");
for (int i = 0; i < 100; i += 2) {
map.put(i, 10 * i);
}
Cursor<Integer, Integer> c = map.cursor(50);
// skip must reset the root of the cursor
c.skip(10);
for (int i = 70; i < 100; i += 2) {
assertTrue(c.hasNext());
assertEquals(i, c.next().intValue());
}
assertFalse(c.hasNext());
for (int i = -1; i < 100; i++) {
long index = map.getKeyIndex(i);
if (i < 0 || (i % 2) != 0) {
assertEquals(i < 0 ? -1 : -(i / 2) - 2, index);
} else {
assertEquals(i / 2, index);
}
}
for (int i = -1; i < 60; i++) {
Integer k = map.getKey(i);
if (i < 0 || i >= 50) {
assertNull(k);
} else {
assertEquals(i * 2, k.intValue());
}
}
// skip
c = map.cursor(0);
assertTrue(c.hasNext());
assertEquals(0, c.next().intValue());
c.skip(0);
assertEquals(2, c.next().intValue());
c.skip(1);
assertEquals(6, c.next().intValue());
c.skip(20);
assertEquals(48, c.next().intValue());
c = map.cursor(0);
c.skip(20);
assertEquals(40, c.next().intValue());
c = map.cursor(0);
assertEquals(0, c.next().intValue());
assertEquals(12, map.keyList().indexOf(24));
assertEquals(24, map.keyList().get(12).intValue());
assertEquals(-14, map.keyList().indexOf(25));
assertEquals(map.size(), map.keyList().size());
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testRecreateMap.
private void testRecreateMap() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName);
MVMap<Integer, Integer> m = s.openMap("test");
m.put(1, 1);
s.commit();
s.removeMap(m);
s.close();
s = openStore(fileName);
m = s.openMap("test");
assertNull(m.get(1));
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testVersion.
private void testVersion() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s;
s = openStore(fileName);
s.setVersionsToKeep(100);
s.setAutoCommitDelay(0);
s.setRetentionTime(Integer.MAX_VALUE);
MVMap<String, String> m = s.openMap("data");
s.commit();
long first = s.getCurrentVersion();
m.put("0", "test");
s.commit();
m.put("1", "Hello");
m.put("2", "World");
for (int i = 10; i < 20; i++) {
m.put("" + i, "data");
}
long old = s.getCurrentVersion();
s.commit();
m.put("1", "Hallo");
m.put("2", "Welt");
MVMap<String, String> mFirst;
mFirst = m.openVersion(first);
assertEquals(0, mFirst.size());
MVMap<String, String> mOld;
assertEquals("Hallo", m.get("1"));
assertEquals("Welt", m.get("2"));
mOld = m.openVersion(old);
assertEquals("Hello", mOld.get("1"));
assertEquals("World", mOld.get("2"));
assertTrue(mOld.isReadOnly());
s.getCurrentVersion();
long old3 = s.commit();
// the old version is still available
assertEquals("Hello", mOld.get("1"));
assertEquals("World", mOld.get("2"));
mOld = m.openVersion(old3);
assertEquals("Hallo", mOld.get("1"));
assertEquals("Welt", mOld.get("2"));
m.put("1", "Hi");
assertEquals("Welt", m.remove("2"));
s.close();
s = openStore(fileName);
m = s.openMap("data");
assertEquals("Hi", m.get("1"));
assertEquals(null, m.get("2"));
mOld = m.openVersion(old3);
assertEquals("Hallo", mOld.get("1"));
assertEquals("Welt", mOld.get("2"));
try {
m.openVersion(-3);
fail();
} catch (IllegalArgumentException e) {
// expected
}
s.close();
}
Aggregations