Berkeley DB: DBcursor->c_get
Description: Berkeley DB: An embedded database programmatic toolkit.
Keywords: embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++

# DBcursor->c_get |

`

###

    #include <db.h>

    int
    DBcursor->c_get(DBC *cursor,
        DBT *key, DBT *data, u_int32_t flags);
    int
    DBcursor->c_pget(DBC *cursor,
        DBT *key, DBT *pkey, DBT *data, u_int32_t flags);

# Description

The DBcursor->c_get function 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 function, the DBcursor->c_get and DBcursor->c_pget functions return the key from the secondary index and the data item from the primary database. In addition, the DBcursor->c_pget function returns the key from the primary database. In databases that are not secondary indices, the DBcursor->c_pget interface will always fail and return EINVAL.

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.

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

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

  If the cursor key/data pair was deleted, DBcursor->c_get will return DB_KEYEMPTY.

  If the cursor is not yet initialized, the DBcursor->c_get function will return EINVAL.

DB_FIRST, DB_LAST
  The cursor is set to refer to the first (last) key/data pair of the database, and that pair is returned. In the presence of duplicate key values, the first (last) data item in the set of duplicates is returned.

  If the database is a Queue or Recno database, DBcursor->c_get using the DB_FIRST (DB_LAST) flags will ignore any keys that exist but were never explicitly created by the application, or were created and later deleted.

  If the database is empty, DBcursor->c_get will return DB_NOTFOUND.

DB_GET_BOTH
  The DB_GET_BOTH flag is identical to the DB_SET flag, except that both the key and the data arguments must be matched by the key and data item in the database.

  When used with the DBcursor->c_pget version of this interface 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 DBcursor->c_get version of this interface and a cursor that has been opened on a secondary index handle.

DB_GET_BOTH_RANGE
  The DB_GET_BOTH_RANGE flag is identical to the DB_GET_BOTH flag, except that, in the case of any database supporting sorted duplicate sets, the returned key/data pair is the smallest data item greater than or equal to the specified data item (as determined by the comparison function), permitting partial matches and range searches in duplicate data sets.

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.

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 function.

DB_NEXT, DB_PREV
  If the cursor is not yet initialized, DB_NEXT (DB_PREV) is identical to DB_FIRST (DB_LAST). Otherwise, the cursor is moved to the next (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, DBcursor->c_get using the DB_NEXT (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.

  If the cursor is already on the last (first) record in the database, DBcursor->c_get will return DB_NOTFOUND.

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. Otherwise, DBcursor->c_get will return DB_NOTFOUND.

  If the cursor is not yet initialized, the DBcursor->c_get function will return EINVAL.

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

  If the database is a Queue or Recno database, DBcursor->c_get using the DB_NEXT_NODUP (DB_PREV_NODUP) flags will ignore any keys that exist but were never explicitly created by the application, or those that were created and later deleted.

  If no non-duplicate key/data pairs occur after (before) the cursor position in the database, DBcursor->c_get will return DB_NOTFOUND.

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

  In the presence of duplicate key values, DBcursor->c_get will return the first data item for the given key.

  If 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, DBcursor->c_get will return DB_KEYEMPTY.

  If no matching keys are found, DBcursor->c_get will return DB_NOTFOUND.

DB_SET_RANGE
  The DB_SET_RANGE flag is identical to the DB_SET flag, except that the key is returned as well as the data item; and, in the case of the Btree access method, the returned key/data pair is the smallest key greater than or equal to the specified key (as determined by the comparison function), permitting partial key matches and range searches.

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_DIRTY_READ
  Read modified but not yet committed data. Silently ignored if the DB_DIRTY_READ flag was not specified when the underlying database was opened.

DB_MULTIPLE
  Return multiple data items. The buffer to which the data argument refers is filled with the specified key's data items. If the first data item associated with the key cannot fit into the buffer, the size field of the data argument is set to the length needed to retrieve it, and the error ENOMEM is returned. Subsequent calls with both the DB_NEXT_DUP and DB_MULTIPLE flags specified will return additional data items associated with the current key or DB_NOTFOUND if there is no additional data items to return.

  If DB_MULTIPLE is specified for the Queue and Recno access methods, the buffer will be filled with as many data records as possible. The record number of the first record will be returned in the key argument. The record number of each subsequent returned record must be calculated from this value.

  The buffer to which the data argument refers should be large relative to the page size of the underlying database, aligned for unsigned integer access, and be a multiple of 1024 bytes in size.

  The DB_MULTIPLE flag may only be used with the DB_CURRENT, DB_FIRST, DB_GET_BOTH, 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 function.

  See DB_MULTIPLE_INIT for more information.

DB_MULTIPLE_KEY
  Return multiple key and data pairs. The buffer to which the data argument refers is filled with key and data pairs. If the first key and data pair cannot fit into the buffer, the size field of the data argument is set to the length needed to retrieve them, and the error ENOMEM is returned.

  The buffer to which the data argument refers should be large relative to the page size of the underlying database, aligned for unsigned integer access, and be a multiple of 1024 bytes in size.

  The DB_MULTIPLE_KEY flag may only be used with the DB_CURRENT, DB_FIRST, DB_GET_BOTH, DB_NEXT, 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 function.

  See DB_MULTIPLE_INIT for more information.

DB_RMW
  Acquire write locks instead of read locks when doing the retrieval. 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.

Otherwise, the DBcursor->c_get function returns a non-zero error value on failure and 0 on success.

If DBcursor->c_get fails for any reason, the state of the cursor will be unchanged.

# Errors

The DBcursor->c_get function may fail and return a non-zero error for the following conditions:

DB_LOCK_DEADLOCK
  The operation was selected to resolve a deadlock.

DB_SECONDARY_BAD
  A secondary index references a nonexistent primary key.

ENOMEM
  There was insufficient memory to return the requested item.

EINVAL
  An invalid flag value or parameter was specified.

  The specified cursor was not currently initialized.

  The DBcursor->c_pget interface was called with a cursor that does not refer to a secondary index.

The DBcursor->c_get function may fail and return a non-zero error for errors specified for other Berkeley DB and C library or system functions. If a catastrophic error has occurred, the DBcursor->c_get function may fail and return DB_RUNRECOVERY, in which case all subsequent Berkeley DB calls will fail in the same way.

# See Also

DBcursor->c_close, DBcursor->c_count, DBcursor->c_del, DBcursor->c_dup, DBcursor->c_get, DBcursor->c_pget, and DBcursor->c_put.`

Copyright Sleepycat Software