| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Logical record numbers</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="am_conf.html" title="Chapter 2. Access Method Configuration" /> |
| 11 | <link rel="prev" href="am_conf_select.html" title="Selecting an access method" /> |
| 12 | <link rel="next" href="general_am_conf.html" title="General access method configuration" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Logical record numbers</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_conf_select.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 2. |
| 26 | Access Method Configuration |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="general_am_conf.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="am_conf_logrec"></a>Logical record numbers</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>The Berkeley DB Btree, Queue and Recno access methods can operate on logical |
| 42 | record numbers. Record numbers are 1-based, not 0-based, that is, the |
| 43 | first record in a database is record number 1.</p> |
| 44 | <p>In all cases for the Queue and Recno access methods, and when calling |
| 45 | the Btree access method using the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> and <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> methods |
| 46 | with the <a href="../api_reference/C/dbget.html#dbget_DB_SET_RECNO" class="olink">DB_SET_RECNO</a> flag specified, the <span class="bold"><strong>data</strong></span> field of |
| 47 | the key <a href="../api_reference/C/dbt.html" class="olink">DBT</a> must be a pointer to a memory location of type |
| 48 | <span class="bold"><strong>db_recno_t</strong></span>, as typedef'd in the standard Berkeley DB include file. |
| 49 | The <span class="bold"><strong>size</strong></span> field of the key <a href="../api_reference/C/dbt.html" class="olink">DBT</a> should be the size of that |
| 50 | type (for example, "sizeof(db_recno_t)" in the C programming language). |
| 51 | The <span class="bold"><strong>db_recno_t</strong></span> type is a 32-bit unsigned type, which limits the |
| 52 | number of logical records in a Queue or Recno database, and the maximum |
| 53 | logical record which may be directly retrieved from a Btree database, |
| 54 | to 4,294,967,295.</p> |
| 55 | <p>Record numbers in Recno databases can be configured to run in either |
| 56 | mutable or fixed mode: mutable, where logical record numbers change as |
| 57 | records are deleted or inserted, and fixed, where record numbers never |
| 58 | change regardless of the database operation. Record numbers in Queue |
| 59 | databases are always fixed, and never change regardless of the database |
| 60 | operation. Record numbers in Btree databases are always mutable, and |
| 61 | as records are deleted or inserted, the logical record number for other |
| 62 | records in the database can change. See |
| 63 | <a class="xref" href="rq_conf.html#am_conf_renumber" title="Logically renumbering records">Logically renumbering records</a> |
| 64 | for more information.</p> |
| 65 | <p>When appending new data items into Queue databases, record numbers wrap |
| 66 | around. When the tail of the queue reaches the maximum record number, |
| 67 | the next record appended will be given record number 1. If the head of |
| 68 | the queue ever catches up to the tail of the queue, Berkeley DB will return |
| 69 | the system error EFBIG. Record numbers do not wrap around when appending |
| 70 | new data items into Recno databases.</p> |
| 71 | <p>Configuring Btree databases to support record numbers can severely limit |
| 72 | the throughput of applications with multiple concurrent threads writing |
| 73 | the database, because locations used to store record counts often become |
| 74 | hot spots that many different threads all need to update. In the case |
| 75 | of a Btree supporting duplicate data items, the logical record number |
| 76 | refers to a key and all of its data items, as duplicate data items are |
| 77 | not individually numbered.</p> |
| 78 | <p>The following is an example function that reads records from standard |
| 79 | input and stores them into a Recno database. The function then uses a |
| 80 | cursor to step through the database and display the stored records.</p> |
| 81 | <a id="prog_am1"></a> |
| 82 | <pre class="programlisting"> |
| 83 | int |
| 84 | recno_build(DB *dbp) |
| 85 | { |
| 86 | DBC *dbcp; |
| 87 | DBT key, data; |
| 88 | db_recno_t recno; |
| 89 | u_int32_t len; |
| 90 | int ret; |
| 91 | char buf[1024]; |
| 92 | |
| 93 | /* Insert records into the database. */ |
| 94 | memset(&key, 0, sizeof(DBT)); |
| 95 | memset(&data, 0, sizeof(DBT)); |
| 96 | for (recno = 1;; ++recno) { |
| 97 | printf("record #%lu> ", (u_long)recno); |
| 98 | fflush(stdout); |
| 99 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 100 | break; |
| 101 | if ((len = strlen(buf)) <= 1) |
| 102 | continue; |
| 103 | |
| 104 | key.data = &recno; |
| 105 | key.size = sizeof(recno); |
| 106 | data.data = buf; |
| 107 | data.size = len - 1; |
| 108 | |
| 109 | switch (ret = dbp->put(dbp, NULL, &key, &data, 0)) { |
| 110 | case 0: |
| 111 | break; |
| 112 | default: |
| 113 | dbp->err(dbp, ret, "DB->put"); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | printf("\n"); |
| 118 | |
| 119 | /* Acquire a cursor for the database. */ |
| 120 | if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { |
| 121 | dbp->err(dbp, ret, "DB->cursor"); |
| 122 | return (1); |
| 123 | } |
| 124 | |
| 125 | /* Re-initialize the key/data pair. */ |
| 126 | memset(&key, 0, sizeof(key)); |
| 127 | memset(&data, 0, sizeof(data)); |
| 128 | |
| 129 | /* Walk through the database and print out the key/data pairs. */ |
| 130 | while ((ret = dbcp->get(dbcp, &key, &data, DB_NEXT)) == 0) |
| 131 | printf("%lu : %.*s\n", |
| 132 | *(u_long *)key.data, (int)data.size, |
| 133 | (char *)data.data); |
| 134 | if (ret != DB_NOTFOUND) |
| 135 | dbp->err(dbp, ret, "DBcursor->get"); |
| 136 | |
| 137 | /* Close the cursor. */ |
| 138 | if ((ret = dbcp->close(dbcp)) != 0) { |
| 139 | dbp->err(dbp, ret, "DBcursor->close"); |
| 140 | return (1); |
| 141 | } |
| 142 | return (0); |
| 143 | }</pre> |
| 144 | </div> |
| 145 | <div class="navfooter"> |
| 146 | <hr /> |
| 147 | <table width="100%" summary="Navigation footer"> |
| 148 | <tr> |
| 149 | <td width="40%" align="left"><a accesskey="p" href="am_conf_select.html">Prev</a> </td> |
| 150 | <td width="20%" align="center"> |
| 151 | <a accesskey="u" href="am_conf.html">Up</a> |
| 152 | </td> |
| 153 | <td width="40%" align="right"> <a accesskey="n" href="general_am_conf.html">Next</a></td> |
| 154 | </tr> |
| 155 | <tr> |
| 156 | <td width="40%" align="left" valign="top">Selecting an access method </td> |
| 157 | <td width="20%" align="center"> |
| 158 | <a accesskey="h" href="index.html">Home</a> |
| 159 | </td> |
| 160 | <td width="40%" align="right" valign="top"> General access method configuration</td> |
| 161 | </tr> |
| 162 | </table> |
| 163 | </div> |
| 164 | </body> |
| 165 | </html> |