Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.txn;
  
 import static com.sleepycat.je.txn.LockStatDefinition.LOCK_READ_LOCKS;
 import static com.sleepycat.je.txn.LockStatDefinition.LOCK_WRITE_LOCKS;
 
 import java.util.Set;
 
A non-transactional Locker that simply tracks locks and releases them when releaseNonTxnLocks or operationEnd is called.
 
 public class BasicLocker extends Locker {
 
     /*
      * A BasicLocker can release all locks, so there is no need to distinguish
      * between read and write locks.
      *
      * ownedLock is used for the first lock obtained, and ownedLockSet is
      * instantiated and used only if more than one lock is obtained.  This is
      * an optimization for the common case where only one lock is held by a
      * non-transactional locker.
      *
      * There's no need to track memory utilization for these non-txnal lockers,
      * because the lockers are short lived.
      */
     private Long ownedLock;
     private Set<LongownedLockSet;
 
     private boolean lockingRequired;

    
Creates a BasicLocker.
 
     protected BasicLocker(EnvironmentImpl env) {
         super(env,
               false// readUncommittedDefault
               false// noWait
               0);    // mandatedId
     }
 
     public static BasicLocker createBasicLocker(EnvironmentImpl env)
         throws DatabaseException {
 
         return new BasicLocker(env);
     }

    
Creates a BasicLocker with a noWait argument.
 
     protected BasicLocker(EnvironmentImpl envboolean noWait) {
         super(env,
               false// readUncommittedDefault
               noWait,
               0);    // mandatedId
     }
 
     public static BasicLocker createBasicLocker(EnvironmentImpl env,
                                                 boolean noWait)
         throws DatabaseException {
 
         return new BasicLocker(envnoWait);
     }

    
BasicLockers always have a fixed id, because they are never used for recovery.
 
     @Override
     protected long generateId(TxnManager txnManager,
                               long ignore /* mandatedId */) {
         return .;
     }
 
     @Override
     protected void checkState(boolean ignoreCalledByAbort) {
         /* Do nothing. */
     }
 
     @Override
     protected LockResult lockInternal(long lsn,
                                       LockType lockType,
                                       boolean noWait,
                                      boolean jumpAheadOfWaiters,
                                      DatabaseImpl database)
        throws DatabaseException {
        /* Does nothing in BasicLocker. synchronized is for posterity. */
        synchronized (this) {
            checkState(false);
        }
        long timeout = 0;
        boolean useNoWait = noWait || ;
        if (!useNoWait) {
            synchronized (this) {
                timeout = getLockTimeout();
            }
        }
        /* Ask for the lock. */
        LockGrantType grant = .lock
            (lsnthislockTypetimeoutuseNoWaitjumpAheadOfWaiters
             database);
        return new LockResult(grantnull);
    }
    @Override
    public void preLogWithoutLock(DatabaseImpl database) {
    }

    
Get the txn that owns the lock on this node. Return null if there's no owning txn found.
    public Locker getWriteOwnerLocker(long lsn)
        throws DatabaseException {
        return .getWriteOwnerLocker(Long.valueOf(lsn));
    }

    
Is never transactional.
    @Override
    public boolean isTransactional() {
        return false;
    }

    
Is never serializable isolation.
    @Override
    public boolean isSerializableIsolation() {
        return false;
    }

    
Is never read-committed isolation.
    @Override
    public boolean isReadCommittedIsolation() {
        return false;
    }

    
No transactional locker is available.
    @Override
    public Txn getTxnLocker() {
        return null;
    }

    
Throws EnvironmentFailureException unconditionally. If we were to create a new BasicLocker here, it would not share locks with this locker, which violates the definition of this method. This method is not currently called in direct uses of BasicLocker and is overridden by subclasses where it is allowed (e.g., ThreadLocker and ReadCommittedLocker).

Throws:
com.sleepycat.je.DatabaseException from subclasses.
    @Override
    public Locker newNonTxnLocker()
        throws DatabaseException {
        throw EnvironmentFailureException.unexpectedState();
    }

    
Releases all locks, since all locks held by this locker are non-transactional.
    @Override
    public synchronized void releaseNonTxnLocks()
        throws DatabaseException {
        /*
         * Don't remove locks from txn's lock collection until iteration is
         * done, lest we get a ConcurrentModificationException during deadlock
         * graph "display".  [#9544]
         */
        if ( != null) {
            .release(this);
             = null;
        }
        if ( != null) {
            Iterator<Longiter = .iterator();
            while (iter.hasNext()) {
                Long nid = iter.next();
                .release(nidthis);
            }
            /* Now clear lock collection. */
            .clear();
        }
        /* Unload delete info, but don't wake up the compressor. */
        if (( != null) &&
            (.size() > 0)) {
                                         false); // no wakeup
            .clear();
        }
    }

    
Release locks and close the cursor at the end of the operation.
    @Override
    public void nonTxnOperationEnd()
        throws DatabaseException {
        operationEnd(true);
    }

    
Release locks and close the cursor at the end of the operation.
    @Override
    public void operationEnd(boolean operationOK)
        throws DatabaseException {
        releaseNonTxnLocks();
        /* Close this Locker. */
        close();
    }

    
This txn doesn't store cursors.

Throws:
com.sleepycat.je.DatabaseException in subclasses.
    @Override
    public void registerCursor(CursorImpl cursor) {
         = cursor.isInternalDbCursor();
    }

    
This txn doesn't store cursors.
    @Override
    public void unRegisterCursor(CursorImpl cursor) {
    }
    @Override
    public boolean lockingRequired() {
        return ;
    }
    /*
     * Transactional methods are all no-oped.
     */

    

Returns:
a dummy WriteLockInfo for this node.
    @Override
    public WriteLockInfo getWriteLockInfo(long lsn) {
        return .;
    }
    @Override
    public void markDeleteAtTxnEnd(DatabaseImpl dbboolean deleteAtCommit)
        throws DatabaseException {
        if (deleteAtCommit) {
            /* releaseDb will be called by startAndFinishDelete. */
            db.startAndFinishDelete();
        } else {
            .getDbTree().releaseDb(db);
        }
    }

    
Add a lock to set owned by this transaction.
    @Override
    protected void addLock(Long lsn,
                           LockType type,
                           LockGrantType grantStatus) {
        if (( != null &&
            .equals(lsn)) ||
            ( != null &&
             .contains(lsn))) {
            return// Already owned
        }
        if ( == null) {
             = lsn;
        } else {
            if ( == null) {
                 = new HashSet<Long>();
            }
            .add(lsn);
        }
    }

    
Remove a lock from the set owned by this txn.
    @Override
    void removeLock(long lsn) {
        if ( != null &&
             == lsn) {
             = null;
        } else if ( != null) {
            .remove(lsn);
        }
    }

    
Always false for this txn.
    @Override
    public boolean createdNode(long lsn) {
        return false;
    }

    
A lock is being demoted. Move it from the write collection into the read collection.
    @Override
    void moveWriteToReadLock(long lsnLock lock) {
    }

    
Stats. Note lack of synchronization while accessing Lock object. Appropriate for unit testing only.
    @Override
    public StatGroup collectStats()
        throws DatabaseException {
        StatGroup stats = 
            new StatGroup("Locker lock counts" ,
                          "Read and write locks held by this locker");
        IntStat nReadLocks = new IntStat(stats);
        IntStat nWriteLocks = new IntStat(stats);
        if ( != null) {
            Lock l = .lookupLock();
            if (l != null) {
                if (l.isOwnedWriteLock(this)) {
                    nWriteLocks.increment();
                } else {
                    nReadLocks.increment();
                }
            }
        }
        if ( != null) {
            for (Long nid : ) {
                Lock l = .lookupLock(nid);
                if (l != null) {
                    if (l.isOwnedWriteLock(this)) {
                        nWriteLocks.increment();
                    } else {
                        nReadLocks.increment();
                    }
                }
            }
        }
        return stats;
    }
New to GrepCode? Check out our FAQ X