Search in sources :

Example 6 with TransactionMap

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

Example 7 with TransactionMap

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

the class TestTransactionStore method testStopWhileCommitting.

private void testStopWhileCommitting() throws Exception {
    String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
    FileUtils.delete(fileName);
    Random r = new Random(0);
    for (int i = 0; i < 10; ) {
        MVStore s;
        TransactionStore ts;
        Transaction tx;
        TransactionMap<Integer, String> m;
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        ts.init();
        tx = ts.begin();
        s.setReuseSpace(false);
        m = tx.openMap("test");
        final String value = "x" + i;
        for (int j = 0; j < 1000; j++) {
            m.put(j, value);
        }
        final AtomicInteger state = new AtomicInteger();
        final MVStore store = s;
        final MVMap<Integer, String> other = s.openMap("other");
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                for (int i = 0; !stop; i++) {
                    state.set(i);
                    other.put(i, value);
                    store.commit();
                }
            }
        };
        task.execute();
        // wait for the task to start
        while (state.get() < 1) {
            Thread.yield();
        }
        // commit while writing in the task
        tx.commit();
        // wait for the task to stop
        task.get();
        store.close();
        s = MVStore.open(fileName);
        // roll back a bit, until we have some undo log entries
        assertTrue(s.hasMap("undoLog"));
        for (int back = 0; back < 100; back++) {
            int minus = r.nextInt(10);
            s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
            MVMap<?, ?> undo = s.openMap("undoLog");
            if (undo.size() > 0) {
                break;
            }
        }
        // re-open the store, because we have opened
        // the undoLog map with the wrong data type
        s.close();
        s = MVStore.open(fileName);
        ts = new TransactionStore(s);
        List<Transaction> list = ts.getOpenTransactions();
        if (list.size() != 0) {
            tx = list.get(0);
            if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
                i++;
            }
        }
        s.close();
        FileUtils.delete(fileName);
        assertFalse(FileUtils.exists(fileName));
    }
}
Also used : Task(org.h2.util.Task) MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStore(org.h2.mvstore.db.TransactionStore) Random(java.util.Random) Transaction(org.h2.mvstore.db.TransactionStore.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 8 with TransactionMap

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

the class MVPrimaryIndex method add.

@Override
public void add(Session session, Row row) {
    if (mainIndexColumn == -1) {
        if (row.getKey() == 0) {
            row.setKey(lastKey.incrementAndGet());
        }
    } else {
        long c = row.getValue(mainIndexColumn).getLong();
        row.setKey(c);
    }
    if (mvTable.getContainsLargeObject()) {
        for (int i = 0, len = row.getColumnCount(); i < len; i++) {
            Value v = row.getValue(i);
            Value v2 = v.copy(database, getId());
            if (v2.isLinkedToTable()) {
                session.removeAtCommitStop(v2);
            }
            if (v != v2) {
                row.setValue(i, v2);
            }
        }
    }
    TransactionMap<Value, Value> map = getMap(session);
    Value key = ValueLong.get(row.getKey());
    Value old = map.getLatest(key);
    if (old != null) {
        String sql = "PRIMARY KEY ON " + table.getSQL();
        if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
            sql += "(" + indexColumns[mainIndexColumn].getSQL() + ")";
        }
        DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, sql);
        e.setSource(this);
        throw e;
    }
    try {
        map.put(key, ValueArray.get(row.getValueList()));
    } catch (IllegalStateException e) {
        throw mvTable.convertException(e);
    }
    // syntax
    if (row.getKey() > lastKey.get()) {
        lastKey.set(row.getKey());
    }
}
Also used : Value(org.h2.value.Value) DbException(org.h2.message.DbException)

Example 9 with TransactionMap

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

the class MVSpatialIndex method remove.

@Override
public void remove(Session session, Row row) {
    SpatialKey key = getKey(row);
    if (key.isNull()) {
        return;
    }
    TransactionMap<SpatialKey, Value> map = getMap(session);
    try {
        Value old = map.remove(key);
        if (old == null) {
            old = map.remove(key);
            throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey());
        }
    } catch (IllegalStateException e) {
        throw mvTable.convertException(e);
    }
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) Value(org.h2.value.Value) VersionedValue(org.h2.mvstore.db.TransactionStore.VersionedValue)

Example 10 with TransactionMap

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

the class MVSpatialIndex method findByGeometry.

@Override
public Cursor findByGeometry(TableFilter filter, SearchRow first, SearchRow last, SearchRow intersection) {
    Session session = filter.getSession();
    if (intersection == null) {
        return find(session, first, last);
    }
    Iterator<SpatialKey> cursor = spatialMap.findIntersectingKeys(getKey(intersection));
    TransactionMap<SpatialKey, Value> map = getMap(session);
    Iterator<SpatialKey> it = map.wrapIterator(cursor, false);
    return new MVStoreCursor(session, it);
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) Value(org.h2.value.Value) VersionedValue(org.h2.mvstore.db.TransactionStore.VersionedValue) Session(org.h2.engine.Session)

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