Search in sources :

Example 21 with TransactionMap

use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.

the class TestTransactionStore method testConcurrentAdd.

private void testConcurrentAdd() {
    MVStore s;
    s = MVStore.open(null);
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    final Random r = new Random(1);
    final AtomicInteger key = new AtomicInteger();
    final AtomicInteger failCount = new AtomicInteger();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Transaction tx = null;
            TransactionMap<Integer, Integer> map = null;
            while (!stop) {
                int k = key.get();
                tx = ts.begin();
                map = tx.openMap("data");
                try {
                    map.put(k, r.nextInt());
                } catch (IllegalStateException e) {
                    failCount.incrementAndGet();
                // ignore and retry
                }
                tx.commit();
            }
        }
    };
    task.execute();
    Transaction tx = null;
    int count = 100000;
    TransactionMap<Integer, Integer> map = null;
    for (int i = 0; i < count; i++) {
        int k = i;
        key.set(k);
        tx = ts.begin();
        map = tx.openMap("data");
        try {
            map.put(k, r.nextInt());
        } catch (IllegalStateException e) {
            failCount.incrementAndGet();
        // ignore and retry
        }
        tx.commit();
        if (failCount.get() > 0 && i > 4000) {
            // stop earlier, if possible
            count = i;
            break;
        }
    }
    // we expect at least 10% the operations were successful
    assertTrue(failCount.toString() + " >= " + (count * 0.9), failCount.get() < count * 0.9);
    // we expect at least a few failures
    assertTrue(failCount.toString(), failCount.get() > 0);
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Task(org.h2.util.Task) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 22 with TransactionMap

use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.

the class TestTransactionStore method testSingleConnection.

private void testSingleConnection() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    // add, rollback
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    assertEquals("Hello", m.get("1"));
    m.put("2", "World");
    assertEquals("World", m.get("2"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    // add, commit
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    m.put("2", "World");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    tx.commit();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    // update+delete+insert, rollback
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    assertNull(m.get("3"));
    // update+delete+insert, commit
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.commit();
    tx = ts.begin();
    m = tx.openMap("test");
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    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 23 with TransactionMap

use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.

the class TestTransactionStore method testMultiStatement.

/**
 * Tests behavior when used for a sequence of SQL statements. Each statement
 * uses a savepoint. Within a statement, changes by the statement itself are
 * not seen; the change is only seen when the statement finished.
 * <p>
 * Update statements that change the key of multiple rows may use delete/add
 * pairs to do so (they don't need to first delete all entries and then
 * re-add them). Trying to add multiple values for the same key is not
 * allowed (an update statement that would result in a duplicate key).
 */
private void testMultiStatement() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    long startUpdate;
    tx = ts.begin();
    // start of statement
    // create table test
    startUpdate = tx.setSavepoint();
    m = tx.openMap("test");
    m.setSavepoint(startUpdate);
    // start of statement
    // insert into test(id, name) values(1, 'Hello'), (2, 'World')
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    assertTrue(m.trySet("1", "Hello", true));
    assertTrue(m.trySet("2", "World", true));
    // not seen yet (within the same statement)
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    // start of statement
    startUpdate = tx.setSavepoint();
    // now we see the newest version
    m.setSavepoint(startUpdate);
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    // update test set primaryKey = primaryKey + 1
    // (this is usually a tricky case)
    assertEquals("Hello", m.get("1"));
    assertTrue(m.trySet("1", null, true));
    assertTrue(m.trySet("2", "Hello", true));
    assertEquals("World", m.get("2"));
    // already updated by this statement, so it has no effect
    // but still returns true because it was changed by this transaction
    assertTrue(m.trySet("2", null, true));
    assertTrue(m.trySet("3", "World", true));
    // not seen within this statement
    assertEquals("Hello", m.get("1"));
    assertEquals("World", m.get("2"));
    assertNull(m.get("3"));
    // start of statement
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    // select * from test
    assertNull(m.get("1"));
    assertEquals("Hello", m.get("2"));
    assertEquals("World", m.get("3"));
    // start of statement
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    // update test set id = 1
    // should fail: duplicate key
    assertTrue(m.trySet("2", null, true));
    assertTrue(m.trySet("1", "Hello", true));
    assertTrue(m.trySet("3", null, true));
    assertFalse(m.trySet("1", "World", true));
    tx.rollbackToSavepoint(startUpdate);
    startUpdate = tx.setSavepoint();
    m.setSavepoint(startUpdate);
    assertNull(m.get("1"));
    assertEquals("Hello", m.get("2"));
    assertEquals("World", m.get("3"));
    tx.commit();
    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 24 with TransactionMap

use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.

the class TestTransactionStore method testConcurrentAddRemove.

private static void testConcurrentAddRemove() throws InterruptedException {
    MVStore s = MVStore.open(null);
    int threadCount = 3;
    final int keyCount = 2;
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    final Random r = new Random(1);
    Task[] tasks = new Task[threadCount];
    for (int i = 0; i < threadCount; i++) {
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                TransactionMap<Integer, Integer> map = null;
                while (!stop) {
                    Transaction tx = ts.begin();
                    map = tx.openMap("data");
                    int k = r.nextInt(keyCount);
                    try {
                        map.remove(k);
                        map.put(k, r.nextInt());
                    } catch (IllegalStateException e) {
                    // ignore and retry
                    }
                    tx.commit();
                }
            }
        };
        task.execute();
        tasks[i] = task;
    }
    Thread.sleep(1000);
    for (Task t : tasks) {
        t.get();
    }
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Task(org.h2.util.Task) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 25 with TransactionMap

use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.

the class TestTransactionStore method testRepeatedChange.

private void testRepeatedChange() {
    MVStore s;
    TransactionStore ts;
    s = MVStore.open(null);
    ts = new TransactionStore(s);
    ts.init();
    Transaction tx0 = ts.begin();
    TransactionMap<Integer, Integer> map0 = tx0.openMap("data");
    map0.put(1, -1);
    tx0.commit();
    Transaction tx = ts.begin();
    TransactionMap<Integer, Integer> map = tx.openMap("data");
    for (int i = 0; i < 2000; i++) {
        map.put(1, i);
    }
    Transaction tx2 = ts.begin();
    TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
    assertEquals(-1, 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)

Aggregations

Transaction (org.h2.mvstore.db.TransactionStore.Transaction)19 MVStore (org.h2.mvstore.MVStore)17 TransactionStore (org.h2.mvstore.db.TransactionStore)17 Value (org.h2.value.Value)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ValueArray (org.h2.value.ValueArray)6 Random (java.util.Random)5 VersionedValue (org.h2.mvstore.db.TransactionStore.VersionedValue)4 SpatialKey (org.h2.mvstore.rtree.SpatialKey)4 Task (org.h2.util.Task)4 ValueLong (org.h2.value.ValueLong)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Entry (java.util.Map.Entry)1 MetaRecord (org.h2.engine.MetaRecord)1 Session (org.h2.engine.Session)1 DbException (org.h2.message.DbException)1 Change (org.h2.mvstore.db.TransactionStore.Change)1