Search in sources :

Example 1 with TransactionMap

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

the class MVSecondaryIndex method find.

private Cursor find(Session session, SearchRow first, boolean bigger, SearchRow last) {
    ValueArray min = convertToKey(first);
    if (min != null) {
        min.getList()[keyColumns - 1] = ValueLong.MIN;
    }
    TransactionMap<Value, Value> map = getMap(session);
    if (bigger && min != null) {
        // search for the next: first skip 1, then 2, 4, 8, until
        // we have a higher key; then skip 4, 2,...
        // (binary search), until 1
        int offset = 1;
        while (true) {
            ValueArray v = (ValueArray) map.relativeKey(min, offset);
            if (v != null) {
                boolean foundHigher = false;
                for (int i = 0; i < keyColumns - 1; i++) {
                    int idx = columnIds[i];
                    Value b = first.getValue(idx);
                    if (b == null) {
                        break;
                    }
                    Value a = v.getList()[i];
                    if (database.compare(a, b) > 0) {
                        foundHigher = true;
                        break;
                    }
                }
                if (!foundHigher) {
                    offset += offset;
                    min = v;
                    continue;
                }
            }
            if (offset > 1) {
                offset /= 2;
                continue;
            }
            if (map.get(v) == null) {
                min = (ValueArray) map.higherKey(min);
                if (min == null) {
                    break;
                }
                continue;
            }
            min = v;
            break;
        }
        if (min == null) {
            return new MVStoreCursor(session, Collections.<Value>emptyList().iterator(), null);
        }
    }
    return new MVStoreCursor(session, map.keyIterator(min), last);
}
Also used : Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 2 with TransactionMap

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

the class MVSecondaryIndex method remove.

@Override
public void remove(Session session) {
    TransactionMap<Value, Value> map = getMap(session);
    if (!map.isClosed()) {
        Transaction t = session.getTransaction();
        t.removeMap(map);
    }
}
Also used : Transaction(org.h2.mvstore.db.TransactionStore.Transaction) Value(org.h2.value.Value)

Example 3 with TransactionMap

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

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

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

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