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 Using Tuples with Key Creators 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="Tuple.html" title="Chapter 5. 		Using Tuples 	" /> 13 <link rel="previous" href="Tuple.html" title="Chapter 5. 		Using Tuples 	" /> 14 <link rel="next" href="tuplekeybindings.html" title=" 		Creating Tuple Key Bindings 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Using Tuples with Key Creators 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="Tuple.html">Prev</a> </td> 26 <th width="60%" align="center">Chapter 5. 27 Using Tuples 28 </th> 29 <td width="20%" align="right"> <a accesskey="n" href="tuplekeybindings.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="tupleswithkeycreators"></a> 39 Using Tuples with Key Creators 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 Key creators were used in prior examples to extract index keys 47 from value objects. The keys were returned as deserialized key 48 objects, since the serial format was used for keys. In this 49 example, the tuple format is used for keys and the key creators 50 return keys by writing information to a tuple. The differences 51 between this example and the prior example are: 52</p> 53 <div class="itemizedlist"> 54 <ul type="disc"> 55 <li> 56 <p> 57 The 58 <a href="/java/com/sleepycat/bind/serial/TupleSerialKeyCreator.html" target="_top">TupleSerialKeyCreator</a> 59 60 base class is used instead of the 61 <a href="/java/com/sleepycat/bind/serial/SerialSerialKeyCreator.html" target="_top">SerialSerialKeyCreator</a> 62 63 base class. 64 </p> 65 </li> 66 <li> 67 <p> 68 For all key input and output parameters, the 69 <a href="/java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a> 70 71 and 72 <a href="/java/com/sleepycat/bind/tuple/TupleOutput.html" target="_top">TupleOutput</a> 73 74 classes are used instead of 75 <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html" target="_top">Object</a> 76 77 (representing a deserialized object). 78 </p> 79 </li> 80 <li> 81 <p> 82 Instead of returning a key output object, these methods call 83 tuple write methods such as 84 <a href="/java/com/sleepycat/bind/tuple/TupleOutput.html#writeString(java.lang.String)" target="_top">TupleOutput.writeString</a>. 85 </p> 86 </li> 87 </ul> 88 </div> 89 <p> 90 In addition to writing key tuples, the 91 <tt class="classname">ShipmentByPartKeyCreator</tt> and 92 <tt class="classname">ShipmentBySupplierKeyCreator</tt> classes also read the key tuple 93 of the primary key. This is because they extract the index key from 94 fields in the Shipment's primary key. Instead of calling getter 95 methods on the <tt class="classname">ShipmentKey</tt> object, as in prior examples, 96 these methods call 97 <a href="/java/com/sleepycat/bind/tuple/TupleInput.html#readString()" target="_top">TupleInput.readString</a>. 98 The <tt class="classname">ShipmentKey</tt> consists of two string fields that are read 99 in sequence. 100</p> 101 <p> 102 The modified key creators are shown below: 103 <tt class="classname">SupplierByCityKeyCreator</tt>, 104 <tt class="classname">ShipmentByPartKeyCreator</tt> 105 and <tt class="classname">ShipmentBySupplierKeyCreator</tt>. 106</p> 107 <a id="tuple_SupplierByCityKeyCreator"></a> 108 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.bind.serial.TupleSerialKeyCreator; 109import com.sleepycat.bind.tuple.TupleInput; 110import com.sleepycat.bind.tuple.TupleOutput; 111...</tt></b> 112public class SampleDatabase 113{ 114 ... 115 private static class SupplierByCityKeyCreator 116<b class="userinput"><tt> extends TupleSerialKeyCreator</tt></b> 117 { 118 private SupplierByCityKeyCreator(StoredClassCatalog catalog, 119 <b class="userinput"><tt>Class valueClass</tt></b>) 120 { 121 super(catalog, valueClass); 122 } 123 124<b class="userinput"><tt> public boolean createSecondaryKey(TupleInput primaryKeyInput, 125 Object valueInput, 126 TupleOutput indexKeyOutput) 127 { 128 SupplierData supplierData = (SupplierData) valueInput; 129 String city = supplierData.getCity(); 130 if (city != null) { 131 indexKeyOutput.writeString(supplierData.getCity()); 132 return true; 133 } else { 134 return false; 135 } 136 }</tt></b> 137 } 138 139 private static class ShipmentByPartKeyCreator 140 <b class="userinput"><tt>extends TupleSerialKeyCreator</tt></b> 141 { 142 private ShipmentByPartKeyCreator(StoredClassCatalog catalog, 143 <b class="userinput"><tt>Class valueClass</tt></b>) 144 { 145 super(catalog, valueClass); 146 } 147 148 <b class="userinput"><tt>public boolean createSecondaryKey(TupleInput primaryKeyInput, 149 Object valueInput, 150 TupleOutput indexKeyOutput) 151 { 152 String partNumber = primaryKeyInput.readString(); 153 // don't bother reading the supplierNumber 154 indexKeyOutput.writeString(partNumber); 155 return true; 156 }</tt></b> 157 } 158 159 private static class ShipmentBySupplierKeyCreator 160 <b class="userinput"><tt>extends TupleSerialKeyCreator</tt></b> 161 { 162 private ShipmentBySupplierKeyCreator(StoredClassCatalog catalog, 163 <b class="userinput"><tt>Class valueClass</tt></b>) 164 { 165 super(catalog, valueClass); 166 } 167 168 <b class="userinput"><tt>public boolean createSecondaryKey(TupleInput primaryKeyInput, 169 Object valueInput, 170 TupleOutput indexKeyOutput) 171 { 172 primaryKeyInput.readString(); // skip the partNumber 173 String supplierNumber = primaryKeyInput.readString(); 174 indexKeyOutput.writeString(supplierNumber); 175 return true; 176 }</tt></b> 177 } 178 ... 179} 180 </pre> 181 </div> 182 <div class="navfooter"> 183 <hr /> 184 <table width="100%" summary="Navigation footer"> 185 <tr> 186 <td width="40%" align="left"><a accesskey="p" href="Tuple.html">Prev</a> </td> 187 <td width="20%" align="center"> 188 <a accesskey="u" href="Tuple.html">Up</a> 189 </td> 190 <td width="40%" align="right"> <a accesskey="n" href="tuplekeybindings.html">Next</a></td> 191 </tr> 192 <tr> 193 <td width="40%" align="left" valign="top">Chapter 5. 194 Using Tuples 195 </td> 196 <td width="20%" align="center"> 197 <a accesskey="h" href="index.html">Home</a> 198 </td> 199 <td width="40%" align="right" valign="top"> 200 Creating Tuple Key Bindings 201 </td> 202 </tr> 203 </table> 204 </div> 205 </body> 206</html> 207