use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testBtreeStore.
private void testBtreeStore() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName);
s.close();
s = openStore(fileName);
MVMap<Integer, String> m = s.openMap("data");
int count = 2000;
for (int i = 0; i < count; i++) {
assertNull(m.put(i, "hello " + i));
assertEquals("hello " + i, m.get(i));
}
s.commit();
assertEquals("hello 0", m.remove(0));
assertNull(m.get(0));
for (int i = 1; i < count; i++) {
assertEquals("hello " + i, m.get(i));
}
s.close();
s = openStore(fileName);
m = s.openMap("data");
assertNull(m.get(0));
for (int i = 1; i < count; i++) {
assertEquals("hello " + i, m.get(i));
}
for (int i = 1; i < count; i++) {
m.remove(i);
}
s.commit();
assertNull(m.get(0));
for (int i = 0; i < count; i++) {
assertNull(m.get(i));
}
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testCompactMapNotOpen.
private void testCompactMapNotOpen() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName, 1000);
MVMap<Integer, String> m = s.openMap("data");
int factor = 100;
for (int j = 0; j < 10; j++) {
for (int i = j * factor; i < 10 * factor; i++) {
m.put(i, "Hello" + j);
}
s.commit();
}
s.close();
s = openStore(fileName);
s.setRetentionTime(0);
Map<String, String> meta = s.getMetaMap();
int chunkCount1 = 0;
for (String k : meta.keySet()) {
if (k.startsWith("chunk.")) {
chunkCount1++;
}
}
s.compact(80, 1);
s.compact(80, 1);
int chunkCount2 = 0;
for (String k : meta.keySet()) {
if (k.startsWith("chunk.")) {
chunkCount2++;
}
}
assertTrue(chunkCount2 >= chunkCount1);
m = s.openMap("data");
for (int i = 0; i < 10; i++) {
sleep(1);
boolean result = s.compact(50, 50 * 1024);
if (!result) {
break;
}
}
assertFalse(s.compact(50, 1024));
int chunkCount3 = 0;
for (String k : meta.keySet()) {
if (k.startsWith("chunk.")) {
chunkCount3++;
}
}
assertTrue(chunkCount1 + ">" + chunkCount2 + ">" + chunkCount3, chunkCount3 < chunkCount1);
for (int i = 0; i < 10 * factor; i++) {
assertEquals("x" + i, "Hello" + (i / factor), m.get(i));
}
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testRemoveMap.
private void testRemoveMap() throws Exception {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = new MVStore.Builder().fileName(fileName).open();
MVMap<Integer, Integer> map;
map = s.openMap("data");
map.put(1, 1);
assertEquals(1, map.get(1).intValue());
s.commit();
s.removeMap(map);
s.commit();
map = s.openMap("data");
assertTrue(map.isEmpty());
map.put(2, 2);
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testVolatileMap.
private void testVolatileMap() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore store = new MVStore.Builder().fileName(fileName).open();
MVMap<String, String> map = store.openMap("test");
assertFalse(map.isVolatile());
map.setVolatile(true);
assertTrue(map.isVolatile());
map.put("1", "Hello");
assertEquals("Hello", map.get("1"));
assertEquals(1, map.size());
store.close();
store = new MVStore.Builder().fileName(fileName).open();
assertTrue(store.hasMap("test"));
map = store.openMap("test");
assertEquals(0, map.size());
store.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testAtomicOperations.
private void testAtomicOperations() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s;
MVMap<Integer, byte[]> m;
s = new MVStore.Builder().fileName(fileName).open();
m = s.openMap("data");
// putIfAbsent
assertNull(m.putIfAbsent(1, new byte[1]));
assertEquals(1, m.putIfAbsent(1, new byte[2]).length);
assertEquals(1, m.get(1).length);
// replace
assertNull(m.replace(2, new byte[2]));
assertNull(m.get(2));
assertEquals(1, m.replace(1, new byte[2]).length);
assertEquals(2, m.replace(1, new byte[3]).length);
assertEquals(3, m.replace(1, new byte[1]).length);
// replace with oldValue
assertFalse(m.replace(1, new byte[2], new byte[10]));
assertTrue(m.replace(1, new byte[1], new byte[2]));
assertTrue(m.replace(1, new byte[2], new byte[1]));
// remove
assertFalse(m.remove(1, new byte[2]));
assertTrue(m.remove(1, new byte[1]));
s.close();
FileUtils.delete(fileName);
}
Aggregations