use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testOldVersion.
private void testOldVersion() {
MVStore s;
for (int op = 0; op <= 1; op++) {
for (int i = 0; i < 5; i++) {
s = openStore(null);
s.setVersionsToKeep(Integer.MAX_VALUE);
MVMap<String, String> m;
m = s.openMap("data");
for (int j = 0; j < 5; j++) {
if (op == 1) {
m.put("1", "" + s.getCurrentVersion());
}
s.commit();
}
for (int j = 0; j < s.getCurrentVersion(); j++) {
MVMap<String, String> old = m.openVersion(j);
if (op == 1) {
assertEquals("" + j, old.get("1"));
}
}
s.close();
}
}
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testRollbackInMemory.
private void testRollbackInMemory() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName, 5);
s.setAutoCommitDelay(0);
assertEquals(0, s.getCurrentVersion());
MVMap<String, String> m = s.openMap("data");
s.rollbackTo(0);
assertTrue(m.isClosed());
assertEquals(0, s.getCurrentVersion());
m = s.openMap("data");
MVMap<String, String> m0 = s.openMap("data0");
MVMap<String, String> m2 = s.openMap("data2");
m.put("1", "Hello");
for (int i = 0; i < 10; i++) {
m2.put("" + i, "Test");
}
long v1 = s.commit();
assertEquals(1, v1);
assertEquals(1, s.getCurrentVersion());
MVMap<String, String> m1 = s.openMap("data1");
assertEquals("Test", m2.get("1"));
m.put("1", "Hallo");
m0.put("1", "Hallo");
m1.put("1", "Hallo");
m2.clear();
assertEquals("Hallo", m.get("1"));
assertEquals("Hallo", m1.get("1"));
s.rollbackTo(v1);
assertEquals(1, s.getCurrentVersion());
for (int i = 0; i < 10; i++) {
assertEquals("Test", m2.get("" + i));
}
assertEquals("Hello", m.get("1"));
assertNull(m0.get("1"));
assertTrue(m1.isClosed());
assertFalse(m0.isReadOnly());
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testTruncateFile.
private void testTruncateFile() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s;
MVMap<Integer, String> m;
s = openStore(fileName);
m = s.openMap("data");
String data = new String(new char[10000]).replace((char) 0, 'x');
for (int i = 1; i < 10; i++) {
m.put(i, data);
s.commit();
}
s.close();
long len = FileUtils.size(fileName);
s = openStore(fileName);
s.setRetentionTime(0);
// remove 75%
m = s.openMap("data");
for (int i = 0; i < 10; i++) {
if (i % 4 != 0) {
sleep(2);
m.remove(i);
s.commit();
}
}
assertTrue(s.compact(100, 50 * 1024));
s.close();
long len2 = FileUtils.size(fileName);
assertTrue("len2: " + len2 + " len: " + len, len2 < len);
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStore method testRollbackStored.
private void testRollbackStored() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVMap<String, String> meta;
MVStore s = openStore(fileName);
assertEquals(45000, s.getRetentionTime());
s.setRetentionTime(0);
assertEquals(0, s.getRetentionTime());
s.setRetentionTime(45000);
assertEquals(45000, s.getRetentionTime());
assertEquals(0, s.getCurrentVersion());
assertFalse(s.hasUnsavedChanges());
MVMap<String, String> m = s.openMap("data");
assertTrue(s.hasUnsavedChanges());
MVMap<String, String> m0 = s.openMap("data0");
m.put("1", "Hello");
assertEquals(1, s.commit());
s.rollbackTo(1);
assertEquals(1, s.getCurrentVersion());
assertEquals("Hello", m.get("1"));
// so a new version is created
m.put("1", "Hello");
long v2 = s.commit();
assertEquals(2, v2);
assertEquals(2, s.getCurrentVersion());
assertFalse(s.hasUnsavedChanges());
assertEquals("Hello", m.get("1"));
s.close();
s = openStore(fileName);
s.setRetentionTime(45000);
assertEquals(2, s.getCurrentVersion());
meta = s.getMetaMap();
m = s.openMap("data");
assertFalse(s.hasUnsavedChanges());
assertEquals("Hello", m.get("1"));
m0 = s.openMap("data0");
MVMap<String, String> m1 = s.openMap("data1");
m.put("1", "Hallo");
m0.put("1", "Hallo");
m1.put("1", "Hallo");
assertEquals("Hallo", m.get("1"));
assertEquals("Hallo", m1.get("1"));
assertTrue(s.hasUnsavedChanges());
s.rollbackTo(v2);
assertFalse(s.hasUnsavedChanges());
assertNull(meta.get("name.data1"));
assertNull(m0.get("1"));
assertEquals("Hello", m.get("1"));
assertEquals(2, s.commit());
s.close();
s = openStore(fileName);
s.setRetentionTime(45000);
assertEquals(2, s.getCurrentVersion());
meta = s.getMetaMap();
assertNotNull(meta.get("name.data"));
assertNotNull(meta.get("name.data0"));
assertNull(meta.get("name.data1"));
m = s.openMap("data");
m0 = s.openMap("data0");
assertNull(m0.get("1"));
assertEquals("Hello", m.get("1"));
assertFalse(m0.isReadOnly());
m.put("1", "Hallo");
s.commit();
long v3 = s.getCurrentVersion();
assertEquals(3, v3);
s.close();
s = openStore(fileName);
s.setRetentionTime(45000);
assertEquals(3, s.getCurrentVersion());
m = s.openMap("data");
m.put("1", "Hi");
s.close();
s = openStore(fileName);
s.setRetentionTime(45000);
m = s.openMap("data");
assertEquals("Hi", m.get("1"));
s.rollbackTo(v3);
assertEquals("Hallo", m.get("1"));
s.close();
s = openStore(fileName);
s.setRetentionTime(45000);
m = s.openMap("data");
assertEquals("Hallo", m.get("1"));
s.close();
}
use of org.h2.mvstore.MVStore in project h2database by h2database.
the class TestMVStoreTool method testCompact.
private void testCompact() {
String fileName = getBaseDir() + "/testCompact.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().pageSplitSize(1000).fileName(fileName).autoCommitDisabled().open();
MVMap<Integer, String> map = s.openMap("data");
for (int i = 0; i < 10; i++) {
map.put(i, "Hello World " + i * 10);
if (i % 3 == 0) {
s.commit();
}
}
for (int i = 0; i < 20; i++) {
map = s.openMap("data" + i);
for (int j = 0; j < i * i; j++) {
map.put(j, "Hello World " + j * 10);
}
s.commit();
}
MVRTreeMap<String> rTreeMap = s.openMap("rtree", new MVRTreeMap.Builder<String>());
Random r = new Random(1);
for (int i = 0; i < 10; i++) {
float x = r.nextFloat();
float y = r.nextFloat();
float width = r.nextFloat() / 10;
float height = r.nextFloat() / 10;
SpatialKey k = new SpatialKey(i, x, x + width, y, y + height);
rTreeMap.put(k, "Hello World " + i * 10);
if (i % 3 == 0) {
s.commit();
}
}
s.close();
MVStoreTool.compact(fileName, fileName + ".new", false);
MVStoreTool.compact(fileName, fileName + ".new.compress", true);
MVStore s1 = new MVStore.Builder().fileName(fileName).readOnly().open();
MVStore s2 = new MVStore.Builder().fileName(fileName + ".new").readOnly().open();
MVStore s3 = new MVStore.Builder().fileName(fileName + ".new.compress").readOnly().open();
assertEquals(s1, s2);
assertEquals(s1, s3);
s1.close();
s2.close();
s3.close();
long size1 = FileUtils.size(fileName);
long size2 = FileUtils.size(fileName + ".new");
long size3 = FileUtils.size(fileName + ".new.compress");
assertTrue("size1: " + size1 + " size2: " + size2 + " size3: " + size3, size2 < size1 && size3 < size2);
MVStoreTool.compact(fileName, false);
assertEquals(size2, FileUtils.size(fileName));
MVStoreTool.compact(fileName, true);
assertEquals(size3, FileUtils.size(fileName));
}
Aggregations