| 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>Retrieving and updating records in bulk</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_misc.html" title="Chapter 4. Access Method Wrapup" /> |
| 11 | <link rel="prev" href="am_misc.html" title="Chapter 4. Access Method Wrapup" /> |
| 12 | <link rel="next" href="am_misc_partial.html" title="Partial record storage and retrieval" /> |
| 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">Retrieving and updating records in bulk</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="am_misc.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 4. |
| 26 | Access Method Wrapup |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="am_misc_partial.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_misc_bulk"></a>Retrieving and updating records in bulk</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="am_misc_bulk.html#am_misc_bulk_get">Bulk retrieval</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | <dt> |
| 49 | <span class="sect2"> |
| 50 | <a href="am_misc_bulk.html#am_misc_bulk_put">Bulk updates</a> |
| 51 | </span> |
| 52 | </dt> |
| 53 | <dt> |
| 54 | <span class="sect2"> |
| 55 | <a href="am_misc_bulk.html#am_misc_bulk_del">Bulk deletes</a> |
| 56 | </span> |
| 57 | </dt> |
| 58 | </dl> |
| 59 | </div> |
| 60 | <p>When retrieving or modifying large numbers of records, the number |
| 61 | of method calls can often dominate performance. Berkeley DB offers bulk get, |
| 62 | put and delete interfaces which can significantly increase performance for some |
| 63 | applications. |
| 64 | </p> |
| 65 | <div class="sect2" lang="en" xml:lang="en"> |
| 66 | <div class="titlepage"> |
| 67 | <div> |
| 68 | <div> |
| 69 | <h3 class="title"><a id="am_misc_bulk_get"></a>Bulk retrieval</h3> |
| 70 | </div> |
| 71 | </div> |
| 72 | </div> |
| 73 | <p> |
| 74 | To retrieve records in bulk, an application buffer must be specified to |
| 75 | the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> methods. This is done in the C API by setting |
| 76 | the <span class="bold"><strong>data</strong></span> and <span class="bold"><strong>ulen</strong></span> fields of the <span class="bold"><strong>data</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> to reference an application |
| 77 | buffer, and the |
| 78 | <span class="bold"><strong>flags</strong></span> field of that structure to |
| 79 | <a href="../api_reference/C/dbt.html#dbt_DB_DBT_USERMEM" class="olink">DB_DBT_USERMEM</a>. In the Berkeley DB C++ and Java APIs, the actions |
| 80 | are similar, although there are API-specific methods to set the <a href="../api_reference/C/dbt.html" class="olink">DBT</a> |
| 81 | values. Then, the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> or <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flags are |
| 82 | specified to the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> methods, which cause multiple |
| 83 | records to be returned in the specified buffer. |
| 84 | </p> |
| 85 | <p> |
| 86 | The difference between <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> and <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> is as |
| 87 | follows: <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> returns multiple data items for a single key. |
| 88 | For example, the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> flag would be used to retrieve all of |
| 89 | the duplicate data items for a single key in a single call. The |
| 90 | <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flag is used to retrieve multiple key/data pairs, |
| 91 | where each returned key may or may not have duplicate data items. |
| 92 | </p> |
| 93 | <p> |
| 94 | Once the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> method has returned, the application will |
| 95 | walk through the buffer handling the returned records. This is |
| 96 | implemented for the C and C++ APIs using four macros: |
| 97 | <a href="../api_reference/C/DB_MULTIPLE_INIT.html" class="olink">DB_MULTIPLE_INIT</a>, <a href="../api_reference/C/DB_MULTIPLE_NEXT.html" class="olink">DB_MULTIPLE_NEXT</a>, <a href="../api_reference/C/DB_MULTIPLE_KEY_NEXT.html" class="olink">DB_MULTIPLE_KEY_NEXT</a>, and |
| 98 | <a href="../api_reference/C/DB_MULTIPLE_RECNO_NEXT.html" class="olink">DB_MULTIPLE_RECNO_NEXT</a>. For the Java API, this is implemented as |
| 99 | three iterator classes: |
| 100 | <a class="ulink" href="../java/com/sleepycat/db/MultipleDataEntry.html" target="_top">MultipleDataEntry</a>, |
| 101 | <a class="ulink" href="../java/com/sleepycat/db/MultipleKeyDataEntry.html" target="_top">MultipleKeyDataEntry</a>, |
| 102 | and <a class="ulink" href="../java/com/sleepycat/db/MultipleRecnoDataEntry.html" target="_top">MultipleRecnoDataEntry</a>. |
| 103 | </p> |
| 104 | <p> |
| 105 | The <a href="../api_reference/C/DB_MULTIPLE_INIT.html" class="olink">DB_MULTIPLE_INIT</a> macro is always called first. It initializes a |
| 106 | local application variable and the <span class="bold"><strong>data</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> for stepping through the set of |
| 107 | returned records. Then, the application calls one of the remaining |
| 108 | three macros: <a href="../api_reference/C/DB_MULTIPLE_NEXT.html" class="olink">DB_MULTIPLE_NEXT</a>, <a href="../api_reference/C/DB_MULTIPLE_KEY_NEXT.html" class="olink">DB_MULTIPLE_KEY_NEXT</a>, and |
| 109 | <a href="../api_reference/C/DB_MULTIPLE_RECNO_NEXT.html" class="olink">DB_MULTIPLE_RECNO_NEXT</a>. |
| 110 | </p> |
| 111 | <p> |
| 112 | If the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> flag was specified to the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> |
| 113 | method, the application will always call the <a href="../api_reference/C/DB_MULTIPLE_NEXT.html" class="olink">DB_MULTIPLE_NEXT</a> macro. |
| 114 | If the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flag was specified to the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> |
| 115 | method, and the underlying database is a Btree or Hash database, the |
| 116 | application will always call the <a href="../api_reference/C/DB_MULTIPLE_KEY_NEXT.html" class="olink">DB_MULTIPLE_KEY_NEXT</a> macro. If the |
| 117 | <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flag was specified to the <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a> or <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> method, |
| 118 | and the underlying database is a Queue or Recno database, the |
| 119 | application will always call the <a href="../api_reference/C/DB_MULTIPLE_RECNO_NEXT.html" class="olink">DB_MULTIPLE_RECNO_NEXT</a> macro. The |
| 120 | <a href="../api_reference/C/DB_MULTIPLE_NEXT.html" class="olink">DB_MULTIPLE_NEXT</a>, <a href="../api_reference/C/DB_MULTIPLE_KEY_NEXT.html" class="olink">DB_MULTIPLE_KEY_NEXT</a>, and |
| 121 | <a href="../api_reference/C/DB_MULTIPLE_RECNO_NEXT.html" class="olink">DB_MULTIPLE_RECNO_NEXT</a> macros are called repeatedly, until the end of |
| 122 | the returned records is reached. The end of the returned records is |
| 123 | detected by the application's local pointer variable being set to NULL. |
| 124 | </p> |
| 125 | <p> |
| 126 | Note that if you want to use a cursor for bulk retrieval of records in |
| 127 | a Btree database, you should open the cursor using the |
| 128 | <code class="literal">DB_CURSOR_BULK</code> flag. This optimizes the cursor for |
| 129 | bulk retrieval. |
| 130 | </p> |
| 131 | <p> |
| 132 | The following is an example of a routine that displays the contents of |
| 133 | a Btree database using the bulk return interfaces. |
| 134 | </p> |
| 135 | <a id="prog_am22"></a> |
| 136 | <pre class="programlisting">int |
| 137 | rec_display(DB *dbp) |
| 138 | { |
| 139 | DBC *dbcp; |
| 140 | DBT key, data; |
| 141 | size_t retklen, retdlen; |
| 142 | void *retkey, *retdata; |
| 143 | int ret, t_ret; |
| 144 | void *p; |
| 145 | |
| 146 | memset(&key, 0, sizeof(key)); |
| 147 | memset(&data, 0, sizeof(data)); |
| 148 | |
| 149 | /* Review the database in 5MB chunks. */ |
| 150 | #define BUFFER_LENGTH (5 * 1024 * 1024) |
| 151 | if ((data.data = malloc(BUFFER_LENGTH)) == NULL) |
| 152 | return (errno); |
| 153 | data.ulen = BUFFER_LENGTH; |
| 154 | data.flags = DB_DBT_USERMEM; |
| 155 | |
| 156 | /* Acquire a cursor for the database. */ |
| 157 | if ((ret = dbp->cursor(dbp, NULL, &dbcp, DB_CURSOR_BULK)) |
| 158 | != 0) { |
| 159 | dbp->err(dbp, ret, "DB->cursor"); |
| 160 | free(data.data); |
| 161 | return (ret); |
| 162 | } |
| 163 | |
| 164 | for (;;) { |
| 165 | /* |
| 166 | * Acquire the next set of key/data pairs. This code |
| 167 | * does not handle single key/data pairs that won't fit |
| 168 | * in a BUFFER_LENGTH size buffer, instead returning |
| 169 | * DB_BUFFER_SMALL to our caller. |
| 170 | */ |
| 171 | if ((ret = dbcp->get(dbcp, |
| 172 | &key, &data, DB_MULTIPLE_KEY | DB_NEXT)) != 0) { |
| 173 | if (ret != DB_NOTFOUND) |
| 174 | dbp->err(dbp, ret, "DBcursor->get"); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | for (DB_MULTIPLE_INIT(p, &data);;) { |
| 179 | DB_MULTIPLE_KEY_NEXT(p, |
| 180 | &data, retkey, retklen, retdata, retdlen); |
| 181 | if (p == NULL) |
| 182 | break; |
| 183 | printf("key: %.*s, data: %.*s\n", |
| 184 | (int)retklen, (char *)retkey, (int)retdlen, |
| 185 | (char *)retdata); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if ((t_ret = dbcp->close(dbcp)) != 0) { |
| 190 | dbp->err(dbp, ret, "DBcursor->close"); |
| 191 | if (ret == 0) |
| 192 | ret = t_ret; |
| 193 | } |
| 194 | |
| 195 | free(data.data); |
| 196 | |
| 197 | return (ret); |
| 198 | }</pre> |
| 199 | </div> |
| 200 | <div class="sect2" lang="en" xml:lang="en"> |
| 201 | <div class="titlepage"> |
| 202 | <div> |
| 203 | <div> |
| 204 | <h3 class="title"><a id="am_misc_bulk_put"></a>Bulk updates</h3> |
| 205 | </div> |
| 206 | </div> |
| 207 | </div> |
| 208 | <p>To put records in bulk with the btree or hash access methods, construct bulk buffers in the <span class="bold"><strong>key</strong></span> and <span class="bold"><strong>data</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_WRITE_INIT.html" class="olink">DB_MULTIPLE_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_WRITE_NEXT.html" class="olink">DB_MULTIPLE_WRITE_NEXT</a>. To put records in bulk with the recno or queue access methods, construct bulk buffers in the <span class="bold"><strong>data</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> as before, but construct the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_INIT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_NEXT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_NEXT</a> with a data size of zero;. In both cases, set the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> flag to <a href="../api_reference/C/dbput.html" class="olink">DB->put()</a>.</p> |
| 209 | <p>Alternatively, for btree and hash access methods, construct a single bulk buffer in the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_WRITE_INIT.html" class="olink">DB_MULTIPLE_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_KEY_WRITE_NEXT.html" class="olink">DB_MULTIPLE_KEY_WRITE_NEXT</a>. For recno and queue access methods, construct a bulk buffer in the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_INIT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_NEXT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_NEXT</a>. In both cases, set the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flag to <a href="../api_reference/C/dbput.html" class="olink">DB->put()</a>. |
| 210 | |
| 211 | </p> |
| 212 | <p>A successful bulk operation is logically equivalent to a loop through each key/data pair, performing a <a href="../api_reference/C/dbput.html" class="olink">DB->put()</a> for each one.</p> |
| 213 | <p> |
| 214 | </p> |
| 215 | </div> |
| 216 | <div class="sect2" lang="en" xml:lang="en"> |
| 217 | <div class="titlepage"> |
| 218 | <div> |
| 219 | <div> |
| 220 | <h3 class="title"><a id="am_misc_bulk_del"></a>Bulk deletes</h3> |
| 221 | </div> |
| 222 | </div> |
| 223 | </div> |
| 224 | <p>To delete all records with a specified set of keys with the btree or hash access methods, construct a bulk buffer in the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_WRITE_INIT.html" class="olink">DB_MULTIPLE_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_WRITE_NEXT.html" class="olink">DB_MULTIPLE_WRITE_NEXT</a>. To delete a set of records with the recno or queue access methods, construct the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_INIT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_NEXT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_NEXT</a> with a data size of zero. In both cases, set the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE" class="olink">DB_MULTIPLE</a> flag to <a href="../api_reference/C/dbdel.html" class="olink">DB->del()</a>. This is equivalent to calling <a href="../api_reference/C/dbdel.html" class="olink">DB->del()</a> for each key in the bulk buffer. In particular, if the database supports duplicates, all records with the matching key are deleted.</p> |
| 225 | <p>Alternatively, to delete a specific set of key/data pairs, which may be items within a set of duplicates, there are also two cases depending on whether the access method uses record numbers for keys. For btree and hash access methods, construct a single bulk buffer in the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_WRITE_INIT.html" class="olink">DB_MULTIPLE_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_KEY_WRITE_NEXT.html" class="olink">DB_MULTIPLE_KEY_WRITE_NEXT</a>. For recno and queue access methods, construct a bulk buffer in the <span class="bold"><strong>key</strong></span> <a href="../api_reference/C/dbt.html" class="olink">DBT</a> using <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_INIT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_INIT</a> and <a href="../api_reference/C/DB_MULTIPLE_RECNO_WRITE_NEXT.html" class="olink">DB_MULTIPLE_RECNO_WRITE_NEXT</a>. In both cases, set the <a href="../api_reference/C/dbcget.html#dbcget_DB_MULTIPLE_KEY" class="olink">DB_MULTIPLE_KEY</a> flag to <a href="../api_reference/C/dbdel.html" class="olink">DB->del()</a>.</p> |
| 226 | <p>A successful bulk operation is logically equivalent to a loop through each key/data pair, performing a <a href="../api_reference/C/dbdel.html" class="olink">DB->del()</a> for each one.</p> |
| 227 | </div> |
| 228 | </div> |
| 229 | <div class="navfooter"> |
| 230 | <hr /> |
| 231 | <table width="100%" summary="Navigation footer"> |
| 232 | <tr> |
| 233 | <td width="40%" align="left"><a accesskey="p" href="am_misc.html">Prev</a> </td> |
| 234 | <td width="20%" align="center"> |
| 235 | <a accesskey="u" href="am_misc.html">Up</a> |
| 236 | </td> |
| 237 | <td width="40%" align="right"> <a accesskey="n" href="am_misc_partial.html">Next</a></td> |
| 238 | </tr> |
| 239 | <tr> |
| 240 | <td width="40%" align="left" valign="top">Chapter 4. |
| 241 | Access Method Wrapup |
| 242 | </td> |
| 243 | <td width="20%" align="center"> |
| 244 | <a accesskey="h" href="index.html">Home</a> |
| 245 | </td> |
| 246 | <td width="40%" align="right" valign="top"> Partial record storage and retrieval</td> |
| 247 | </tr> |
| 248 | </table> |
| 249 | </div> |
| 250 | </body> |
| 251 | </html> |