Debian Codegraph @

docs/collections/tutorial/retrievingbyindexkey.html

raw
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>Retrieving Items by Index Key</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="indexedcollections.html" title="Creating Indexed Collections" />
12 <link rel="next" href="Entity.html" title="Chapter 4.  Using Entity Classes" />
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 Retrieving Items by Index Key
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="indexedcollections.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="Entity.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="retrievingbyindexkey"></a>
40 Retrieving Items by Index Key
41 </h2>
42 </div>
43 </div>
44 </div>
45 <p>
46 Retrieving information via database index keys can be
47 accomplished using the standard Java collections API, using a
48 collection created from a
49
50 <a class="ulink" href="../../java/com/sleepycat/db/SecondaryDatabase.html" target="_top">SecondaryDatabase</a>
51
52 rather than a
53
54 <span>
55 <a class="ulink" href="../../java/com/sleepycat/db/Database.html" target="_top">Database</a>.
56 </span>
57 However, the standard Java API does not support <span class="emphasis"><em>duplicate keys</em></span>: more
58 than one element in a collection having the same key. All three
59 indices created in the prior section have duplicate keys because of
60 the nature of the city, part number and supplier number index keys.
61 More than one supplier may be in the same city, and more than one
62 shipment may have the same part number or supplier number. This
63 section describes how to use extended methods for stored
64 collections to return all values for a given key.
65 </p>
66 <p>
67 Using the standard Java collections API, the
68 <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>
69
70 method for a stored collection with duplicate keys will return only
71 the first value for a given key. To obtain all values for a given
72 key, the
73 <a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a>
74
75 method may be called. This returns a
76 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
77
78 of values for the given key. If duplicate keys are not allowed, the
79 returned collection will have at most one value. If the key is not
80 present in the map, an empty collection is returned.
81 </p>
82 <p>
83 The <code class="classname">Sample</code> class is extended to retrieve duplicates for
84 specific index keys that are present in the database.
85 </p>
86 <a id="index_sampleviewsprintdatabase"></a>
87 <pre class="programlisting">import java.util.Iterator;
88 ...
89 public class Sample
90 {
91 ...
92 private SampleViews views;
93 ...
94 private class PrintDatabase implements TransactionWorker
95 {
96 public void doWork()
97 throws Exception
98 {
99 printEntries("Parts",
100 views.getPartEntrySet().iterator());
101 printEntries("Suppliers",
102 views.getSupplierEntrySet().iterator());
103 <strong class="userinput"><code> printValues("Suppliers for City Paris",
104 views.getSupplierByCityMap().duplicates(
105 "Paris").iterator());</code></strong>
106 printEntries("Shipments",
107 views.getShipmentEntrySet().iterator());
108 <strong class="userinput"><code> printValues("Shipments for Part P1",
109 views.getShipmentByPartMap().duplicates(
110 new PartKey("P1")).iterator());
111 printValues("Shipments for Supplier S1",
112 views.getShipmentBySupplierMap().duplicates(
113 new
114 SupplierKey("S1")).iterator());</code></strong>
115 }
116 }
117
118 <strong class="userinput"><code> private void printValues(String label, Iterator iterator)
119 {
120 System.out.println("\n--- " + label + " ---");
121 while (iterator.hasNext())
122 {
123 System.out.println(iterator.next().toString());
124 }
125 } </code></strong>
126 ...
127 } </pre>
128 <p>
129 The
130 <a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a>
131
132 method is called passing the desired key. The returned value is a
133 standard Java
134 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html" target="_top">Collection</a>
135
136 containing the values for the specified key. A standard Java
137 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html" target="_top">Iterator</a>
138
139 is then obtained for this collection and all values returned by
140 that iterator are printed.
141 </p>
142 <p>
143 Another technique for retrieving duplicates is to use the
144 collection returned by
145 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#entrySet()" target="_top">Map.entrySet</a>.
146 When duplicate keys are present, a
147 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html" target="_top">Map.Entry</a>
148
149 object will be present in this collection for each duplicate. This
150 collection can then be iterated or a subset can be created from it,
151 all using the standard Java collection API.
152 </p>
153 <p>
154 Note that we did not discuss how duplicates keys can be
155 explicitly added or removed in a collection. For index keys, the
156 addition and deletion of duplicate keys happens automatically when
157 records containing the index key are added, updated, or
158 removed.
159 </p>
160 <p>
161 While not shown in the example program, it is also possible to
162 create a store with duplicate keys in the same way as an index with
163 duplicate keys — by calling
164 <code class="methodname">DatabaseConfig.setSortedDuplicates()</code> method. In that case,
165 calling
166 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#put(K, V)" target="_top">Map.put</a>
167
168 will add duplicate keys. To remove all duplicate keys, call
169 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Map.html#remove(java.lang.Object)" target="_top">Map.remove</a>.
170 To remove a specific duplicate key, call
171 <a class="ulink" href="../../java/com/sleepycat/collections/StoredMap.html#duplicates(java.lang.Object)" target="_top">StoredMap.duplicates</a>
172
173 and then call
174 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#remove(java.lang.Object)" target="_top">Collection.remove</a>
175
176 using the returned collection. Duplicate
177 values may also be added to this collection using
178 <a class="ulink" href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/Collection.html#add(E)" target="_top"> Collection.add</a>.
179 </p>
180 <p>
181 The output of the example program is shown below.
182 </p>
183 <pre class="programlisting">Adding Suppliers
184 Adding Parts
185 Adding Shipments
186
187 --- Parts ---
188 PartKey: number=P1
189 PartData: name=Nut color=Red weight=[12.0 grams] city=London
190 PartKey: number=P2
191 PartData: name=Bolt color=Green weight=[17.0 grams] city=Paris
192 PartKey: number=P3
193 PartData: name=Screw color=Blue weight=[17.0 grams] city=Rome
194 PartKey: number=P4
195 PartData: name=Screw color=Red weight=[14.0 grams] city=London
196 PartKey: number=P5
197 PartData: name=Cam color=Blue weight=[12.0 grams] city=Paris
198 PartKey: number=P6
199 PartData: name=Cog color=Red weight=[19.0 grams] city=London
200
201 --- Suppliers ---
202 SupplierKey: number=S1
203 SupplierData: name=Smith status=20 city=London
204 SupplierKey: number=S2
205 SupplierData: name=Jones status=10 city=Paris
206 SupplierKey: number=S3
207 SupplierData: name=Blake status=30 city=Paris
208 SupplierKey: number=S4
209 SupplierData: name=Clark status=20 city=London
210 SupplierKey: number=S5
211 SupplierData: name=Adams status=30 city=Athens
212
213 <strong class="userinput"><code>--- Suppliers for City Paris ---
214 SupplierData: name=Jones status=10 city=Paris
215 SupplierData: name=Blake status=30 city=Paris</code></strong>
216
217 --- Shipments ---
218 ShipmentKey: supplier=S1 part=P1
219 ShipmentData: quantity=300
220 ShipmentKey: supplier=S2 part=P1
221 ShipmentData: quantity=300
222 ShipmentKey: supplier=S1 part=P2
223 ShipmentData: quantity=200
224 ShipmentKey: supplier=S2 part=P2
225 ShipmentData: quantity=400
226 ShipmentKey: supplier=S3 part=P2
227 ShipmentData: quantity=200
228 ShipmentKey: supplier=S4 part=P2
229 ShipmentData: quantity=200
230 ShipmentKey: supplier=S1 part=P3
231 ShipmentData: quantity=400
232 ShipmentKey: supplier=S1 part=P4
233 ShipmentData: quantity=200
234 ShipmentKey: supplier=S4 part=P4
235 ShipmentData: quantity=300
236 ShipmentKey: supplier=S1 part=P5
237 ShipmentData: quantity=100
238 ShipmentKey: supplier=S4 part=P5
239 ShipmentData: quantity=400
240 ShipmentKey: supplier=S1 part=P6
241 ShipmentData: quantity=100 <strong class="userinput"><code>
242
243 --- Shipments for Part P1 ---
244 ShipmentData: quantity=300
245 ShipmentData: quantity=300
246
247 --- Shipments for Supplier S1 ---
248 ShipmentData: quantity=300
249 ShipmentData: quantity=200
250 ShipmentData: quantity=400
251 ShipmentData: quantity=200
252 ShipmentData: quantity=100
253 ShipmentData: quantity=100</code></strong> </pre>
254 </div>
255 <div class="navfooter">
256 <hr />
257 <table width="100%" summary="Navigation footer">
258 <tr>
259 <td width="40%" align="left"><a accesskey="p" href="indexedcollections.html">Prev</a> </td>
260 <td width="20%" align="center">
261 <a accesskey="u" href="UsingSecondaries.html">Up</a>
262 </td>
263 <td width="40%" align="right"> <a accesskey="n" href="Entity.html">Next</a></td>
264 </tr>
265 <tr>
266 <td width="40%" align="left" valign="top">
267 Creating Indexed Collections
268  </td>
269 <td width="20%" align="center">
270 <a accesskey="h" href="index.html">Home</a>
271 </td>
272 <td width="40%" align="right" valign="top"> Chapter 4. 
273 Using Entity Classes
274 </td>
275 </tr>
276 </table>
277 </div>
278 </body>
279 </html>