Search in sources :

Example 1 with TransactionStore

use of org.h2.mvstore.db.TransactionStore 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)

Example 2 with TransactionStore

use of org.h2.mvstore.db.TransactionStore 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 3 with TransactionStore

use of org.h2.mvstore.db.TransactionStore 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 4 with TransactionStore

use of org.h2.mvstore.db.TransactionStore 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 5 with TransactionStore

use of org.h2.mvstore.db.TransactionStore 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)

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