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
|
<?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>Chapter 4. Working with Indices</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="Getting Started with Berkeley DB" />
<link rel="up" href="dpl.html" title="Part I. Programming with the Direct Persistence Layer" />
<link rel="prev" href="saveret.html" title="Saving and Retrieving Data" />
<link rel="next" href="dplindexcreate.html" title="Creating Indexes" />
</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">Chapter 4. Working with Indices</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href="saveret.html">Prev</a> </td>
<th width="60%" align="center">Part I. Programming with the Direct Persistence Layer</th>
<td width="20%" align="right"> <a accesskey="n" href="dplindexcreate.html">Next</a></td>
</tr>
</table>
<hr />
</div>
<div class="chapter" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title"><a id="persist_index"></a>Chapter 4. Working with Indices</h2>
</div>
</div>
</div>
<div class="toc">
<p>
<b>Table of Contents</b>
</p>
<dl>
<dt>
<span class="sect1">
<a href="persist_index.html#dplindexaccess">Accessing Indexes</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="persist_index.html#primaryindexaccess">Accessing Primary Indices</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="persist_index.html#secondaryindexaccess">Accessing Secondary Indices</a>
</span>
</dt>
</dl>
</dd>
<dt>
<span class="sect1">
<a href="dplindexcreate.html">Creating Indexes</a>
</span>
</dt>
<dd>
<dl>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#dplprimaryidxdecl">Declaring a Primary Indexes</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#dplsecondaryidxdecl">Declaring Secondary Indexes</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="dplindexcreate.html#foreignkey">Foreign Key Constraints</a>
</span>
</dt>
</dl>
</dd>
</dl>
</div>
<p>
All entity classes stored in DB using the DPL must have a
primary index, or key, identified for them. All such classes may
also have one or more secondary keys declared for them. This
chapter describes primary and secondary indexes in detail, and
shows how to access the indexes created for a given entity class.
</p>
<p>
One way to organize access to your primary and secondary
indexes is to create a <span class="emphasis"><em>data accessor</em></span>
class. We show an implementation of a data accessor class in
<a class="xref" href="simpleda.html" title="SimpleDA.class">SimpleDA.class</a>.
</p>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id="dplindexaccess"></a>Accessing Indexes</h2>
</div>
</div>
</div>
<div class="toc">
<dl>
<dt>
<span class="sect2">
<a href="persist_index.html#primaryindexaccess">Accessing Primary Indices</a>
</span>
</dt>
<dt>
<span class="sect2">
<a href="persist_index.html#secondaryindexaccess">Accessing Secondary Indices</a>
</span>
</dt>
</dl>
</div>
<p>
In order to retrieve any object from an entity store, you
must access at least the primary index for that object.
Different entity classes stored in an entity store can have
different primary indexes, but all entity classes must have a
primary index declared for it. The primary index is just
the default index used for the class. (That is, it is the
data's primary <span class="emphasis"><em>key</em></span> for the underlying database.)
</p>
<p>
Entity classes can optionally have secondary indexes
declared for them. In order to access these secondary
indexes, you must first access the primary index.
</p>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="primaryindexaccess"></a>Accessing Primary Indices</h3>
</div>
</div>
</div>
<p>
You retrieve a primary index using the
<code class="methodname">EntityStore.getPrimaryIndex()</code>
method. To do this, you indicate the index key type
(that is, whether it is a String, Integer, and
so forth) and the class of the entities stored
in the index.
</p>
<p>
For example, the following retrieves the
primary index for an <code class="classname">Inventory</code>
class (we provide an implementation of this class in
<a class="xref" href="inventoryclass.html" title="Inventory.java">Inventory.java</a>).
These index keys are of type <code class="classname">String</code>.
</p>
<pre class="programlisting">PrimaryIndex<String,Inventory> inventoryBySku =
store.getPrimaryIndex(String.class, Inventory.class); </pre>
</div>
<div class="sect2" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h3 class="title"><a id="secondaryindexaccess"></a>Accessing Secondary Indices</h3>
</div>
</div>
</div>
<p>
You retrieve a secondary index using the
<code class="methodname">EntityStore.getSecondaryIndex()</code>
method. Because secondary indices actually
refer to a primary index somewhere in your data
store, to access a secondary index you:
</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>
Provide the primary index as
returned by
<code class="methodname">EntityStore.getPrimaryIndex()</code>.
</p>
</li>
<li>
<p>
Identify the key data type used by
the secondary index
(<code class="classname">String</code>,
<code class="classname">Long</code>,
and so forth).
</p>
</li>
<li>
<p>
Identify the name of the
secondary key field.
When you declare the
<code class="classname">SecondaryIndex</code>
object, you identify the entity class
to which the secondary index
must refer.
</p>
</li>
</ol>
</div>
<p>
For example, the following first retrieves the
primary index, and then uses that to retrieve a secondary
index. The secondary key is held by the
<code class="literal">itemName</code> field of the
<code class="classname">Inventory</code> class.
</p>
<pre class="programlisting">PrimaryIndex<String,Inventory> inventoryBySku =
store.getPrimaryIndex(String.class, Inventory.class);
SecondaryIndex<String,String,Inventory> inventoryByName =
store.getSecondaryIndex(inventoryBySku, String.class, "itemName"); </pre>
</div>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href="saveret.html">Prev</a> </td>
<td width="20%" align="center">
<a accesskey="u" href="dpl.html">Up</a>
</td>
<td width="40%" align="right"> <a accesskey="n" href="dplindexcreate.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Saving and Retrieving Data </td>
<td width="20%" align="center">
<a accesskey="h" href="index.html">Home</a>
</td>
<td width="40%" align="right" valign="top"> Creating Indexes</td>
</tr>
</table>
</div>
</body>
</html>
|