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">
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Appendix A. API Notes and Details</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 Collections Tutorial" />
10 <link rel="up" href="index.html" title="Berkeley DB Collections Tutorial" />
11 <link rel="prev" href="Summary.html" title="Chapter 7. Summary" />
12 <link rel="next" href="UsingCollectionsAPI.html" title="Using the DB Java Collections API" />
15 <div xmlns="" class="navheader">
17 <p>Library Version 11.2.5.3</p>
19 <table width="100%" summary="Navigation header">
21 <th colspan="3" align="center">Appendix A.
26 <td width="20%" align="left"><a accesskey="p" href="Summary.html">Prev</a> </td>
27 <th width="60%" align="center"> </th>
28 <td width="20%" align="right"> <a accesskey="n" href="UsingCollectionsAPI.html">Next</a></td>
33 <div class="appendix" lang="en" xml:lang="en">
34 <div class="titlepage">
37 <h2 class="title"><a id="collectionOverview"></a>Appendix A.
44 This appendix contains information useful to the collections programmer
45 that is too detailed to easily fit into the format of a tutorial.
46 Specifically, this appendix contains the following information:
48 <div class="itemizedlist">
52 <a class="xref" href="collectionOverview.html#UsingDataBindings" title="Using Data Bindings">
59 <a class="xref" href="UsingCollectionsAPI.html" title="Using the DB Java Collections API">
60 Using the DB Java Collections API
66 <a class="xref" href="UsingStoredCollections.html" title="Using Stored Collections">
67 Using Stored Collections
73 <a class="xref" href="SerializedObjectStorage.html" title="Serialized Object Storage">
74 Serialized Object Storage
80 <div class="sect1" lang="en" xml:lang="en">
81 <div class="titlepage">
84 <h2 class="title" style="clear: both"><a id="UsingDataBindings"></a>
94 <a href="collectionOverview.html#SelectingBindingFormats">
95 Selecting Binding Formats
101 <a href="collectionOverview.html#RecordNumberBindings">Record Number Bindings</a>
106 <a href="collectionOverview.html#SelectingDataBindings">
107 Selecting Data Bindings
113 <a href="collectionOverview.html#ImplementingBindings">
114 Implementing Bindings
120 <a href="collectionOverview.html#UsingBindings">
127 <a href="collectionOverview.html#SecondaryKeyCreators">
128 Secondary Key Creators
135 Data bindings determine how keys and values are represented as
136 stored data (byte arrays) in the database, and how stored data is
137 converted to and from Java objects.
140 The selection of data bindings is, in general, independent of
142 <span> access methods and </span>
143 collection views. In other
144 words, any binding can be used with any
145 <span> access method or </span>
148 One exception to this rule is described under
149 <a class="xref" href="collectionOverview.html#RecordNumberBindings" title="Record Number Bindings">Record Number Bindings</a>
153 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
154 <h3 class="title">Note</h3>
156 In this document, bindings are described in the
157 context of their use for stored data in a database. However,
158 bindings may also be used independently of a database to operate on
159 an arbitrary byte array. This allows using bindings when data is to
160 be written to a file or sent over a network, for example.
163 <div class="sect2" lang="en" xml:lang="en">
164 <div class="titlepage">
167 <h3 class="title"><a id="SelectingBindingFormats"></a>
168 Selecting Binding Formats
174 For the key and value of each stored collection, you may select
175 one of the following types of bindings.
177 <div class="informaltable">
178 <table border="1" width="80%">
186 <th>Binding Format</th>
194 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
199 The data is stored using a compact form of Java serialization,
200 where the class descriptions are stored separately in a catalog
201 database. Arbitrary Java objects are supported.
206 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a>
211 The data is stored using a series of fixed length primitive
212 values or zero terminated character arrays (strings). Class/type
213 evolution is not supported.
218 <a class="ulink" href="../../java/com/sleepycat/bind/RecordNumberBinding.html" target="_top">RecordNumberBinding</a>
223 The data is a 32-bit integer stored in a platform-dependent format.
227 <td>Custom binding format</td>
228 <td>User-defined</td>
230 The data storage format and ordering is determined by the
231 custom binding implementation.
238 As shown in the table above, the tuple format supports built-in ordering
239 (without specifying a custom comparator), while the serial format does
240 not. This means that when a specific key order is needed, tuples should
241 be used instead of serial data. Alternatively, a custom Btree comparator should be
243 <code class="methodname">DatabaseConfig.setBtreeComparator()</code>. Note that
244 a custom Btree comparator will usually execute more slowly than the
245 default byte-by-byte comparison. This makes using tuples an attractive
246 option, since they provide ordering along with optimal performance.
249 The tuple binding uses less space and executes faster than the
250 serial binding. But once a tuple is written to a database, the
251 order of fields in the tuple may not be changed and fields may not
252 be deleted. The only type evolution allowed is the addition of
253 fields at the end of the tuple, and this must be explicitly
254 supported by the custom binding implementation.
257 The serial binding supports the full generality of Java
258 serialization including type evolution. But serialized data can
259 only be accessed by Java applications, its size is larger, and its
260 bindings are slower to execute.
263 <div class="sect2" lang="en" xml:lang="en">
264 <div class="titlepage">
267 <h3 class="title"><a id="RecordNumberBindings"></a>Record Number Bindings</h3>
272 Any use of an access method with record number keys, and therefore any
273 use of a stored list view, requires using
274 <a class="ulink" href="../../java/com/sleepycat/bind/RecordNumberBinding.html" target="_top">RecordNumberBinding</a>
276 as the key binding. Since Berkeley DB stores record number keys using
277 a platform-dependent byte order,
278 <a class="ulink" href="../../java/com/sleepycat/bind/RecordNumberBinding.html" target="_top">RecordNumberBinding</a>
280 is needed to store record numbers properly. See
281 <span class="html"><a class="ulink" href="../../programmer_reference/am_conf_logrec.html" target="_top">logical record numbers</a> in</span>
282 the <em class="citetitle">Berkeley DB Programmer's Reference Guide</em>
283 for more information on storing DB record numbers.
285 <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
286 <h3 class="title">Note</h3>
289 <a class="ulink" href="../../java/com/sleepycat/bind/RecordNumberBinding.html" target="_top">RecordNumberBinding</a>
291 except with record number keys, as determined by the access
293 <a class="ulink" href="../../java/com/sleepycat/bind/RecordNumberBinding.html" target="_top">RecordNumberBinding</a>
295 in other cases will create a database that is not portable
296 between platforms. When constructing the stored collection,
297 the DB Java Collections API will throw an
298 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/IllegalArgumentException.html" target="_top">IllegalArgumentException</a>
304 <div class="sect2" lang="en" xml:lang="en">
305 <div class="titlepage">
308 <h3 class="title"><a id="SelectingDataBindings"></a>
309 Selecting Data Bindings
315 There are two types of binding interfaces. Simple entry bindings
317 <a class="ulink" href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>
319 interface and can be used for key or value objects. Entity bindings
321 <a class="ulink" href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>
323 interface and are used for combined key and value objects called
327 Simple entry bindings map between the key or value data stored
328 by Berkeley DB and a key or value object. This is a simple
332 Simple entry bindings are easy to implement and in some cases
333 require no coding. For example, a
334 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
336 can be used for keys or values without writing any additional
337 code. A tuple binding for a single-item tuple can also be used without
338 writing any code; see the
339 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html#getPrimitiveBinding(java.lang.Class)" target="_top">TupleBinding.getPrimitiveBinding</a>
344 Entity bindings must divide an entity object into its key and
345 value data, and then combine the key and value data to re-create
346 the entity object. This is a two-to-one mapping.
349 Entity bindings are useful when a stored application object
350 naturally has its primary key as a property, which is very common.
351 For example, an Employee object would naturally have an
352 EmployeeNumber property (its primary key) and an entity binding
353 would then be needed. Of course, entity bindings are more complex
354 to implement, especially if their key and data formats are
358 Note that even when an entity binding is used a key binding is
359 also usually needed. For example, a key binding is used to create
360 key objects that are passed to the
361 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#get" target="_top">Map.get()</a>
363 method. A key object is passed to this method even though it may
364 return an entity that also contains the key.
367 <div class="sect2" lang="en" xml:lang="en">
368 <div class="titlepage">
371 <h3 class="title"><a id="ImplementingBindings"></a>
372 Implementing Bindings
378 There are two ways to implement bindings. The first way is to
379 create a binding class that implements one of the two binding
381 <a class="ulink" href="../../java/com/sleepycat/bind/EntryBinding.html" target="_top">EntryBinding</a>
384 <a class="ulink" href="../../java/com/sleepycat/bind/EntityBinding.html" target="_top">EntityBinding</a>.
385 For tuple bindings and serial bindings there are a number of
386 abstract classes that make this easier. For example, you can extend
387 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleBinding.html" target="_top">TupleBinding</a>
389 to implement a simple binding for a tuple key or value. Abstract
390 classes are also provided for entity bindings and are named after
391 the format names of the key and value. For example, you can extend
392 <a class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialBinding.html" target="_top">TupleSerialBinding</a>
394 to implement an entity binding with a tuple key and serial
398 Another way to implement bindings is with marshalling
399 interfaces. These are interfaces which perform the binding
400 operations and are implemented by the key, value or entity classes
401 themselves. With marshalling you use a binding which calls the
402 marshalling interface and you implement the marshalling interface
403 for each key, value or entity class. For example, you can use
404 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleMarshalledBinding.html" target="_top">TupleMarshalledBinding</a>
406 along with key or value classes that implement the
407 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/MarshalledTupleEntry.html" target="_top">MarshalledTupleEntry</a>
412 <div class="sect2" lang="en" xml:lang="en">
413 <div class="titlepage">
416 <h3 class="title"><a id="UsingBindings"></a>
423 Bindings are specified whenever a stored collection is created.
424 A key binding must be specified for map, key set and entry set
425 views. A value binding or entity binding must be specified for map,
426 value set and entry set views.
429 Any number of bindings may be created for the same stored data.
430 This allows multiple views over the same data. For example, a tuple
431 might be bound to an array of values or to a class with properties
435 It is important to be careful of bindings that only use a subset
436 of the stored data. This can be useful to simplify a view or to
437 hide information that should not be accessible. However, if you
438 write records using these bindings you may create stored data that
439 is invalid from the application's point of view. It is up to the
440 application to guard against this by creating a read-only
441 collection when such bindings are used.
444 <div class="sect2" lang="en" xml:lang="en">
445 <div class="titlepage">
448 <h3 class="title"><a id="SecondaryKeyCreators"></a>
449 Secondary Key Creators
455 Secondary Key Creators are needed whenever database indices are
456 used. For each secondary index
459 (<a class="ulink" href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>)
461 a key creator is used to derive index key data from key/value data.
462 Key creators are objects whose classes implement the
464 <a class="ulink" href="../../java/com/sleepycat/db/SecondaryKeyCreator.html" target="_top">SecondaryKeyCreator</a>
469 Like bindings, key creators may be implemented using a separate
470 key creator class or using a marshalling interface. Abstract key
471 creator classes and marshalling interfaces are provided in the
472 com.sleepycat.bind.tuple and com.sleepycat.bind.serial
476 Unlike bindings, key creators fundamentally operate on key and
477 value data, not necessarily on the objects derived from the data by
478 bindings. In this sense key creators are a part of a database
479 definition, and may be independent of the various bindings that may
480 be used to view data in a database. However, key creators are not
481 prohibited from using higher level objects produced by bindings,
482 and doing so may be convenient for some applications. For example,
483 marshalling interfaces, which are defined for objects produced by
484 bindings, are a convenient way to define key creators.
489 <div class="navfooter">
491 <table width="100%" summary="Navigation footer">
493 <td width="40%" align="left"><a accesskey="p" href="Summary.html">Prev</a> </td>
494 <td width="20%" align="center"> </td>
495 <td width="40%" align="right"> <a accesskey="n" href="UsingCollectionsAPI.html">Next</a></td>
498 <td width="40%" align="left" valign="top">Chapter 7.
501 <td width="20%" align="center">
502 <a accesskey="h" href="index.html">Home</a>
504 <td width="40%" align="right" valign="top">
505 Using the DB Java Collections API