Interface Cursor
- All Superinterfaces:
AutoCloseable
,Closeable
,ValueAccessor
View
. Cursor instances can only be
safely used by one thread at a time, and they must be reset
when no longer needed. Instances can be exchanged by threads, as long as a
happens-before relationship is established. Without proper exclusion,
multiple threads interacting with a Cursor instance may cause database
corruption.
Methods which return LockResult
might acquire a lock to access
the requested entry. The return type indicates if the lock is still held
, and in what fashion. Except where indicated, a
LockTimeoutException
is thrown when a lock cannot be acquired in
time. When cursor is linked to a transaction, it defines the
locking behavior and timeout. Otherwise, a lock is always acquired, with the
default timeout.
If a LockFailureException
is thrown from any method, the Cursor
is positioned at the desired key, but the value is NOT_LOADED
.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]
Empty marker which indicates that value exists but has not been loaded. -
Method Summary
Modifier and TypeMethodDescriptionboolean
autoload()
Returns the current autoload mode.boolean
autoload
(boolean mode) By default, values are loaded automatically, as they are seen.default void
close()
Equivalent to the reset method, which moves the Cursor to an undefined position.default void
commit
(byte[] value) Combined store and commit to the linked transaction.default Comparator
<byte[]> Returns a key comparator for the ordering of this view, or null if unordered.default int
compareKeyTo
(byte[] rkey) Compare the current key to the one given.default int
compareKeyTo
(byte[] rkey, int offset, int length) Compare the current key to the one given.copy()
Returns a new independent Cursor, positioned where this one is, and linked to the same transaction.default void
delete()
Deletes the value for the current entry, leaving the position unchanged.boolean
exists()
Quickly check if the value still exists without locking it.find
(byte[] key) Moves the Cursor to find the given key.default LockResult
findGe
(byte[] key) Moves the Cursor to find the closest available entry greater than or equal to the given key.default LockResult
findGt
(byte[] key) Moves the Cursor to find the closest available entry greater than the given key.default LockResult
findLe
(byte[] key) Moves the Cursor to find the closest available entry less than or equal to the given key.default LockResult
findLt
(byte[] key) Moves the Cursor to find the closest available entry less than the given key.default LockResult
findNearby
(byte[] key) Optimized version of the regularfind
method, which can perform fewer search steps if the given key is in close proximity to the current one.default LockResult
findNearbyGe
(byte[] key) Optimized version of the regularfindGe
method, which can perform fewer search steps if the given key is in close proximity to the current one.default LockResult
findNearbyGt
(byte[] key) Optimized version of the regularfindGt
method, which can perform fewer search steps if the given key is in close proximity to the current one.default LockResult
findNearbyLe
(byte[] key) Optimized version of the regularfindLe
method, which can perform fewer search steps if the given key is in close proximity to the current one.default LockResult
findNearbyLt
(byte[] key) Optimized version of the regularfindLt
method, which can perform fewer search steps if the given key is in close proximity to the current one.first()
Moves the Cursor to find the first available entry.byte[]
key()
Returns an uncopied reference to the current key, or null if Cursor is unpositioned.last()
Moves the Cursor to find the last available entry.link()
Returns the transaction the cursor is currently linked to.link
(Transaction txn) Link to a transaction, which can be null for auto-commit mode.load()
Loads or reloads the value at the cursor's current position.default LockResult
lock()
Locks the current entry, as if by calling load.next()
Moves to the Cursor to the next available entry.default LockResult
nextLe
(byte[] limitKey) Moves to the Cursor to the next available entry, but only when less than or equal to the given limit key.default LockResult
nextLt
(byte[] limitKey) Moves to the Cursor to the next available entry, but only when less than the given limit key.ordering()
Returns the key ordering for this cursor.previous()
Moves to the Cursor to the previous available entry.default LockResult
previousGe
(byte[] limitKey) Moves to the Cursor to the previous available entry, but only when greater than or equal to the given limit key.default LockResult
previousGt
(byte[] limitKey) Moves to the Cursor to the previous available entry, but only when greater than the given limit key.random
(byte[] lowKey, boolean lowInclusive, byte[] highKey, boolean highInclusive) Moves the Cursor to a random entry, but not guaranteed to be chosen from a uniform distribution.default LockResult
random
(byte[] lowKey, byte[] highKey) Moves the Cursor to a random entry, but not guaranteed to be chosen from a uniform distribution.default boolean
register()
Attempt to register this cursor for direct redo operations, which can improve replication performance when modifying a range of values.void
reset()
Resets the Cursor and moves it to an undefined position.skip
(long amount) Moves the Cursor by a relative amount of entries.default LockResult
skip
(long amount, byte[] limitKey, boolean inclusive) Moves the Cursor by a relative amount of entries, stopping sooner if the limit key is reached.void
store
(byte[] value) Stores a value into the current entry, leaving the position unchanged.default void
Unregisters the cursor for direct redo operations.byte[]
value()
Returns an uncopied reference to the current value, which might be null orNOT_LOADED
.Methods inherited from interface org.cojen.tupl.ValueAccessor
newValueInputStream, newValueInputStream, newValueOutputStream, newValueOutputStream, valueClear, valueLength, valueLength, valueRead, valueWrite
-
Field Details
-
NOT_LOADED
static final byte[] NOT_LOADEDEmpty marker which indicates that value exists but has not been loaded.
-
-
Method Details
-
ordering
Ordering ordering()Returns the key ordering for this cursor. -
comparator
Returns a key comparator for the ordering of this view, or null if unordered. -
link
Link to a transaction, which can be null for auto-commit mode. All entries visited by the cursor become part of the given transaction. To continue using a cursor after the transaction is complete, link it to null or another transaction. Otherwise, the original transaction will be resurrected.- Returns:
- prior linked transaction
- Throws:
IllegalStateException
- if transaction belongs to another database instance
-
link
Transaction link()Returns the transaction the cursor is currently linked to. -
key
byte[] key()Returns an uncopied reference to the current key, or null if Cursor is unpositioned. Array contents must not be modified. -
value
byte[] value()Returns an uncopied reference to the current value, which might be null orNOT_LOADED
. Array contents can be safely modified. Altering the value via theValueAccessor
methods doesn't affect the object returned by this method. -
autoload
boolean autoload(boolean mode) By default, values are loaded automatically, as they are seen. When disabled, values might need to be manually loaded. When aTransformer
is used, the value might still be loaded automatically. When the value exists but hasn't been loaded, the value field of the cursor is set toNOT_LOADED
.- Parameters:
mode
- false to disable- Returns:
- prior autoload mode
-
autoload
boolean autoload()Returns the current autoload mode. -
compareKeyTo
default int compareKeyTo(byte[] rkey) Compare the current key to the one given.- Parameters:
rkey
- key to compare to- Returns:
- a negative integer, zero, or a positive integer as current key is less than, equal to, or greater than the rkey.
- Throws:
NullPointerException
- if current key or rkey is null
-
compareKeyTo
default int compareKeyTo(byte[] rkey, int offset, int length) Compare the current key to the one given.- Parameters:
rkey
- key to compare tooffset
- offset into rkeylength
- length of rkey- Returns:
- a negative integer, zero, or a positive integer as current key is less than, equal to, or greater than the rkey.
- Throws:
NullPointerException
- if current key or rkey is null
-
register
Attempt to register this cursor for direct redo operations, which can improve replication performance when modifying a range of values. Without registration, replicas must perform a full find operation for each modification made in the range. Registration isn't useful when using the cursor for single updates.The cursor is automatically unregistered when reset, or when moved to an undefined position, or when moving the cursor non-incrementally. Methods whose name starts with "next", "previous", "skip", or "findNearby" are considered to move the cursor incrementally. The use of these methods generally indicates that registering the cursor might be beneficial.
To be effective, cursor registration must be performed after the cursor is initially positioned:
Cursor c = ... c.findGe(startKey); c.register(); // register after initial positioning while (more updates to perform) { c.store(...); c.next(); // incremental move }
- Throws:
IOException
-
unregister
default void unregister()Unregisters the cursor for direct redo operations.- See Also:
-
first
Moves the Cursor to find the first available entry. Cursor key and value are set to null if no entries exist, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
IOException
-
last
Moves the Cursor to find the last available entry. Cursor key and value are set to null if no entries exist, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
IOException
-
skip
Moves the Cursor by a relative amount of entries. Pass a positive amount to skip forward, and pass a negative amount to skip backwards. If less than the given amount of entries are available, the Cursor is reset.Skipping by 1 is equivalent to calling
next
, and skipping by -1 is equivalent to callingprevious
. A skip of 0 merely checks and returns the lock state for the current key. Lock acquisition only applies to the target entry — no locks are acquired for entries in between.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
skip
Moves the Cursor by a relative amount of entries, stopping sooner if the limit key is reached. Pass a positive amount to skip forward, and pass a negative amount to skip backwards. If the limit key is reached, or if less than the given amount of entries are available, the Cursor is reset.Skipping by 1 is equivalent to calling
nextLe
,nextLt
ornext
, depending on which type of limit was provided. Likewise, skipping by -1 is equivalent to callingpreviousGe
,previousGt
orprevious
. A skip of 0 merely checks and returns the lock state for the current key. Lock acquisition only applies to the target entry — no locks are acquired for entries in between.- Parameters:
limitKey
- limit key; pass null for no limitinclusive
- true if limit is inclusive, false for exclusive- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
next
Moves to the Cursor to the next available entry. Cursor key and value are set to null if no next entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
nextLe
Moves to the Cursor to the next available entry, but only when less than or equal to the given limit key. Cursor key and value are set to null if no applicable entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if limit key is nullUnpositionedCursorException
- if position is undefined at invocation timeIOException
-
nextLt
Moves to the Cursor to the next available entry, but only when less than the given limit key. Cursor key and value are set to null if no applicable entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if limit key is nullUnpositionedCursorException
- if position is undefined at invocation timeIOException
-
previous
Moves to the Cursor to the previous available entry. Cursor key and value are set to null if no previous entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
previousGe
Moves to the Cursor to the previous available entry, but only when greater than or equal to the given limit key. Cursor key and value are set to null if no applicable entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if limit key is nullUnpositionedCursorException
- if position is undefined at invocation timeIOException
-
previousGt
Moves to the Cursor to the previous available entry, but only when greater than the given limit key. Cursor key and value are set to null if no applicable entry exists, and position will be undefined.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if limit key is nullUnpositionedCursorException
- if position is undefined at invocation timeIOException
-
find
Moves the Cursor to find the given key. If no such key exists, the cursor is still positioned at the key, but the value is null.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findGe
Moves the Cursor to find the closest available entry greater than or equal to the given key. If no such key exists, the cursor is unpositioned. Logically equivalent toNavigableMap.ceilingEntry
.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findGt
Moves the Cursor to find the closest available entry greater than the given key. If no such key exists, the cursor is unpositioned. Logically equivalent toNavigableMap.higherEntry
.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findLe
Moves the Cursor to find the closest available entry less than or equal to the given key. If no such key exists, the cursor is unpositioned. Logically equivalent toNavigableMap.floorEntry
.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findLt
Moves the Cursor to find the closest available entry less than the given key. If no such key exists, the cursor is unpositioned. Logically equivalent toNavigableMap.lowerEntry
.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findNearby
Optimized version of the regularfind
method, which can perform fewer search steps if the given key is in close proximity to the current one. Even if not in close proximity, the find outcome is identical, although it may perform more slowly.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findNearbyGe
Optimized version of the regularfindGe
method, which can perform fewer search steps if the given key is in close proximity to the current one. Even if not in close proximity, the find outcome is identical, although it may perform more slowly.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findNearbyGt
Optimized version of the regularfindGt
method, which can perform fewer search steps if the given key is in close proximity to the current one. Even if not in close proximity, the find outcome is identical, although it may perform more slowly.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findNearbyLe
Optimized version of the regularfindLe
method, which can perform fewer search steps if the given key is in close proximity to the current one. Even if not in close proximity, the find outcome is identical, although it may perform more slowly.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
findNearbyLt
Optimized version of the regularfindLt
method, which can perform fewer search steps if the given key is in close proximity to the current one. Even if not in close proximity, the find outcome is identical, although it may perform more slowly.Ownership of the key instance transfers to the Cursor, and it must not be modified after calling this method.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
NullPointerException
- if key is nullIOException
-
random
Moves the Cursor to a random entry, but not guaranteed to be chosen from a uniform distribution. If no entries exists, or if random searches aren't supported, the cursor is unpositioned.- Parameters:
lowKey
- inclusive lowest key in the selectable range; pass null for open rangehighKey
- exclusive highest key in the selectable range; pass null for open range- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
IOException
-
random
LockResult random(byte[] lowKey, boolean lowInclusive, byte[] highKey, boolean highInclusive) throws IOException Moves the Cursor to a random entry, but not guaranteed to be chosen from a uniform distribution. If no entries exists, or if random searches aren't supported, the cursor is unpositioned.- Parameters:
lowKey
- lowest key in the selectable range; pass null for open rangelowInclusive
- true for inclusive key, false for exclusive, ignored if key is nullhighKey
- highest key in the selectable range; pass null for open rangehighInclusive
- true for inclusive key, false for exclusive, ignored if key is null- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
IOException
-
exists
Quickly check if the value still exists without locking it.- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
lock
Locks the current entry, as if by calling load. Locking is performed automatically within transactions, and so invocation of this method is necessary only when manually tweaking the lock mode. If a lock was acquired (even if not retained), the cursor value field is updated according to the current autoload mode.By default, this method simply calls load. Subclasses are encouraged to provide a more efficient implementation.
- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
load
Loads or reloads the value at the cursor's current position. Cursor value is set to null if entry no longer exists, but the position remains unmodified.- Returns:
UNOWNED
,ACQUIRED
,OWNED_SHARED
,OWNED_UPGRADABLE
, orOWNED_EXCLUSIVE
- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeIOException
-
store
Stores a value into the current entry, leaving the position unchanged. An entry may be inserted, updated or deleted by this method. A null value deletes the entry. Unless an exception is thrown, the object returned by thevalue
method will be the same instance as was provided to this method.- Parameters:
value
- value to store; pass null to delete- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeViewConstraintException
- if value is not permittedIOException
-
delete
Deletes the value for the current entry, leaving the position unchanged. Unless an exception is thrown, the object returned by thevalue
method will be null after calling this method.- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeViewConstraintException
- if not permittedIOException
-
commit
Combined store and commit to the linked transaction. Although similar to storing and committing explicitly, additional optimizations can be applied. In particular, no undo log entry is required when committing the outermost transaction scope. This is the same optimization used by null transactions (auto-commit).- Parameters:
value
- value to store; pass null to delete- Throws:
UnpositionedCursorException
- if position is undefined at invocation timeViewConstraintException
- if value is not permittedIOException
- See Also:
-
copy
Cursor copy()Returns a new independent Cursor, positioned where this one is, and linked to the same transaction. The original and copied Cursor can be acted upon without affecting each other's state. -
reset
void reset()Resets the Cursor and moves it to an undefined position. The key and value references are set to null. -
close
Equivalent to the reset method, which moves the Cursor to an undefined position. The Cursor is re-opened automatically if positioned again.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceValueAccessor
- Throws:
IOException
-