Search in sources :

Example 51 with MVStore

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

the class TestMVStore method testIsEmpty.

private void testIsEmpty() throws Exception {
    MVStore s = new MVStore.Builder().pageSplitSize(50).open();
    Map<Integer, byte[]> m = s.openMap("data");
    m.put(1, new byte[50]);
    m.put(2, new byte[50]);
    m.put(3, new byte[50]);
    m.remove(1);
    m.remove(2);
    m.remove(3);
    assertEquals(0, m.size());
    assertTrue(m.isEmpty());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 52 with MVStore

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

the class TestMVStore method testLargerThan2G.

private void testLargerThan2G() {
    if (!config.big) {
        return;
    }
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore store = new MVStore.Builder().cacheSize(16).fileName(fileName).open();
    try {
        MVMap<Integer, String> map = store.openMap("test");
        long last = System.nanoTime();
        String data = new String(new char[2500]).replace((char) 0, 'x');
        for (int i = 0; ; i++) {
            map.put(i, data);
            if (i % 10000 == 0) {
                store.commit();
                long time = System.nanoTime();
                if (time - last > TimeUnit.SECONDS.toNanos(2)) {
                    long mb = store.getFileStore().size() / 1024 / 1024;
                    trace(mb + "/4500");
                    if (mb > 4500) {
                        break;
                    }
                    last = time;
                }
            }
        }
        store.commit();
        store.close();
    } finally {
        store.closeImmediately();
    }
    FileUtils.delete(fileName);
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 53 with MVStore

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

the class TestMVStore method testConcurrentOpen.

private void testConcurrentOpen() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = new MVStore.Builder().fileName(fileName).open();
    try {
        MVStore s1 = new MVStore.Builder().fileName(fileName).open();
        s1.close();
        fail();
    } catch (IllegalStateException e) {
    // expected
    }
    try {
        MVStore s1 = new MVStore.Builder().fileName(fileName).readOnly().open();
        s1.close();
        fail();
    } catch (IllegalStateException e) {
    // expected
    }
    assertFalse(s.getFileStore().isReadOnly());
    s.close();
    s = new MVStore.Builder().fileName(fileName).readOnly().open();
    assertTrue(s.getFileStore().isReadOnly());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 54 with MVStore

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

the class TestMVStore method testExample.

private void testExample() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    // open the store (in-memory if fileName is null)
    MVStore s = MVStore.open(fileName);
    // create/get the map named "data"
    MVMap<Integer, String> map = s.openMap("data");
    // add and read some data
    map.put(1, "Hello World");
    // System.out.println(map.get(1));
    // close the store (this will persist changes)
    s.close();
    s = MVStore.open(fileName);
    map = s.openMap("data");
    assertEquals("Hello World", map.get(1));
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 55 with MVStore

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

the class TestMVStore method testWriteBuffer.

private void testWriteBuffer() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    MVMap<Integer, byte[]> m;
    byte[] data = new byte[1000];
    long lastSize = 0;
    int len = 1000;
    for (int bs = 0; bs <= 1; bs++) {
        s = new MVStore.Builder().fileName(fileName).autoCommitBufferSize(bs).open();
        m = s.openMap("data");
        for (int i = 0; i < len; i++) {
            m.put(i, data);
        }
        long size = s.getFileStore().size();
        assertTrue("last:" + lastSize + " now: " + size, size > lastSize);
        lastSize = size;
        s.close();
    }
    s = new MVStore.Builder().fileName(fileName).open();
    m = s.openMap("data");
    assertTrue(m.containsKey(1));
    m.put(-1, data);
    s.commit();
    m.put(-2, data);
    s.close();
    s = new MVStore.Builder().fileName(fileName).open();
    m = s.openMap("data");
    assertTrue(m.containsKey(-1));
    assertTrue(m.containsKey(-2));
    s.close();
    FileUtils.delete(fileName);
}
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