ForwardCursor<V>
, java.lang.Iterable<V>
public interface EntityCursor<V> extends ForwardCursor<V>
EntityCursor
objects are not thread-safe. Cursors
should be opened, used and closed by a single thread.
Cursors are opened using the EntityIndex.keys()
and EntityIndex.entities()
family of methods. These methods are available for
objects of any class that implements EntityIndex
: PrimaryIndex
, SecondaryIndex
, and the indices returned by SecondaryIndex.keysIndex
and SecondaryIndex.subIndex(SK)
. A ForwardCursor
, which implements a subset of cursor operations, is also
available via the EntityJoin.keys()
and EntityJoin.entities()
methods.
Values are always returned by a cursor in key order, where the key is
defined by the underlying EntityIndex
. For example, a cursor on a
SecondaryIndex
returns values ordered by secondary key, while an
index on a PrimaryIndex
or a SecondaryIndex.subIndex(SK)
returns
values ordered by primary key.
WARNING: Cursors must always be closed to prevent resource leaks
which could lead to the index becoming unusable or cause an
OutOfMemoryError
. To ensure that a cursor is closed in the
face of exceptions, call close()
in a finally block. For example,
the following code traverses all Employee entities and closes the cursor
whether or not an exception occurs:
@Entity class Employee { @PrimaryKey long id; @SecondaryKey(relate=MANY_TO_ONE) String department; String name; private Employee() {} } EntityStore store = ...PrimaryIndex<Long, Employee>
primaryIndex = store.getPrimaryIndex(Long.class, Employee.class);EntityCursor<Employee>
cursor = primaryIndex.entities(); try { for (Employee entity = cursor.first(); entity != null; entity = cursor.next()) { // Do something with the entity... } } finally { cursor.close(); }
When it is opened, a cursor is not initially positioned on any value; in
other words, it is uninitialized. Most methods in this interface initialize
the cursor position but certain methods, for example, current()
and
delete()
, throw IllegalStateException
when called for an
uninitialized cursor.
Note that the next()
and prev()
methods return the first or
last value respectively for an uninitialized cursor. This allows the loop
in the example above to be rewritten as follows:
EntityCursor<Employee>
cursor = primaryIndex.entities();
try {
Employee entity;
while ((entity = cursor.next()) != null) {
// Do something with the entity...
}
} finally {
cursor.close();
}
The iterator()
method can be used to return a standard Java
Iterator
that returns the same values that the cursor returns. For
example:
EntityCursor<Employee>
cursor = primaryIndex.entities(); try {Iterator<Employee>
i = cursor.iterator(); while (i.hasNext()) { Employee entity = i.next(); // Do something with the entity... } } finally { cursor.close(); }
The Iterable
interface is also extended by EntityCursor
to allow using the cursor as the target of a Java "foreach" statement:
EntityCursor<Employee>
cursor = primaryIndex.entities();
try {
for (Employee entity : cursor) {
// Do something with the entity...
}
} finally {
cursor.close();
}
The iterator uses the cursor directly, so any changes to the cursor
position impact the iterator and vice versa. The iterator advances the
cursor by calling next()
when Iterator.hasNext()
or Iterator.next()
is called. Because of this interaction, to keep things
simple it is best not to mix the use of an EntityCursor
Iterator
with the use of the EntityCursor
traversal methods
such as next()
, for a single EntityCursor
object.
A key range may be specified when opening the cursor, to restrict the
key range of the cursor to a subset of the complete range of keys in the
index. A fromKey
and/or toKey
parameter may be specified
when calling EntityIndex.keys(Object,boolean,Object,boolean)
or
EntityIndex.entities(Object,boolean,Object,boolean)
. The key
arguments may be specified as inclusive or exclusive values.
Whenever a cursor with a key range is moved, the key range bounds will be
checked, and the cursor will never be positioned outside the range. The
first()
cursor value is the first existing value in the range, and
the last()
cursor value is the last existing value in the range. For
example, the following code traverses Employee entities with keys from 100
(inclusive) to 200 (exclusive):
EntityCursor<Employee>
cursor = primaryIndex.entities(100, true, 200, false);
try {
for (Employee entity : cursor) {
// Do something with the entity...
}
} finally {
cursor.close();
}
When using a cursor for a SecondaryIndex
, the keys in the index
may be non-unique (duplicates) if SecondaryKey.relate()
is MANY_TO_ONE
or MANY_TO_MANY
. For example, a MANY_TO_ONE
Employee.department
secondary key is non-unique because there are multiple
Employee entities with the same department key value. The nextDup()
,
prevDup()
, nextNoDup()
and prevNoDup()
methods may be
used to control how non-unique keys are returned by the cursor.
nextDup()
and prevDup()
return the next or previous value
only if it has the same key as the current value, and null is returned when
a different key is encountered. For example, these methods can be used to
return all employees in a given department.
nextNoDup()
and prevNoDup()
return the next or previous
value with a unique key, skipping over values that have the same key. For
example, these methods can be used to return the first employee in each
department.
For example, the following code will find the first employee in each
department with nextNoDup()
until it finds a department name that
matches a particular regular expression. For each matching department it
will find all employees in that department using nextDup()
.
SecondaryIndex<String, Long, Employee>
secondaryIndex = store.getSecondaryIndex(primaryIndex, String.class, "department"); String regex = ...;EntityCursor<Employee>
cursor = secondaryIndex.entities(); try { for (Employee entity = cursor.first(); entity != null; entity = cursor.nextNoDup()) { if (entity.department.matches(regex)) { while (entity != null) { // Do something with the matching entities... entity = cursor.nextDup(); } } } } finally { cursor.close(); }
The update(V)
and delete()
methods operate on the entity at
the current cursor position. Cursors on any type of index may be used to
delete entities. For example, the following code deletes all employees in
departments which have names that match a particular regular expression:
SecondaryIndex<String, Long, Employee>
secondaryIndex = store.getSecondaryIndex(primaryIndex, String.class, "department"); String regex = ...;EntityCursor<Employee>
cursor = secondaryIndex.entities(); try { for (Employee entity = cursor.first(); entity != null; entity = cursor.nextNoDup()) { if (entity.department.matches(regex)) { while (entity != null) { cursor.delete(); entity = cursor.nextDup(); } } } } finally { cursor.close(); }
Note that the cursor can be moved to the next (or previous) value after
deleting the entity at the current position. This is an important property
of cursors, since without it you would not be able to easily delete while
processing multiple values with a cursor. A cursor positioned on a deleted
entity is in a special state. In this state, current()
will return
null, delete()
will return false, and update(V)
will return
false.
The update(V)
method is supported only if the value type is an
entity class (not a key class) and the underlying index is a PrimaryIndex
; in other words, for a cursor returned by one of the EntityIndex.entities()
methods. For example, the following code changes all
employee names to uppercase:
EntityCursor<Employee>
cursor = primaryIndex.entities();
try {
for (Employee entity = cursor.first();
entity != null;
entity = cursor.next()) {
entity.name = entity.name.toUpperCase();
cursor.update(entity);
}
} finally {
cursor.close();
}
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes the cursor.
|
int |
count() |
Returns the number of values (duplicates) for the key at the cursor
position, or returns zero if all values for the key have been deleted.
|
V |
current() |
Returns the value at the cursor position, or null if the value at the
cursor position has been deleted.
|
V |
current(LockMode lockMode) |
Returns the value at the cursor position, or null if the value at the
cursor position has been deleted.
|
boolean |
delete() |
Deletes the entity at the cursor position.
|
EntityCursor<V> |
dup() |
Duplicates the cursor at the cursor position.
|
V |
first() |
Moves the cursor to the first value and returns it, or returns null if
the cursor range is empty.
|
V |
first(LockMode lockMode) |
Moves the cursor to the first value and returns it, or returns null if
the cursor range is empty.
|
java.util.Iterator<V> |
iterator() |
Returns an iterator over the key range, starting with the value
following the current position or at the first value if the cursor is
uninitialized.
|
java.util.Iterator<V> |
iterator(LockMode lockMode) |
Returns an iterator over the key range, starting with the value
following the current position or at the first value if the cursor is
uninitialized.
|
V |
last() |
Moves the cursor to the last value and returns it, or returns null if
the cursor range is empty.
|
V |
last(LockMode lockMode) |
Moves the cursor to the last value and returns it, or returns null if
the cursor range is empty.
|
V |
next() |
Moves the cursor to the next value and returns it, or returns null
if there are no more values in the cursor range.
|
V |
next(LockMode lockMode) |
Moves the cursor to the next value and returns it, or returns null
if there are no more values in the cursor range.
|
V |
nextDup() |
Moves the cursor to the next value with the same key (duplicate) and
returns it, or returns null if no more values are present for the key at
the current position.
|
V |
nextDup(LockMode lockMode) |
Moves the cursor to the next value with the same key (duplicate) and
returns it, or returns null if no more values are present for the key at
the current position.
|
V |
nextNoDup() |
Moves the cursor to the next value with a different key and returns it,
or returns null if there are no more unique keys in the cursor range.
|
V |
nextNoDup(LockMode lockMode) |
Moves the cursor to the next value with a different key and returns it,
or returns null if there are no more unique keys in the cursor range.
|
V |
prev() |
Moves the cursor to the previous value and returns it, or returns null
if there are no preceding values in the cursor range.
|
V |
prev(LockMode lockMode) |
Moves the cursor to the previous value and returns it, or returns null
if there are no preceding values in the cursor range.
|
V |
prevDup() |
Moves the cursor to the previous value with the same key (duplicate) and
returns it, or returns null if no preceding values are present for the
key at the current position.
|
V |
prevDup(LockMode lockMode) |
Moves the cursor to the previous value with the same key (duplicate) and
returns it, or returns null if no preceding values are present for the
key at the current position.
|
V |
prevNoDup() |
Moves the cursor to the preceding value with a different key and returns
it, or returns null if there are no preceding unique keys in the cursor
range.
|
V |
prevNoDup(LockMode lockMode) |
Moves the cursor to the preceding value with a different key and returns
it, or returns null if there are no preceding unique keys in the cursor
range.
|
boolean |
update(V entity) |
Replaces the entity at the cursor position with the given entity.
|
V first() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
DatabaseException
- the base class for all BDB exceptions.V first(LockMode lockMode) throws DatabaseException
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V last() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
DatabaseException
- the base class for all BDB exceptions.V last(LockMode lockMode) throws DatabaseException
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V next() throws DatabaseException
first()
.
LockMode.DEFAULT
is used implicitly.
next
in interface ForwardCursor<V>
DatabaseException
- the base class for all BDB exceptions.V next(LockMode lockMode) throws DatabaseException
first()
.next
in interface ForwardCursor<V>
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V nextDup() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.V nextDup(LockMode lockMode) throws DatabaseException
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.V nextNoDup() throws DatabaseException
first()
.
LockMode.DEFAULT
is used implicitly.
DatabaseException
- the base class for all BDB exceptions.V nextNoDup(LockMode lockMode) throws DatabaseException
first()
.lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V prev() throws DatabaseException
last()
.
LockMode.DEFAULT
is used implicitly.
DatabaseException
- the base class for all BDB exceptions.V prev(LockMode lockMode) throws DatabaseException
last()
.lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V prevDup() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.V prevDup(LockMode lockMode) throws DatabaseException
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.V prevNoDup() throws DatabaseException
last()
.
LockMode.DEFAULT
is used implicitly.
DatabaseException
- the base class for all BDB exceptions.V prevNoDup(LockMode lockMode) throws DatabaseException
last()
.lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.DatabaseException
- the base class for all BDB exceptions.V current() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.V current(LockMode lockMode) throws DatabaseException
lockMode
- the lock mode to use for this operation, or null to
use LockMode.DEFAULT
.java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.int count() throws DatabaseException
LockMode.DEFAULT
is used implicitly.
java.lang.IllegalStateException
- if the cursor is uninitialized.DatabaseException
- the base class for all BDB exceptions.java.util.Iterator<V> iterator()
LockMode.DEFAULT
is used implicitly.
iterator
in interface ForwardCursor<V>
iterator
in interface java.lang.Iterable<V>
java.util.Iterator<V> iterator(LockMode lockMode)
iterator
in interface ForwardCursor<V>
lockMode
- the lock mode to use for all operations performed
using the iterator, or null to use LockMode.DEFAULT
.boolean update(V entity) throws DatabaseException
entity
- the entity to replace the entity at the current position.java.lang.IllegalStateException
- if the cursor is uninitialized.java.lang.UnsupportedOperationException
- if the index is read only or if
the value type is not an entity type.DatabaseException
- the base class for all BDB exceptions.boolean delete() throws DatabaseException
java.lang.IllegalStateException
- if the cursor is uninitialized.java.lang.UnsupportedOperationException
- if the index is read only.DatabaseException
- the base class for all BDB exceptions.EntityCursor<V> dup() throws DatabaseException
Transaction
and CursorConfig
.DatabaseException
- the base class for all BDB exceptions.void close() throws DatabaseException
close
in interface ForwardCursor<V>
DatabaseException
- the base class for all BDB exceptions.Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.