| 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>DB->associate()</title> |
| 7 | <link rel="stylesheet" href="apiReference.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 C API Reference" /> |
| 10 | <link rel="up" href="db.html" title="Chapter 2. The DB Handle" /> |
| 11 | <link rel="prev" href="db.html" title="Chapter 2. The DB Handle" /> |
| 12 | <link rel="next" href="dbassociate_foreign.html" title="DB->associate_foreign()" /> |
| 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">DB->associate()</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="db.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 2. |
| 26 | The DB Handle |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="dbassociate_foreign.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="dbassociate"></a>DB->associate()</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <pre class="programlisting">#include <db.h> |
| 42 | |
| 43 | int |
| 44 | DB->associate(DB *primary, DB_TXN *txnid, DB *secondary, |
| 45 | int (*callback)(DB *secondary, |
| 46 | const DBT *key, const DBT *data, DBT *result), u_int32_t flags); </pre> |
| 47 | <p> |
| 48 | The <code class="methodname">DB->associate()</code> function is used to declare one database a |
| 49 | secondary index for a primary database. The |
| 50 | <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a> handle that you call the |
| 51 | <code class="methodname">associate()</code> method from is the primary |
| 52 | database. |
| 53 | </p> |
| 54 | <p> |
| 55 | After a secondary database has been "associated" with a primary |
| 56 | database, all updates to the primary will be automatically |
| 57 | reflected in the secondary and all reads from the secondary will |
| 58 | return corresponding data from the primary. Note that as primary |
| 59 | keys must be unique for secondary indices to work, the primary |
| 60 | database must be configured without support for duplicate data |
| 61 | items. See |
| 62 | <a href="../../programmer_reference/am_second.html" class="olink">Secondary Indices</a> |
| 63 | in the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em> for more information. |
| 64 | </p> |
| 65 | <p> |
| 66 | The <code class="methodname">DB->associate()</code> <span> |
| 67 | <span> |
| 68 | method returns a non-zero error value on failure and 0 on success. |
| 69 | </span> |
| 70 | |
| 71 | </span> |
| 72 | </p> |
| 73 | <div class="sect2" lang="en" xml:lang="en"> |
| 74 | <div class="titlepage"> |
| 75 | <div> |
| 76 | <div> |
| 77 | <h3 class="title"><a id="idp49278832"></a>Parameters</h3> |
| 78 | </div> |
| 79 | </div> |
| 80 | </div> |
| 81 | <div class="sect3" lang="en" xml:lang="en"> |
| 82 | <div class="titlepage"> |
| 83 | <div> |
| 84 | <div> |
| 85 | <h4 class="title"><a id="idp49294248"></a>primary</h4> |
| 86 | </div> |
| 87 | </div> |
| 88 | </div> |
| 89 | <p> |
| 90 | The <span class="bold"><strong>primary</strong></span> parameter should be a |
| 91 | database handle for the primary database that is to be indexed. |
| 92 | </p> |
| 93 | </div> |
| 94 | <div class="sect3" lang="en" xml:lang="en"> |
| 95 | <div class="titlepage"> |
| 96 | <div> |
| 97 | <div> |
| 98 | <h4 class="title"><a id="idp49284296"></a>txnid</h4> |
| 99 | </div> |
| 100 | </div> |
| 101 | </div> |
| 102 | <p> |
| 103 | If the operation is part of an application-specified transaction, the |
| 104 | <span class="bold"><strong>txnid</strong></span> parameter is a transaction |
| 105 | handle returned from <a class="xref" href="txnbegin.html" title="DB_ENV->txn_begin()">DB_ENV->txn_begin()</a>; |
| 106 | if the operation is part of a Berkeley DB Concurrent Data Store group, the |
| 107 | <span class="bold"><strong>txnid</strong></span> parameter is a handle returned |
| 108 | from <a class="xref" href="envcdsgroup_begin.html" title="DB_ENV->cdsgroup_begin()">DB_ENV->cdsgroup_begin()</a>; |
| 109 | otherwise NULL. If no transaction handle is specified, but the |
| 110 | operation occurs in a transactional database, the operation will be |
| 111 | implicitly transaction protected. |
| 112 | </p> |
| 113 | </div> |
| 114 | <div class="sect3" lang="en" xml:lang="en"> |
| 115 | <div class="titlepage"> |
| 116 | <div> |
| 117 | <div> |
| 118 | <h4 class="title"><a id="idp49285856"></a>secondary</h4> |
| 119 | </div> |
| 120 | </div> |
| 121 | </div> |
| 122 | <p> |
| 123 | The <span class="bold"><strong>secondary</strong></span> parameter should be an |
| 124 | open database handle of either a newly created and empty database that |
| 125 | is to be used to store a secondary index, or of a database that was |
| 126 | previously associated with the same primary and contains a secondary |
| 127 | index. Note that it is not safe to associate as a secondary database |
| 128 | a handle that is in use by another thread of control or has open |
| 129 | cursors. If the handle was opened with the |
| 130 | <a class="link" href="envopen.html#envopen_DB_THREAD">DB_THREAD</a> flag it is |
| 131 | safe to use it in multiple threads of control after the |
| 132 | <code class="methodname">DB->associate()</code> method has returned. Note also that either secondary |
| 133 | keys must be unique or the secondary database must be configured with |
| 134 | support for duplicate data items. |
| 135 | </p> |
| 136 | </div> |
| 137 | <div class="sect3" lang="en" xml:lang="en"> |
| 138 | <div class="titlepage"> |
| 139 | <div> |
| 140 | <div> |
| 141 | <h4 class="title"><a id="idp49289920"></a>callback</h4> |
| 142 | </div> |
| 143 | </div> |
| 144 | </div> |
| 145 | <p> |
| 146 | The <span class="bold"><strong>callback</strong></span> parameter is a callback |
| 147 | function that creates the set of secondary keys corresponding to a |
| 148 | given primary key and data pair. |
| 149 | </p> |
| 150 | <p> |
| 151 | The callback parameter may be NULL if both the primary and secondary |
| 152 | database handles were opened with the |
| 153 | <a class="link" href="dbopen.html#dbopen_DB_RDONLY">DB_RDONLY</a> flag. |
| 154 | </p> |
| 155 | <p> |
| 156 | The callback takes four arguments: |
| 157 | </p> |
| 158 | <div class="itemizedlist"> |
| 159 | <ul type="disc"> |
| 160 | <li> |
| 161 | <p> |
| 162 | <code class="literal">secondary</code> |
| 163 | </p> |
| 164 | <p> |
| 165 | The <span class="bold"><strong>secondary</strong></span> parameter is the |
| 166 | database handle for the secondary. |
| 167 | </p> |
| 168 | </li> |
| 169 | <li> |
| 170 | <p> |
| 171 | <code class="literal">key</code> |
| 172 | </p> |
| 173 | <p> |
| 174 | The <span class="bold"><strong>key</strong></span> parameter is a |
| 175 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary key. |
| 176 | </p> |
| 177 | </li> |
| 178 | <li> |
| 179 | <p> |
| 180 | <code class="literal">data</code> |
| 181 | </p> |
| 182 | <p> |
| 183 | The <span class="bold"><strong>data</strong></span> parameter is a |
| 184 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> referencing the primary data |
| 185 | item. |
| 186 | </p> |
| 187 | </li> |
| 188 | <li> |
| 189 | <p> |
| 190 | <code class="literal">result</code> |
| 191 | </p> |
| 192 | <p> |
| 193 | The <span class="bold"><strong>result</strong></span> parameter is a zeroed |
| 194 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> in which the callback |
| 195 | function should fill in <span class="bold"><strong>data</strong></span> and |
| 196 | <span class="bold"><strong>size</strong></span> fields that describe the |
| 197 | secondary key or keys. |
| 198 | </p> |
| 199 | </li> |
| 200 | </ul> |
| 201 | </div> |
| 202 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 203 | <h3 class="title">Note</h3> |
| 204 | <p> |
| 205 | Berkeley DB is not re-entrant. Callback functions should not |
| 206 | attempt to make library calls (for example, to release locks or |
| 207 | close open handles). Re-entering Berkeley DB is not guaranteed |
| 208 | to work correctly, and the results are undefined. |
| 209 | </p> |
| 210 | </div> |
| 211 | <p> |
| 212 | The result <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> can |
| 213 | have the following flags set in its |
| 214 | <span class="bold"><strong>flags</strong></span> field: |
| 215 | </p> |
| 216 | <div class="itemizedlist"> |
| 217 | <ul type="disc"> |
| 218 | <li> |
| 219 | <p><a id="associate_DB_DBT_APPMALLOC"></a> |
| 220 | <code class="literal">DB_DBT_APPMALLOC</code> |
| 221 | </p> |
| 222 | <p> |
| 223 | If the callback function needs to allocate memory for the <span class="bold"><strong>result</strong></span> data field (rather than simply pointing |
| 224 | into the primary key or datum), DB_DBT_APPMALLOC should be set in the |
| 225 | <span class="bold"><strong>flags</strong></span> field of the |
| 226 | <span class="bold"><strong>result</strong></span> |
| 227 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>, which indicates that |
| 228 | Berkeley DB should free the memory when it is done with it. |
| 229 | </p> |
| 230 | </li> |
| 231 | <li> |
| 232 | <p><a id="associate_DB_DBT_MULTIPLE"></a> |
| 233 | <code class="literal">DB_DBT_MULTIPLE</code> |
| 234 | </p> |
| 235 | <p> |
| 236 | To return multiple secondary keys, DB_DBT_MULTIPLE should be set in |
| 237 | the <span class="bold"><strong>flags</strong></span> field of the <span class="bold"><strong>result</strong></span> <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>, |
| 238 | which indicates Berkeley DB should treat the |
| 239 | <span class="bold"><strong>size</strong></span> field as the |
| 240 | number of secondary keys (zero or more), and the <span class="bold"><strong>data</strong></span> field as a pointer to an array of that |
| 241 | number of <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>s describing |
| 242 | the set of secondary keys. |
| 243 | </p> |
| 244 | <p> |
| 245 | <span class="bold"><strong>When multiple secondary keys are returned, keys |
| 246 | may not be repeated</strong></span>. In other words, there must be no |
| 247 | repeated record numbers in the array for Recno and Queue databases, |
| 248 | and keys must not compare equally using the secondary database's |
| 249 | comparison function for Btree and Hash databases. If keys are |
| 250 | repeated, operations may fail and the secondary may become |
| 251 | inconsistent with the primary. |
| 252 | </p> |
| 253 | <p> |
| 254 | The DB_DBT_APPMALLOC flag may be set for any |
| 255 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> in the array of returned |
| 256 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>'s to indicate that |
| 257 | Berkeley DB should free the memory referenced by that particular |
| 258 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>'s data field when it |
| 259 | is done with it. |
| 260 | </p> |
| 261 | <p> |
| 262 | The DB_DBT_APPMALLOC flag may be combined with DB_DBT_MULTIPLE in the |
| 263 | <span class="bold"><strong>result</strong></span> |
| 264 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a>'s <span class="bold"><strong>flag</strong></span> field to indicate that Berkeley DB should |
| 265 | free the array once it is done with all of the returned keys. |
| 266 | </p> |
| 267 | </li> |
| 268 | </ul> |
| 269 | </div> |
| 270 | <p> |
| 271 | In addition, the callback can optionally return the following special value: |
| 272 | </p> |
| 273 | <div class="itemizedlist"> |
| 274 | <ul type="disc"> |
| 275 | <li> |
| 276 | <p><a id="associate_DB_DONOTINDEX"></a> |
| 277 | <code class="literal">DB_DONOTINDEX</code> |
| 278 | </p> |
| 279 | <p> |
| 280 | If any key/data pair in the primary yields a null secondary key and |
| 281 | should be left out of the secondary index, the callback function may |
| 282 | optionally return DB_DONOTINDEX. Otherwise, the callback function |
| 283 | should return 0 in case of success or an error outside of the Berkeley |
| 284 | DB name space in case of failure; the error code will be returned from |
| 285 | the Berkeley DB call that initiated the callback. |
| 286 | </p> |
| 287 | <p> |
| 288 | If the callback function returns DB_DONOTINDEX for any key/data pairs |
| 289 | in the primary database, the secondary index will not contain any |
| 290 | reference to those key/data pairs, and such operations as cursor |
| 291 | iterations and range queries will reflect only the corresponding |
| 292 | subset of the database. If this is not desirable, the application |
| 293 | should ensure that the callback function is well-defined for all |
| 294 | possible values and never returns DB_DONOTINDEX. |
| 295 | </p> |
| 296 | <p> |
| 297 | Returning DB_DONOTINDEX is equivalent to setting DB_DBT_MULTIPLE on |
| 298 | the <span class="bold"><strong>result</strong></span> |
| 299 | <a class="link" href="dbt.html" title="Chapter 4. The DBT Handle">DBT</a> and setting the <span class="bold"><strong>size</strong></span> field to zero. |
| 300 | </p> |
| 301 | </li> |
| 302 | </ul> |
| 303 | </div> |
| 304 | </div> |
| 305 | <div class="sect3" lang="en" xml:lang="en"> |
| 306 | <div class="titlepage"> |
| 307 | <div> |
| 308 | <div> |
| 309 | <h4 class="title"><a id="idp49301168"></a>flags</h4> |
| 310 | </div> |
| 311 | </div> |
| 312 | </div> |
| 313 | <p> |
| 314 | The <span class="bold"><strong>flags</strong></span> parameter must be set to 0 |
| 315 | or by bitwise inclusively <span class="bold"><strong>OR</strong></span>'ing |
| 316 | together one or more of the following values: |
| 317 | </p> |
| 318 | <div class="itemizedlist"> |
| 319 | <ul type="disc"> |
| 320 | <li> |
| 321 | <p><a id="associate_DB_CREATE"></a> |
| 322 | <code class="literal">DB_CREATE</code> |
| 323 | </p> |
| 324 | <p> |
| 325 | If the secondary database is empty, walk through the primary and |
| 326 | create an index to it in the empty secondary. This operation is |
| 327 | potentially very expensive. |
| 328 | </p> |
| 329 | <p> |
| 330 | If the secondary database has been opened in an environment |
| 331 | configured with transactions, the entire secondary index |
| 332 | creation is performed in the context of a single transaction. |
| 333 | </p> |
| 334 | <p> |
| 335 | Care should be taken not to use a newly-populated secondary database |
| 336 | in another thread of control until the <code class="methodname">DB->associate()</code> call has |
| 337 | returned successfully in the first thread. |
| 338 | </p> |
| 339 | <p> |
| 340 | If transactions are not being used, care should be taken not to modify |
| 341 | a primary database being used to populate a secondary database, in |
| 342 | another thread of control, until the <code class="methodname">DB->associate()</code> call has |
| 343 | returned successfully in the first thread. If transactions are being |
| 344 | used, Berkeley DB will perform appropriate locking and the application |
| 345 | need not do any special operation ordering. |
| 346 | </p> |
| 347 | </li> |
| 348 | <li> |
| 349 | <p><a id="associate_DB_IMMUTABLE_KEY"></a> |
| 350 | <code class="literal">DB_IMMUTABLE_KEY</code> |
| 351 | </p> |
| 352 | <p> |
| 353 | Specifies the secondary key is immutable. |
| 354 | </p> |
| 355 | <p> |
| 356 | This flag can be used to optimize updates when the secondary key in a |
| 357 | primary record will never be changed after the primary record is |
| 358 | inserted. For immutable secondary keys, a best effort is made to |
| 359 | avoid calling the secondary callback function when primary records are |
| 360 | updated. This optimization may reduce the overhead of update |
| 361 | operations significantly if the callback function is expensive. |
| 362 | </p> |
| 363 | <p> |
| 364 | Be sure to specify this flag only if the secondary key in the primary |
| 365 | record is never changed. If this rule is violated, the secondary |
| 366 | index will become corrupted, that is, it will become out of sync with |
| 367 | the primary. |
| 368 | </p> |
| 369 | </li> |
| 370 | </ul> |
| 371 | </div> |
| 372 | </div> |
| 373 | </div> |
| 374 | <div class="sect2" lang="en" xml:lang="en"> |
| 375 | <div class="titlepage"> |
| 376 | <div> |
| 377 | <div> |
| 378 | <h3 class="title"><a id="idp49348360"></a>Errors</h3> |
| 379 | </div> |
| 380 | </div> |
| 381 | </div> |
| 382 | <p> |
| 383 | The <code class="methodname">DB->associate()</code> <span> |
| 384 | <span> |
| 385 | method may fail and return one of the following non-zero errors: |
| 386 | </span> |
| 387 | |
| 388 | </span> |
| 389 | </p> |
| 390 | <div class="sect3" lang="en" xml:lang="en"> |
| 391 | <div class="titlepage"> |
| 392 | <div> |
| 393 | <div> |
| 394 | <h4 class="title"><a id="idp49313056"></a> DB_REP_HANDLE_DEAD</h4> |
| 395 | </div> |
| 396 | </div> |
| 397 | </div> |
| 398 | <p> |
| 399 | When a client synchronizes with the master, it is possible for committed |
| 400 | transactions to be rolled back. This invalidates all the database and cursor |
| 401 | handles opened in the replication environment. Once this occurs, an attempt to use |
| 402 | such a handle will |
| 403 | |
| 404 | return <code class="literal">DB_REP_HANDLE_DEAD</code>. |
| 405 | The application will need to discard the handle and open a new one in order to |
| 406 | continue processing. |
| 407 | </p> |
| 408 | </div> |
| 409 | <div class="sect3" lang="en" xml:lang="en"> |
| 410 | <div class="titlepage"> |
| 411 | <div> |
| 412 | <div> |
| 413 | <h4 class="title"><a id="idp49302160"></a>DB_REP_LOCKOUT</h4> |
| 414 | </div> |
| 415 | </div> |
| 416 | </div> |
| 417 | <p> |
| 418 | The operation was blocked by client/master synchronization. |
| 419 | </p> |
| 420 | </div> |
| 421 | <div class="sect3" lang="en" xml:lang="en"> |
| 422 | <div class="titlepage"> |
| 423 | <div> |
| 424 | <div> |
| 425 | <h4 class="title"><a id="idp49321256"></a>EINVAL</h4> |
| 426 | </div> |
| 427 | </div> |
| 428 | </div> |
| 429 | <p> |
| 430 | If the secondary database handle has already been associated with this |
| 431 | or another database handle; the secondary database handle is not open; |
| 432 | the primary database has been configured to allow duplicates; or if an |
| 433 | invalid flag value or parameter was specified. |
| 434 | </p> |
| 435 | </div> |
| 436 | </div> |
| 437 | <div class="sect2" lang="en" xml:lang="en"> |
| 438 | <div class="titlepage"> |
| 439 | <div> |
| 440 | <div> |
| 441 | <h3 class="title"><a id="idp49283880"></a>Class</h3> |
| 442 | </div> |
| 443 | </div> |
| 444 | </div> |
| 445 | <p> |
| 446 | <a class="link" href="db.html" title="Chapter 2. The DB Handle">DB</a> |
| 447 | </p> |
| 448 | </div> |
| 449 | <div class="sect2" lang="en" xml:lang="en"> |
| 450 | <div class="titlepage"> |
| 451 | <div> |
| 452 | <div> |
| 453 | <h3 class="title"><a id="idm8031240"></a>See Also</h3> |
| 454 | </div> |
| 455 | </div> |
| 456 | </div> |
| 457 | <p> |
| 458 | <a class="xref" href="db.html#dblist" title="Database and Related Methods">Database and Related Methods</a> |
| 459 | </p> |
| 460 | </div> |
| 461 | </div> |
| 462 | <div class="navfooter"> |
| 463 | <hr /> |
| 464 | <table width="100%" summary="Navigation footer"> |
| 465 | <tr> |
| 466 | <td width="40%" align="left"><a accesskey="p" href="db.html">Prev</a> </td> |
| 467 | <td width="20%" align="center"> |
| 468 | <a accesskey="u" href="db.html">Up</a> |
| 469 | </td> |
| 470 | <td width="40%" align="right"> <a accesskey="n" href="dbassociate_foreign.html">Next</a></td> |
| 471 | </tr> |
| 472 | <tr> |
| 473 | <td width="40%" align="left" valign="top">Chapter 2. |
| 474 | The DB Handle |
| 475 | </td> |
| 476 | <td width="20%" align="center"> |
| 477 | <a accesskey="h" href="index.html">Home</a> |
| 478 | </td> |
| 479 | <td width="40%" align="right" valign="top"> DB->associate_foreign()</td> |
| 480 | </tr> |
| 481 | </table> |
| 482 | </div> |
| 483 | </body> |
| 484 | </html> |