2
<!--Copyright 1997-2002 by Sleepycat Software, Inc.-->
3
<!--All rights reserved.-->
4
<!--See the file LICENSE for redistribution information.-->
7
<title>Berkeley DB Reference Guide: Retrieving Btree records by logical record number</title>
8
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
9
<meta name="keywords" content="embedded,database,programmatic,toolkit,b+tree,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,java,C,C++">
12
<a name="2"><!--meow--></a><a name="3"><!--meow--></a>
13
<table width="100%"><tr valign=top>
14
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></h3></td>
15
<td align=right><a href="../../ref/am_conf/bt_minkey.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_conf/h_ffactor.html"><img src="../../images/next.gif" alt="Next"></a>
18
<h1 align=center>Retrieving Btree records by logical record number</h1>
19
<p>The Btree access method optionally supports retrieval by logical record
20
numbers. To configure a Btree to support record numbers, call the
21
<a href="../../api_c/db_set_flags.html">DB->set_flags</a> method with the <a href="../../api_c/db_set_flags.html#DB_RECNUM">DB_RECNUM</a> flag.
22
<p>Configuring a Btree for record numbers should not be done lightly.
23
While often useful, it may significantly slow down the speed at which
24
items can be stored into the database, and can severely impact
25
application throughput. Generally it should be avoided in trees with
26
a need for high write concurrency.
27
<p>To retrieve by record number, use the <a href="../../api_c/db_get.html#DB_SET_RECNO">DB_SET_RECNO</a> flag to the
28
<a href="../../api_c/db_get.html">DB->get</a> and <a href="../../api_c/dbc_get.html">DBcursor->c_get</a> methods. The following is an example of
29
a routine that displays the data item for a Btree database created with
30
the <a href="../../api_c/db_set_flags.html#DB_RECNUM">DB_RECNUM</a> option.
31
<p><blockquote><pre>int
32
rec_display(dbp, recno)
39
memset(&key, 0, sizeof(key));
41
key.size = sizeof(recno);
42
memset(&data, 0, sizeof(data));
44
if ((ret = dbp->get(dbp, NULL, &key, &data, DB_SET_RECNO)) != 0)
46
printf("data for %lu: %.*s\n",
47
(u_long)recno, (int)data.size, (char *)data.data);
50
<p>To determine a key's record number, use the <a href="../../api_c/dbc_get.html#DB_GET_RECNO">DB_GET_RECNO</a> flag
51
to the <a href="../../api_c/dbc_get.html">DBcursor->c_get</a> method. The following is an example of a routine that
52
displays the record number associated with a specific key.
53
<p><blockquote><pre>int
54
recno_display(dbp, keyvalue)
63
/* Acquire a cursor for the database. */
64
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
65
dbp->err(dbp, ret, "DB->cursor");
69
/* Position the cursor. */
70
memset(&key, 0, sizeof(key));
72
key.size = strlen(keyvalue);
73
memset(&data, 0, sizeof(data));
74
if ((ret = dbcp->c_get(dbcp, &key, &data, DB_SET)) != 0) {
75
dbp->err(dbp, ret, "DBC->c_get(DB_SET): %s", keyvalue);
80
* Request the record number, and store it into appropriately
81
* sized and aligned local memory.
83
memset(&data, 0, sizeof(data));
85
data.ulen = sizeof(recno);
86
data.flags = DB_DBT_USERMEM;
87
if ((ret = dbcp->c_get(dbcp, &key, &data, DB_GET_RECNO)) != 0) {
88
dbp->err(dbp, ret, "DBC->c_get(DB_GET_RECNO)");
92
printf("key for requested key was %lu\n", (u_long)recno);
94
err: /* Close the cursor. */
95
if ((t_ret = dbcp->c_close(dbcp)) != 0) {
98
dbp->err(dbp, ret, "DBC->close");
102
<table width="100%"><tr><td><br></td><td align=right><a href="../../ref/am_conf/bt_minkey.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../../reftoc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../../ref/am_conf/h_ffactor.html"><img src="../../images/next.gif" alt="Next"></a>
104
<p><font size=1><a href="http://www.sleepycat.com">Copyright Sleepycat Software</a></font>