Search in sources :

Example 56 with MVStore

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();
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 57 with MVStore

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();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 58 with MVStore

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);
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 59 with MVStore

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();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 60 with MVStore

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));
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVStore(org.h2.mvstore.MVStore) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random)

Aggregations

MVStore (org.h2.mvstore.MVStore)123 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)69 Random (java.util.Random)23 Task (org.h2.util.Task)20 TransactionStore (org.h2.mvstore.db.TransactionStore)19 Transaction (org.h2.mvstore.db.TransactionStore.Transaction)17 MVRTreeMap (org.h2.mvstore.rtree.MVRTreeMap)8 SpatialKey (org.h2.mvstore.rtree.SpatialKey)8 IOException (java.io.IOException)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 FileStore (org.h2.mvstore.FileStore)5 StreamStore (org.h2.mvstore.StreamStore)5 ArrayList (java.util.ArrayList)4 TreeMap (java.util.TreeMap)4 Store (org.h2.mvstore.db.MVTableEngine.Store)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 PreparedStatement (java.sql.PreparedStatement)3