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