Search in sources :

Example 21 with MVStore

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

the class TestMVRTree method testRandomInsert.

private void testRandomInsert() {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s;
    s = new MVStore.Builder().fileName(fileName).pageSplitSize(100).open();
    MVRTreeMap<String> map = s.openMap("data", new MVRTreeMap.Builder<String>());
    Random r = new Random(1);
    for (int i = 0; i < 1000; i++) {
        if (i % 100 == 0) {
            r.setSeed(1);
        }
        float x = r.nextFloat() * 50, y = r.nextFloat() * 50;
        SpatialKey k = new SpatialKey(i % 100, x, x + 2, y, y + 1);
        map.put(k, "i:" + i);
        if (i % 10 == 0) {
            s.commit();
        }
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) SpatialKey(org.h2.mvstore.rtree.SpatialKey) MVRTreeMap(org.h2.mvstore.rtree.MVRTreeMap) Random(java.util.Random)

Example 22 with MVStore

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

the class TestMVTableEngine method testGarbageCollectionForLOB.

private void testGarbageCollectionForLOB() throws SQLException {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int, data blob)");
    stat.execute("insert into test select x, repeat('0', 10000) " + "from system_range(1, 10)");
    stat.execute("drop table test");
    stat.execute("create table test2(id int, data blob)");
    PreparedStatement prep = conn.prepareStatement("insert into test2 values(?, ?)");
    prep.setInt(1, 1);
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setBinaryStream(1, createFailingStream(new IOException()));
    prep.setInt(1, 2);
    assertThrows(ErrorCode.IO_EXCEPTION_1, prep).setBinaryStream(1, createFailingStream(new IllegalStateException()));
    conn.close();
    MVStore s = MVStore.open(getBaseDir() + "/" + getTestName() + ".mv.db");
    assertTrue(s.hasMap("lobData"));
    MVMap<Long, byte[]> lobData = s.openMap("lobData");
    assertEquals(0, lobData.sizeAsLong());
    assertTrue(s.hasMap("lobMap"));
    MVMap<Long, byte[]> lobMap = s.openMap("lobMap");
    assertEquals(0, lobMap.sizeAsLong());
    assertTrue(s.hasMap("lobRef"));
    MVMap<Long, byte[]> lobRef = s.openMap("lobRef");
    assertEquals(0, lobRef.sizeAsLong());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 23 with MVStore

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

the class TestRandomMapOps method openStore.

private static MVStore openStore(String fileName) {
    MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(50).autoCommitDisabled().open();
    s.setRetentionTime(1000);
    return s;
}
Also used : MVStore(org.h2.mvstore.MVStore)

Example 24 with MVStore

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

the class TestRandomMapOps method testOps.

private void testOps(String fileName, int size, int seed) {
    FileUtils.delete(fileName);
    MVStore s = openStore(fileName);
    MVMap<Integer, byte[]> m = s.openMap("data");
    Random r = new Random(seed);
    op = 0;
    TreeMap<Integer, byte[]> map = new TreeMap<>();
    for (; op < size; op++) {
        int k = r.nextInt(100);
        byte[] v = new byte[r.nextInt(10) * 10];
        int type = r.nextInt(12);
        switch(type) {
            case 0:
            case 1:
            case 2:
            case 3:
                log(op, k, v, "m.put({0}, {1})");
                m.put(k, v);
                map.put(k, v);
                break;
            case 4:
            case 5:
                log(op, k, v, "m.remove({0})");
                m.remove(k);
                map.remove(k);
                break;
            case 6:
                log(op, k, v, "s.compact(90, 1024)");
                s.compact(90, 1024);
                break;
            case 7:
                log(op, k, v, "m.clear()");
                m.clear();
                map.clear();
                break;
            case 8:
                log(op, k, v, "s.commit()");
                s.commit();
                break;
            case 9:
                log(op, k, v, "s.commit()");
                s.commit();
                log(op, k, v, "s.close()");
                s.close();
                log(op, k, v, "s = openStore(fileName)");
                s = openStore(fileName);
                log(op, k, v, "m = s.openMap(\"data\")");
                m = s.openMap("data");
                break;
            case 10:
                log(op, k, v, "s.commit()");
                s.commit();
                log(op, k, v, "s.compactMoveChunks()");
                s.compactMoveChunks();
                break;
            case 11:
                log(op, k, v, "m.getKeyIndex({0})");
                ArrayList<Integer> keyList = new ArrayList<>(map.keySet());
                int index = Collections.binarySearch(keyList, k, null);
                int index2 = (int) m.getKeyIndex(k);
                assertEquals(index, index2);
                if (index >= 0) {
                    int k2 = m.getKey(index);
                    assertEquals(k2, k);
                }
                break;
        }
        assertEqualsMapValues(map.get(k), m.get(k));
        assertEquals(map.ceilingKey(k), m.ceilingKey(k));
        assertEquals(map.floorKey(k), m.floorKey(k));
        assertEquals(map.higherKey(k), m.higherKey(k));
        assertEquals(map.lowerKey(k), m.lowerKey(k));
        assertEquals(map.isEmpty(), m.isEmpty());
        assertEquals(map.size(), m.size());
        if (!map.isEmpty()) {
            assertEquals(map.firstKey(), m.firstKey());
            assertEquals(map.lastKey(), m.lastKey());
        }
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap)

Example 25 with MVStore

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

the class TestTransactionStore method testTransactionAge.

private void testTransactionAge() throws Exception {
    MVStore s;
    TransactionStore ts;
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    ts.setMaxTransactionId(16);
    ArrayList<Transaction> openList = new ArrayList<>();
    for (int i = 0, j = 1; i < 64; i++) {
        Transaction t = ts.begin();
        openList.add(t);
        assertEquals(j, t.getId());
        j++;
        if (j > 16) {
            j = 1;
        }
        if (openList.size() >= 16) {
            t = openList.remove(0);
            t.commit();
        }
    }
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    ts.setMaxTransactionId(16);
    ArrayList<Transaction> fifo = New.arrayList();
    int open = 0;
    for (int i = 0; i < 64; i++) {
        Transaction t = null;
        if (open >= 16) {
            try {
                t = ts.begin();
                fail();
            } catch (IllegalStateException e) {
            // expected - too many open
            }
            Transaction first = fifo.remove(0);
            first.commit();
            open--;
        }
        t = ts.begin();
        t.openMap("data").put(i, i);
        fifo.add(t);
        open++;
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) ArrayList(java.util.ArrayList)

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