Berkeley DB Reference Guide:
Access Methods

PrevRefNext

Retrieving Btree records by logical record number

The Btree access method optionally supports retrieval by logical record numbers. To configure a Btree to support record numbers, call the DB->set_flags method with the DB_RECNUM flag.

Configuring a Btree for record numbers should not be done lightly. While often useful, it may significantly slow down the speed at which items can be stored into the database, and can severely impact application throughput. Generally it should be avoided in trees with a need for high write concurrency.

To retrieve by record number, use the DB_SET_RECNO flag to the DB->get and DBcursor->c_get methods. The following is an example of a routine that displays the data item for a Btree database created with the DB_RECNUM option.

int
rec_display(dbp, recno)
	DB *dbp;
	db_recno_t recno;
{
	DBT key, data;
	int ret;

memset(&key, 0, sizeof(key)); key.data = &recno; key.size = sizeof(recno); memset(&data, 0, sizeof(data));

if ((ret = dbp->get(dbp, NULL, &key, &data, DB_SET_RECNO)) != 0) return (ret); printf("data for %lu: %.*s\n", (u_long)recno, (int)data.size, (char *)data.data); return (0); }

To determine a key's record number, use the DB_GET_RECNO flag to the DBcursor->c_get method. The following is an example of a routine that displays the record number associated with a specific key.

int
recno_display(dbp, keyvalue)
	DB *dbp;
	char *keyvalue;
{
	DBC *dbcp;
	DBT key, data;
	db_recno_t recno;
	int ret, t_ret;

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

/* Position the cursor. */ memset(&key, 0, sizeof(key)); key.data = keyvalue; key.size = strlen(keyvalue); memset(&data, 0, sizeof(data)); if ((ret = dbcp->c_get(dbcp, &key, &data, DB_SET)) != 0) { dbp->err(dbp, ret, "DBC->c_get(DB_SET): %s", keyvalue); goto err; }

/* * Request the record number, and store it into appropriately * sized and aligned local memory. */ memset(&data, 0, sizeof(data)); data.data = &recno; data.ulen = sizeof(recno); data.flags = DB_DBT_USERMEM; if ((ret = dbcp->c_get(dbcp, &key, &data, DB_GET_RECNO)) != 0) { dbp->err(dbp, ret, "DBC->c_get(DB_GET_RECNO)"); goto err; }

printf("key for requested key was %lu\n", (u_long)recno);

err: /* Close the cursor. */ if ((t_ret = dbcp->c_close(dbcp)) != 0) { if (ret == 0) ret = t_ret; dbp->err(dbp, ret, "DBC->close"); } return (ret); }


PrevRefNext

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