Berkeley DB Reference Version 5.3.33

DB->get()

#include <db.h>

int
DB->get(DB *db,
    DB_TXN *txnid, DBT *key, DBT *data, u_int32_t flags);

int
DB->pget(DB *db,
    DB_TXN *txnid, DBT *key, DBT *pkey, DBT *data, u_int32_t flags);  

The DB->get() method retrieves key/data pairs from the database. The address and length of the data associated with the specified key are returned in the structure to which data refers.

In the presence of duplicate key values, DB->get() will return the first data item for the designated key. Duplicates are sorted by:

Retrieval of duplicates requires the use of cursor operations. See DBcursor->get() for details.

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

The DB->get() method will return DB_NOTFOUND if the specified key is not in the database. The DB->get() method will return DB_KEYEMPTY 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. Unless otherwise specified, the DB->get() method returns a non-zero error value on failure and 0 on success.

Parameters

txnid

If the operation is part of an application-specified transaction, the txnid parameter is a transaction handle returned from DB_ENV->txn_begin(); if the operation is part of a Berkeley DB Concurrent Data Store group, the txnid parameter is a handle returned from DB_ENV->cdsgroup_begin(); otherwise NULL. If no transaction handle is specified, but the operation occurs in a transactional database, the operation will be implicitly transaction protected.

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 not set to DB_CONSUME DB_CONSUME_WAIT, or DB_SET_RECNO, then this method will fail and return EINVAL.

pkey

The pkey parameter is 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 0 or one of the following values:

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

Errors

The DB->get() method may fail and return one of the following non-zero errors:

DB_BUFFER_SMALL

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

DB_LOCK_DEADLOCK

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

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.

You attempted to open a database handle that is configured for no waiting exclusive locking, but the exclusive lock could not be immediately obtained. See DB->set_lk_exclusive() for more information.

DB_LOCK_NOTGRANTED

The DB_CONSUME_WAIT flag was specified, lock or transaction timers were configured and the lock could not be granted before the wait-time expired.

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

DB_REP_LOCKOUT

The operation was blocked by client/master synchronization.

DB_SECONDARY_BAD

A secondary index references a nonexistent primary key.

EINVAL

If a record number of 0 was specified; the DB_THREAD flag was specified to the DB->open() method and none of the DB_DBT_MALLOC, DB_DBT_REALLOC or DB_DBT_USERMEM flags were set in the DBT; the DB->pget() method was called with a DB handle that does not refer to a secondary index; or if an invalid flag value or parameter was specified.

Class

DB

See Also

Database and Related Methods