Search in sources :

Example 41 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestMVStore method testFileHeader.

private void testFileHeader() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    s.setRetentionTime(Integer.MAX_VALUE);
    long time = System.currentTimeMillis();
    Map<String, Object> m = s.getStoreHeader();
    assertEquals("1", m.get("format").toString());
    long creationTime = (Long) m.get("created");
    assertTrue(Math.abs(time - creationTime) < 100);
    m.put("test", "123");
    forceWriteStoreHeader(s);
    s.close();
    s = openStore(fileName);
    Object test = s.getStoreHeader().get("test");
    assertNotNull(test);
    assertEquals("123", test.toString());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 42 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestMVStore method testRemoveMapRollback.

private void testRemoveMapRollback() {
    MVStore store = new MVStore.Builder().open();
    MVMap<String, String> map = store.openMap("test");
    map.put("1", "Hello");
    store.commit();
    store.removeMap(map);
    store.rollback();
    assertTrue(store.hasMap("test"));
    map = store.openMap("test");
    // TODO the data should get back alive
    assertNull(map.get("1"));
    store.close();
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    store = new MVStore.Builder().autoCommitDisabled().fileName(fileName).open();
    map = store.openMap("test");
    map.put("1", "Hello");
    store.commit();
    store.removeMap(map);
    store.rollback();
    assertTrue(store.hasMap("test"));
    map = store.openMap("test");
    // the data will get back alive
    assertEquals("Hello", map.get("1"));
    store.close();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 43 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestMVStore method testVersionsToKeep2.

private void testVersionsToKeep2() {
    MVStore s = new MVStore.Builder().autoCommitDisabled().open();
    s.setVersionsToKeep(2);
    final MVMap<Integer, String> m = s.openMap("data");
    s.commit();
    assertEquals(1, s.getCurrentVersion());
    m.put(1, "version 1");
    s.commit();
    assertEquals(2, s.getCurrentVersion());
    m.put(1, "version 2");
    s.commit();
    assertEquals(3, s.getCurrentVersion());
    m.put(1, "version 3");
    s.commit();
    m.put(1, "version 4");
    assertEquals("version 4", m.openVersion(4).get(1));
    assertEquals("version 3", m.openVersion(3).get(1));
    assertEquals("version 2", m.openVersion(2).get(1));
    new AssertThrows(IllegalArgumentException.class) {

        @Override
        public void test() throws Exception {
            m.openVersion(1);
        }
    };
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertThrows(org.h2.test.utils.AssertThrows)

Example 44 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestMVStore method testCustomMapType.

private void testCustomMapType() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    SequenceMap seq = s.openMap("data", new SequenceMap.Builder());
    StringBuilder buff = new StringBuilder();
    for (long x : seq.keySet()) {
        buff.append(x).append(';');
    }
    assertEquals("1;2;3;4;5;6;7;8;9;10;", buff.toString());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 45 with MVStore

use of org.h2.mvstore.MVStore in project h2database by h2database.

the class TestMVStore method testNewerWriteVersion.

private void testNewerWriteVersion() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = new MVStore.Builder().encryptionKey("007".toCharArray()).fileName(fileName).open();
    s.setRetentionTime(Integer.MAX_VALUE);
    Map<String, Object> header = s.getStoreHeader();
    assertEquals("1", header.get("format").toString());
    header.put("formatRead", "1");
    header.put("format", "2");
    forceWriteStoreHeader(s);
    MVMap<Integer, String> m = s.openMap("data");
    forceWriteStoreHeader(s);
    m.put(0, "Hello World");
    s.close();
    try {
        s = new MVStore.Builder().encryptionKey("007".toCharArray()).fileName(fileName).open();
        header = s.getStoreHeader();
        fail(header.toString());
    } catch (IllegalStateException e) {
        assertEquals(DataUtils.ERROR_UNSUPPORTED_FORMAT, DataUtils.getErrorCode(e.getMessage()));
    }
    s = new MVStore.Builder().encryptionKey("007".toCharArray()).readOnly().fileName(fileName).open();
    assertTrue(s.getFileStore().isReadOnly());
    m = s.openMap("data");
    assertEquals("Hello World", m.get(0));
    s.close();
    FileUtils.setReadOnly(fileName);
    s = new MVStore.Builder().encryptionKey("007".toCharArray()).fileName(fileName).open();
    assertTrue(s.getFileStore().isReadOnly());
    m = s.openMap("data");
    assertEquals("Hello World", m.get(0));
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

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