Search in sources :

Example 96 with MVStore

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

the class TestMVStore method testReuseSpace.

private void testReuseSpace() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    long initialLength = 0;
    for (int j = 0; j < 20; j++) {
        sleep(2);
        MVStore s = openStore(fileName);
        s.setRetentionTime(0);
        MVMap<Integer, String> m = s.openMap("data");
        for (int i = 0; i < 10; i++) {
            m.put(i, "Hello");
        }
        s.commit();
        for (int i = 0; i < 10; i++) {
            assertEquals("Hello", m.get(i));
            assertEquals("Hello", m.remove(i));
        }
        s.close();
        long len = FileUtils.size(fileName);
        if (initialLength == 0) {
            initialLength = len;
        } else {
            assertTrue("len: " + len + " initial: " + initialLength + " j: " + j, len <= initialLength * 5);
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 97 with MVStore

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

the class TestMVStore method testOffHeapStorage.

private void testOffHeapStorage() throws Exception {
    OffHeapStore offHeap = new OffHeapStore();
    MVStore s = new MVStore.Builder().fileStore(offHeap).open();
    int count = 1000;
    Map<Integer, String> map = s.openMap("data");
    for (int i = 0; i < count; i++) {
        map.put(i, "Hello " + i);
        s.commit();
    }
    assertTrue(offHeap.getWriteCount() > count);
    s.close();
    s = new MVStore.Builder().fileStore(offHeap).open();
    map = s.openMap("data");
    for (int i = 0; i < count; i++) {
        assertEquals("Hello " + i, map.get(i));
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffHeapStore(org.h2.mvstore.OffHeapStore)

Example 98 with MVStore

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

the class TestMVStore method testFileHeaderCorruption.

private void testFileHeaderCorruption() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).autoCommitDisabled().open();
    s.setRetentionTime(0);
    MVMap<Integer, byte[]> map;
    map = s.openMap("test");
    map.put(0, new byte[100]);
    for (int i = 0; i < 10; i++) {
        map = s.openMap("test" + i);
        map.put(0, new byte[1000]);
        s.commit();
    }
    FileStore fs = s.getFileStore();
    long size = fs.getFile().size();
    for (int i = 0; i < 100; i++) {
        map = s.openMap("test" + i);
        s.removeMap(map);
        s.commit();
        s.compact(100, 1);
        if (fs.getFile().size() <= size) {
            break;
        }
    }
    // the last chunk is at the end
    s.setReuseSpace(false);
    map = s.openMap("test2");
    map.put(1, new byte[1000]);
    s.close();
    FilePath f = FilePath.get(fileName);
    int blockSize = 4 * 1024;
    // test corrupt file headers
    for (int i = 0; i <= blockSize; i += blockSize) {
        FileChannel fc = f.open("rw");
        if (i == 0) {
            // corrupt the last block (the end header)
            fc.write(ByteBuffer.allocate(256), fc.size() - 256);
        }
        ByteBuffer buff = ByteBuffer.allocate(4 * 1024);
        fc.read(buff, i);
        String h = new String(buff.array(), StandardCharsets.UTF_8).trim();
        int idx = h.indexOf("fletcher:");
        int old = Character.digit(h.charAt(idx + "fletcher:".length()), 16);
        int bad = (old + 1) & 15;
        buff.put(idx + "fletcher:".length(), (byte) Character.forDigit(bad, 16));
        buff.rewind();
        fc.write(buff, i);
        fc.close();
        if (i == 0) {
            // if the first header is corrupt, the second
            // header should be used
            s = openStore(fileName);
            map = s.openMap("test");
            assertEquals(100, map.get(0).length);
            map = s.openMap("test2");
            assertFalse(map.containsKey(1));
            s.close();
        } else {
            // both headers are corrupt
            try {
                s = openStore(fileName);
                fail();
            } catch (Exception e) {
            // expected
            }
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FilePath(org.h2.store.fs.FilePath) FileStore(org.h2.mvstore.FileStore) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Example 99 with MVStore

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

the class TestMVStore method testFileFormatExample.

private void testFileFormatExample() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = MVStore.open(fileName);
    MVMap<Integer, String> map = s.openMap("data");
    for (int i = 0; i < 400; i++) {
        map.put(i, "Hello");
    }
    s.commit();
    for (int i = 0; i < 100; i++) {
        map.put(0, "Hi");
    }
    s.commit();
    s.close();
// ;MVStoreTool.dump(fileName);
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 100 with MVStore

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

the class TestMVStore method testInMemory.

private void testInMemory() {
    for (int j = 0; j < 1; j++) {
        MVStore s = openStore(null);
        // s.setMaxPageSize(10);
        int len = 100;
        // TreeMap<Integer, String> m = new TreeMap<Integer, String>();
        // HashMap<Integer, String> m = New.hashMap();
        MVMap<Integer, String> m = s.openMap("data");
        for (int i = 0; i < len; i++) {
            assertNull(m.put(i, "Hello World"));
        }
        for (int i = 0; i < len; i++) {
            assertEquals("Hello World", m.get(i));
        }
        for (int i = 0; i < len; i++) {
            assertEquals("Hello World", m.remove(i));
        }
        assertEquals(null, m.get(0));
        assertEquals(0, m.size());
        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