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
|
<?xml version="1.0" encoding="ISO-8859-1" 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=ISO-8859-1" />
<title>
Creating Bindings and Collections
</title>
<link rel="stylesheet" href="gettingStarted.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
<link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
<link rel="up" href="BasicProgram.html" title="Chapter2. 		The Basic Program 	" />
<link rel="previous" href="opendatabases.html" title=" 		Opening and Closing Databases 	" />
<link rel="next" href="implementingmain.html" title=" 		Implementing the Main Program 	" />
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">
Creating Bindings and Collections
</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="opendatabases.html">Prev</a></td>
<th width="60%" align="center">Chapter2.
The Basic Program
</th>
<td width="20%" align="right"><a accesskey="n" href="implementingmain.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="createbindingscollections"></a>
Creating Bindings and Collections
</h2>
</div>
</div>
<div></div>
</div>
<p>
<span class="emphasis"><em>Bindings</em></span> translate between stored records and Java objects.
In this example, Java serialization bindings are used. Serial
bindings are the simplest type of bindings because no mapping of
fields or type conversion is needed. Tuple bindings — which are
more difficult to create than serial bindings but have some
advantages — will be introduced later in the Tuple example
program.
</p>
<p>
Standard Java <span class="emphasis"><em>collections</em></span> are used to access records in a
database. Stored collections use bindings transparently to convert
the records to objects when they are retrieved from the collection,
and to convert the objects to records when they are stored in the
collection.
</p>
<p>
An important characteristic of stored collections is that they
do <span class="emphasis"><em>not</em></span> perform object caching. Every time an object is
accessed via a collection it will be added to or retrieved from the
database, and the bindings will be invoked to convert the data.
Objects are therefore always passed and returned by value, not by
reference. Because Berkeley DB is an embedded database, efficient
caching of stored raw record data is performed by the database library.
</p>
<p>
The <tt class="classname">SampleViews</tt> class is used to create the bindings and
collections. This class is separate from the <tt class="classname">SampleDatabase</tt>
class to illustrate the idea that a single set of stored data can
be accessed via multiple bindings and collections, or <span class="emphasis"><em>views</em></span>.
The skeleton for the <tt class="classname">SampleViews</tt> class follows.
</p>
<a id="cb_sampleviews"></a>
<pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.EntryBinding;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.collections.StoredEntrySet;
import com.sleepycat.collections.StoredMap;
...
public class SampleViews
{
private StoredMap partMap;
private StoredMap supplierMap;
private StoredMap shipmentMap;
...
public SampleViews(SampleDatabase db)
{
}
}</tt></b> </pre>
<p>
A
<a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
field is used for each database. The StoredMap class implements the
standard Java
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.html" target="_top">Map</a>
interface, which has methods for obtaining a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html" target="_top">Set</a>
of keys, a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Collection.html" target="_top">Collection</a>
of values, or a
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Set.html" target="_top">Set</a>
of
<a href="http://java.sun.com/j2se/1.3/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
key/value pairs. Because databases contain key/value pairs, any
Berkeley DB database may be represented as a Java map.
</p>
<p>
The following statements create the key and data bindings using
the
<a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
class.
</p>
<a id="cb_sampleviews1"></a>
<pre class="programlisting"> public SampleViews(SampleDatabase db)
{
<b class="userinput"><tt> ClassCatalog catalog = db.getClassCatalog();
EntryBinding partKeyBinding =
new SerialBinding(catalog, PartKey.class);
EntryBinding partValueBinding =
new SerialBinding(catalog, PartData.class);
EntryBinding supplierKeyBinding =
new SerialBinding(catalog, SupplierKey.class);
EntryBinding supplierValueBinding =
new SerialBinding(catalog, SupplierData.class);
EntryBinding shipmentKeyBinding =
new SerialBinding(catalog, ShipmentKey.class);
EntryBinding shipmentValueBinding =
new SerialBinding(catalog, ShipmentData.class);</tt></b>
...
} </pre>
<p>
The first parameter of the
<a href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>
constructor is the class catalog, and is used to store the class
descriptions of the serialized objects.
</p>
<p>
The second parameter is the base class for the serialized
objects and is used for type checking of the stored objects. If
<tt class="literal">null</tt> or <tt class="literal">Object.class</tt> is specified, then any Java
class is allowed. Otherwise, all objects stored in that format must
be instances of the specified class or derived from the specified
class. In the example, specific classes are used to enable strong
type checking.
</p>
<p>
The following statements create standard Java maps using the
<a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
class.
</p>
<a id="cb_sampleviews2"></a>
<pre class="programlisting"> public SampleViews(SampleDatabase db)
{
...
<b class="userinput"><tt> partMap =
new StoredMap(db.getPartDatabase(),
partKeyBinding, partValueBinding, true);
supplierMap =
new StoredMap(db.getSupplierDatabase(),
supplierKeyBinding, supplierValueBinding, true);
shipmentMap =
new StoredMap(db.getShipmentDatabase(),
shipmentKeyBinding, shipmentValueBinding, true);</tt></b>
...
} </pre>
<p>
The first parameter of the
<a href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
constructor is the database. In a StoredMap, the database keys (the primary
keys) are used as the map keys. The Index
example shows how to use secondary index keys as map keys.
</p>
<p>
The second and third parameters are the key and value bindings
to use when storing and retrieving objects via the map.
</p>
<p>
The fourth and last parameter specifies whether changes will be
allowed via the collection. If false is passed, the collection will
be read-only.
</p>
<p>
The following getter methods return the stored maps for use by
other classes in the example program. Convenience methods for
returning entry sets are also included.
</p>
<a id="cb_sampleviewsgetters"></a>
<pre class="programlisting">public class SampleViews
{
...
<b class="userinput"><tt> public final StoredMap getPartMap()
{
return partMap;
}
public final StoredMap getSupplierMap()
{
return supplierMap;
}
public final StoredMap getShipmentMap()
{
return shipmentMap;
}
public final StoredEntrySet getPartEntrySet()
{
return (StoredEntrySet) partMap.entrySet();
}
public final StoredEntrySet getSupplierEntrySet()
{
return (StoredEntrySet) supplierMap.entrySet();
}
public final StoredEntrySet getShipmentEntrySet()
{
return (StoredEntrySet) shipmentMap.entrySet();
}</tt></b>
...
} </pre>
<p>
Note that StoredMap and StoredEntrySet are returned rather than
just returning Map and Set. Since StoredMap implements the Map
interface and StoredEntrySet implements the Set interface, you may
ask why Map and Set were not returned directly.
</p>
<p>
<tt class="classname">StoredMap</tt>, <tt class="classname">StoredEntrySet</tt>,
and other stored collection classes
have a small number of extra methods beyond those in the Java
collection interfaces. The stored collection types are therefore
returned to avoid casting when using the extended methods.
Normally, however, only a Map or Set is needed, and may be used as
follows.
</p>
<a id="cb_sampleviews_usage"></a>
<pre class="programlisting"><b class="userinput"><tt> SampleDatabase sd = new SampleDatabase(new String("/home"));
SampleViews views = new SampleViews(sd);
Map partMap = views.getPartMap();
Set supplierEntries = views.getSupplierEntrySet();</tt></b> </pre>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="opendatabases.html">Prev</a></td>
<td width="20%" align="center">
<a accesskey="u" href="BasicProgram.html">Up</a>
</td>
<td width="40%" align="right"><a accesskey="n" href="implementingmain.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">
Opening and Closing Databases
</td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top">
Implementing the Main Program
</td>
</tr>
</table>
</div>
</body>
</html>
|