Debian Codegraph @

docs/collections/tutorial/tuple-serialentitybindings.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>Creating Tuple-Serial Entity Bindings</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="tuplekeybindings.html" title="Creating Tuple Key Bindings" />
12 <link rel="next" href="sortedcollections.html" title="Using Sorted 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 Creating Tuple-Serial Entity Bindings
23 </th>
24 </tr>
25 <tr>
26 <td width="20%" align="left"><a accesskey="p" href="tuplekeybindings.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="sortedcollections.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="tuple-serialentitybindings"></a>
40 Creating Tuple-Serial Entity Bindings
41 </h2>
42 </div>
43 </div>
44 </div>
45 <p>
46 In the prior example serial keys and serial values were used,
47 and the
48 <a class="ulink" href="../../java/com/sleepycat/bind/serial/SerialSerialBinding.html" target="_top">SerialSerialBinding</a>
49
50 base class was used for entity bindings. In this example, tuple
51 keys and serial values are used and therefore the
52 <a class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialBinding.html" target="_top">TupleSerialBinding</a>
53
54 base class is used for entity bindings.
55 </p>
56 <p>
57 As with any entity binding, a key and value is converted to an
58 entity in the
59 <a class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialBinding.html#entryToObject(com.sleepycat.bind.tuple.TupleInput,%20java.lang.Object)" target="_top">TupleSerialBinding.entryToObject</a>
60
61 method, and from an entity to
62 a key and value in the
63 <a class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialBinding.html#objectToKey(java.lang.Object,%20com.sleepycat.db.DatabaseEntry)" target="_top">TupleSerialBinding.objectToKey</a>
64
65 and
66 <a class="ulink" href="../../java/com/sleepycat/bind/serial/TupleSerialBinding.html#objectToData(java.lang.Object)" target="_top">TupleSerialBinding.objectToData</a>
67
68 methods. But since keys are
69 stored as tuples, not as serialized objects, key fields are read
70 and written using the
71 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleInput.html" target="_top">TupleInput</a>
72
73 and
74 <a class="ulink" href="../../java/com/sleepycat/bind/tuple/TupleOutput.html" target="_top">TupleOutput</a>
75
76 parameters.
77 </p>
78 <p>
79 The <code class="classname">SampleViews</code> class contains the modified entity
80 binding classes that were defined in the prior example:
81 <code class="classname">PartBinding</code>, <code class="classname">SupplierBinding</code> and
82 <code class="classname">ShipmentBinding</code>.
83 </p>
84 <a id="tuple_partbinding"></a>
85 <pre class="programlisting"><strong class="userinput"><code>import com.sleepycat.bind.serial.TupleSerialBinding;
86 import com.sleepycat.bind.tuple.TupleInput;
87 import com.sleepycat.bind.tuple.TupleOutput;
88 ...</code></strong>
89 public class SampleViews
90 {
91 ...
92 private static class PartBinding extends <strong class="userinput"><code>TupleSerialBinding</code></strong>
93 {
94 private PartBinding(ClassCatalog classCatalog, <strong class="userinput"><code>Class dataClass</code></strong>)
95 {
96 super(classCatalog, dataClass);
97 }
98 public Object entryToObject(<strong class="userinput"><code>TupleInput</code></strong> keyInput, Object dataInput)
99 {
100 <strong class="userinput"><code> String number = keyInput.readString();</code></strong>
101 PartData data = (PartData) dataInput;
102 return new Part(<strong class="userinput"><code>number</code></strong>, data.getName(), data.getColor(),
103 data.getWeight(), data.getCity());
104 }
105 public void objectToKey(Object object, TupleOutput output)
106 {
107 Part part = (Part) object;
108 <strong class="userinput"><code> output.writeString(part.getNumber());</code></strong>
109 }
110 public Object objectToData(Object object)
111 {
112 Part part = (Part) object;
113 return new PartData(part.getName(), part.getColor(),
114 part.getWeight(), part.getCity());
115 }
116 }
117 ...
118 private static class SupplierBinding extends <strong class="userinput"><code>TupleSerialBinding</code></strong>
119 {
120 private SupplierBinding(ClassCatalog classCatalog, <strong class="userinput"><code>Class dataClass</code></strong>)
121 {
122 super(classCatalog, dataClass);
123 }
124 public Object entryToObject(<strong class="userinput"><code>TupleInput</code></strong> keyInput, Object dataInput)
125 {
126 <strong class="userinput"><code> String number = keyInput.readString();</code></strong>
127 SupplierData data = (SupplierData) dataInput;
128 return new Supplier(<strong class="userinput"><code>number</code></strong>, data.getName(),
129 data.getStatus(), data.getCity());
130 }
131 public void objectToKey(Object object, TupleOutput output)
132 {
133 Supplier supplier = (Supplier) object;
134 <strong class="userinput"><code> output.writeString(supplier.getNumber());</code></strong>
135 }
136 public Object objectToData(Object object)
137 {
138 Supplier supplier = (Supplier) object;
139 return new SupplierData(supplier.getName(),
140 supplier.getStatus(),
141 supplier.getCity());
142 }
143 }
144 ...
145 private static class ShipmentBinding extends <strong class="userinput"><code>TupleSerialBinding</code></strong>
146 {
147 private ShipmentBinding(ClassCatalog classCatalog, <strong class="userinput"><code>Class dataClass</code></strong>)
148 {
149 super(classCatalog, dataClass);
150 }
151 public Object entryToObject(<strong class="userinput"><code>TupleInput</code></strong> keyInput, Object dataInput)
152 {
153 <strong class="userinput"><code> String partNumber = keyInput.readString();
154 String supplierNumber = keyInput.readString();</code></strong>
155 ShipmentData data = (ShipmentData) dataInput;
156 return new Shipment(<strong class="userinput"><code>partNumber, supplierNumber</code></strong>,
157 data.getQuantity());
158 }
159 public void objectToKey(Object object, TupleOutput output)
160 {
161 Shipment shipment = (Shipment) object;
162 <strong class="userinput"><code> output.writeString(shipment.getPartNumber());
163 output.writeString(shipment.getSupplierNumber());</code></strong>
164 }
165 public Object objectToData(Object object)
166 {
167 Shipment shipment = (Shipment) object;
168 return new ShipmentData(shipment.getQuantity());
169 }
170 }
171 ...
172 } </pre>
173 </div>
174 <div class="navfooter">
175 <hr />
176 <table width="100%" summary="Navigation footer">
177 <tr>
178 <td width="40%" align="left"><a accesskey="p" href="tuplekeybindings.html">Prev</a> </td>
179 <td width="20%" align="center">
180 <a accesskey="u" href="Tuple.html">Up</a>
181 </td>
182 <td width="40%" align="right"> <a accesskey="n" href="sortedcollections.html">Next</a></td>
183 </tr>
184 <tr>
185 <td width="40%" align="left" valign="top">
186 Creating Tuple Key Bindings
187  </td>
188 <td width="20%" align="center">
189 <a accesskey="h" href="index.html">Home</a>
190 </td>
191 <td width="40%" align="right" valign="top"> 
192 Using Sorted Collections
193 </td>
194 </tr>
195 </table>
196 </div>
197 </body>
198 </html>