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 8 More Secondary Key Indices 9 </title> 10 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 11 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 12 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 13 <link rel="up" href="UsingSecondaries.html" title="Chapter��3.�� 		Using Secondary Indices 	" /> 14 <link rel="previous" href="UsingSecondaries.html" title="Chapter��3.�� 		Using Secondary Indices 	" /> 15 <link rel="next" href="indexedcollections.html" title=" 		Creating Indexed Collections 	" /> 16 </head> 17 <body> 18 <div class="navheader"> 19 <table width="100%" summary="Navigation header"> 20 <tr> 21 <th colspan="3" align="center"> 22 23 More Secondary Key Indices 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></div> 47 </div> 48 <p> 49 This section builds on the prior section describing secondary key indices. 50 Two more secondary key indices are defined for indexing the Shipment record 51 by PartNumber and by SupplierNumber. 52</p> 53 <p> 54 The <tt class="classname">SampleDatabase</tt> class is extended to open the 55 Shipment-by-Part and Shipment-by-Supplier secondary key 56 indices. 57</p> 58 <a id="index_java_sampledatabase1"></a> 59 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.serial.SerialSerialKeyCreator; 60import com.sleepycat.db.SecondaryConfig; 61import com.sleepycat.db.SecondaryDatabase;</tt></b> 62... 63public class SampleDatabase 64{ 65 ... 66<b class="userinput"><tt> private static final String SHIPMENT_PART_INDEX = "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 ...</tt></b> 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<b class="userinput"><tt> 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);</tt></b> 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 <tt class="classname">ShipmentByPartKeyCreator</tt> 113 and <tt class="classname">ShipmentBySupplierKeyCreator</tt> 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<b class="userinput"><tt> private static class ShipmentByPartKeyCreator 121 extends SerialSerialKeyCreator 122 { 123 private ShipmentByPartKeyCreator(StoredClassCatalog 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(StoredClassCatalog 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 }</tt></b> 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<b class="userinput"><tt> public final SecondaryDatabase getShipmentByPartDatabase() 177 { 178 return shipmentByPartDb; 179 } 180 181 public final SecondaryDatabase getShipmentBySupplierDatabase() 182 { 183 return shipmentBySupplierDb; 184 }</tt></b> 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<b class="userinput"><tt> shipmentByPartDb.close(); 199 shipmentBySupplierDb.close();</tt></b> 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> 238