Search in sources :

Example 26 with Transaction

use of org.cojen.tupl.Transaction in project Tupl by cojen.

the class MergeCursor method perform.

/**
 * Performs the given action with an appropriate lock mode.
 */
private LockResult perform(Action action) throws IOException {
    Transaction txn = mTxn;
    if (mView.mCombiner.combineLocks()) {
        if (txn == null) {
            txn = mView.newTransaction(null);
            try {
                txn.lockMode(LockMode.REPEATABLE_READ);
                action.perform(txn);
            } finally {
                txn.reset();
            }
            return LockResult.UNOWNED;
        } else if (txn.lockMode() == LockMode.READ_COMMITTED) {
            LockResult result;
            final LockMode original = txn.lockMode();
            try {
                txn.lockMode(LockMode.REPEATABLE_READ);
                result = action.perform(txn);
                if (result.isAcquired()) {
                    txn.unlock();
                    result = LockResult.UNOWNED;
                }
            } finally {
                txn.lockMode(original);
            }
            return result;
        }
    }
    return action.perform(txn);
}
Also used : LockResult(org.cojen.tupl.LockResult) Transaction(org.cojen.tupl.Transaction) LockMode(org.cojen.tupl.LockMode)

Example 27 with Transaction

use of org.cojen.tupl.Transaction in project Tupl by cojen.

the class ViewUtils method findNoLock.

public static void findNoLock(Cursor c, byte[] key) throws IOException {
    final boolean auto = c.autoload(false);
    final Transaction txn = c.link(Transaction.BOGUS);
    try {
        c.find(key);
    } finally {
        c.link(txn);
        c.autoload(auto);
    }
}
Also used : Transaction(org.cojen.tupl.Transaction)

Example 28 with Transaction

use of org.cojen.tupl.Transaction in project Tupl by cojen.

the class ViewUtils method previousCmp.

/**
 * @param cmp 0 for previousGt, -1 for previousGe
 */
public static LockResult previousCmp(Cursor c, byte[] limitKey, int cmp) throws IOException {
    Utils.keyCheck(limitKey);
    while (true) {
        final boolean auto = c.autoload(false);
        final Transaction txn = c.link(Transaction.BOGUS);
        try {
            c.previous();
        } finally {
            c.link(txn);
            c.autoload(auto);
        }
        if (c.key() != null) {
            if (c.compareKeyTo(limitKey) > cmp) {
                LockResult result = auto ? c.load() : c.lock();
                if (c.value() != null) {
                    return result;
                }
                continue;
            }
            c.reset();
        }
        return LockResult.UNOWNED;
    }
}
Also used : LockResult(org.cojen.tupl.LockResult) Transaction(org.cojen.tupl.Transaction)

Example 29 with Transaction

use of org.cojen.tupl.Transaction in project Tupl by cojen.

the class CursorUpgradableUpdater method step.

@Override
public boolean step() throws IOException {
    tryStep: {
        Cursor c = mCursor;
        try {
            Transaction txn = c.link();
            LockMode original = txn.lockMode();
            txn.lockMode(LockMode.UPGRADABLE_READ);
            try {
                c.next();
            } finally {
                txn.lockMode(original);
            }
        } catch (UnpositionedCursorException e) {
            break tryStep;
        } catch (Throwable e) {
            throw Utils.fail(this, e);
        }
        if (c.key() != null) {
            return true;
        }
    }
    return false;
}
Also used : UnpositionedCursorException(org.cojen.tupl.UnpositionedCursorException) Transaction(org.cojen.tupl.Transaction) LockMode(org.cojen.tupl.LockMode) Cursor(org.cojen.tupl.Cursor)

Example 30 with Transaction

use of org.cojen.tupl.Transaction in project Tupl by cojen.

the class UpgradableRowUpdater method toFirst.

@Override
protected LockResult toFirst(Cursor c) throws IOException {
    Transaction txn = c.link();
    LockMode original = txn.lockMode();
    txn.lockMode(LockMode.UPGRADABLE_READ);
    try {
        return super.toFirst(c);
    } finally {
        txn.lockMode(original);
    }
}
Also used : Transaction(org.cojen.tupl.Transaction) LockMode(org.cojen.tupl.LockMode)

Aggregations

Transaction (org.cojen.tupl.Transaction)36 Cursor (org.cojen.tupl.Cursor)12 Index (org.cojen.tupl.Index)10 LockMode (org.cojen.tupl.LockMode)8 LockResult (org.cojen.tupl.LockResult)8 Database (org.cojen.tupl.Database)6 UnpositionedCursorException (org.cojen.tupl.UnpositionedCursorException)5 IOException (java.io.IOException)4 UnmodifiableReplicaException (org.cojen.tupl.UnmodifiableReplicaException)4 PrepareHandler (org.cojen.tupl.ext.PrepareHandler)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 WeakReference (java.lang.ref.WeakReference)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Label (org.cojen.maker.Label)2 MethodMaker (org.cojen.maker.MethodMaker)2 Variable (org.cojen.maker.Variable)2