<?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.62.4" />
<link rel="home" 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="previous" href="saveret.html" title="Saving a Retrieving Data" />
<link rel="next" href="dplindexcreate.html" title="Creating Indexes" />
</head>
<body>
<div class="navheader">
<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>
</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 href="simpleda.html">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>
</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 <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></div>
</div>
<p>
You retrieve a primary index using the
<tt class="methodname">EntityStore.getPrimaryIndex()</tt>
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 <tt class="classname">Inventory</tt>
class (we provide an implementation of this class in
<a href="inventoryclass.html">Inventory.class</a>).
These index keys are of type <tt class="classname">String</tt>.
</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></div>
</div>
<p>
You retrieve a secondary index using the
<tt class="methodname">EntityStore.getSecondaryIndex()</tt>
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
<tt class="methodname">EntityStore.getPrimaryIndex()</tt>.
</p>
</li>
<li>
<p>
Identify the key data type used by
the secondary index
(<tt class="classname">String</tt>,
<tt class="classname">Long</tt>,
and so forth).
</p>
</li>
<li>
<p>
Identify the name of the
secondary key field.
When you declare the
<tt class="classname">SecondaryIndex</tt>
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
<tt class="literal">itemName</tt> field of the
<tt class="classname">Inventory</tt> 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 a 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>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez