Search in sources :

Example 76 with MVStore

use of org.h2.mvstore.MVStore 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 77 with MVStore

use of org.h2.mvstore.MVStore 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 78 with MVStore

use of org.h2.mvstore.MVStore 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 79 with MVStore

use of org.h2.mvstore.MVStore 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 80 with MVStore

use of org.h2.mvstore.MVStore 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

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