DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

Berkeley DB Reference Guide:
Access Methods

PrevRefNext

Retrieving records with a cursor

The DBcursor->c_get method retrieves records from the database using a cursor. The DBcursor->c_get method takes a flag which controls how the cursor is positioned within the database and returns the key/data item associated with that positioning. Similar to DB->get, DBcursor->c_get may also take a supplied key and retrieve the data associated with that key from the database. There are several flags that you can set to customize retrieval.

Cursor position flags

DB_FIRST, DB_LAST
Return the first (last) record in the database.
DB_NEXT, DB_PREV
Return the next (previous) record in the database.
DB_NEXT_DUP
Return the next record in the database, if it is a duplicate data item for the current key.
DB_NEXT_NODUP, DB_PREV_NODUP
Return the next (previous) record in the database that is not a duplicate data item for the current key.
DB_CURRENT
Return the record from the database to which the cursor currently refers.

Retrieving specific key/data pairs

DB_SET
Return the record from the database that matches the supplied key. In the case of duplicates the first duplicate is returned and the cursor is positioned at the beginning of the duplicate list. The user can then traverse the duplicate entries for the key.
DB_SET_RANGE
Return the smallest record in the database greater than or equal to the supplied key. This functionality permits partial key matches and range searches in the Btree access method.
DB_GET_BOTH
Return the record from the database that matches both the supplied key and data items. This is particularly useful when there are large numbers of duplicate records for a key, as it allows the cursor to easily be positioned at the correct place for traversal of some part of a large set of duplicate records.
DB_GET_BOTH_RANGE
Return the smallest record in the database greater than or equal to the supplied key and data items.

Retrieving based on record numbers

DB_SET_RECNO
If the underlying database is a Btree, and was configured so that it is possible to search it by logical record number, retrieve a specific record based on a record number argument.
DB_GET_RECNO
If the underlying database is a Btree, and was configured so that it is possible to search it by logical record number, return the record number for the record to which the cursor refers.

Special-purpose flags

DB_CONSUME
Read-and-delete: the first record (the head) of the queue is returned and deleted. The underlying database must be a Queue.
DB_RMW
Read-modify-write: acquire write locks instead of read locks during retrieval. This can enhance performance in threaded applications by reducing the chance of deadlock.

In all cases, the cursor is repositioned by a DBcursor->c_get operation to point to the newly-returned key/data pair in the database.

The following is a code example showing a cursor walking through a database and displaying the records it contains to the standard output:

int
display(database)
	char *database;
{
	DB *dbp;
	DBC *dbcp;
	DBT key, data;
	int close_db, close_dbc, ret;

close_db = close_dbc = 0;

/* Open the database. */ if ((ret = db_create(&dbp, NULL, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", progname, db_strerror(ret)); return (1); } close_db = 1;

/* Turn on additional error output. */ dbp->set_errfile(dbp, stderr); dbp->set_errpfx(dbp, progname);

/* Open the database. */ if ((ret = dbp->open(dbp, NULL, database, NULL, DB_UNKNOWN, DB_RDONLY, 0)) != 0) { dbp->err(dbp, ret, "%s: DB->open", database); goto err; }

/* Acquire a cursor for the database. */ if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { dbp->err(dbp, ret, "DB->cursor"); goto err; } close_dbc = 1;

/* Initialize the key/data return pair. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data));

/* Walk through the database and print out the key/data pairs. */ while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) printf("%.*s : %.*s\n", (int)key.size, (char *)key.data, (int)data.size, (char *)data.data); if (ret != DB_NOTFOUND) { dbp->err(dbp, ret, "DBcursor->get"); goto err; }

err: if (close_dbc && (ret = dbcp->c_close(dbcp)) != 0) dbp->err(dbp, ret, "DBcursor->close"); if (close_db && (ret = dbp->close(dbp, 0)) != 0) fprintf(stderr, "%s: DB->close: %s\n", progname, db_strerror(ret)); return (0); }


PrevRefNext

Copyright (c) 1996-2005 Sleepycat Software, Inc. - All rights reserved.