| 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>Using Tuples with Key Creators</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="Tuple.html" title="Chapter 5. Using Tuples" /> |
| 11 |
<link rel="prev" href="Tuple.html" title="Chapter 5. Using Tuples" /> |
| 12 |
<link rel="next" href="tuplekeybindings.html" title="Creating Tuple Key Bindings" /> |
| 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 |
Using Tuples with Key Creators |
| 23 |
</th> |
| 24 |
</tr> |
| 25 |
<tr> |
| 26 |
<td width="20%" align="left"><a accesskey="p" href="Tuple.html">Prev</a> </td> |
| 27 |
<th width="60%" align="center">Chapter 5. |
| 28 |
Using Tuples |
| 29 |
</th> |
| 30 |
<td width="20%" align="right"> <a accesskey="n" href="tuplekeybindings.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="tupleswithkeycreators"></a> |
| 40 |
Using Tuples with Key Creators |
| 41 |
</h2> |
| 42 |
</div> |
| 43 |
</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 class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialKeyCreator.html" target="_top">TupleSerialKeyCreator</a> |
| 59 |
|
| 60 |
base class is used instead of the |
| 61 |
<a class="ulink" 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 class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a> |
| 70 |
|
| 71 |
and |
| 72 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleOutput.html" target="_top">TupleOutput</a> |
| 73 |
|
| 74 |
classes are used instead of |
| 75 |
<a class="ulink" href="http://download.oracle.com/javase/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 class="ulink" 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 |
<code class="classname">ShipmentByPartKeyCreator</code> and |
| 92 |
<code class="classname">ShipmentBySupplierKeyCreator</code> 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 <code class="classname">ShipmentKey</code> object, as in prior examples, |
| 96 |
these methods call |
| 97 |
<a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleInput.html#readString()" target="_top">TupleInput.readString</a>. |
| 98 |
The <code class="classname">ShipmentKey</code> consists of two string fields that are read |
| 99 |
in sequence. |
| 100 |
</p> |
| 101 |
<p> |
| 102 |
The modified key creators are shown below: |
| 103 |
<code class="classname">SupplierByCityKeyCreator</code>, |
| 104 |
<code class="classname">ShipmentByPartKeyCreator</code> |
| 105 |
and <code class="classname">ShipmentBySupplierKeyCreator</code>. |
| 106 |
</p> |
| 107 |
<a id="tuple_SupplierByCityKeyCreator"></a> |
| 108 |
<pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.serial.TupleSerialKeyCreator; |
| 109 |
import com.sleepycat.bind.tuple.TupleInput; |
| 110 |
import com.sleepycat.bind.tuple.TupleOutput; |
| 111 |
...</code></strong> |
| 112 |
public class SampleDatabase |
| 113 |
{ |
| 114 |
... |
| 115 |
private static class SupplierByCityKeyCreator |
| 116 |
<strong class="userinput"><code> extends TupleSerialKeyCreator</code></strong> |
| 117 |
{ |
| 118 |
private SupplierByCityKeyCreator(ClassCatalog catalog, |
| 119 |
<strong class="userinput"><code>Class valueClass</code></strong>) |
| 120 |
{ |
| 121 |
super(catalog, valueClass); |
| 122 |
} |
| 123 |
|
| 124 |
<strong class="userinput"><code> 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 |
}</code></strong> |
| 137 |
} |
| 138 |
|
| 139 |
private static class ShipmentByPartKeyCreator |
| 140 |
<strong class="userinput"><code>extends TupleSerialKeyCreator</code></strong> |
| 141 |
{ |
| 142 |
private ShipmentByPartKeyCreator(ClassCatalog catalog, |
| 143 |
<strong class="userinput"><code>Class valueClass</code></strong>) |
| 144 |
{ |
| 145 |
super(catalog, valueClass); |
| 146 |
} |
| 147 |
|
| 148 |
<strong class="userinput"><code>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 |
}</code></strong> |
| 157 |
} |
| 158 |
|
| 159 |
private static class ShipmentBySupplierKeyCreator |
| 160 |
<strong class="userinput"><code>extends TupleSerialKeyCreator</code></strong> |
| 161 |
{ |
| 162 |
private ShipmentBySupplierKeyCreator(ClassCatalog catalog, |
| 163 |
<strong class="userinput"><code>Class valueClass</code></strong>) |
| 164 |
{ |
| 165 |
super(catalog, valueClass); |
| 166 |
} |
| 167 |
|
| 168 |
<strong class="userinput"><code>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 |
}</code></strong> |
| 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> |