Search in sources :

Example 16 with TransactionStore

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

the class TestTransactionStore method testKeyIterator.

private void testKeyIterator() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx, tx2;
    TransactionMap<String, String> m, m2;
    Iterator<String> it, it2;
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    m.put("2", "World");
    m.put("3", ".");
    tx.commit();
    tx2 = ts.begin();
    m2 = tx2.openMap("test");
    m2.remove("2");
    m2.put("3", "!");
    m2.put("4", "?");
    tx = ts.begin();
    m = tx.openMap("test");
    it = m.keyIterator(null);
    assertTrue(it.hasNext());
    assertEquals("1", it.next());
    assertTrue(it.hasNext());
    assertEquals("2", it.next());
    assertTrue(it.hasNext());
    assertEquals("3", it.next());
    assertFalse(it.hasNext());
    it2 = m2.keyIterator(null);
    assertTrue(it2.hasNext());
    assertEquals("1", it2.next());
    assertTrue(it2.hasNext());
    assertEquals("3", it2.next());
    assertTrue(it2.hasNext());
    assertEquals("4", it2.next());
    assertFalse(it2.hasNext());
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 17 with TransactionStore

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

the class TestTransactionStore method testSavepoint.

private void testSavepoint() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m;
    tx = ts.begin();
    m = tx.openMap("test");
    m.put("1", "Hello");
    m.put("2", "World");
    m.put("1", "Hallo");
    m.remove("2");
    m.put("3", "!");
    long logId = tx.setSavepoint();
    m.put("1", "Hi");
    m.put("2", ".");
    m.remove("3");
    tx.rollbackToSavepoint(logId);
    assertEquals("Hallo", m.get("1"));
    assertNull(m.get("2"));
    assertEquals("!", m.get("3"));
    tx.rollback();
    tx = ts.begin();
    m = tx.openMap("test");
    assertNull(m.get("1"));
    assertNull(m.get("2"));
    assertNull(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 18 with TransactionStore

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

the class TestTransactionStore method testStoreMultiThreadedReads.

private static void testStoreMultiThreadedReads() throws Exception {
    MVStore s = MVStore.open(null);
    final TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction t = ts.begin();
    TransactionMap<Integer, Integer> mapA = t.openMap("a");
    mapA.put(1, 0);
    t.commit();
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            for (int i = 0; !stop; i++) {
                Transaction tx = ts.begin();
                TransactionMap<Integer, Integer> mapA = tx.openMap("a");
                while (!mapA.tryPut(1, i)) {
                // repeat
                }
                tx.commit();
                // map B transaction
                // the other thread will get a map A uncommitted value,
                // but by the time it tries to walk back to the committed
                // value, the undoLog has changed
                tx = ts.begin();
                TransactionMap<Integer, Integer> mapB = tx.openMap("b");
                // put a new value to the map; this will cause a map B
                // undoLog entry to be created with a null pre-image value
                mapB.tryPut(i, -i);
                // this is where the real race condition occurs:
                // some other thread might get the B log entry
                // for this transaction rather than the uncommitted A log
                // entry it is expecting
                tx.commit();
            }
        }
    };
    task.execute();
    try {
        for (int i = 0; i < 10000; i++) {
            Transaction tx = ts.begin();
            mapA = tx.openMap("a");
            if (mapA.get(1) == null) {
                throw new AssertionError("key not found");
            }
            tx.commit();
        }
    } finally {
        task.get();
    }
    ts.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) Transaction(org.h2.mvstore.db.TransactionStore.Transaction)

Example 19 with TransactionStore

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

the class TestTransactionStore method testGetModifiedMaps.

private void testGetModifiedMaps() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    ts.init();
    Transaction tx;
    TransactionMap<String, String> m1, m2, m3;
    long sp;
    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    assertFalse(tx.getChanges(0).hasNext());
    tx.commit();
    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    m1.put("1", "100");
    sp = tx.setSavepoint();
    m2.put("1", "100");
    m3.put("1", "100");
    Iterator<Change> it = tx.getChanges(sp);
    assertTrue(it.hasNext());
    Change c;
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    tx.rollbackToSavepoint(sp);
    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());
    tx.commit();
    s.close();
}
Also used : MVStore(org.h2.mvstore.MVStore) TransactionStore(org.h2.mvstore.db.TransactionStore) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Change(org.h2.mvstore.db.TransactionStore.Change)

Aggregations

MVStore (org.h2.mvstore.MVStore)19 TransactionStore (org.h2.mvstore.db.TransactionStore)19 Transaction (org.h2.mvstore.db.TransactionStore.Transaction)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Random (java.util.Random)5 Task (org.h2.util.Task)4 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Savepoint (java.sql.Savepoint)1 ArrayList (java.util.ArrayList)1 MetaRecord (org.h2.engine.MetaRecord)1 JdbcConnection (org.h2.jdbc.JdbcConnection)1 Change (org.h2.mvstore.db.TransactionStore.Change)1 TransactionMap (org.h2.mvstore.db.TransactionStore.TransactionMap)1 ValueDataType (org.h2.mvstore.db.ValueDataType)1 ObjectDataType (org.h2.mvstore.type.ObjectDataType)1 SimpleRow (org.h2.result.SimpleRow)1