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