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
|
<?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>Using Stored Collections</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 Collections Tutorial" />
<link rel="up" href="collectionOverview.html" title="Appendix A. API Notes and Details" />
<link rel="prev" href="UsingCollectionsAPI.html" title="Using the DB Java Collections API" />
<link rel="next" href="SerializedObjectStorage.html" title="Serialized Object Storage" />
</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">
Using Stored Collections
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="UsingCollectionsAPI.html">Prev</a> </td>
<th width="60%" align="center">Appendix A.
API Notes and Details
</th>
<td width="20%" align="right"> <a accesskey="n" href="SerializedObjectStorage.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="UsingStoredCollections"></a>
Using Stored Collections
</h2>
</div>
</div>
</div>
<div class="toc">
<dl>
<dt>
<span class="sect2">
<a href="UsingStoredCollections.html#StoredCollectionAccessMethods">
Stored Collection and Access Methods
</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="UsingStoredCollections.html#StoredVersusStandardCollections">
Stored Collections Versus Standard Java Collections
</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="UsingStoredCollections.html#StoredCollectionCharacteristics">
Other Stored Collection Characteristics
</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="UsingStoredCollections.html#WhyJavaCollections">
Why Java Collections for Berkeley DB
</a>
</span>
</dt>
</dl>
</div>
<p>
When a stored collection is created it is based on either a
<a class="ulink" href="../../java/com/sleepycat/db/Database.html" target="_top">Database</a>
or a
<span>
<a class="ulink" href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>.
</span>
When a database is used, the primary key of the database is used as
the collection key. When a secondary database is used, the index
key is used as the collection key. Indexed collections can be used
for reading elements and removing elements but not for adding or
updating elements.
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="StoredCollectionAccessMethods"></a>
Stored Collection and Access Methods
</h3>
</div>
</div>
</div>
<p>
The use of stored collections is constrained in certain respects as
described below.
<span>
Most of these restrictions have to do with
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html" target="_top">List</a>
interfaces; for
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html" target="_top">Map</a>
interfaces, most all access modes are fully supported since the
Berkeley DB model is map-like.
</span>
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html" target="_top">SortedMap</a>
interfaces may only be used if keys are ordered. This means ordered keys are required for creating a
<a class="ulink" href="../../java/com/sleepycat/collections/StoredSortedEntrySet.html" target="_top">StoredSortedEntrySet</a>,
<a class="ulink" href="../../java/com/sleepycat/collections/StoredSortedKeySet.html" target="_top">StoredSortedKeySet</a>,
<a class="ulink" href="../../java/com/sleepycat/collections/StoredSortedMap.html" target="_top">StoredSortedMap</a>, or
<a class="ulink" href="../../java/com/sleepycat/collections/StoredSortedValueSet.html" target="_top">StoredSortedValueSet</a>.
</p>
</li>
<li>
<p>
All iterators for stored collections implement the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html" target="_top">ListIterator</a>
interface as well as the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html" target="_top">Iterator</a>
interface.
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#hasPrevious()" target="_top">ListIterator.hasPrevious()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#previous()" target="_top">ListIterator.previous()</a>
work in all cases.
<span>
However, the following
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html" target="_top">ListIterator</a>
method behavior is dependent on the access method.
</span>
</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#nextIndex()" target="_top">ListIterator.nextIndex()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#previousIndex()" target="_top">ListIterator.previousIndex()</a>
only work when record number keys are used, and throw
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>
otherwise.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
inserts before the current position and renumbers following keys if the RECNO-RENUMBER
access method is used.
</p>
</li>
<li>
<p>
For all access methods other than RECNO-RENUMBER:
</p>
<div class="itemizedlist">
<ul type="square">
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
throws
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>
if duplicates are not allowed.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
inserts a duplicate before the current position
if duplicates are unsorted.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
inserts a duplicate in sorted order if
duplicates are sorted.
</p>
</li>
</ul>
</div>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#set()" target="_top">ListIterator.set()</a>
throws
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>
if sorted duplicates are configured, since updating with sorted duplicates would change the
iterator position.
</p>
</li>
</ul>
</div>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html#setValue()" target="_top">Map.Entry.setValue()</a>
throws
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>
if duplicates are sorted.
</p>
</li>
<li>
<p>
Only the access methods that use a record number key may be used
with a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html" target="_top">List</a>
<code class="classname">List</code>
view.
</p>
</li>
<li>
<p>
To create a stored List that supports the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#add()" target="_top">List.add()</a>
<code class="methodname">List.add()</code>
method, only the RECNO-RENUMBER access method may be used.
</p>
</li>
<li>
<p>
For List access methods that do not support
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#add()" target="_top">List.add()</a>
<code class="methodname">List.add()</code>
(RECNO, QUEUE, and BTREE-RECNUM):
</p>
<div class="itemizedlist">
<ul type="circle">
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#add()" target="_top">List.add()</a>
<code class="methodname">List.add()</code>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
<code class="methodname">ListIterator.add()</code>
always throw
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a> .
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#remove()" target="_top">List.remove()</a>
<code class="methodname">List.remove()</code>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#remove()" target="_top">ListIterator.remove()</a>
<code class="methodname">ListIterator.remove()</code>
do not cause list indices to be renumbered. However, iterators will skip
the removed values.
</p>
</li>
</ul>
</div>
<p>
For these access methods, stored Lists are most useful as
read-only collections where indices are not required to be
sequential.
</p>
</li>
<li>
<p>
When duplicates are allowed the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
interfaces are modified in several ways as described in the next
section.
</p>
</li>
</ul>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="StoredVersusStandardCollections"></a>
Stored Collections Versus Standard Java Collections
</h3>
</div>
</div>
</div>
<p>
Stored collections have the following differences with the
standard Java collection interfaces. Some of these are interface
contract violations.
</p>
<p>
The Java collections interface does not support duplicate keys
(multi-maps or multi-sets). When the access method allows duplicate
keys, the collection interfaces are defined as follows.
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#entrySet()" target="_top">Map.entrySet()</a>
may contain multiple
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
objects with the same key.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#keySet()" target="_top">Map.keySet()</a>
always contains unique keys, it does not contain duplicates.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#values()" target="_top">Map.values()</a>
contains all values including the values
associated with duplicate keys.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put()" target="_top">Map.put()</a>
appends a duplicate if the key already exists rather than replacing
the existing value, and always returns null.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#remove()" target="_top">Map.remove()</a>
removes all duplicates for the specified key.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#get()" target="_top">Map.get()</a>
returns the first duplicate for the specified key.
</p>
</li>
<li>
<p>
<a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates()</a>
is an additional method for returning the values for a given key as a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>.
</p>
</li>
</ul>
</div>
<p>
Other differences are:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
Collection.size() and Map.size() always throws
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>.
This is because the number of
records in a database cannot be determined reliably or
cheaply.
</p>
</li>
<li>
<p>
Because the size() method cannot be used, the bulk operation
methods of standard Java collections cannot be passed stored
collections as parameters, since the implementations rely on
size(). However, the bulk operation methods of stored collections
can be passed standard Java collections as parameters.
<code class="literal">storedCollection.addAll(standardCollection)</code> is allowed
while <code class="literal">standardCollection.addAll(storedCollection)</code> is
<span class="emphasis"><em>not</em></span> allowed. This restriction applies to the standard
collection constructors that take a Collection parameter (copy
constructors), the Map.putAll() method, and the following
Collection methods: addAll(), containsAll(), removeAll() and
retainAll().
</p>
</li>
<li>
<p>
The <code class="methodname">ListIterator.nextIndex()</code> method
returns <code class="literal">Integer.MAX_VALUE</code>
for stored lists when positioned at the end of the list, rather
than returning the list size as specified by the ListIterator
interface. Again, this is because the database size is not
available.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Comparator.html" target="_top">Comparator</a>
objects cannot be used and the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html#comparator()" target="_top">SortedMap.comparator()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html#comparator()" target="_top">SortedSet.comparator()</a>
methods always return null. The
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html" target="_top">Comparable</a>
interface is not supported. However, Comparators that operate on
byte arrays may be specified using
<span>
<a class="ulink" href="../../java/com/sleepycat/db/DatabaseConfig.html#setBtreeComparator(java.util.Comparator)" target="_top">DatabaseConfig.setBtreeComparator</a>.
</span>
</p>
</li>
<li>
<p>
The
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals()" target="_top">Object.equals()</a>
method is not used to determine whether a key
or value is contained in a collection, to locate a value by key,
etc. Instead the byte array representation of the keys and values
are used. However, the equals() method <span class="emphasis"><em>is</em></span> called for each
key and value when comparing two collections for equality. It is
the responsibility of the application to make sure that the
equals() method returns true if and only if the byte array
representations of the two objects are equal. Normally this occurs
naturally since the byte array representation is derived from the
object's fields.
</p>
</li>
</ul>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="StoredCollectionCharacteristics"></a>
Other Stored Collection Characteristics
</h3>
</div>
</div>
</div>
<p>
The following characteristics of stored collections are
extensions of the definitions in the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/package-summary.html" target="_top">java.util</a>
package. These differences do not violate the Java
collections interface contract.
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
All stored collections are thread safe (can be used by multiple
threads concurrently)
<span>
whenever the Berkeley DB Concurrent Data Store or
Transactional Data Store environment is used.
</span>
Locking is handled by the Berkeley DB
environment. To access a collection from multiple threads, creation
of synchronized collections using the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html" target="_top">Collections</a>
class is not necessary
<span>
except when using the Data Store environment.
</span>
Iterators, however, should always be used only by a single thread.
</p>
</li>
<li>
<p>
All stored collections may be read-only if desired by passing
false for the writeAllowed parameter of their constructor. Creation
of immutable collections using the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html" target="_top">Collections</a>
class is not necessary.
</p>
</li>
<li>
<p>
A stored collection is partially read-only if a secondary
index is used. Specifically, values may be removed but may not be
added or updated. The following methods will throw
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>
when an index is used:
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#add()" target="_top">Collection.add()</a>,
<span><a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#set()" target="_top">List.set()</a>,</span>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#set()" target="_top">ListIterator.set()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html#setValue()" target="_top">Map.Entry.setValue()</a>.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html#entrySet()" target="_top">SortedMap.entrySet()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html#keySet()" target="_top">SortedMap.keySet()</a>
return a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>,
not just a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Set.html" target="_top">Set</a>
as specified in Java collections interface. This allows using the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>
methods on the returned collection.
</p>
</li>
<li>
<p>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html#values()" target="_top">SortedMap.values()</a>
returns a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>,
not just a
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>,
whenever the keys of the map can be derived from the values using
an entity binding. Note that the sorted set returned is not really
a set if duplicates are allowed, since it is technically a
collection; however, the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>
methods (for example, subSet()), can still be used.
</p>
</li>
<li>
<p>
For
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedSet.html" target="_top">SortedSet</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html" target="_top">SortedMap</a>
views, additional subSet() and subMap() methods are provided that
allow control over whether keys are treated as inclusive or
exclusive values in the key range.
</p>
</li>
<li>
<p>
Keys and values are stored by value, not by reference. This is
because objects that are added to collections are converted to byte
arrays (by bindings) and stored in the database. When they are
retrieved from the collection they are read from the database and
converted from byte arrays to objects. Therefore, the object
reference added to a collection will not be the same as the
reference later retrieved from the collection.
</p>
</li>
<li>
<p>
A runtime exception,
<a class="ulink" href="../../java/com/sleepycat/util/RuntimeExceptionWrapper.html" target="_top">RuntimeExceptionWrapper</a>,
is thrown whenever database exceptions occur which are not runtime
exceptions. The
<a class="ulink" href="../../java/com/sleepycat/util/RuntimeExceptionWrapper.html#getCause()" target="_top">RuntimeExceptionWrapper.getCause()</a>
method can be called to get the underlying exception.
</p>
</li>
<li>
<p>
All iterators for stored collections implement the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html" target="_top">ListIterator</a>
interface as well as the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html" target="_top">Iterator</a>
interface. This is to allow use of the
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#hasPrevious()" target="_top">ListIterator.hasPrevious()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#previous()" target="_top">ListIterator.previous()</a>
methods, which work for all collections
since Berkeley DB provides bidirectional cursors.
</p>
</li>
<li>
<p>
All stored collections have a
<a class="ulink" href="../../java/com/sleepycat/collections/StoredCollection.html#iterator(boolean)" target="_top">StoredCollection.iterator(boolean)</a>
method that allows creating
a read-only iterator for a writable collection. For the standard
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#iterator()" target="_top">Collection.iterator()</a>
method, the iterator is read-only only
when the collection is read-only.
<span>Read-only iterators are important
for using the Berkeley DB Concurrent Data Store environment, since
only one write cursors may be open at one time.</span>
</p>
</li>
<li>
<p>
Iterator stability for stored collections is greater than the
iterator stability defined by the Java collections interfaces.
Stored iterator stability is the same as the cursor stability
defined by Berkeley DB.
</p>
</li>
<li>
<p>
When an entity binding is used, updating (setting) a value is
not allowed if the key in the entity is not equal to the original
key. For example, calling
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put()" target="_top">Map.put()</a>
is not allowed when the key parameter is not equal to the key of
the entity parameter.
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put()" target="_top">Map.put()</a>,
<span><a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#set()" target="_top">List.set()</a>,</span>
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#set()" target="_top">ListIterator.set()</a>,
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html#setValue()" target="_top">Map.Entry.setValue()</a>
will throw
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/IllegalArgumentException.html" target="_top">IllegalArgumentException</a>
in this situation.
</p>
</li>
<li>
<p>
Adding and removing items from stored lists is not allowed for
sublists. This is simply an unimplemented feature and may be
changed in the future. Currently for sublists the following
methods throw
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/UnsupportedOperationException.html" target="_top">UnsupportedOperationException</a>:
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#add()" target="_top">List.add()</a><code class="methodname">List.add()</code>,
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#remove()" target="_top">List.remove()</a><code class="methodname">List.remove()</code>,
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#add()" target="_top">ListIterator.add()</a>
and
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/ListIterator.html#remove()" target="_top">ListIterator.remove()</a><code class="methodname">ListIterator.remove()</code>.
</p>
</li>
<li>
<p>
The
<span>
<a class="ulink" href="../../java/com/sleepycat/collections/StoredList.html#append(java.lang.Object)" target="_top">StoredList.append(java.lang.Object)</a>
and
</span>
<a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#append(java.lang.Object)" target="_top">StoredMap.append(java.lang.Object)</a>
extension method<span>s</span> allows
adding a new record with an automatically assigned key.
<span>
Record number assignment by the database itself is supported
for QUEUE, RECNO and RECNO-RENUMBER databases.
</span>
An application-defined
<a class="ulink" href="../../java/com/sleepycat/collections/PrimaryKeyAssigner.html" target="_top">PrimaryKeyAssigner</a>
is used to assign the key value.
</p>
</li>
</ul>
</div>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="WhyJavaCollections"></a>
Why Java Collections for Berkeley DB
</h3>
</div>
</div>
</div>
<p>
The Java collections interface was chosen as the best Java API
for DB given these requirements:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Provide the Java developer with an API that is as familiar and
easy to use as possible.
</p>
</li>
<li>
<p>
Provide access to all, or a large majority, of the features of
the underlying Berkeley DB storage system.
</p>
</li>
<li>
<p>
Compared to the DB API, provide a higher-level API
that is oriented toward Java developers.
</p>
</li>
<li>
<p>
For ease of use, support object-to-data bindings, per-thread
transactions, and some traditional database features such as
foreign keys.
</p>
</li>
<li>
<p>
Provide a thin layer that can be thoroughly tested and which
does not significantly impact the reliability and performance of
DB.
</p>
</li>
</ol>
</div>
<p>
Admittedly there are several things about the Java Collections
API that don't quite fit with DB or with any transactional
database, and therefore there are some new rules for applying the
Java Collections API. However, these disadvantages are considered
to be smaller than the disadvantages of the alternatives:
</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>
A new API not based on the Java Collections API could have been
designed that maps well to DB but is higher-level.
However, this would require designing an entirely new model. The
exceptions for using the Java Collections API are considered easier
to learn than a whole new model. A new model would also require a
long design stabilization period before being as complete and
understandable as either the Java Collections API or the DB
API.
</p>
</li>
<li>
<p>
The ODMG API or another object persistence API could have been
implemented on top of DB. However, an object persistence
implementation would add much code and require a long stabilization
period. And while it may work well for applications that require
object persistence, it would probably never perform well enough for
many other applications.
</p>
</li>
</ul>
</div>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="UsingCollectionsAPI.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="collectionOverview.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="SerializedObjectStorage.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">
Using the DB Java Collections API
</td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top">
Serialized Object Storage
</td>
</tr>
</table>
</div>
</body>
</html>
|