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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
|
<?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>Cursor operations</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
<link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
<link rel="up" href="am.html" title="Chapter 3. Access Method Operations" />
<link rel="prev" href="am_foreign.html" title="Foreign key indexes" />
<link rel="next" href="am_misc.html" title="Chapter 4. Access Method Wrapup" />
</head>
<body>
<div xmlns="" class="navheader">
<div class="libver">
<p>Library Version 11.2.5.3</p>
</div>
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">Cursor operations</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="am_foreign.html">Prev</a> </td>
<th width="60%" align="center">Chapter 3.
Access Method Operations
</th>
<td width="20%" align="right"> <a accesskey="n" href="am_misc.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="am_cursor"></a>Cursor operations</h2>
</div>
</div>
</div>
<div class="toc">
<dl>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_curget">Retrieving records with a cursor</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_curput">Storing records with a cursor</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_curdel">Deleting records with a cursor</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_curdup">Duplicating a cursor</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_join">Equality Join</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_count">Data item count</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="am_cursor.html#am_curclose">Cursor close</a>
</span>
</dt>
</dl>
</div>
<p>
A database cursor refers to a single key/data pair in the database.
It supports traversal of the database and is the only way to access
individual duplicate data items. Cursors are used for operating on
collections of records, for iterating over a database, and for
saving handles to individual records, so that they can be modified
after they have been read.
</p>
<p>
The <a href="../api_reference/C/dbcursor.html" class="olink">DB->cursor()</a> method opens a cursor into a database. Upon return
the cursor is uninitialized, cursor positioning occurs as part of
the first cursor operation.
</p>
<p>
Once a database cursor has been opened, records may be retrieved
(<a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a>), stored (<a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a>), and deleted (<a href="../api_reference/C/dbcdel.html" class="olink">DBC->del()</a>).
</p>
<p>
Additional operations supported by the cursor handle include
duplication (<a href="../api_reference/C/dbcdup.html" class="olink">DBC->dup()</a>), equality join (<a href="../api_reference/C/dbjoin.html" class="olink">DB->join()</a>), and a count of
duplicate data items (<a href="../api_reference/C/dbccount.html" class="olink">DBC->count()</a>). Cursors are eventually closed
using <a href="../api_reference/C/dbcclose.html" class="olink">DBC->close()</a>.
</p>
<p>
For more information on the operations supported by the cursor
handle, see the
<a href="../api_reference/C/dbc.html#dbclist" class="olink">Database Cursors and Related Methods</a>
section in the <em class="citetitle">Berkeley DB C API Reference Guide.</em>
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_curget"></a>Retrieving records with a cursor</h3>
</div>
</div>
</div>
<p>
The <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> method retrieves records from the database using a
cursor. The <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> method takes a flag which controls how the
cursor is positioned within the database and returns the key/data
item associated with that positioning. Similar to <a href="../api_reference/C/dbget.html" class="olink">DB->get()</a>,
<a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a> may also take a supplied key and retrieve the data
associated with that key from the database. There are several
flags that you can set to customize retrieval.
</p>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp51071280"></a>Cursor position flags</h4>
</div>
</div>
</div>
<div class="variablelist">
<dl>
<dt>
<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>
</dt>
<dd>
Return the first (last) record in the database.
</dd>
<dt>
<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>
</dt>
<dd>
Return the next (previous) record in the database.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT_DUP" class="olink">DB_NEXT_DUP</a>
</span>
</dt>
<dd>
Return the next record in the database, if it is a
duplicate data item for the current key. For Heap
databases, this flag always results in the cursor
returning the <code class="literal">DB_NOTFOUND</code>
error.
</dd>
<dt>
<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>
</dt>
<dd>
Return the next (previous) record in the database that
is not a duplicate data item for the current
key.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_CURRENT" class="olink">DB_CURRENT</a>
</span>
</dt>
<dd>
Return the record from the database to which the cursor
currently refers.
</dd>
</dl>
</div>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp51079560"></a>Retrieving specific key/data pairs</h4>
</div>
</div>
</div>
<div class="variablelist">
<dl>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_SET" class="olink">DB_SET</a>
</span>
</dt>
<dd>
Return the record from the database that matches the
supplied key. In the case of duplicates the first
duplicate is returned and the cursor is positioned at
the beginning of the duplicate list. The user can then
traverse the duplicate entries for the
key.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_SET_RANGE" class="olink">DB_SET_RANGE</a>
</span>
</dt>
<dd>
Return the smallest record in the database greater than
or equal to the supplied key. This functionality
permits partial key matches and range searches in the
Btree access method.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_GET_BOTH" class="olink">DB_GET_BOTH</a>
</span>
</dt>
<dd>
Return the record from the database that matches both
the supplied key and data items. This is particularly
useful when there are large numbers of duplicate
records for a key, as it allows the cursor to easily be
positioned at the correct place for traversal of some
part of a large set of duplicate records.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_GET_BOTH_RANGE" class="olink">DB_GET_BOTH_RANGE</a>
</span>
</dt>
<dd>
If used on a database configured for sorted duplicates, this
returns the smallest record in the database greater than or equal
to the supplied key and data items. If used on a database that is
<span class="emphasis"><em>not</em></span> configured for sorted duplicates, this
flag behaves identically to <code class="literal">DB_GET_BOTH</code>.
</dd>
</dl>
</div>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp51066936"></a>Retrieving based on record numbers</h4>
</div>
</div>
</div>
<div class="variablelist">
<dl>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_SET_RECNO" class="olink">DB_SET_RECNO</a>
</span>
</dt>
<dd>
If the underlying database is a Btree, and was
configured so that it is possible to search it by
logical record number, retrieve a specific record based
on a record number argument.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_GET_RECNO" class="olink">DB_GET_RECNO</a>
</span>
</dt>
<dd>
If the underlying database is a Btree, and was
configured so that it is possible to search it by
logical record number, return the record number for the
record to which the cursor refers.
</dd>
</dl>
</div>
</div>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp51078104"></a>Special-purpose flags</h4>
</div>
</div>
</div>
<div class="variablelist">
<dl>
<dt>
<span class="term">
<a href="../api_reference/C/dbget.html#dbget_DB_CONSUME" class="olink">DB_CONSUME</a>
</span>
</dt>
<dd>
Read-and-delete: the first record (the head) of the
queue is returned and deleted. The underlying database
must be a Queue.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_RMW" class="olink">DB_RMW</a>
</span>
</dt>
<dd>
Read-modify-write: acquire write locks instead of read
locks during retrieval. This can enhance performance in
threaded applications by reducing the chance of
deadlock.
</dd>
</dl>
</div>
<p>
In all cases, the cursor is repositioned by a <a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a>
operation to point to the newly-returned key/data pair in the
database.
</p>
<p>
The following is a code example showing a cursor walking
through a database and displaying the records it contains to
the standard output:
</p>
<a id="prog_am19"></a>
<pre class="programlisting">int
display(char *database)
{
DB *dbp;
DBC *dbcp;
DBT key, data;
int close_db, close_dbc, ret;
close_db = close_dbc = 0;
/* Open the database. */
if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr,
"%s: db_create: %s\n", progname, db_strerror(ret));
return (1);
}
close_db = 1;
/* Turn on additional error output. */
dbp->set_errfile(dbp, stderr);
dbp->set_errpfx(dbp, progname);
/* Open the database. */
if ((ret = dbp->open(dbp, NULL, database, NULL,
DB_UNKNOWN, DB_RDONLY, 0)) != 0) {
dbp->err(dbp, ret, "%s: DB->open", database);
goto err;
}
/* Acquire a cursor for the database. */
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
dbp->err(dbp, ret, "DB->cursor");
goto err;
}
close_dbc = 1;
/* Initialize the key/data return pair. */
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
/* Walk through the database and print out the key/data pairs. */
while ((ret = dbcp->get(dbcp, &key, &data, DB_NEXT)) == 0)
printf("%.*s : %.*s\n",
(int)key.size, (char *)key.data,
(int)data.size, (char *)data.data);
if (ret != DB_NOTFOUND) {
dbp->err(dbp, ret, "DBcursor->get");
goto err;
}
err: if (close_dbc && (ret = dbcp->close(dbcp)) != 0)
dbp->err(dbp, ret, "DBcursor->close");
if (close_db && (ret = dbp->close(dbp, 0)) != 0)
fprintf(stderr,
"%s: DB->close: %s\n", progname, db_strerror(ret));
return (0);
}</pre>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_curput"></a>Storing records with a cursor</h3>
</div>
</div>
</div>
<p>
The <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> method stores records into the database using a
cursor. In general, <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> takes a key and inserts the
associated data into the database, at a location controlled by a
specified flag.
</p>
<p>
There are several flags that you can set to customize storage:
</p>
<div class="variablelist">
<dl>
<dt>
<span class="term">
<a href="../api_reference/C/dbcput.html#dbcput_DB_AFTER" class="olink">DB_AFTER</a>
</span>
</dt>
<dd>
Create a new record, immediately after the record to which
the cursor refers.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcput.html#dbcput_DB_BEFORE" class="olink">DB_BEFORE</a>
</span>
</dt>
<dd>
Create a new record, immediately before the record to
which the cursor refers.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcget.html#dbcget_DB_CURRENT" class="olink">DB_CURRENT</a>
</span>
</dt>
<dd>
Replace the data part of the record to which the cursor
refers.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcput.html#dbcput_DB_KEYFIRST" class="olink">DB_KEYFIRST</a>
</span>
</dt>
<dd>
Create a new record as the first of the duplicate records
for the supplied key.
</dd>
<dt>
<span class="term">
<a href="../api_reference/C/dbcput.html#dbcput_DB_KEYLAST" class="olink">DB_KEYLAST</a>
</span>
</dt>
<dd>
Create a new record, as the last of the duplicate records
for the supplied key.
</dd>
</dl>
</div>
<p>
In all cases, the cursor is repositioned by a <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> operation to
point to the newly inserted key/data pair in the database.
</p>
<p>
The following is a code example showing a cursor storing two data
items in a database that supports duplicate data items:
</p>
<a id="prog_am20"></a>
<pre class="programlisting">int
store(DB *dbp)
{
DBC *dbcp;
DBT key, data;
int ret;
/*
* The DB handle for a Btree database supporting duplicate data
* items is the argument; acquire a cursor for the database.
*/
if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
dbp->err(dbp, ret, "DB->cursor");
goto err;
}
/* Initialize the key. */
memset(&key, 0, sizeof(key));
key.data = "new key";
key.size = strlen(key.data) + 1;
/* Initialize the data to be the first of two duplicate records. */
memset(&data, 0, sizeof(data));
data.data = "new key's data: entry #1";
data.size = strlen(data.data) + 1;
/* Store the first of the two duplicate records. */
if ((ret = dbcp->put(dbcp, &key, &data, DB_KEYFIRST)) != 0)
dbp->err(dbp, ret, "DB->cursor");
/* Initialize the data to be the second of two duplicate records. */
data.data = "new key's data: entry #2";
data.size = strlen(data.data) + 1;
/*
* Store the second of the two duplicate records. No duplicate
* record sort function has been specified, so we explicitly
* store the record as the last of the duplicate set.
*/
if ((ret = dbcp->put(dbcp, &key, &data, DB_KEYLAST)) != 0)
dbp->err(dbp, ret, "DB->cursor");
err: if ((ret = dbcp->close(dbcp)) != 0)
dbp->err(dbp, ret, "DBcursor->close");
return (0);
}</pre>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p>
If you are using the Heap access method and you are creating a
new record in the database, then the key that you provide to
the <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> method should be empty. The <a href="../api_reference/C/dbcput.html" class="olink">DBC->put()</a> method will
return the record's ID (RID) in the key. The RID is
automatically created for you when Heap database records are
created.
</p>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_curdel"></a>Deleting records with a cursor</h3>
</div>
</div>
</div>
<p>The <a href="../api_reference/C/dbcdel.html" class="olink">DBC->del()</a> method deletes records from the database using a cursor.
The <a href="../api_reference/C/dbcdel.html" class="olink">DBC->del()</a> method deletes the record to which the cursor currently
refers. In all cases, the cursor position is unchanged after a
delete.</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_curdup"></a>Duplicating a cursor</h3>
</div>
</div>
</div>
<p>Once a cursor has been initialized (for example, by a call to
<a href="../api_reference/C/dbcget.html" class="olink">DBC->get()</a>), it can be thought of as identifying a particular
location in a database. The <a href="../api_reference/C/dbcdup.html" class="olink">DBC->dup()</a> method permits an application to
create a new cursor that has the same locking and transactional
information as the cursor from which it is copied, and which optionally
refers to the same position in the database.</p>
<p>In order to maintain a cursor position when an application is using
locking, locks are maintained on behalf of the cursor until the cursor is
closed. In cases when an application is using locking without
transactions, cursor duplication is often required to avoid
self-deadlocks. For further details, refer to
<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>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_join"></a>Equality Join</h3>
</div>
</div>
</div>
<p>Berkeley DB supports "equality" (also known as "natural"), joins on secondary
indices. An equality join is a method of retrieving data from a primary
database using criteria stored in a set of secondary indices. It
requires the data be organized as a primary database which contains the
primary key and primary data field, and a set of secondary indices.
Each of the secondary indices is indexed by a different secondary key,
and, for each key in a secondary index, there is a set of duplicate data
items that match the primary keys in the primary database.</p>
<p>For example, let's assume the need for an application that will return
the names of stores in which one can buy fruit of a given color. We
would first construct a primary database that lists types of fruit as
the key item, and the store where you can buy them as the data item:</p>
<div class="informaltable">
<table border="1" width="80%">
<colgroup>
<col />
<col />
</colgroup>
<thead>
<tr>
<th>Primary key:</th>
<th>Primary data:</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">apple</td>
<td align="left">Convenience Store</td>
</tr>
<tr>
<td align="left">blueberry</td>
<td align="left">Farmer's Market</td>
</tr>
<tr>
<td align="left">peach</td>
<td align="left">Shopway</td>
</tr>
<tr>
<td align="left">pear</td>
<td align="left">Farmer's Market</td>
</tr>
<tr>
<td align="left">raspberry</td>
<td align="left">Shopway</td>
</tr>
<tr>
<td align="left">strawberry</td>
<td align="left">Farmer's Market</td>
</tr>
</tbody>
</table>
</div>
<p>We would then create a secondary index with the key <span class="bold"><strong>color</strong></span>, and,
as the data items, the names of fruits of different colors.</p>
<div class="informaltable">
<table border="1" width="80%">
<colgroup>
<col />
<col />
</colgroup>
<thead>
<tr>
<th>Secondary key:</th>
<th>Secondary data:</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">blue</td>
<td align="left">blueberry</td>
</tr>
<tr>
<td align="left">red</td>
<td align="left">apple</td>
</tr>
<tr>
<td align="left">red</td>
<td align="left">raspberry</td>
</tr>
<tr>
<td align="left">red</td>
<td align="left">strawberry</td>
</tr>
<tr>
<td align="left">yellow</td>
<td align="left">peach</td>
</tr>
<tr>
<td align="left">yellow</td>
<td align="left">pear</td>
</tr>
</tbody>
</table>
</div>
<p>This secondary index would allow an application to look up a color, and
then use the data items to look up the stores where the colored fruit
could be purchased. For example, by first looking up <span class="bold"><strong>blue</strong></span>,
the data item <span class="bold"><strong>blueberry</strong></span> could be used as the lookup key in the
primary database, returning <span class="bold"><strong>Farmer's Market</strong></span>.</p>
<p>Your data must be organized in the following manner in order to use the
<a href="../api_reference/C/dbjoin.html" class="olink">DB->join()</a> method:</p>
<div class="orderedlist">
<ol type="1">
<li>The actual data should be stored in the database represented by the
<a href="../api_reference/C/db.html" class="olink">DB</a> object used to invoke this method. Generally, this
<a href="../api_reference/C/db.html" class="olink">DB</a> object is called the <span class="emphasis"><em>primary</em></span>.</li>
<li>Secondary indices should be stored in separate databases, whose keys
are the values of the secondary indices and whose data items are the
primary keys corresponding to the records having the designated
secondary key value. It is acceptable (and expected) that there may be
duplicate entries in the secondary indices.
<p>These duplicate entries should be sorted for performance reasons, although
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
to the <a href="../api_reference/C/dbset_flags.html" class="olink">DB->set_flags()</a> method.</p></li>
</ol>
</div>
<p>What the <a href="../api_reference/C/dbjoin.html" class="olink">DB->join()</a> method does is review a list of secondary keys, and,
when it finds a data item that appears as a data item for all of the
secondary keys, it uses that data item as a lookup into the primary
database, and returns the associated data item.</p>
<p>If there were another secondary index that had as its key the <span class="bold"><strong>cost</strong></span>
of the fruit, a similar lookup could be done on stores where inexpensive
fruit could be purchased:</p>
<div class="informaltable">
<table border="1" width="80%">
<colgroup>
<col />
<col />
</colgroup>
<thead>
<tr>
<th>Secondary key:</th>
<th>Secondary data:</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">expensive</td>
<td align="left">blueberry</td>
</tr>
<tr>
<td align="left">expensive</td>
<td align="left">peach</td>
</tr>
<tr>
<td align="left">expensive</td>
<td align="left">pear</td>
</tr>
<tr>
<td align="left">expensive</td>
<td align="left">strawberry</td>
</tr>
<tr>
<td align="left">inexpensive</td>
<td align="left">apple</td>
</tr>
<tr>
<td align="left">inexpensive</td>
<td align="left">pear</td>
</tr>
<tr>
<td align="left">inexpensive</td>
<td align="left">raspberry</td>
</tr>
</tbody>
</table>
</div>
<p>The <a href="../api_reference/C/dbjoin.html" class="olink">DB->join()</a> method provides equality join functionality. While not
strictly cursor functionality, in that it is not a method off a cursor
handle, it is more closely related to the cursor operations than to the
standard <a href="../api_reference/C/db.html" class="olink">DB</a> operations.</p>
<p>It is also possible to do lookups based on multiple criteria in a single
operation. For example, it is possible to look up fruits that are both
red and expensive in a single operation. If the same fruit appeared as
a data item in both the color and expense indices, then that fruit name
would be used as the key for retrieval from the primary index, and would
then return the store where expensive, red fruit could be purchased.</p>
<div class="sect3" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h4 class="title"><a id="idp51124888"></a>Example</h4>
</div>
</div>
</div>
<p>Consider the following three databases:</p>
<div class="variablelist">
<dl>
<dt>
<span class="term">personnel</span>
</dt>
<dd>
<div class="itemizedlist">
<ul type="disc">
<li>key = SSN</li>
<li>data = record containing name, address, phone number, job title</li>
</ul>
</div>
</dd>
<dt>
<span class="term">lastname</span>
</dt>
<dd>
<div class="itemizedlist">
<ul type="disc">
<li>key = lastname</li>
<li>data = SSN</li>
</ul>
</div>
</dd>
<dt>
<span class="term">jobs</span>
</dt>
<dd>
<div class="itemizedlist">
<ul type="disc">
<li>key = job title</li>
<li>data = SSN</li>
</ul>
</div>
</dd>
</dl>
</div>
<p>Consider the following query:</p>
<pre class="programlisting">Return the personnel records of all people named smith with the job
title manager.</pre>
<p>This query finds are all the records in the primary database (personnel)
for whom the criteria <span class="bold"><strong>lastname=smith and job title=manager</strong></span> is
true.</p>
<p>Assume that all databases have been properly opened and have the
handles: pers_db, name_db, job_db. We also assume that we have an
active transaction to which the handle txn refers.</p>
<a id="prog_am21"></a>
<pre class="programlisting">DBC *name_curs, *job_curs, *join_curs;
DBC *carray[3];
DBT key, data;
int ret, tret;
name_curs = NULL;
job_curs = NULL;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
if ((ret =
name_db->cursor(name_db, txn, &name_curs, 0)) != 0)
goto err;
key.data = "smith";
key.size = sizeof("smith");
if ((ret =
name_curs->get(name_curs, &key, &data, DB_SET)) != 0)
goto err;
if ((ret = job_db->cursor(job_db, txn, &job_curs, 0)) != 0)
goto err;
key.data = "manager";
key.size = sizeof("manager");
if ((ret =
job_curs->get(job_curs, &key, &data, DB_SET)) != 0)
goto err;
carray[0] = name_curs;
carray[1] = job_curs;
carray[2] = NULL;
if ((ret =
pers_db->join(pers_db, carray, &join_curs, 0)) != 0)
goto err;
while ((ret =
join_curs->get(join_curs, &key, &data, 0)) == 0) {
/* Process record returned in key/data. */
}
/*
* If we exited the loop because we ran out of records,
* then it has completed successfully.
*/
if (ret == DB_NOTFOUND)
ret = 0;
err:
if (join_curs != NULL &&
(tret = join_curs->close(join_curs)) != 0 && ret == 0)
ret = tret;
if (name_curs != NULL &&
(tret = name_curs->close(name_curs)) != 0 && ret == 0)
ret = tret;
if (job_curs != NULL &&
(tret = job_curs->close(job_curs)) != 0 && ret == 0)
ret = tret;
return (ret);</pre>
<p>The name cursor is positioned at the beginning of the duplicate list
for <span class="bold"><strong>smith</strong></span> and the job cursor is placed at the beginning of
the duplicate list for <span class="bold"><strong>manager</strong></span>. The join cursor is returned
from the join method. This code then loops over the join cursor getting
the personnel records of each one until there are no more.</p>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_count"></a>Data item count</h3>
</div>
</div>
</div>
<p>Once a cursor has been initialized to refer to a particular key in the
database, it can be used to determine the number of data items that are
stored for any particular key. The <a href="../api_reference/C/dbccount.html" class="olink">DBC->count()</a> method returns
this number of data items. The returned value is always one, unless
the database supports duplicate data items, in which case it may be any
number of items.</p>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="am_curclose"></a>Cursor close</h3>
</div>
</div>
</div>
<p>The <a href="../api_reference/C/dbcclose.html" class="olink">DBC->close()</a> method closes the <a href="../api_reference/C/dbc.html" class="olink">DBC</a> cursor, after which the
cursor may no longer be used. Although cursors are implicitly closed
when the database they point to are closed, it is good programming
practice to explicitly close cursors. In addition, in transactional
systems, cursors may not exist outside of a transaction and so must be
explicitly closed.</p>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="am_foreign.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="am.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="am_misc.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Foreign key indexes </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Chapter 4.
Access Method Wrapup
</td>
</tr>
</table>
</div>
</body>
</html>
|