1<!--$Id: bt_recnum.so,v 10.23 2002/06/24 14:50:23 bostic Exp $--> 2<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 3<!--See the file LICENSE for redistribution information.--> 4<html> 5<head> 6<title>Berkeley DB Reference Guide: Retrieving Btree records by logical record number</title> 7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> 8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 9</head> 10<body bgcolor=white> 11<a name="2"><!--meow--></a><a name="3"><!--meow--></a> 12<table width="100%"><tr valign=top> 13<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td> 14<td align=right><a href="/am_conf/bt_minkey.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/am_conf/h_ffactor.html"><img src="/images/next.gif" alt="Next"></a> 15</td></tr></table> 16<p align=center><b>Retrieving Btree records by logical record number</b></p> 17<p>The Btree access method optionally supports retrieval by logical record 18numbers. To configure a Btree to support record numbers, call the 19<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.</p> 20<p>Configuring a Btree for record numbers should not be done lightly. 21While often useful, it may significantly slow down the speed at which 22items can be stored into the database, and can severely impact 23application throughput. Generally it should be avoided in trees with 24a need for high write concurrency.</p> 25<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 26<a href="/api_c/db_get.html">DB->get</a> and <a href="/api_c/dbc_get.html">DBcursor->get</a> methods. The following is an example of 27a routine that displays the data item for a Btree database created with 28the <a href="/api_c/db_set_flags.html#DB_RECNUM">DB_RECNUM</a> option.</p> 29<blockquote><pre>int 30rec_display(dbp, recno) 31 DB *dbp; 32 db_recno_t recno; 33{ 34 DBT key, data; 35 int ret; 36<p> 37 memset(&key, 0, sizeof(key)); 38 key.data = &recno; 39 key.size = sizeof(recno); 40 memset(&data, 0, sizeof(data)); 41<p> 42 if ((ret = dbp->get(dbp, NULL, &key, &data, DB_SET_RECNO)) != 0) 43 return (ret); 44 printf("data for %lu: %.*s\n", 45 (u_long)recno, (int)data.size, (char *)data.data); 46 return (0); 47}</pre></blockquote> 48<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 49to the <a href="/api_c/dbc_get.html">DBcursor->get</a> method. The following is an example of a routine that 50displays the record number associated with a specific key.</p> 51<blockquote><pre>int 52recno_display(dbp, keyvalue) 53 DB *dbp; 54 char *keyvalue; 55{ 56 DBC *dbcp; 57 DBT key, data; 58 db_recno_t recno; 59 int ret, t_ret; 60<p> 61 /* Acquire a cursor for the database. */ 62 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { 63 dbp->err(dbp, ret, "DB->cursor"); 64 goto err; 65 } 66<p> 67 /* Position the cursor. */ 68 memset(&key, 0, sizeof(key)); 69 key.data = keyvalue; 70 key.size = strlen(keyvalue); 71 memset(&data, 0, sizeof(data)); 72 if ((ret = dbcp->c_get(dbcp, &key, &data, DB_SET)) != 0) { 73 dbp->err(dbp, ret, "DBC->c_get(DB_SET): %s", keyvalue); 74 goto err; 75 } 76<p> 77 /* 78 * Request the record number, and store it into appropriately 79 * sized and aligned local memory. 80 */ 81 memset(&data, 0, sizeof(data)); 82 data.data = &recno; 83 data.ulen = sizeof(recno); 84 data.flags = DB_DBT_USERMEM; 85 if ((ret = dbcp->c_get(dbcp, &key, &data, DB_GET_RECNO)) != 0) { 86 dbp->err(dbp, ret, "DBC->c_get(DB_GET_RECNO)"); 87 goto err; 88 } 89<p> 90 printf("key for requested key was %lu\n", (u_long)recno); 91<p> 92err: /* Close the cursor. */ 93 if ((t_ret = dbcp->c_close(dbcp)) != 0) { 94 if (ret == 0) 95 ret = t_ret; 96 dbp->err(dbp, ret, "DBC->close"); 97 } 98 return (ret); 99}</pre></blockquote> 100<table width="100%"><tr><td><br></td><td align=right><a href="/am_conf/bt_minkey.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/am_conf/h_ffactor.html"><img src="/images/next.gif" alt="Next"></a> 101</td></tr></table> 102<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 103</body> 104</html> 105