Search in sources :

Example 66 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestMVStoreStopCompact method testStopCompact.

private void testStopCompact(int retentionTime, int timeout) throws InterruptedException {
    String fileName = getBaseDir() + "/testStopCompact.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().fileName(fileName).open();
    s.setRetentionTime(retentionTime);
    MVMap<Integer, String> map = s.openMap("data");
    long start = System.currentTimeMillis();
    Random r = new Random(1);
    for (int i = 0; i < 4000000; i++) {
        long time = System.currentTimeMillis() - start;
        if (time > timeout) {
            break;
        }
        int x = r.nextInt(10000000);
        map.put(x, "Hello World " + i * 10);
    }
    s.setAutoCommitDelay(100);
    long oldWriteCount = s.getFileStore().getWriteCount();
    // expect background write to stop after 5 seconds
    Thread.sleep(5000);
    long newWriteCount = s.getFileStore().getWriteCount();
    // expect that compaction didn't cause many writes
    assertTrue(newWriteCount - oldWriteCount < 30);
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) Random(java.util.Random)

Example 67 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testDetectIllegalId.

private void testDetectIllegalId() throws IOException {
    Map<Long, byte[]> map = new HashMap<>();
    StreamStore store = new StreamStore(map);
    try {
        store.length(new byte[] { 3, 0, 0 });
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        store.remove(new byte[] { 3, 0, 0 });
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
    map.put(0L, new byte[] { 3, 0, 0 });
    InputStream in = store.get(new byte[] { 2, 1, 0 });
    try {
        in.read();
        fail();
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : StreamStore(org.h2.mvstore.StreamStore) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 68 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testLoop.

private void testLoop() throws IOException {
    Map<Long, byte[]> map = new HashMap<>();
    StreamStore store = new StreamStore(map);
    assertEquals(256 * 1024, store.getMaxBlockSize());
    assertEquals(256, store.getMinBlockSize());
    store.setNextKey(0);
    assertEquals(0, store.getNextKey());
    test(store, 10, 20, 1000);
    for (int i = 0; i < 20; i++) {
        test(store, 0, 128, i);
        test(store, 10, 128, i);
    }
    for (int i = 20; i < 200; i += 10) {
        test(store, 0, 128, i);
        test(store, 10, 128, i);
    }
}
Also used : StreamStore(org.h2.mvstore.StreamStore) HashMap(java.util.HashMap)

Example 69 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestStreamStore method testWithFullMap.

private void testWithFullMap() throws IOException {
    final AtomicInteger tests = new AtomicInteger();
    Map<Long, byte[]> map = new HashMap<Long, byte[]>() {

        private static final long serialVersionUID = 1L;

        @Override
        public boolean containsKey(Object k) {
            tests.incrementAndGet();
            if (((Long) k) < Long.MAX_VALUE / 2) {
                // simulate a *very* full map
                return true;
            }
            return super.containsKey(k);
        }
    };
    StreamStore store = new StreamStore(map);
    store.setMinBlockSize(20);
    store.setMaxBlockSize(100);
    store.setNextKey(0);
    store.put(new ByteArrayInputStream(new byte[100]));
    assertEquals(1, map.size());
    assertEquals(64, tests.get());
    assertEquals(Long.MAX_VALUE / 2 + 1, store.getNextKey());
}
Also used : StreamStore(org.h2.mvstore.StreamStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 70 with Store

use of org.h2.mvstore.db.MVTableEngine.Store in project h2database by h2database.

the class TestReorderWrites method testMVStore.

private void testMVStore() {
    FilePathReorderWrites fs = FilePathReorderWrites.register();
    String fileName = "reorder:memFS:test.mv";
    try {
        for (int i = 0; i < 1000; i++) {
            log(i + " --------------------------------");
            // this test is not interested in power off failures during
            // initial creation
            fs.setPowerOffCountdown(0, 0);
            // release the static data this test generates
            FileUtils.delete("memFS:test.mv");
            FileUtils.delete("memFS:test.mv.copy");
            MVStore store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
            // store.setRetentionTime(10);
            Map<Integer, byte[]> map = store.openMap("data");
            map.put(-1, new byte[1]);
            store.commit();
            store.getFileStore().sync();
            Random r = new Random(i);
            int stop = 4 + r.nextInt(20);
            log("countdown start");
            fs.setPowerOffCountdown(stop, i);
            try {
                for (int j = 1; j < 100; j++) {
                    Map<Integer, Integer> newMap = store.openMap("d" + j);
                    newMap.put(j, j * 10);
                    int key = r.nextInt(10);
                    int len = 10 * r.nextInt(1000);
                    if (r.nextBoolean()) {
                        map.remove(key);
                    } else {
                        map.put(key, new byte[len]);
                    }
                    log("op " + j + ": ");
                    store.commit();
                    switch(r.nextInt(10)) {
                        case 0:
                            log("op compact");
                            store.compact(100, 10 * 1024);
                            break;
                        case 1:
                            log("op compactMoveChunks");
                            store.compactMoveChunks();
                            log("op compactMoveChunks done");
                            break;
                    }
                }
                // write has to fail at some point
                fail();
            } catch (IllegalStateException e) {
                log("stop " + e + ", cause: " + e.getCause());
            // expected
            }
            try {
                store.close();
            } catch (IllegalStateException e) {
                // expected
                store.closeImmediately();
            }
            log("verify");
            fs.setPowerOffCountdown(100, 0);
            if (LOG) {
                MVStoreTool.dump(fileName, true);
            }
            store = new MVStore.Builder().fileName(fileName).autoCommitDisabled().open();
            map = store.openMap("data");
            if (!map.containsKey(-1)) {
                fail("key not found, size=" + map.size() + " i=" + i);
            } else {
                assertEquals("i=" + i, 1, map.get(-1).length);
            }
            for (int j = 0; j < 100; j++) {
                Map<Integer, Integer> newMap = store.openMap("d" + j);
                newMap.get(j);
            }
            map.keySet();
            store.close();
        }
    } finally {
        // release the static data this test generates
        FileUtils.delete("memFS:test.mv");
        FileUtils.delete("memFS:test.mv.copy");
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) FilePathReorderWrites(org.h2.test.utils.FilePathReorderWrites)

Aggregations

MVStore (org.h2.mvstore.MVStore)29 IOException (java.io.IOException)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)8 HashMap (java.util.HashMap)8 DbException (org.h2.message.DbException)8 StreamStore (org.h2.mvstore.StreamStore)8 PageStore (org.h2.store.PageStore)8 Random (java.util.Random)7 OutputStream (java.io.OutputStream)6 FileStore (org.h2.store.FileStore)6 BufferedInputStream (java.io.BufferedInputStream)5 ArrayList (java.util.ArrayList)5 FileStore (org.h2.mvstore.FileStore)5 Store (org.h2.mvstore.db.MVTableEngine.Store)5 Row (org.h2.result.Row)5 FileStoreInputStream (org.h2.store.FileStoreInputStream)5 PrintWriter (java.io.PrintWriter)4 Database (org.h2.engine.Database)4