Debian Codegraph @

docs/programmer_reference/am_cursor.html

raw symbols SCIP
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>Cursor operations</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.html" title="Chapter 3.  Access Method Operations" />
11 <link rel="prev" href="am_foreign.html" title="Foreign key indexes" />
12 <link rel="next" href="am_misc.html" title="Chapter 4.  Access Method Wrapup" />
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">Cursor operations</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="am_foreign.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 3. 
26 Access Method Operations
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="am_misc.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_cursor"></a>Cursor operations</h2>
38 </div>
39 </div>
40 </div>
41 <div class="toc">
42 <dl>
43 <dt>
44 <span class="sect2">
45 <a href="am_cursor.html#am_curget">Retrieving records with a cursor</a>
46 </span>
47 </dt>
48 <dt>
49 <span class="sect2">
50 <a href="am_cursor.html#am_curput">Storing records with a cursor</a>
51 </span>
52 </dt>
53 <dt>
54 <span class="sect2">
55 <a href="am_cursor.html#am_curdel">Deleting records with a cursor</a>
56 </span>
57 </dt>
58 <dt>
59 <span class="sect2">
60 <a href="am_cursor.html#am_curdup">Duplicating a cursor</a>
61 </span>
62 </dt>
63 <dt>
64 <span class="sect2">
65 <a href="am_cursor.html#am_join">Equality Join</a>
66 </span>
67 </dt>
68 <dt>
69 <span class="sect2">
70 <a href="am_cursor.html#am_count">Data item count</a>
71 </span>
72 </dt>
73 <dt>
74 <span class="sect2">
75 <a href="am_cursor.html#am_curclose">Cursor close</a>
76 </span>
77 </dt>
78 </dl>
79 </div>
80 <p>
81 A database cursor refers to a single key/data pair in the database.
82 It supports traversal of the database and is the only way to access
83 individual duplicate data items. Cursors are used for operating on
84 collections of records, for iterating over a database, and for
85 saving handles to individual records, so that they can be modified
86 after they have been read.
87 </p>
88 <p>
89 The <a href="../api_reference/C/dbcursor.html" class="olink">DB-&gt;cursor()</a> method opens a cursor into a database. Upon return
90 the cursor is uninitialized, cursor positioning occurs as part of
91 the first cursor operation.
92 </p>
93 <p>
94 Once a database cursor has been opened, records may be retrieved
95 (<a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a>), stored (<a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a>), and deleted (<a href="../api_reference/C/dbcdel.html" class="olink">DBC-&gt;del()</a>).
96 </p>
97 <p>
98 Additional operations supported by the cursor handle include
99 duplication (<a href="../api_reference/C/dbcdup.html" class="olink">DBC-&gt;dup()</a>), equality join (<a href="../api_reference/C/dbjoin.html" class="olink">DB-&gt;join()</a>), and a count of
100 duplicate data items (<a href="../api_reference/C/dbccount.html" class="olink">DBC-&gt;count()</a>). Cursors are eventually closed
101 using <a href="../api_reference/C/dbcclose.html" class="olink">DBC-&gt;close()</a>.
102 </p>
103 <p>
104 For more information on the operations supported by the cursor
105 handle, see the
106 <a href="../api_reference/C/dbc.html#dbclist" class="olink">Database Cursors and Related Methods</a>
107 section in the <em class="citetitle">Berkeley DB C API Reference Guide.</em>
108 </p>
109 <div class="sect2" lang="en" xml:lang="en">
110 <div class="titlepage">
111 <div>
112 <div>
113 <h3 class="title"><a id="am_curget"></a>Retrieving records with a cursor</h3>
114 </div>
115 </div>
116 </div>
117 <p>
118 The <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> method retrieves records from the database using a
119 cursor. The <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> method takes a flag which controls how the
120 cursor is positioned within the database and returns the key/data
121 item associated with that positioning. Similar to <a href="../api_reference/C/dbget.html" class="olink">DB-&gt;get()</a>,
122 <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a> may also take a supplied key and retrieve the data
123 associated with that key from the database. There are several
124 flags that you can set to customize retrieval.
125 </p>
126 <div class="sect3" lang="en" xml:lang="en">
127 <div class="titlepage">
128 <div>
129 <div>
130 <h4 class="title"><a id="idp51071280"></a>Cursor position flags</h4>
131 </div>
132 </div>
133 </div>
134 <div class="variablelist">
135 <dl>
136 <dt>
137 <span class="term"><a href="../api_reference/C/dbcget.html#dbcget_DB_FIRST" class="olink">DB_FIRST</a>, <a href="../api_reference/C/dbcget.html#dbcget_DB_LAST" class="olink">DB_LAST</a></span>
138 </dt>
139 <dd>
140 Return the first (last) record in the database.
141 </dd>
142 <dt>
143 <span class="term"><a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT" class="olink">DB_NEXT</a>, <a href="../api_reference/C/dbcget.html#dbcget_DB_PREV" class="olink">DB_PREV</a></span>
144 </dt>
145 <dd>
146 Return the next (previous) record in the database.
147 </dd>
148 <dt>
149 <span class="term">
150 <a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT_DUP" class="olink">DB_NEXT_DUP</a>
151 </span>
152 </dt>
153 <dd>
154 Return the next record in the database, if it is a
155 duplicate data item for the current key. For Heap
156 databases, this flag always results in the cursor
157 returning the <code class="literal">DB_NOTFOUND</code>
158 error.
159 </dd>
160 <dt>
161 <span class="term"><a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT_NODUP" class="olink">DB_NEXT_NODUP</a>, <a href="../api_reference/C/dbcget.html#dbcget_DB_PREV_NODUP" class="olink">DB_PREV_NODUP</a></span>
162 </dt>
163 <dd>
164 Return the next (previous) record in the database that
165 is not a duplicate data item for the current
166 key.
167 </dd>
168 <dt>
169 <span class="term">
170 <a href="../api_reference/C/dbcget.html#dbcget_DB_CURRENT" class="olink">DB_CURRENT</a>
171 </span>
172 </dt>
173 <dd>
174 Return the record from the database to which the cursor
175 currently refers.
176 </dd>
177 </dl>
178 </div>
179 </div>
180 <div class="sect3" lang="en" xml:lang="en">
181 <div class="titlepage">
182 <div>
183 <div>
184 <h4 class="title"><a id="idp51079560"></a>Retrieving specific key/data pairs</h4>
185 </div>
186 </div>
187 </div>
188 <div class="variablelist">
189 <dl>
190 <dt>
191 <span class="term">
192 <a href="../api_reference/C/dbcget.html#dbcget_DB_SET" class="olink">DB_SET</a>
193 </span>
194 </dt>
195 <dd>
196 Return the record from the database that matches the
197 supplied key. In the case of duplicates the first
198 duplicate is returned and the cursor is positioned at
199 the beginning of the duplicate list. The user can then
200 traverse the duplicate entries for the
201 key.
202 </dd>
203 <dt>
204 <span class="term">
205 <a href="../api_reference/C/dbcget.html#dbcget_DB_SET_RANGE" class="olink">DB_SET_RANGE</a>
206 </span>
207 </dt>
208 <dd>
209 Return the smallest record in the database greater than
210 or equal to the supplied key. This functionality
211 permits partial key matches and range searches in the
212 Btree access method.
213 </dd>
214 <dt>
215 <span class="term">
216 <a href="../api_reference/C/dbcget.html#dbcget_DB_GET_BOTH" class="olink">DB_GET_BOTH</a>
217 </span>
218 </dt>
219 <dd>
220 Return the record from the database that matches both
221 the supplied key and data items. This is particularly
222 useful when there are large numbers of duplicate
223 records for a key, as it allows the cursor to easily be
224 positioned at the correct place for traversal of some
225 part of a large set of duplicate records.
226 </dd>
227 <dt>
228 <span class="term">
229 <a href="../api_reference/C/dbcget.html#dbcget_DB_GET_BOTH_RANGE" class="olink">DB_GET_BOTH_RANGE</a>
230 </span>
231 </dt>
232 <dd>
233 If used on a database configured for sorted duplicates, this
234 returns the smallest record in the database greater than or equal
235 to the supplied key and data items. If used on a database that is
236 <span class="emphasis"><em>not</em></span> configured for sorted duplicates, this
237 flag behaves identically to <code class="literal">DB_GET_BOTH</code>.
238 </dd>
239 </dl>
240 </div>
241 </div>
242 <div class="sect3" lang="en" xml:lang="en">
243 <div class="titlepage">
244 <div>
245 <div>
246 <h4 class="title"><a id="idp51066936"></a>Retrieving based on record numbers</h4>
247 </div>
248 </div>
249 </div>
250 <div class="variablelist">
251 <dl>
252 <dt>
253 <span class="term">
254 <a href="../api_reference/C/dbcget.html#dbcget_DB_SET_RECNO" class="olink">DB_SET_RECNO</a>
255 </span>
256 </dt>
257 <dd>
258 If the underlying database is a Btree, and was
259 configured so that it is possible to search it by
260 logical record number, retrieve a specific record based
261 on a record number argument.
262 </dd>
263 <dt>
264 <span class="term">
265 <a href="../api_reference/C/dbcget.html#dbcget_DB_GET_RECNO" class="olink">DB_GET_RECNO</a>
266 </span>
267 </dt>
268 <dd>
269 If the underlying database is a Btree, and was
270 configured so that it is possible to search it by
271 logical record number, return the record number for the
272 record to which the cursor refers.
273 </dd>
274 </dl>
275 </div>
276 </div>
277 <div class="sect3" lang="en" xml:lang="en">
278 <div class="titlepage">
279 <div>
280 <div>
281 <h4 class="title"><a id="idp51078104"></a>Special-purpose flags</h4>
282 </div>
283 </div>
284 </div>
285 <div class="variablelist">
286 <dl>
287 <dt>
288 <span class="term">
289 <a href="../api_reference/C/dbget.html#dbget_DB_CONSUME" class="olink">DB_CONSUME</a>
290 </span>
291 </dt>
292 <dd>
293 Read-and-delete: the first record (the head) of the
294 queue is returned and deleted. The underlying database
295 must be a Queue.
296 </dd>
297 <dt>
298 <span class="term">
299 <a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a>
300 </span>
301 </dt>
302 <dd>
303 Read-modify-write: acquire write locks instead of read
304 locks during retrieval. This can enhance performance in
305 threaded applications by reducing the chance of
306 deadlock.
307 </dd>
308 </dl>
309 </div>
310 <p>
311 In all cases, the cursor is repositioned by a <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a>
312 operation to point to the newly-returned key/data pair in the
313 database.
314 </p>
315 <p>
316 The following is a code example showing a cursor walking
317 through a database and displaying the records it contains to
318 the standard output:
319 </p>
320 <a id="prog_am19"></a>
321 <pre class="programlisting">int
322 display(char *database)
323
324 {
325 DB *dbp;
326 DBC *dbcp;
327 DBT key, data;
328 int close_db, close_dbc, ret;
329
330 close_db = close_dbc = 0;
331
332 /* Open the database. */
333 if ((ret = db_create(&amp;dbp, NULL, 0)) != 0) {
334 fprintf(stderr,
335 "%s: db_create: %s\n", progname, db_strerror(ret));
336 return (1);
337 }
338 close_db = 1;
339
340 /* Turn on additional error output. */
341 dbp-&gt;set_errfile(dbp, stderr);
342 dbp-&gt;set_errpfx(dbp, progname);
343
344 /* Open the database. */
345 if ((ret = dbp-&gt;open(dbp, NULL, database, NULL,
346 DB_UNKNOWN, DB_RDONLY, 0)) != 0) {
347 dbp-&gt;err(dbp, ret, "%s: DB-&gt;open", database);
348 goto err;
349 }
350
351 /* Acquire a cursor for the database. */
352 if ((ret = dbp-&gt;cursor(dbp, NULL, &amp;dbcp, 0)) != 0) {
353 dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
354 goto err;
355 }
356 close_dbc = 1;
357
358 /* Initialize the key/data return pair. */
359 memset(&amp;key, 0, sizeof(key));
360 memset(&amp;data, 0, sizeof(data));
361
362 /* Walk through the database and print out the key/data pairs. */
363 while ((ret = dbcp-&gt;get(dbcp, &amp;key, &amp;data, DB_NEXT)) == 0)
364 printf("%.*s : %.*s\n",
365 (int)key.size, (char *)key.data,
366 (int)data.size, (char *)data.data);
367 if (ret != DB_NOTFOUND) {
368 dbp-&gt;err(dbp, ret, "DBcursor-&gt;get");
369 goto err;
370 }
371
372 err: if (close_dbc &amp;&amp; (ret = dbcp-&gt;close(dbcp)) != 0)
373 dbp-&gt;err(dbp, ret, "DBcursor-&gt;close");
374 if (close_db &amp;&amp; (ret = dbp-&gt;close(dbp, 0)) != 0)
375 fprintf(stderr,
376 "%s: DB-&gt;close: %s\n", progname, db_strerror(ret));
377 return (0);
378 }</pre>
379 </div>
380 </div>
381 <div class="sect2" lang="en" xml:lang="en">
382 <div class="titlepage">
383 <div>
384 <div>
385 <h3 class="title"><a id="am_curput"></a>Storing records with a cursor</h3>
386 </div>
387 </div>
388 </div>
389 <p>
390 The <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> method stores records into the database using a
391 cursor. In general, <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> takes a key and inserts the
392 associated data into the database, at a location controlled by a
393 specified flag.
394 </p>
395 <p>
396 There are several flags that you can set to customize storage:
397 </p>
398 <div class="variablelist">
399 <dl>
400 <dt>
401 <span class="term">
402 <a href="../api_reference/C/dbcput.html#dbcput_DB_AFTER" class="olink">DB_AFTER</a>
403 </span>
404 </dt>
405 <dd>
406 Create a new record, immediately after the record to which
407 the cursor refers.
408 </dd>
409 <dt>
410 <span class="term">
411 <a href="../api_reference/C/dbcput.html#dbcput_DB_BEFORE" class="olink">DB_BEFORE</a>
412 </span>
413 </dt>
414 <dd>
415 Create a new record, immediately before the record to
416 which the cursor refers.
417 </dd>
418 <dt>
419 <span class="term">
420 <a href="../api_reference/C/dbcget.html#dbcget_DB_CURRENT" class="olink">DB_CURRENT</a>
421 </span>
422 </dt>
423 <dd>
424 Replace the data part of the record to which the cursor
425 refers.
426 </dd>
427 <dt>
428 <span class="term">
429 <a href="../api_reference/C/dbcput.html#dbcput_DB_KEYFIRST" class="olink">DB_KEYFIRST</a>
430 </span>
431 </dt>
432 <dd>
433 Create a new record as the first of the duplicate records
434 for the supplied key.
435 </dd>
436 <dt>
437 <span class="term">
438 <a href="../api_reference/C/dbcput.html#dbcput_DB_KEYLAST" class="olink">DB_KEYLAST</a>
439 </span>
440 </dt>
441 <dd>
442 Create a new record, as the last of the duplicate records
443 for the supplied key.
444 </dd>
445 </dl>
446 </div>
447 <p>
448 In all cases, the cursor is repositioned by a <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> operation to
449 point to the newly inserted key/data pair in the database.
450 </p>
451 <p>
452 The following is a code example showing a cursor storing two data
453 items in a database that supports duplicate data items:
454 </p>
455 <a id="prog_am20"></a>
456 <pre class="programlisting">int
457 store(DB *dbp)
458
459 {
460 DBC *dbcp;
461 DBT key, data;
462 int ret;
463
464 /*
465 * The DB handle for a Btree database supporting duplicate data
466 * items is the argument; acquire a cursor for the database.
467 */
468 if ((ret = dbp-&gt;cursor(dbp, NULL, &amp;dbcp, 0)) != 0) {
469 dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
470 goto err;
471 }
472
473 /* Initialize the key. */
474 memset(&amp;key, 0, sizeof(key));
475 key.data = "new key";
476 key.size = strlen(key.data) + 1;
477
478 /* Initialize the data to be the first of two duplicate records. */
479 memset(&amp;data, 0, sizeof(data));
480 data.data = "new key's data: entry #1";
481 data.size = strlen(data.data) + 1;
482
483 /* Store the first of the two duplicate records. */
484 if ((ret = dbcp-&gt;put(dbcp, &amp;key, &amp;data, DB_KEYFIRST)) != 0)
485 dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
486
487 /* Initialize the data to be the second of two duplicate records. */
488 data.data = "new key's data: entry #2";
489 data.size = strlen(data.data) + 1;
490
491 /*
492 * Store the second of the two duplicate records. No duplicate
493 * record sort function has been specified, so we explicitly
494 * store the record as the last of the duplicate set.
495 */
496 if ((ret = dbcp-&gt;put(dbcp, &amp;key, &amp;data, DB_KEYLAST)) != 0)
497 dbp-&gt;err(dbp, ret, "DB-&gt;cursor");
498
499 err: if ((ret = dbcp-&gt;close(dbcp)) != 0)
500 dbp-&gt;err(dbp, ret, "DBcursor-&gt;close");
501
502 return (0);
503 }</pre>
504 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
505 <h3 class="title">Note</h3>
506 <p>
507 If you are using the Heap access method and you are creating a
508 new record in the database, then the key that you provide to
509 the <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> method should be empty. The <a href="../api_reference/C/dbcput.html" class="olink">DBC-&gt;put()</a> method will
510 return the record's ID (RID) in the key. The RID is
511 automatically created for you when Heap database records are
512 created.
513 </p>
514 </div>
515 </div>
516 <div class="sect2" lang="en" xml:lang="en">
517 <div class="titlepage">
518 <div>
519 <div>
520 <h3 class="title"><a id="am_curdel"></a>Deleting records with a cursor</h3>
521 </div>
522 </div>
523 </div>
524 <p>The <a href="../api_reference/C/dbcdel.html" class="olink">DBC-&gt;del()</a> method deletes records from the database using a cursor.
525 The <a href="../api_reference/C/dbcdel.html" class="olink">DBC-&gt;del()</a> method deletes the record to which the cursor currently
526 refers. In all cases, the cursor position is unchanged after a
527 delete.</p>
528 </div>
529 <div class="sect2" lang="en" xml:lang="en">
530 <div class="titlepage">
531 <div>
532 <div>
533 <h3 class="title"><a id="am_curdup"></a>Duplicating a cursor</h3>
534 </div>
535 </div>
536 </div>
537 <p>Once a cursor has been initialized (for example, by a call to
538 <a href="../api_reference/C/dbcget.html" class="olink">DBC-&gt;get()</a>), it can be thought of as identifying a particular
539 location in a database. The <a href="../api_reference/C/dbcdup.html" class="olink">DBC-&gt;dup()</a> method permits an application to
540 create a new cursor that has the same locking and transactional
541 information as the cursor from which it is copied, and which optionally
542 refers to the same position in the database.</p>
543 <p>In order to maintain a cursor position when an application is using
544 locking, locks are maintained on behalf of the cursor until the cursor is
545 closed. In cases when an application is using locking without
546 transactions, cursor duplication is often required to avoid
547 self-deadlocks. For further details, refer to
548 <a class="xref" href="lock_am_conv.html" title="Berkeley DB Transactional Data Store locking conventions">Berkeley DB Transactional Data Store locking conventions</a>.</p>
549 </div>
550 <div class="sect2" lang="en" xml:lang="en">
551 <div class="titlepage">
552 <div>
553 <div>
554 <h3 class="title"><a id="am_join"></a>Equality Join</h3>
555 </div>
556 </div>
557 </div>
558 <p>Berkeley DB supports "equality" (also known as "natural"), joins on secondary
559 indices. An equality join is a method of retrieving data from a primary
560 database using criteria stored in a set of secondary indices. It
561 requires the data be organized as a primary database which contains the
562 primary key and primary data field, and a set of secondary indices.
563 Each of the secondary indices is indexed by a different secondary key,
564 and, for each key in a secondary index, there is a set of duplicate data
565 items that match the primary keys in the primary database.</p>
566 <p>For example, let's assume the need for an application that will return
567 the names of stores in which one can buy fruit of a given color. We
568 would first construct a primary database that lists types of fruit as
569 the key item, and the store where you can buy them as the data item:</p>
570 <div class="informaltable">
571 <table border="1" width="80%">
572 <colgroup>
573 <col />
574 <col />
575 </colgroup>
576 <thead>
577 <tr>
578 <th>Primary key:</th>
579 <th>Primary data:</th>
580 </tr>
581 </thead>
582 <tbody>
583 <tr>
584 <td align="left">apple</td>
585 <td align="left">Convenience Store</td>
586 </tr>
587 <tr>
588 <td align="left">blueberry</td>
589 <td align="left">Farmer's Market</td>
590 </tr>
591 <tr>
592 <td align="left">peach</td>
593 <td align="left">Shopway</td>
594 </tr>
595 <tr>
596 <td align="left">pear</td>
597 <td align="left">Farmer's Market</td>
598 </tr>
599 <tr>
600 <td align="left">raspberry</td>
601 <td align="left">Shopway</td>
602 </tr>
603 <tr>
604 <td align="left">strawberry</td>
605 <td align="left">Farmer's Market</td>
606 </tr>
607 </tbody>
608 </table>
609 </div>
610 <p>We would then create a secondary index with the key <span class="bold"><strong>color</strong></span>, and,
611 as the data items, the names of fruits of different colors.</p>
612 <div class="informaltable">
613 <table border="1" width="80%">
614 <colgroup>
615 <col />
616 <col />
617 </colgroup>
618 <thead>
619 <tr>
620 <th>Secondary key:</th>
621 <th>Secondary data:</th>
622 </tr>
623 </thead>
624 <tbody>
625 <tr>
626 <td align="left">blue</td>
627 <td align="left">blueberry</td>
628 </tr>
629 <tr>
630 <td align="left">red</td>
631 <td align="left">apple</td>
632 </tr>
633 <tr>
634 <td align="left">red</td>
635 <td align="left">raspberry</td>
636 </tr>
637 <tr>
638 <td align="left">red</td>
639 <td align="left">strawberry</td>
640 </tr>
641 <tr>
642 <td align="left">yellow</td>
643 <td align="left">peach</td>
644 </tr>
645 <tr>
646 <td align="left">yellow</td>
647 <td align="left">pear</td>
648 </tr>
649 </tbody>
650 </table>
651 </div>
652 <p>This secondary index would allow an application to look up a color, and
653 then use the data items to look up the stores where the colored fruit
654 could be purchased. For example, by first looking up <span class="bold"><strong>blue</strong></span>,
655 the data item <span class="bold"><strong>blueberry</strong></span> could be used as the lookup key in the
656 primary database, returning <span class="bold"><strong>Farmer's Market</strong></span>.</p>
657 <p>Your data must be organized in the following manner in order to use the
658 <a href="../api_reference/C/dbjoin.html" class="olink">DB-&gt;join()</a> method:</p>
659 <div class="orderedlist">
660 <ol type="1">
661 <li>The actual data should be stored in the database represented by the
662 <a href="../api_reference/C/db.html" class="olink">DB</a> object used to invoke this method. Generally, this
663 <a href="../api_reference/C/db.html" class="olink">DB</a> object is called the <span class="emphasis"><em>primary</em></span>.</li>
664 <li>Secondary indices should be stored in separate databases, whose keys
665 are the values of the secondary indices and whose data items are the
666 primary keys corresponding to the records having the designated
667 secondary key value. It is acceptable (and expected) that there may be
668 duplicate entries in the secondary indices.
669 <p>These duplicate entries should be sorted for performance reasons, although
670 it is not required. For more information see the <a href="../api_reference/C/dbset_flags.html#dbset_flags_DB_DUPSORT" class="olink">DB_DUPSORT</a> flag
671 to the <a href="../api_reference/C/dbset_flags.html" class="olink">DB-&gt;set_flags()</a> method.</p></li>
672 </ol>
673 </div>
674 <p>What the <a href="../api_reference/C/dbjoin.html" class="olink">DB-&gt;join()</a> method does is review a list of secondary keys, and,
675 when it finds a data item that appears as a data item for all of the
676 secondary keys, it uses that data item as a lookup into the primary
677 database, and returns the associated data item.</p>
678 <p>If there were another secondary index that had as its key the <span class="bold"><strong>cost</strong></span>
679 of the fruit, a similar lookup could be done on stores where inexpensive
680 fruit could be purchased:</p>
681 <div class="informaltable">
682 <table border="1" width="80%">
683 <colgroup>
684 <col />
685 <col />
686 </colgroup>
687 <thead>
688 <tr>
689 <th>Secondary key:</th>
690 <th>Secondary data:</th>
691 </tr>
692 </thead>
693 <tbody>
694 <tr>
695 <td align="left">expensive</td>
696 <td align="left">blueberry</td>
697 </tr>
698 <tr>
699 <td align="left">expensive</td>
700 <td align="left">peach</td>
701 </tr>
702 <tr>
703 <td align="left">expensive</td>
704 <td align="left">pear</td>
705 </tr>
706 <tr>
707 <td align="left">expensive</td>
708 <td align="left">strawberry</td>
709 </tr>
710 <tr>
711 <td align="left">inexpensive</td>
712 <td align="left">apple</td>
713 </tr>
714 <tr>
715 <td align="left">inexpensive</td>
716 <td align="left">pear</td>
717 </tr>
718 <tr>
719 <td align="left">inexpensive</td>
720 <td align="left">raspberry</td>
721 </tr>
722 </tbody>
723 </table>
724 </div>
725 <p>The <a href="../api_reference/C/dbjoin.html" class="olink">DB-&gt;join()</a> method provides equality join functionality. While not
726 strictly cursor functionality, in that it is not a method off a cursor
727 handle, it is more closely related to the cursor operations than to the
728 standard <a href="../api_reference/C/db.html" class="olink">DB</a> operations.</p>
729 <p>It is also possible to do lookups based on multiple criteria in a single
730 operation. For example, it is possible to look up fruits that are both
731 red and expensive in a single operation. If the same fruit appeared as
732 a data item in both the color and expense indices, then that fruit name
733 would be used as the key for retrieval from the primary index, and would
734 then return the store where expensive, red fruit could be purchased.</p>
735 <div class="sect3" lang="en" xml:lang="en">
736 <div class="titlepage">
737 <div>
738 <div>
739 <h4 class="title"><a id="idp51124888"></a>Example</h4>
740 </div>
741 </div>
742 </div>
743 <p>Consider the following three databases:</p>
744 <div class="variablelist">
745 <dl>
746 <dt>
747 <span class="term">personnel</span>
748 </dt>
749 <dd>
750 <div class="itemizedlist">
751 <ul type="disc">
752 <li>key = SSN</li>
753 <li>data = record containing name, address, phone number, job title</li>
754 </ul>
755 </div>
756 </dd>
757 <dt>
758 <span class="term">lastname</span>
759 </dt>
760 <dd>
761 <div class="itemizedlist">
762 <ul type="disc">
763 <li>key = lastname</li>
764 <li>data = SSN</li>
765 </ul>
766 </div>
767 </dd>
768 <dt>
769 <span class="term">jobs</span>
770 </dt>
771 <dd>
772 <div class="itemizedlist">
773 <ul type="disc">
774 <li>key = job title</li>
775 <li>data = SSN</li>
776 </ul>
777 </div>
778 </dd>
779 </dl>
780 </div>
781 <p>Consider the following query:</p>
782 <pre class="programlisting">Return the personnel records of all people named smith with the job
783 title manager.</pre>
784 <p>This query finds are all the records in the primary database (personnel)
785 for whom the criteria <span class="bold"><strong>lastname=smith and job title=manager</strong></span> is
786 true.</p>
787 <p>Assume that all databases have been properly opened and have the
788 handles: pers_db, name_db, job_db. We also assume that we have an
789 active transaction to which the handle txn refers.</p>
790 <a id="prog_am21"></a>
791 <pre class="programlisting">DBC *name_curs, *job_curs, *join_curs;
792 DBC *carray[3];
793 DBT key, data;
794 int ret, tret;
795
796 name_curs = NULL;
797 job_curs = NULL;
798 memset(&amp;key, 0, sizeof(key));
799 memset(&amp;data, 0, sizeof(data));
800
801 if ((ret =
802 name_db-&gt;cursor(name_db, txn, &amp;name_curs, 0)) != 0)
803 goto err;
804 key.data = "smith";
805 key.size = sizeof("smith");
806 if ((ret =
807 name_curs-&gt;get(name_curs, &amp;key, &amp;data, DB_SET)) != 0)
808 goto err;
809
810 if ((ret = job_db-&gt;cursor(job_db, txn, &amp;job_curs, 0)) != 0)
811 goto err;
812 key.data = "manager";
813 key.size = sizeof("manager");
814 if ((ret =
815 job_curs-&gt;get(job_curs, &amp;key, &amp;data, DB_SET)) != 0)
816 goto err;
817
818 carray[0] = name_curs;
819 carray[1] = job_curs;
820 carray[2] = NULL;
821
822 if ((ret =
823 pers_db-&gt;join(pers_db, carray, &amp;join_curs, 0)) != 0)
824 goto err;
825 while ((ret =
826 join_curs-&gt;get(join_curs, &amp;key, &amp;data, 0)) == 0) {
827 /* Process record returned in key/data. */
828 }
829
830 /*
831 * If we exited the loop because we ran out of records,
832 * then it has completed successfully.
833 */
834 if (ret == DB_NOTFOUND)
835 ret = 0;
836
837 err:
838 if (join_curs != NULL &amp;&amp;
839 (tret = join_curs-&gt;close(join_curs)) != 0 &amp;&amp; ret == 0)
840 ret = tret;
841 if (name_curs != NULL &amp;&amp;
842 (tret = name_curs-&gt;close(name_curs)) != 0 &amp;&amp; ret == 0)
843 ret = tret;
844 if (job_curs != NULL &amp;&amp;
845 (tret = job_curs-&gt;close(job_curs)) != 0 &amp;&amp; ret == 0)
846 ret = tret;
847
848 return (ret);</pre>
849 <p>The name cursor is positioned at the beginning of the duplicate list
850 for <span class="bold"><strong>smith</strong></span> and the job cursor is placed at the beginning of
851 the duplicate list for <span class="bold"><strong>manager</strong></span>. The join cursor is returned
852 from the join method. This code then loops over the join cursor getting
853 the personnel records of each one until there are no more.</p>
854 </div>
855 </div>
856 <div class="sect2" lang="en" xml:lang="en">
857 <div class="titlepage">
858 <div>
859 <div>
860 <h3 class="title"><a id="am_count"></a>Data item count</h3>
861 </div>
862 </div>
863 </div>
864 <p>Once a cursor has been initialized to refer to a particular key in the
865 database, it can be used to determine the number of data items that are
866 stored for any particular key. The <a href="../api_reference/C/dbccount.html" class="olink">DBC-&gt;count()</a> method returns
867 this number of data items. The returned value is always one, unless
868 the database supports duplicate data items, in which case it may be any
869 number of items.</p>
870 </div>
871 <div class="sect2" lang="en" xml:lang="en">
872 <div class="titlepage">
873 <div>
874 <div>
875 <h3 class="title"><a id="am_curclose"></a>Cursor close</h3>
876 </div>
877 </div>
878 </div>
879 <p>The <a href="../api_reference/C/dbcclose.html" class="olink">DBC-&gt;close()</a> method closes the <a href="../api_reference/C/dbc.html" class="olink">DBC</a> cursor, after which the
880 cursor may no longer be used. Although cursors are implicitly closed
881 when the database they point to are closed, it is good programming
882 practice to explicitly close cursors. In addition, in transactional
883 systems, cursors may not exist outside of a transaction and so must be
884 explicitly closed.</p>
885 </div>
886 </div>
887 <div class="navfooter">
888 <hr />
889 <table width="100%" summary="Navigation footer">
890 <tr>
891 <td width="40%" align="left"><a accesskey="p" href="am_foreign.html">Prev</a> </td>
892 <td width="20%" align="center">
893 <a accesskey="u" href="am.html">Up</a>
894 </td>
895 <td width="40%" align="right"> <a accesskey="n" href="am_misc.html">Next</a></td>
896 </tr>
897 <tr>
898 <td width="40%" align="left" valign="top">Foreign key indexes </td>
899 <td width="20%" align="center">
900 <a accesskey="h" href="index.html">Home</a>
901 </td>
902 <td width="40%" align="right" valign="top"> Chapter 4. 
903 Access Method Wrapup
904 </td>
905 </tr>
906 </table>
907 </div>
908 </body>
909 </html>