Search in sources :

Example 46 with MVStore

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

Example 47 with MVStore

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

Example 48 with MVStore

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

Example 49 with MVStore

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

Example 50 with MVStore

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

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