Dbc::get()

#include <db_cxx.h>

int
Dbc::get(Dbt *key, Dbt *data, u_int32_t flags);

int
Dbc::pget(Dbt *key, Dbt *pkey, Dbt *data, u_int32_t flags); 

The Dbc::get() method retrieves key/data pairs from the database. The address and length of the key are returned in the object to which key refers (except for the case of the DB_SET flag, in which the key object is unchanged), and the address and length of the data are returned in the object to which data refers.

When called on a cursor opened on a database that has been made into a secondary index using the Db::associate() method, the Dbc::get() and Dbc::pget() methods return the key from the secondary index and the data item from the primary database. In addition, the Dbc::pget() method returns the key from the primary database. In databases that are not secondary indices, the Dbc::pget() method will always fail.

Modifications to the database during a sequential scan will be reflected in the scan; that is, records inserted behind a cursor will not be returned while records inserted in front of a cursor will be returned.

In Queue and Recno databases, missing entries (that is, entries that were never explicitly created or that were created and then deleted) will be skipped during a sequential scan.

Unless otherwise specified, the Dbc::get() method either returns a non-zero error value or throws an exception that encapsulates a non-zero error value on failure, and returns 0 on success.

If Dbc::get() fails for any reason, the state of the cursor will be unchanged.

Parameters

key

The key Dbt operated on.

If DB_DBT_PARTIAL is set for the Dbt used for this parameter, and if the flags parameter is set to DB_GET_BOTH, DB_GET_BOTH_RANGE, DB_SET, or DB_SET_RECNO, then this method will fail and return EINVAL.

pkey

The return key from the primary database. If DB_DBT_PARTIAL is set for the Dbt used for this parameter, then this method will fail and return EINVAL.

data

The data Dbt operated on.

flags

The flags parameter must be set to one of the following values:

  • DB_CURRENT

    Return the key/data pair to which the cursor refers.

    The Dbc::get() method will return DB_KEYEMPTY if DB_CURRENT is set and the cursor key/data pair was deleted.

  • DB_FIRST

    The cursor is set to refer to the first key/data pair of the database, and that pair is returned. If the first key has duplicate values, the first data item in the set of duplicates is returned.

    If the database is a Queue or Recno database, Dbc::get() using the DB_FIRST flag will ignore any keys that exist but were never explicitly created by the application, or were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_FIRST is set and the database is empty.

  • DB_GET_BOTH

    Move the cursor to the specified key/data pair of the database. The cursor is positioned to a key/data pair if both the key and data match the values provided on the key and data parameters.

    In all other ways, this flag is identical to the DB_SET flag.

    When used with Dbc::pget() on a secondary index handle, both the secondary and primary keys must be matched by the secondary and primary key item in the database. It is an error to use the DB_GET_BOTH flag with the Dbc::get() version of this method and a cursor that has been opened on a secondary index handle.

  • DB_GET_BOTH_RANGE

    Move the cursor to the specified key/data pair of the database. The key parameter must be an exact match with a key in the database. The data item retrieved is the item in a duplicate set that is the smallest value which is greater than or equal to the value provided by the data parameter (as determined by the comparison function). If this flag is specified on a database configured without sorted duplicate support, the behavior is identical to the DB_GET_BOTH flag. Returns the datum associated with the given key/data pair.

    In all other ways, this flag is identical to the DB_GET_BOTH flag.

  • DB_GET_RECNO

    Return the record number associated with the cursor. The record number will be returned in data, as described in Dbt. The key parameter is ignored.

    For DB_GET_RECNO to be specified, the underlying database must be of type Btree, and it must have been created with the DB_RECNUM flag.

    When called on a cursor opened on a database that has been made into a secondary index, the Dbc::get() and Dbc::pget() methods return the record number of the primary database in data. In addition, the Dbc::pget() method returns the record number of the secondary index in pkey. If either underlying database is not of type Btree or is not created with the DB_RECNUM flag, the out-of-band record number of 0 is returned.

  • DB_JOIN_ITEM

    Do not use the data value found in all of the cursors as a lookup key for the primary database, but simply return it in the key parameter instead. The data parameter is left unchanged.

    For DB_JOIN_ITEM to be specified, the underlying cursor must have been returned from the Db::join() method.

    This flag is not supported for Heap databases.

  • DB_LAST

    The cursor is set to refer to the last key/data pair of the database, and that pair is returned. If the last key has duplicate values, the last data item in the set of duplicates is returned.

    If the database is a Queue or Recno database, Dbc::get() using the DB_LAST flag will ignore any keys that exist but were never explicitly created by the application, or were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_LAST is set and the database is empty.

  • DB_NEXT

    If the cursor is not yet initialized, DB_NEXT is identical to DB_FIRST. Otherwise, the cursor is moved to the next key/data pair of the database, and that pair is returned. In the presence of duplicate key values, the value of the key may not change.

    If the database is a Queue or Recno database, Dbc::get() using the DB_NEXT flag will skip any keys that exist but were never explicitly created by the application, or those that were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_NEXT is set and the cursor is already on the last record in the database.

  • DB_NEXT_DUP

    If the next key/data pair of the database is a duplicate data record for the current key/data pair, the cursor is moved to the next key/data pair of the database, and that pair is returned.

    The Dbc::get() method will return DB_NOTFOUND if DB_NEXT_DUP is set and the next key/data pair of the database is not a duplicate data record for the current key/data pair.

    If using a Heap database, this flag results in this method returning DB_NOTFOUND.

  • DB_NEXT_NODUP

    If the cursor is not yet initialized, DB_NEXT_NODUP is identical to DB_FIRST. Otherwise, the cursor is moved to the next non-duplicate key of the database, and that key/data pair is returned.

    If the database is a Queue or Recno database, Dbc::get() using the DB_NEXT_NODUP flag will ignore any keys that exist but were never explicitly created by the application, or those that were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_NEXT_NODUP is set and no non-duplicate key/data pairs exist after the cursor position in the database.

    If using a Heap database, this flag is identical to the DB_NEXT flag.

  • DB_PREV

    If the cursor is not yet initialized, DB_PREV is identical to DB_LAST. Otherwise, the cursor is moved to the previous key/data pair of the database, and that pair is returned. In the presence of duplicate key values, the value of the key may not change.

    If the database is a Queue or Recno database, Dbc::get() using the DB_PREV flag will skip any keys that exist but were never explicitly created by the application, or those that were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_PREV is set and the cursor is already on the first record in the database.

  • DB_PREV_DUP

    If the previous key/data pair of the database is a duplicate data record for the current key/data pair, the cursor is moved to the previous key/data pair of the database, and that pair is returned.

    The Dbc::get() method will return DB_NOTFOUND if DB_PREV_DUP is set and the previous key/data pair of the database is not a duplicate data record for the current key/data pair.

    If using a Heap database, this flag results in this method returning DB_NOTFOUND.

  • DB_PREV_NODUP

    If the cursor is not yet initialized, DB_PREV_NODUP is identical to DB_LAST. Otherwise, the cursor is moved to the previous non-duplicate key of the database, and that key/data pair is returned.

    If the database is a Queue or Recno database, Dbc::get() using the DB_PREV_NODUP flag will ignore any keys that exist but were never explicitly created by the application, or those that were created and later deleted.

    The Dbc::get() method will return DB_NOTFOUND if DB_PREV_NODUP is set and no non-duplicate key/data pairs exist before the cursor position in the database.

    If using a Heap database, this flag is identical to the DB_PREV flag.

  • DB_SET

    Move the cursor to the specified key/data pair of the database, and return the datum associated with the given key.

    The Dbc::get() method will return DB_NOTFOUND if DB_SET is set and no matching keys are found. The Dbc::get() method will return DB_KEYEMPTY if DB_SET is set and the database is a Queue or Recno database, and the specified key exists, but was never explicitly created by the application or was later deleted. In the presence of duplicate key values, Dbc::get() will return the first data item for the given key.

  • DB_SET_RANGE

    Move the cursor to the specified key/data pair of the database. In the case of the Btree access method, the key is returned as well as the data item and the returned key/data pair is the smallest key greater than or equal to the specified key (as determined by the Btree comparison function), permitting partial key matches and range searches.

    In all other ways the behavior of this flag is the same as the DB_SET flag.

  • DB_SET_RECNO

    Move the cursor to the specific numbered record of the database, and return the associated key/data pair. The data field of the specified key must be a pointer to a memory location from which a db_recno_t may be read, as described in Dbt. This memory location will be read to determine the record to be retrieved.

    For DB_SET_RECNO to be specified, the underlying database must be of type Btree, and it must have been created with the DB_RECNUM flag.

In addition, the following flags may be set by bitwise inclusively OR'ing them into the flags parameter:

  • DB_IGNORE_LEASE

    This flag is relevant only when using a replicated environment.

    Return the data item irrespective of the state of master leases. The item will be returned under all conditions: if master leases are not configured, if the request is made to a client, if the request is made to a master with a valid lease, or if the request is made to a master without a valid lease.

  • DB_READ_COMMITTED

    Configure a transactional get operation to have degree 2 isolation (the read is not repeatable).

  • DB_READ_UNCOMMITTED

    Database items read during a transactional call will have degree 1 isolation, including modified but not yet committed data. Silently ignored if the DB_READ_UNCOMMITTED flag was not specified when the underlying database was opened.

  • DB_MULTIPLE

    Return multiple data items in the data parameter.

    In the case of Btree or Hash databases, duplicate data items for the current key, starting at the current cursor position, are entered into the buffer. Subsequent calls with both the DB_NEXT_DUP and DB_MULTIPLE flags specified will return additional duplicate data items associated with the current key or DB_NOTFOUND if there are no additional duplicate data items to return. Subsequent calls with both the DB_NEXT and DB_MULTIPLE flags specified will return additional duplicate data items associated with the current key or if there are no additional duplicate data items will return the next key and its data items or DB_NOTFOUND if there are no additional keys in the database.

    In the case of Queue or Recno databases, data items starting at the current cursor position are entered into the buffer. The record number of the first record will be returned in the key parameter. The record number of each subsequent returned record must be calculated from this value. Subsequent calls with the DB_MULTIPLE flag specified will return additional data items or DB_NOTFOUND if there are no additional data items to return.

    The buffer to which the data parameter refers must be provided from user memory (see DB_DBT_USERMEM ). The buffer must be at least as large as the page size of the underlying database, aligned for unsigned integer access, and be a multiple of 1024 bytes in size. If the buffer size is insufficient, then upon return from the call the size field of the data parameter will have been set to an estimated buffer size, and the error DB_BUFFER_SMALL is returned. (The size is an estimate as the exact size needed may not be known until all entries are read. It is best to initially provide a relatively large buffer, but applications should be prepared to resize the buffer as necessary and repeatedly call the method.)

    The multiple data items can be iterated over using the DbMultipleDataIterator class.

    The DB_MULTIPLE flag may only be used with the DB_CURRENT, DB_FIRST, DB_GET_BOTH, DB_GET_BOTH_RANGE, DB_NEXT, DB_NEXT_DUP, DB_NEXT_NODUP, DB_SET, DB_SET_RANGE, and DB_SET_RECNO options. The DB_MULTIPLE flag may not be used when accessing databases made into secondary indices using the Db::associate() method.

  • DB_MULTIPLE_KEY

    Return multiple key and data pairs in the data parameter.

    Key and data pairs, starting at the current cursor position, are entered into the buffer. Subsequent calls with both the DB_NEXT and DB_MULTIPLE_KEY flags specified will return additional key and data pairs or DB_NOTFOUND if there are no additional key and data items to return.

    In the case of Btree or Hash databases, the multiple key and data pairs can be iterated over using the DbMultipleKeyDataIterator class.

    In the case of Queue or Recno databases, the multiple record number and data pairs can be iterated over using the DbMultipleRecnoDataIterator class.

    The buffer to which the data parameter refers must be provided from user memory (see DB_DBT_USERMEM ). The buffer must be at least as large as the page size of the underlying database, aligned for unsigned integer access, and be a multiple of 1024 bytes in size. If the buffer size is insufficient, then upon return from the call the size field of the data parameter will have been set to an estimated buffer size, and the error DB_BUFFER_SMALL is returned. (The size is an estimate as the exact size needed may not be known until all entries are read. It is best to initially provide a relatively large buffer, but applications should be prepared to resize the buffer as necessary and repeatedly call the method.)

    The DB_MULTIPLE_KEY flag may only be used with the DB_CURRENT, DB_FIRST, DB_GET_BOTH, DB_GET_BOTH_RANGE, DB_NEXT, DB_NEXT_DUP, DB_NEXT_NODUP, DB_SET, DB_SET_RANGE, and DB_SET_RECNO options. The DB_MULTIPLE_KEY flag may not be used when accessing databases made into secondary indices using the Db::associate() method.

  • DB_RMW

    Acquire write locks instead of read locks when doing the read, if locking is configured. Setting this flag can eliminate deadlock during a read-modify-write cycle by acquiring the write lock during the read part of the cycle so that another thread of control acquiring a read lock for the same item, in its own read-modify-write cycle, will not result in deadlock.

Errors

The Dbc::get() method may fail and throw a DbException exception, encapsulating one of the following non-zero errors, or return one of the following non-zero errors:

DbMemoryException or DB_BUFFER_SMALL

The requested item could not be returned due to undersized buffer.

DbMemoryException is thrown if your Berkeley DB API is configured to throw exceptions. Otherwise, DB_BUFFER_SMALL is returned.

DbDeadlockException or DB_LOCK_DEADLOCK

A transactional database environment operation was selected to resolve a deadlock.

DbDeadlockException is thrown if your Berkeley DB API is configured to throw exceptions. Otherwise, DB_LOCK_DEADLOCK is returned.

DbLockNotGrantedException or DB_LOCK_NOTGRANTED

A Berkeley DB Concurrent Data Store database environment configured for lock timeouts was unable to grant a lock in the allowed time.

DbLockNotGrantedException is thrown if your Berkeley DB API is configured to throw exceptions. Otherwise, DB_LOCK_NOTGRANTED is returned.

DbRepHandleDeadException or DB_REP_HANDLE_DEAD

When a client synchronizes with the master, it is possible for committed transactions to be rolled back. This invalidates all the database and cursor handles opened in the replication environment. Once this occurs, an attempt to use such a handle will throw a DbRepHandleDeadException (if your application is configured to throw exceptions), or return DB_REP_HANDLE_DEAD. The application will need to discard the handle and open a new one in order to continue processing.

DB_REP_LEASE_EXPIRED

The operation failed because the site's replication master lease has expired.

DbDeadlockException or DB_REP_LOCKOUT

The operation was blocked by client/master synchronization.

DbDeadlockException is thrown if your Berkeley DB API is configured to throw exceptions. Otherwise, DB_REP_LOCKOUT is returned.

DB_SECONDARY_BAD

A secondary index references a nonexistent primary key.

EINVAL

If the DB_CURRENT, DB_NEXT_DUP or DB_PREV_DUP flags were specified and the cursor has not been initialized; the Dbc::pget() method was called with a cursor that does not refer to a secondary index; or if an invalid flag value or parameter was specified.

Class

Dbc

See Also

Database Cursors and Related Methods