| 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>Creating Indexed Collections</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="UsingSecondaries.html" title="Chapter 3. Using Secondary Indices" /> |
| 11 |
<link rel="prev" href="openingforeignkeys.html" title="More Secondary Key Indices" /> |
| 12 |
<link rel="next" href="retrievingbyindexkey.html" title="Retrieving Items by Index Key" /> |
| 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"> |
| 22 |
Creating Indexed Collections |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="openingforeignkeys.html">Prev</a> </td> |
| 27 |
<th width="60%" align="center">Chapter 3. |
| 28 |
Using Secondary Indices |
| 29 |
</th> |
| 30 |
<td width="20%" align="right"> <a accesskey="n" href="retrievingbyindexkey.html">Next</a></td> |
| 31 |
</tr> |
| 32 |
</table> |
| 33 |
<hr /> |
| 34 |
</div> |
| 35 |
<div class="sect1" lang="en" xml:lang="en"> |
| 36 |
<div class="titlepage"> |
| 37 |
<div> |
| 38 |
<div> |
| 39 |
<h2 class="title" style="clear: both"><a id="indexedcollections"></a> |
| 40 |
Creating Indexed Collections |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
</div> |
| 45 |
<p> |
| 46 |
In the prior Basic example, bindings and Java collections were |
| 47 |
created for accessing databases via their primary keys. In this |
| 48 |
example, bindings and collections are added for accessing the same |
| 49 |
databases via their index keys. As in the prior example, serial |
| 50 |
bindings and the Java |
| 51 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html" target="_top">Map</a> |
| 52 |
|
| 53 |
class are used. |
| 54 |
</p> |
| 55 |
<p> |
| 56 |
When a map is created from a |
| 57 |
|
| 58 |
<span> |
| 59 |
<a class="ulink" href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>, |
| 60 |
</span> |
| 61 |
the keys of the map will be the index keys. However, the values of |
| 62 |
the map will be the values of the primary database associated with |
| 63 |
the index. This is how index keys can be used to access the values |
| 64 |
in a primary database. |
| 65 |
</p> |
| 66 |
<p> |
| 67 |
For example, the Supplier's City field is an index key that can |
| 68 |
be used to access the Supplier database. When a map is created |
| 69 |
using the <code class="methodname">supplierByCityDb()</code> method, the key to the map will be the |
| 70 |
City field, a |
| 71 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html" target="_top">String</a> |
| 72 |
|
| 73 |
object. When |
| 74 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#get(java.lang.Object)" target="_top">Map.get</a> |
| 75 |
|
| 76 |
is called passing the City as the key parameter, a |
| 77 |
<code class="classname">SupplierData</code> |
| 78 |
object will be returned. |
| 79 |
</p> |
| 80 |
<p> |
| 81 |
The <code class="classname">SampleViews</code> class is extended to create an index key |
| 82 |
binding for the Supplier's City field and three Java maps based on |
| 83 |
the three indices created in the prior section. |
| 84 |
</p> |
| 85 |
<a id="index_sampleviews"></a> |
| 86 |
<pre class="programlisting">import com.sleepycat.bind.EntryBinding; |
| 87 |
import com.sleepycat.bind.serial.SerialBinding; |
| 88 |
import com.sleepycat.collections.StoredEntrySet; |
| 89 |
import com.sleepycat.collections.StoredMap; |
| 90 |
... |
| 91 |
|
| 92 |
public class SampleViews |
| 93 |
{ |
| 94 |
... |
| 95 |
<strong class="userinput"><code> private StoredMap supplierByCityMap; |
| 96 |
private StoredMap shipmentByPartMap; |
| 97 |
private StoredMap shipmentBySupplierMap;</code></strong> |
| 98 |
... |
| 99 |
|
| 100 |
public SampleViews(SampleDatabase db) |
| 101 |
{ |
| 102 |
ClassCatalog catalog = db.getClassCatalog(); |
| 103 |
... |
| 104 |
<strong class="userinput"><code> EntryBinding cityKeyBinding = |
| 105 |
new SerialBinding(catalog, String.class); |
| 106 |
... |
| 107 |
supplierByCityMap = |
| 108 |
new StoredMap(db.getSupplierByCityDatabase(), |
| 109 |
cityKeyBinding, supplierValueBinding, true); |
| 110 |
shipmentByPartMap = |
| 111 |
new StoredMap(db.getShipmentByPartDatabase(), |
| 112 |
partKeyBinding, shipmentValueBinding, true); |
| 113 |
shipmentBySupplierMap = |
| 114 |
new StoredMap(db.getShipmentBySupplierDatabase(), |
| 115 |
supplierKeyBinding, shipmentValueBinding, true); </code></strong> |
| 116 |
... |
| 117 |
} |
| 118 |
} </pre> |
| 119 |
<p> |
| 120 |
In general, the indexed maps are created here in the same way as |
| 121 |
the unindexed maps were created in the Basic example. The |
| 122 |
differences are: |
| 123 |
</p> |
| 124 |
<div class="itemizedlist"> |
| 125 |
<ul type="disc"> |
| 126 |
<li> |
| 127 |
<p> |
| 128 |
The first parameter of the |
| 129 |
<a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a> |
| 130 |
|
| 131 |
constructor is a |
| 132 |
|
| 133 |
<a class="ulink" href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a> |
| 134 |
|
| 135 |
rather than a |
| 136 |
|
| 137 |
<span> |
| 138 |
<a class="ulink" href="../../java/com/sleepycat/db/Database.html" target="_top">Database</a>. |
| 139 |
</span> |
| 140 |
</p> |
| 141 |
</li> |
| 142 |
<li> |
| 143 |
<p> |
| 144 |
The second parameter is the index key binding rather than the |
| 145 |
primary key binding. |
| 146 |
</p> |
| 147 |
</li> |
| 148 |
</ul> |
| 149 |
</div> |
| 150 |
<p> |
| 151 |
For the <code class="literal">supplierByCityMap</code>, the <code class="literal">cityKeyBinding</code> must |
| 152 |
first be created. This binding was not created in the Basic example |
| 153 |
because the City field is not a primary key. |
| 154 |
</p> |
| 155 |
<p> |
| 156 |
Like the bindings created earlier for keys and values, the |
| 157 |
<code class="literal">cityKeyBinding</code> is a |
| 158 |
<a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialBinding.html" target="_top">SerialBinding</a>. |
| 159 |
Unlike the bindings created earlier, it is an example of creating a |
| 160 |
binding for a built-in Java class, |
| 161 |
<a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html" target="_top">String</a>, |
| 162 |
instead of an application-defined class. Any serializable class may |
| 163 |
be used. |
| 164 |
</p> |
| 165 |
<p> |
| 166 |
For the <code class="literal">shipmentByPartMap</code> and |
| 167 |
<code class="literal">shipmentBySupplierMap</code>, the <code class="literal">partKeyBinding</code> and |
| 168 |
<code class="literal">supplierKeyBinding</code> are used. These were created in the Basic |
| 169 |
example and used as the primary key bindings for the <code class="literal">partMap</code> |
| 170 |
and <code class="literal">supplierMap</code>. |
| 171 |
</p> |
| 172 |
<p> |
| 173 |
The value bindings — <code class="literal">supplierValueBinding</code> and |
| 174 |
<code class="literal">shipmentValueBinding</code> — were also created in the Basic |
| 175 |
example. |
| 176 |
</p> |
| 177 |
<p> |
| 178 |
This illustrates that bindings and formats may and should be |
| 179 |
reused where appropriate for creating maps and other |
| 180 |
collections. |
| 181 |
</p> |
| 182 |
<p> |
| 183 |
The following getter methods return the stored maps for use by |
| 184 |
other classes in the example program. Convenience methods for |
| 185 |
returning entry sets are also included. |
| 186 |
</p> |
| 187 |
<a id="index_sampleviewsgetters"></a> |
| 188 |
<pre class="programlisting">public class SampleViews |
| 189 |
{ |
| 190 |
... |
| 191 |
<strong class="userinput"><code> public final StoredMap getShipmentByPartMap() |
| 192 |
{ |
| 193 |
return shipmentByPartMap; |
| 194 |
} |
| 195 |
|
| 196 |
public final StoredMap getShipmentBySupplierMap() |
| 197 |
{ |
| 198 |
return shipmentBySupplierMap; |
| 199 |
} |
| 200 |
|
| 201 |
public final StoredMap getSupplierByCityMap() |
| 202 |
{ |
| 203 |
return supplierByCityMap; |
| 204 |
} |
| 205 |
|
| 206 |
public final StoredEntrySet getShipmentByPartEntrySet() |
| 207 |
{ |
| 208 |
return (StoredEntrySet) shipmentByPartMap.entrySet(); |
| 209 |
} |
| 210 |
|
| 211 |
public final StoredEntrySet getShipmentBySupplierEntrySet() |
| 212 |
{ |
| 213 |
return (StoredEntrySet) shipmentBySupplierMap.entrySet(); |
| 214 |
} |
| 215 |
|
| 216 |
public final StoredEntrySet getSupplierByCityEntrySet() |
| 217 |
{ |
| 218 |
return (StoredEntrySet) supplierByCityMap.entrySet(); |
| 219 |
}</code></strong> |
| 220 |
... |
| 221 |
} </pre> |
| 222 |
</div> |
| 223 |
<div class="navfooter"> |
| 224 |
<hr /> |
| 225 |
<table width="100%" summary="Navigation footer"> |
| 226 |
<tr> |
| 227 |
<td width="40%" align="left"><a accesskey="p" href="openingforeignkeys.html">Prev</a> </td> |
| 228 |
<td width="20%" align="center"> |
| 229 |
<a accesskey="u" href="UsingSecondaries.html">Up</a> |
| 230 |
</td> |
| 231 |
<td width="40%" align="right"> <a accesskey="n" href="retrievingbyindexkey.html">Next</a></td> |
| 232 |
</tr> |
| 233 |
<tr> |
| 234 |
<td width="40%" align="left" valign="top"> |
| 235 |
|
| 236 |
<span>More Secondary Key Indices</span> |
| 237 |
</td> |
| 238 |
<td width="20%" align="center"> |
| 239 |
<a accesskey="h" href="index.html">Home</a> |
| 240 |
</td> |
| 241 |
<td width="40%" align="right" valign="top"> |
| 242 |
Retrieving Items by Index Key |
| 243 |
</td> |
| 244 |
</tr> |
| 245 |
</table> |
| 246 |
</div> |
| 247 |
</body> |
| 248 |
</html> |