• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/timemachine/db-4.7.25.NC/docs/collections/tutorial/
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>
7		Creating Indexed Collections
8	</title>
9    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
10    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
11    <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" />
12    <link rel="up" href="UsingSecondaries.html" title="Chapter��3.��&#10;&#9;&#9;Using Secondary Indices&#10;&#9;" />
13    <link rel="previous" href="openingforeignkeys.html" title="&#10;&#9;&#9;&#10;&#9;&#9;More Secondary Key Indices&#10;&#9;" />
14    <link rel="next" href="retrievingbyindexkey.html" title="&#10;&#9;&#9;Retrieving Items by Index Key&#10;&#9;" />
15  </head>
16  <body>
17    <div class="navheader">
18      <table width="100%" summary="Navigation header">
19        <tr>
20          <th colspan="3" align="center">
21		Creating Indexed Collections
22	</th>
23        </tr>
24        <tr>
25          <td width="20%" align="left"><a accesskey="p" href="openingforeignkeys.html">Prev</a>��</td>
26          <th width="60%" align="center">Chapter��3.��
27		Using Secondary Indices
28	</th>
29          <td width="20%" align="right">��<a accesskey="n" href="retrievingbyindexkey.html">Next</a></td>
30        </tr>
31      </table>
32      <hr />
33    </div>
34    <div class="sect1" lang="en" xml:lang="en">
35      <div class="titlepage">
36        <div>
37          <div>
38            <h2 class="title" style="clear: both"><a id="indexedcollections"></a>
39		Creating Indexed Collections
40	</h2>
41          </div>
42        </div>
43        <div></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 href="http://java.sun.com/j2se/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 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 <tt class="methodname">supplierByCityDb()</tt> method, the key to the map will be the
70	City field, a 
71    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html" target="_top">String</a>
72    
73	object. When 
74    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html#get" target="_top">Map.get</a>
75    
76	is called passing the City as the key parameter, a
77	<tt class="classname">SupplierData</tt>
78    object will be returned.
79</p>
80      <p>
81    The <tt class="classname">SampleViews</tt> 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;
87import com.sleepycat.bind.serial.SerialBinding;
88import com.sleepycat.collections.StoredEntrySet;
89import com.sleepycat.collections.StoredMap;
90...
91
92public class SampleViews
93{
94    ...
95<b class="userinput"><tt>    private StoredMap supplierByCityMap;
96    private StoredMap shipmentByPartMap;
97    private StoredMap shipmentBySupplierMap;</tt></b>
98    ...
99
100    public SampleViews(SampleDatabase db)
101    {
102        ClassCatalog catalog = db.getClassCatalog();
103        ...
104<b class="userinput"><tt>        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); </tt></b>
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 href="../../java/com/sleepycat/collections/StoredMap.html" target="_top">StoredMap</a>
130            
131            constructor is a 
132            
133            <a href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>
134            
135            rather than a 
136            
137            <span>
138                <a 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 <tt class="literal">supplierByCityMap</tt>, the <tt class="literal">cityKeyBinding</tt> 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	<tt class="literal">cityKeyBinding</tt> is a 
158    <a 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 href="http://java.sun.com/j2se/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 <tt class="literal">shipmentByPartMap</tt> and
167	<tt class="literal">shipmentBySupplierMap</tt>, the <tt class="literal">partKeyBinding</tt> and
168	<tt class="literal">supplierKeyBinding</tt> are used. These were created in the Basic
169	example and used as the primary key bindings for the <tt class="literal">partMap</tt>
170	and <tt class="literal">supplierMap</tt>.
171</p>
172      <p>
173    The value bindings ��� <tt class="literal">supplierValueBinding</tt> and
174	<tt class="literal">shipmentValueBinding</tt> ��� 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<b class="userinput"><tt>    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    }</tt></b>
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		More Secondary Key Indices
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>
249