Debian Codegraph @

docs/collections/tutorial/openingforeignkeys.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>More Secondary Key Indices</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="UsingSecondaries.html" title="Chapter 3.  Using Secondary Indices" />
12 <link rel="next" href="indexedcollections.html" title="Creating Indexed Collections" />
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
23 <span xmlns="http://www.w3.org/1999/xhtml">More Secondary Key Indices</span>
24 </th>
25 </tr>
26 <tr>
27 <td width="20%" align="left"><a accesskey="p" href="UsingSecondaries.html">Prev</a> </td>
28 <th width="60%" align="center">Chapter 3. 
29 Using Secondary Indices
30 </th>
31 <td width="20%" align="right"> <a accesskey="n" href="indexedcollections.html">Next</a></td>
32 </tr>
33 </table>
34 <hr />
35 </div>
36 <div class="sect1" lang="en" xml:lang="en">
37 <div class="titlepage">
38 <div>
39 <div>
40 <h2 class="title" style="clear: both"><a id="openingforeignkeys"></a>
41
42 <span>More Secondary Key Indices</span>
43 </h2>
44 </div>
45 </div>
46 </div>
47 <p>
48 This section builds on the prior section describing secondary key indices.
49 Two more secondary key indices are defined for indexing the Shipment record
50 by PartNumber and by SupplierNumber.
51 </p>
52 <p>
53 The <code class="classname">SampleDatabase</code> class is extended to open the
54 Shipment-by-Part and Shipment-by-Supplier secondary key
55 indices.
56 </p>
57 <a id="index_java_sampledatabase1"></a>
58 <pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.serial.SerialSerialKeyCreator;
59 import com.sleepycat.db.SecondaryConfig;
60 import com.sleepycat.db.SecondaryDatabase;</code></strong>
61 ...
62 public class SampleDatabase
63 {
64 ...
65 <strong class="userinput"><code> private static final String SHIPMENT_PART_INDEX =
66 "shipment_part_index";
67 private static final String SHIPMENT_SUPPLIER_INDEX =
68 "shipment_supplier_index";
69 ...
70 private SecondaryDatabase shipmentByPartDb;
71 private SecondaryDatabase shipmentBySupplierDb;
72 ...</code></strong>
73 public SampleDatabase(String homeDirectory)
74 throws DatabaseException, FileNotFoundException
75 {
76 ...
77 SecondaryConfig secConfig = new SecondaryConfig();
78 secConfig.setTransactional(true);
79 secConfig.setAllowCreate(true);
80 secConfig.setType(DatabaseType.BTREE);
81 secConfig.setSortedDuplicates(true);
82 ...
83 <strong class="userinput"><code> secConfig.setKeyCreator(
84 new ShipmentByPartKeyCreator(javaCatalog,
85 ShipmentKey.class,
86 ShipmentData.class,
87 PartKey.class));
88 shipmentByPartDb = env.openSecondaryDatabase(null,
89 SHIPMENT_PART_INDEX,
90 null,
91 shipmentDb,
92 secConfig);
93
94 secConfig.setKeyCreator(
95 new ShipmentBySupplierKeyCreator(javaCatalog,
96 ShipmentKey.class,
97 ShipmentData.class,
98 SupplierKey.class));
99 shipmentBySupplierDb = env.openSecondaryDatabase(null,
100 SHIPMENT_SUPPLIER_INDEX,
101 null,
102 shipmentDb,
103 secConfig);</code></strong>
104 ...
105 }
106 } </pre>
107 <p>
108 The statements in this example are very similar to the statements used in
109 the previous section for opening a secondary index.
110 </p>
111 <p>
112 The application-defined <code class="classname">ShipmentByPartKeyCreator</code>
113 and <code class="classname">ShipmentBySupplierKeyCreator</code> classes are shown below. They
114 were used above to configure the secondary database objects.
115 </p>
116 <a id="index_shipmentbypartkeycreator"></a>
117 <pre class="programlisting">public class SampleDatabase
118 {
119 ...
120 <strong class="userinput"><code> private static class ShipmentByPartKeyCreator
121 extends SerialSerialKeyCreator
122 {
123 private ShipmentByPartKeyCreator(ClassCatalog catalog,
124 Class primaryKeyClass,
125 Class valueClass,
126 Class indexKeyClass)
127 {
128 super(catalog, primaryKeyClass, valueClass, indexKeyClass);
129 }
130
131 public Object createSecondaryKey(Object primaryKeyInput,
132 Object valueInput)
133 {
134 ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
135 return new PartKey(shipmentKey.getPartNumber());
136 }
137 }
138
139 private static class ShipmentBySupplierKeyCreator
140 extends SerialSerialKeyCreator
141 {
142 private ShipmentBySupplierKeyCreator(ClassCatalog catalog,
143 Class primaryKeyClass,
144 Class valueClass,
145 Class indexKeyClass)
146 {
147 super(catalog, primaryKeyClass, valueClass, indexKeyClass);
148 }
149
150 public Object createSecondaryKey(Object primaryKeyInput,
151 Object valueInput)
152 {
153 ShipmentKey shipmentKey = (ShipmentKey) primaryKeyInput;
154 return new SupplierKey(shipmentKey.getSupplierNumber());
155 }
156 }</code></strong>
157 ...
158 } </pre>
159 <p>
160 The key creator classes above are almost identical to the one
161 defined in the previous section for use with a secondary index. The
162 index key fields are different, of course, but the interesting
163 difference is that the index keys are extracted from the key, not
164 the value, of the Shipment record. This illustrates that an index
165 key may be derived from the primary database record key, value, or
166 both.
167 </p>
168 <p>
169 The following getter methods return the secondary database
170 objects for use by other classes in the example program.
171 </p>
172 <a id="index_sampledatabasegetters"></a>
173 <pre class="programlisting">public class SampleDatabase
174 {
175 ...
176 <strong class="userinput"><code> public final SecondaryDatabase getShipmentByPartDatabase()
177 {
178 return shipmentByPartDb;
179 }
180
181 public final SecondaryDatabase getShipmentBySupplierDatabase()
182 {
183 return shipmentBySupplierDb;
184 }</code></strong>
185 ...
186 } </pre>
187 <p>
188 The following statements close the secondary databases.
189 </p>
190 <a id="index_close2"></a>
191 <pre class="programlisting">public class SampleDatabase
192 {
193 ...
194 public void close()
195 throws DatabaseException {
196
197 supplierByCityDb.close();
198 <strong class="userinput"><code> shipmentByPartDb.close();
199 shipmentBySupplierDb.close();</code></strong>
200 partDb.close();
201 supplierDb.close();
202 shipmentDb.close();
203 javaCatalog.close();
204 env.close();
205 }
206 ...
207 } </pre>
208 <p>
209 Secondary databases must be closed before closing their
210 associated primary database.
211 </p>
212 </div>
213 <div class="navfooter">
214 <hr />
215 <table width="100%" summary="Navigation footer">
216 <tr>
217 <td width="40%" align="left"><a accesskey="p" href="UsingSecondaries.html">Prev</a> </td>
218 <td width="20%" align="center">
219 <a accesskey="u" href="UsingSecondaries.html">Up</a>
220 </td>
221 <td width="40%" align="right"> <a accesskey="n" href="indexedcollections.html">Next</a></td>
222 </tr>
223 <tr>
224 <td width="40%" align="left" valign="top">Chapter 3. 
225 Using Secondary Indices
226  </td>
227 <td width="20%" align="center">
228 <a accesskey="h" href="index.html">Home</a>
229 </td>
230 <td width="40%" align="right" valign="top"> 
231 Creating Indexed Collections
232 </td>
233 </tr>
234 </table>
235 </div>
236 </body>
237 </html>