Search in sources :

Example 26 with MVStore

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

the class TestTransactionStore method testConcurrentTransactionsReadCommitted.

private void testConcurrentTransactionsReadCommitted() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx1, tx2;
    TransactionMap<String, String> m1, m2;
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hi");
    m1.put("3", ".");
    tx1.commit();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hello");
    m1.put("2", "World");
    m1.remove("3");
    tx1.commit();
    // start new transaction to read old data
    tx2 = ts.begin();
    m2 = tx2.openMap("test");
    // start transaction tx1, update/delete/add
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("1", "Hallo");
    m1.remove("2");
    m1.put("3", "!");
    assertEquals("Hello", m2.get("1"));
    assertEquals("World", m2.get("2"));
    assertNull(m2.get("3"));
    tx1.commit();
    assertEquals("Hallo", m2.get("1"));
    assertNull(m2.get("2"));
    assertEquals("!", m2.get("3"));
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    m1.put("2", "World");
    assertNull(m2.get("2"));
    assertFalse(m2.tryRemove("2"));
    assertFalse(m2.tryPut("2", "Welt"));
    tx2 = ts.begin();
    m2 = tx2.openMap("test");
    assertNull(m2.get("2"));
    m1.remove("2");
    assertNull(m2.get("2"));
    tx1.commit();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    assertNull(m1.get("2"));
    m1.put("2", "World");
    m1.put("2", "Welt");
    tx1.rollback();
    tx1 = ts.begin();
    m1 = tx1.openMap("test");
    assertNull(m1.get("2"));
    ts.close();
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 27 with MVStore

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

the class TestTransactionStore method testConcurrentUpdate.

private void testConcurrentUpdate() {
    MVStore s;
    TransactionStore ts;
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    Transaction tx1 = ts.begin();
    TransactionMap<Integer, Integer> map1 = tx1.openMap("data");
    map1.put(1, 10);
    Transaction tx2 = ts.begin();
    TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
    try {
        map2.put(1, 20);
        fail();
    } catch (IllegalStateException e) {
        assertEquals(DataUtils.ERROR_TRANSACTION_LOCKED, DataUtils.getErrorCode(e.getMessage()));
    }
    assertEquals(10, map1.get(1).intValue());
    assertNull(map2.get(1));
    tx1.commit();
    assertEquals(10, map2.get(1).intValue());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 28 with MVStore

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

the class TestTransactionStore method testHCLFKey.

private void testHCLFKey() {
    MVStore s = MVStore.open(null);
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction t = ts.begin();
    ObjectDataType keyType = new ObjectDataType();
    TransactionMap<Long, Long> map = t.openMap("test", keyType, keyType);
    // firstKey()
    assertNull(map.firstKey());
    // lastKey()
    assertNull(map.lastKey());
    map.put(10L, 100L);
    map.put(20L, 200L);
    map.put(30L, 300L);
    map.put(40L, 400L);
    t.commit();
    t = ts.begin();
    map = t.openMap("test", keyType, keyType);
    map.put(15L, 150L);
    // The same transaction
    assertEquals((Object) 15L, map.higherKey(10L));
    t = ts.begin();
    map = t.openMap("test", keyType, keyType);
    // Another transaction
    // higherKey()
    assertEquals((Object) 20L, map.higherKey(10L));
    assertEquals((Object) 20L, map.higherKey(15L));
    assertNull(map.higherKey(40L));
    // ceilingKey()
    assertEquals((Object) 10L, map.ceilingKey(10L));
    assertEquals((Object) 20L, map.ceilingKey(15L));
    assertEquals((Object) 40L, map.ceilingKey(40L));
    assertNull(map.higherKey(45L));
    // lowerKey()
    assertNull(map.lowerKey(10L));
    assertEquals((Object) 10L, map.lowerKey(15L));
    assertEquals((Object) 10L, map.lowerKey(20L));
    assertEquals((Object) 20L, map.lowerKey(25L));
    // floorKey()
    assertNull(map.floorKey(5L));
    assertEquals((Object) 10L, map.floorKey(10L));
    assertEquals((Object) 10L, map.floorKey(15L));
    assertEquals((Object) 30L, map.floorKey(35L));
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) ObjectDataType(org.h2.mvstore.type.ObjectDataType)

Example 29 with MVStore

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

the class TestTransactionStore method testCountWithOpenTransactions.

private void testCountWithOpenTransactions() {
    MVStore s;
    TransactionStore ts;
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    Transaction tx1 = ts.begin();
    TransactionMap<Integer, Integer> map1 = tx1.openMap("data");
    int size = 150;
    for (int i = 0; i < size; i++) {
        map1.put(i, i * 10);
    }
    tx1.commit();
    tx1 = ts.begin();
    map1 = tx1.openMap("data");
    Transaction tx2 = ts.begin();
    TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
    Random r = new Random(1);
    for (int i = 0; i < size * 3; i++) {
        assertEquals("op: " + i, size, (int) map1.sizeAsLong());
        // keep the first 10%, and add 10%
        int k = size / 10 + r.nextInt(size);
        if (r.nextBoolean()) {
            map2.remove(k);
        } else {
            map2.put(k, i);
        }
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Random(java.util.Random)

Example 30 with MVStore

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

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) 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