private long nextUnadjustedValue ( ) throws FetchException , PersistException { if ( mHasReservedValues ) { long next = mNextValue + mIncrement ; mNextValue = next ; if ( next < mStoredSequence . getNextValue ( ) ) { return next ; } mHasReservedValues = false ; } Transaction txn = mRepository . enterTopTransaction ( null ) ; txn . setForUpdate ( true ) ; try { // Assume that StoredSequence is stale, so reload. mStoredSequence . load ( ) ; long next = mStoredSequence . getNextValue ( ) ; long nextStored = next + mReserveAmount * mIncrement ; if ( next >= 0 && nextStored < 0 ) { // Wrapped around. There might be just a few values left. long avail = ( Long . MAX_VALUE - next ) / mIncrement ; if ( avail > 0 ) { nextStored = next + avail * mIncrement ; } else { // Throw a PersistException since sequences are applied during // insert operations, and inserts can only throw PersistExceptions. throw new PersistException ( "Sequence exhausted: " + mStoredSequence . getName ( ) ) ; } } mStoredSequence . setNextValue ( nextStored ) ; mStoredSequence . update ( ) ; txn . commit ( ) ; mNextValue = next ; mHasReservedValues = true ; return next ; } finally { txn . exit ( ) ; } }
Assumes caller has synchronized on mStoredSequence