Search in sources :

Example 1 with LockMode

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

the class NonRepeatableRowUpdater 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 mLockResult = super.toFirst(c);
    } finally {
        txn.lockMode(original);
    }
}
Also used : Transaction(org.cojen.tupl.Transaction) LockMode(org.cojen.tupl.LockMode)

Example 2 with LockMode

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

the class CursorNonRepeatableUpdater method step.

@Override
public boolean step() throws IOException {
    LockResult result = mLockResult;
    if (result == null) {
        return false;
    }
    Cursor c = mCursor;
    Transaction txn = c.link();
    tryStep: {
        try {
            if (result.isAcquired()) {
                // If the transaction is being acted upon independently of this updater,
                // then this technique might throw an IllegalStateException.
                txn.unlock();
            }
            LockMode original = txn.lockMode();
            txn.lockMode(LockMode.UPGRADABLE_READ);
            try {
                result = c.next();
            } finally {
                txn.lockMode(original);
            }
        } catch (UnpositionedCursorException e) {
            break tryStep;
        } catch (Throwable e) {
            throw Utils.fail(this, e);
        }
        if (c.key() != null) {
            mLockResult = result;
            return true;
        }
    }
    mLockResult = null;
    return false;
}
Also used : LockResult(org.cojen.tupl.LockResult) UnpositionedCursorException(org.cojen.tupl.UnpositionedCursorException) Transaction(org.cojen.tupl.Transaction) LockMode(org.cojen.tupl.LockMode) Cursor(org.cojen.tupl.Cursor)

Example 3 with LockMode

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

the class UpgradableRowUpdater method toNext.

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

Example 4 with LockMode

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

the class CursorNonRepeatableUpdater method update.

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

Example 5 with LockMode

use of org.cojen.tupl.LockMode 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)

Aggregations

LockMode (org.cojen.tupl.LockMode)8 Transaction (org.cojen.tupl.Transaction)8 LockResult (org.cojen.tupl.LockResult)4 Cursor (org.cojen.tupl.Cursor)3 UnpositionedCursorException (org.cojen.tupl.UnpositionedCursorException)3