com.sleepycat.je.dbi
Class CursorImpl

java.lang.Object
  extended by com.sleepycat.je.dbi.CursorImpl
All Implemented Interfaces:
Cloneable

public class CursorImpl
extends Object
implements Cloneable

A CursorImpl is the internal implementation of the cursor.


Nested Class Summary
static class CursorImpl.KeyChangeStatus
          Holder for an OperationStatus and a keyChange flag.
static class CursorImpl.SearchMode
          public for Cursor et al
 
Field Summary
static int EXACT_DATA
           
static int EXACT_KEY
           
static int FOUND
           
static int FOUND_LAST
           
 
Constructor Summary
CursorImpl(DatabaseImpl database, Locker locker)
          Creates a cursor with retainNonTxnLocks=true.
CursorImpl(DatabaseImpl database, Locker locker, boolean retainNonTxnLocks)
          Creates a cursor.
 
Method Summary
 void addCursor()
          Add to the current cursor.
 void addCursor(BIN bin)
           
 boolean advanceCursor(DatabaseEntry key, DatabaseEntry data)
          Advance a cursor.
 void checkCursorState(boolean mustBeInitialized)
          Check that the cursor is open and optionally if it is initialized.
 void checkEnv()
           
 CursorImpl cloneCursor(boolean addCursor)
          Shallow copy.
 CursorImpl cloneCursor(boolean addCursor, CursorImpl usePosition)
          Shallow copy.
 void close()
          Close a cursor.
 int count()
           
 OperationStatus delete()
          Delete the item pointed to by the cursor.
 void dump()
          dump the cursor for debugging purposes.
 void dump(boolean verbose)
          Dump the cursor for debugging purposes.
 String dumpToString(boolean verbose)
           
 void dumpTree()
           
 CursorImpl dup(boolean samePosition)
          Return a new copy of the cursor.
 void evict()
          Evict the LN node at the cursor position.
 BIN getBIN()
           
 BIN getBINToBeRemoved()
           
 OperationStatus getCurrent(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType)
          Retrieve the current record.
 OperationStatus getCurrentAlreadyLatched(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType, boolean first)
          Retrieve the current record.
 LN getCurrentLN(LockType lockType)
          Retrieve the current LN, return with the target bin unlatched.
 LN getCurrentLNAlreadyLatched(LockType lockType)
          Retrieve the current LN, assuming the BIN is already latched.
 DBIN getDupBIN()
           
 DBIN getDupBINToBeRemoved()
           
 int getDupIndex()
           
 OperationStatus getFirstDuplicate(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType)
          Retrieve the first duplicate at the current cursor position.
 int getIndex()
           
 Locker getLocker()
           
 CursorImpl getLockerNext()
           
 CursorImpl getLockerPrev()
           
 LockStats getLockStats()
           
 OperationStatus getNext(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType, boolean forward, boolean alreadyLatched)
           
 OperationStatus getNextDuplicate(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType, boolean forward, boolean alreadyLatched)
          Enter with dupBin unlatched.
 OperationStatus getNextNoDup(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType, boolean forward, boolean alreadyLatched)
           
 CursorImpl.KeyChangeStatus getNextWithKeyChangeStatus(DatabaseEntry foundKey, DatabaseEntry foundData, LockType lockType, boolean forward, boolean alreadyLatched)
          Move the cursor forward and return the next record.
 void incrementLNCount()
           
 boolean isClosed()
           
 boolean isNotInitialized()
           
 BIN latchBIN()
           
 void latchBINs()
           
 DBIN latchDBIN()
           
 void lockEofNode(LockType lockType)
          Locks the logical EOF node for the database.
 void lockNextKeyForInsert(DatabaseEntry key, DatabaseEntry data)
          Search for the next key (or duplicate) following the given key (and datum), and acquire a range insert lock on it.
 boolean positionFirstOrLast(boolean first, DIN duplicateRoot)
          Position the cursor at the first or last record of the database.
 OperationStatus put(DatabaseEntry key, DatabaseEntry data, DatabaseEntry foundData)
          Insert or overwrite the key/data pair.
 OperationStatus putCurrent(DatabaseEntry data, DatabaseEntry foundKey, DatabaseEntry foundData)
          Modify the current record with this data.
 OperationStatus putLN(byte[] key, LN ln, boolean allowDuplicates)
          Insert the given LN in the tree or return KEYEXIST if the key is already present.
 OperationStatus putNoDupData(DatabaseEntry key, DatabaseEntry data)
          Insert the key/data pair as long as no entry for key/data exists yet.
 OperationStatus putNoOverwrite(DatabaseEntry key, DatabaseEntry data)
          Insert the key/data pair in No Overwrite mode.
 void releaseBIN()
           
 void releaseBINs()
           
 void releaseDBIN()
           
 void reset()
          Reset a cursor to an uninitialized state, but unlike close(), allow it to be used further.
 int searchAndPosition(DatabaseEntry matchKey, DatabaseEntry matchData, CursorImpl.SearchMode searchMode, LockType lockType)
          Position the cursor at the key.
 void setAllowEviction(boolean allowed)
          Disables or enables eviction during cursor operations for an internal cursor.
 void setBIN(BIN newBin)
           
 void setDupBIN(DBIN newDupBin)
           
 void setDupIndex(int dupIdx)
           
 void setIndex(int idx)
           
 void setLockerNext(CursorImpl n)
           
 void setLockerPrev(CursorImpl p)
           
 void setTestHook(TestHook hook)
           
 void setTreeStatsAccumulator(TreeWalkerStatsAccumulator tSA)
           
 void updateBin(BIN bin, int index)
           
 void updateDBin(DBIN dupBin, int dupIndex)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FOUND

public static final int FOUND
See Also:
Constant Field Values

EXACT_KEY

public static final int EXACT_KEY
See Also:
Constant Field Values

EXACT_DATA

public static final int EXACT_DATA
See Also:
Constant Field Values

FOUND_LAST

public static final int FOUND_LAST
See Also:
Constant Field Values
Constructor Detail

CursorImpl

public CursorImpl(DatabaseImpl database,
                  Locker locker)
           throws DatabaseException
Creates a cursor with retainNonTxnLocks=true.

Throws:
DatabaseException

CursorImpl

public CursorImpl(DatabaseImpl database,
                  Locker locker,
                  boolean retainNonTxnLocks)
           throws DatabaseException
Creates a cursor.

Parameters:
retainNonTxnLocks - is true if non-transactional locks should be retained (not released automatically) when the cursor is closed.
Throws:
DatabaseException
Method Detail

incrementLNCount

public void incrementLNCount()

setAllowEviction

public void setAllowEviction(boolean allowed)
Disables or enables eviction during cursor operations for an internal cursor. For example, a cursor used to implement eviction should not itself perform eviction. Eviction is enabled by default.


cloneCursor

public CursorImpl cloneCursor(boolean addCursor)
                       throws DatabaseException
Shallow copy. addCursor() is optionally called.

Throws:
DatabaseException

cloneCursor

public CursorImpl cloneCursor(boolean addCursor,
                              CursorImpl usePosition)
                       throws DatabaseException
Shallow copy. addCursor() is optionally called. Allows inheriting the BIN position from some other cursor.

Throws:
DatabaseException

getIndex

public int getIndex()

setIndex

public void setIndex(int idx)

getBIN

public BIN getBIN()

setBIN

public void setBIN(BIN newBin)

getBINToBeRemoved

public BIN getBINToBeRemoved()

getDupIndex

public int getDupIndex()

setDupIndex

public void setDupIndex(int dupIdx)

getDupBIN

public DBIN getDupBIN()

setDupBIN

public void setDupBIN(DBIN newDupBin)

getDupBINToBeRemoved

public DBIN getDupBINToBeRemoved()

setTreeStatsAccumulator

public void setTreeStatsAccumulator(TreeWalkerStatsAccumulator tSA)

advanceCursor

public boolean advanceCursor(DatabaseEntry key,
                             DatabaseEntry data)
Advance a cursor. Used so that verify can advance a cursor even in the face of an exception [12932].

Parameters:
key - on return contains the key if available, or null.
data - on return contains the data if available, or null.

latchBIN

public BIN latchBIN()
             throws DatabaseException
Throws:
DatabaseException

releaseBIN

public void releaseBIN()
                throws LatchNotHeldException
Throws:
LatchNotHeldException

latchBINs

public void latchBINs()
               throws DatabaseException
Throws:
DatabaseException

releaseBINs

public void releaseBINs()
                 throws LatchNotHeldException
Throws:
LatchNotHeldException

latchDBIN

public DBIN latchDBIN()
               throws DatabaseException
Throws:
DatabaseException

releaseDBIN

public void releaseDBIN()
                 throws LatchNotHeldException
Throws:
LatchNotHeldException

getLocker

public Locker getLocker()

addCursor

public void addCursor(BIN bin)

addCursor

public void addCursor()
Add to the current cursor. (For dups)


updateBin

public void updateBin(BIN bin,
                      int index)
               throws DatabaseException
Throws:
DatabaseException

updateDBin

public void updateDBin(DBIN dupBin,
                       int dupIndex)

dumpTree

public void dumpTree()
              throws DatabaseException
Throws:
DatabaseException

isClosed

public boolean isClosed()
Returns:
true if this cursor is closed

isNotInitialized

public boolean isNotInitialized()
Returns:
true if this cursor is not initialized

reset

public void reset()
           throws DatabaseException
Reset a cursor to an uninitialized state, but unlike close(), allow it to be used further.

Throws:
DatabaseException

close

public void close()
           throws DatabaseException
Close a cursor.

Throws:
DatabaseException - if the cursor was previously closed.

count

public int count()
          throws DatabaseException
Throws:
DatabaseException

delete

public OperationStatus delete()
                       throws DatabaseException
Delete the item pointed to by the cursor. If cursor is not initialized or item is already deleted, return appropriate codes. Returns with nothing latched. bin and dupBin are latched as appropriate.

Returns:
0 on success, appropriate error code otherwise.
Throws:
DatabaseException

dup

public CursorImpl dup(boolean samePosition)
               throws DatabaseException
Return a new copy of the cursor. If position is true, position the returned cursor at the same position.

Throws:
DatabaseException

evict

public void evict()
           throws DatabaseException
Evict the LN node at the cursor position. This is used for internal databases only.

Throws:
DatabaseException

lockNextKeyForInsert

public void lockNextKeyForInsert(DatabaseEntry key,
                                 DatabaseEntry data)
                          throws DatabaseException
Search for the next key (or duplicate) following the given key (and datum), and acquire a range insert lock on it. If there are no more records following the given key and datum, lock the special EOF node for the database.

Throws:
DatabaseException

putLN

public OperationStatus putLN(byte[] key,
                             LN ln,
                             boolean allowDuplicates)
                      throws DatabaseException
Insert the given LN in the tree or return KEYEXIST if the key is already present.

This method is called directly internally for putting tree map LNs and file summary LNs. It should not be used otherwise, and in the future we should find a way to remove this special case.

Throws:
DatabaseException

put

public OperationStatus put(DatabaseEntry key,
                           DatabaseEntry data,
                           DatabaseEntry foundData)
                    throws DatabaseException
Insert or overwrite the key/data pair.

Parameters:
key -
data -
Returns:
0 if successful, failure status value otherwise
Throws:
DatabaseException

putNoOverwrite

public OperationStatus putNoOverwrite(DatabaseEntry key,
                                      DatabaseEntry data)
                               throws DatabaseException
Insert the key/data pair in No Overwrite mode.

Parameters:
key -
data -
Returns:
0 if successful, failure status value otherwise
Throws:
DatabaseException

putNoDupData

public OperationStatus putNoDupData(DatabaseEntry key,
                                    DatabaseEntry data)
                             throws DatabaseException
Insert the key/data pair as long as no entry for key/data exists yet.

Throws:
DatabaseException

putCurrent

public OperationStatus putCurrent(DatabaseEntry data,
                                  DatabaseEntry foundKey,
                                  DatabaseEntry foundData)
                           throws DatabaseException
Modify the current record with this data.

Parameters:
data -
Throws:
DatabaseException

getCurrent

public OperationStatus getCurrent(DatabaseEntry foundKey,
                                  DatabaseEntry foundData,
                                  LockType lockType)
                           throws DatabaseException
Retrieve the current record.

Throws:
DatabaseException

getCurrentAlreadyLatched

public OperationStatus getCurrentAlreadyLatched(DatabaseEntry foundKey,
                                                DatabaseEntry foundData,
                                                LockType lockType,
                                                boolean first)
                                         throws DatabaseException
Retrieve the current record. Assume the bin is already latched. Return with the target bin unlatched.

Throws:
DatabaseException

getCurrentLN

public LN getCurrentLN(LockType lockType)
                throws DatabaseException
Retrieve the current LN, return with the target bin unlatched.

Throws:
DatabaseException

getCurrentLNAlreadyLatched

public LN getCurrentLNAlreadyLatched(LockType lockType)
                              throws DatabaseException
Retrieve the current LN, assuming the BIN is already latched. Return with the target BIN unlatched.

Throws:
DatabaseException

getNext

public OperationStatus getNext(DatabaseEntry foundKey,
                               DatabaseEntry foundData,
                               LockType lockType,
                               boolean forward,
                               boolean alreadyLatched)
                        throws DatabaseException
Throws:
DatabaseException

getNextWithKeyChangeStatus

public CursorImpl.KeyChangeStatus getNextWithKeyChangeStatus(DatabaseEntry foundKey,
                                                             DatabaseEntry foundData,
                                                             LockType lockType,
                                                             boolean forward,
                                                             boolean alreadyLatched)
                                                      throws DatabaseException
Move the cursor forward and return the next record. This will cross BIN boundaries and dip into duplicate sets.

Parameters:
foundKey - DatabaseEntry to use for returning key
foundData - DatabaseEntry to use for returning data
forward - if true, move forward, else move backwards
alreadyLatched - if true, the bin that we're on is already latched.
Returns:
the status and an indication of whether we advanced to a new key during the operation.
Throws:
DatabaseException

getNextNoDup

public OperationStatus getNextNoDup(DatabaseEntry foundKey,
                                    DatabaseEntry foundData,
                                    LockType lockType,
                                    boolean forward,
                                    boolean alreadyLatched)
                             throws DatabaseException
Throws:
DatabaseException

getFirstDuplicate

public OperationStatus getFirstDuplicate(DatabaseEntry foundKey,
                                         DatabaseEntry foundData,
                                         LockType lockType)
                                  throws DatabaseException
Retrieve the first duplicate at the current cursor position.

Throws:
DatabaseException

getNextDuplicate

public OperationStatus getNextDuplicate(DatabaseEntry foundKey,
                                        DatabaseEntry foundData,
                                        LockType lockType,
                                        boolean forward,
                                        boolean alreadyLatched)
                                 throws DatabaseException
Enter with dupBin unlatched. Pass foundKey == null to just advance cursor to next duplicate without fetching data.

Throws:
DatabaseException

positionFirstOrLast

public boolean positionFirstOrLast(boolean first,
                                   DIN duplicateRoot)
                            throws DatabaseException
Position the cursor at the first or last record of the database. It's okay if this record is deleted. Returns with the target bin latched.

Returns:
true if a first or last position is found, false if the tree being searched is empty.
Throws:
DatabaseException

searchAndPosition

public int searchAndPosition(DatabaseEntry matchKey,
                             DatabaseEntry matchData,
                             CursorImpl.SearchMode searchMode,
                             LockType lockType)
                      throws DatabaseException
Position the cursor at the key. This returns a three part value that's bitwise or'ed into the int. We find out if there was any kind of match and if the match was exact. Note that this match focuses on whether the searching criteria (key, or key and data, depending on the search type) is met.

Note this returns with the BIN latched!

If this method returns without the FOUND bit set, the caller can assume that no match is possible. Otherwise, if the FOUND bit is set, the caller should check the EXACT_KEY and EXACT_DATA bits. If EXACT_KEY is not set (or for BOTH and BOTH_RANGE, if EXACT_DATA is not set), an approximate match was found. In an approximate match, the cursor is always positioned before the target key/data. This allows the caller to perform a 'next' operation to advance to the value that is equal or higher than the target key/data.

Even if the search returns an exact result, the record may be deleted. The caller must therefore check for both an approximate match and for whether the cursor is positioned on a deleted record.

If SET or BOTH is specified, the FOUND bit will only be returned if an exact match is found. However, the record found may be deleted.

There is one special case where this method may be called without checking the EXACT_KEY (and EXACT_DATA) bits and without checking for a deleted record: If SearchMode.SET is specified then only the FOUND bit need be checked. When SET is specified and FOUND is returned, it is guaranteed to be an exact match on a non-deleted record. It is for this case only that this method is public.

If FOUND is set, FOUND_LAST may also be set if the cursor is positioned on the last record in the database. Note that this state can only be counted on as long as the BIN is latched, so it is not set if this method must release the latch to lock the record. Therefore, it should only be used for optimizations. If FOUND_LAST is set, the cursor is positioned on the last record and the BIN is latched. If FOUND_LAST is not set, the cursor may or may not be positioned on the last record. Note that exact searches always perform an unlatch and a lock, so FOUND_LAST will only be set for inexact (range) searches.

Be aware that when an approximate match is returned, the index or dupIndex may be set to -1. This is done intentionally so that a 'next' operation will increment it.

Throws:
DatabaseException

checkCursorState

public void checkCursorState(boolean mustBeInitialized)
                      throws DatabaseException
Check that the cursor is open and optionally if it is initialized.

Throws:
DatabaseException

lockEofNode

public void lockEofNode(LockType lockType)
                 throws DatabaseException
Locks the logical EOF node for the database.

Throws:
DatabaseException

checkEnv

public void checkEnv()
              throws RunRecoveryException
Throws:
RunRecoveryException - if the underlying environment is invalid.

getLockerPrev

public CursorImpl getLockerPrev()

getLockerNext

public CursorImpl getLockerNext()

setLockerPrev

public void setLockerPrev(CursorImpl p)

setLockerNext

public void setLockerNext(CursorImpl n)

dump

public void dump(boolean verbose)
Dump the cursor for debugging purposes. Dump the bin and dbin that the cursor refers to if verbose is true.


dump

public void dump()
dump the cursor for debugging purposes.


dumpToString

public String dumpToString(boolean verbose)

getLockStats

public LockStats getLockStats()
                       throws DatabaseException
Throws:
DatabaseException

setTestHook

public void setTestHook(TestHook hook)


Copyright 2004-2005 Sleepycat, Inc. All Rights Reserved.