KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > DiskNode


1 /* Copyright (c) 1995-2000, The Hypersonic SQL Group.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the Hypersonic SQL Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE HYPERSONIC SQL GROUP,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This software consists of voluntary contributions made by many individuals
31  * on behalf of the Hypersonic SQL Group.
32  *
33  *
34  * For work added by the HSQL Development Group:
35  *
36  * Copyright (c) 2001-2005, The HSQL Development Group
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  *
42  * Redistributions of source code must retain the above copyright notice, this
43  * list of conditions and the following disclaimer.
44  *
45  * Redistributions in binary form must reproduce the above copyright notice,
46  * this list of conditions and the following disclaimer in the documentation
47  * and/or other materials provided with the distribution.
48  *
49  * Neither the name of the HSQL Development Group nor the names of its
50  * contributors may be used to endorse or promote products derived from this
51  * software without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
54  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
57  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
60  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */

65
66
67 package org.hsqldb;
68
69 import java.io.IOException JavaDoc;
70
71 import org.hsqldb.lib.IntLookup;
72 import org.hsqldb.rowio.RowInputInterface;
73 import org.hsqldb.rowio.RowOutputInterface;
74
75 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
76
// fredt@users 20020920 - path 1.7.1 - refactoring to cut mamory footprint
77
// fredt@users 20021205 - path 1.7.2 - enhancements
78

79 /**
80  * Cached table Node implementation.<p>
81  * Only integral references to left, right and parent nodes in the AVL tree
82  * are held and used as pointers data.<p>
83  *
84  * iId is a reference to the Index object that contains this node.<br>
85  * This fields can be eliminated in the future, by changing the
86  * method signatures to take a Index parameter from Index.java (fredt@users)
87  *
88  * New class derived from the Hypersonic code
89  *
90  * @author Thomas Mueller (Hypersonic SQL Group)
91  * @version 1.7.2
92  * @since Hypersonic SQL
93  */

94 class DiskNode extends Node {
95
96     protected Row rData;
97     private int iLeft = NO_POS;
98     private int iRight = NO_POS;
99     private int iParent = NO_POS;
100     private int iId; // id of Index object for this Node
101
static final int SIZE_IN_BYTE = 4 * 4;
102
103     DiskNode(CachedRow r, RowInputInterface in,
104              int id) throws IOException JavaDoc, HsqlException {
105
106         iId = id;
107         rData = r;
108         iBalance = in.readIntData();
109         iLeft = in.readIntData();
110
111         if (iLeft <= 0) {
112             iLeft = NO_POS;
113         }
114
115         iRight = in.readIntData();
116
117         if (iRight <= 0) {
118             iRight = NO_POS;
119         }
120
121         iParent = in.readIntData();
122
123         if (iParent <= 0) {
124             iParent = NO_POS;
125         }
126
127         if (Trace.DOASSERT) {
128
129             // fredt - assert not correct - row can be deleted from one index but
130
// not yet deleted from other indexes while the process of finding
131
// the node is in progress which may require saving the row
132
// to make way for new rows in the cache and loading it back
133
// Trace.doAssert(iBalance != -2);
134
}
135     }
136
137     DiskNode(CachedRow r, int id) {
138         iId = id;
139         rData = r;
140     }
141
142     void delete() {
143         rData = null;
144         iBalance = -2;
145     }
146
147     int getKey() {
148
149         if (rData != null) {
150             return ((CachedRow) rData).iPos;
151         }
152
153         return NO_POS;
154     }
155
156     Row getRow() throws HsqlException {
157
158         if (Trace.DOASSERT) {
159             Trace.doAssert(rData != null);
160         }
161
162         return rData;
163     }
164
165     private Node findNode(int pos) throws HsqlException {
166
167         Node ret = null;
168         Row r = ((CachedRow) rData).getTable().getRow(pos);
169
170         if (r != null) {
171             ret = r.getNode(iId);
172         }
173
174         return ret;
175     }
176
177     boolean isLeft(Node node) throws HsqlException {
178
179         if (node == null) {
180             return iLeft == NO_POS;
181         }
182
183         return iLeft == ((DiskNode) node).getKey();
184     }
185
186     boolean isRight(Node node) throws HsqlException {
187
188         if (node == null) {
189             return iRight == NO_POS;
190         }
191
192         return iRight == ((DiskNode) node).getKey();
193     }
194
195     Node getLeft() throws HsqlException {
196
197         if (Trace.DOASSERT) {
198             Trace.doAssert(iBalance != -2);
199         }
200
201         if (iLeft == NO_POS) {
202             return null;
203         }
204
205         return findNode(iLeft);
206     }
207
208     Node getRight() throws HsqlException {
209
210         if (Trace.DOASSERT) {
211             Trace.doAssert(iBalance != -2);
212         }
213
214         if (iRight == NO_POS) {
215             return null;
216         }
217
218         return findNode(iRight);
219     }
220
221     Node getParent() throws HsqlException {
222
223         if (Trace.DOASSERT) {
224             Trace.doAssert(iBalance != -2);
225         }
226
227         if (iParent == NO_POS) {
228             return null;
229         }
230
231         return findNode(iParent);
232     }
233
234     boolean isRoot() {
235         return iParent == Node.NO_POS;
236     }
237
238     boolean isFromLeft() throws HsqlException {
239
240         if (this.isRoot()) {
241             return true;
242         }
243
244         if (Trace.DOASSERT) {
245             Trace.doAssert(getParent() != null);
246         }
247
248         DiskNode parent = (DiskNode) getParent();
249
250         return getKey() == parent.iLeft;
251     }
252
253     Object JavaDoc[] getData() throws HsqlException {
254
255         if (Trace.DOASSERT) {
256             Trace.doAssert(iBalance != -2);
257         }
258
259         return rData.getData();
260     }
261
262     void setParent(Node n) throws HsqlException {
263
264         if (Trace.DOASSERT) {
265             Trace.doAssert(iBalance != -2);
266         }
267
268         ((CachedRow) rData).setChanged();
269
270         iParent = n == null ? NO_POS
271                             : n.getKey();
272     }
273
274     void setBalance(int b) throws HsqlException {
275
276         if (Trace.DOASSERT) {
277             Trace.doAssert(iBalance != -2);
278         }
279
280         if (iBalance != b) {
281             ((CachedRow) rData).setChanged();
282
283             iBalance = b;
284         }
285     }
286
287     void setLeft(Node n) throws HsqlException {
288
289         if (Trace.DOASSERT) {
290             Trace.doAssert(iBalance != -2);
291         }
292
293         ((CachedRow) rData).setChanged();
294
295         iLeft = n == null ? NO_POS
296                           : n.getKey();
297     }
298
299     void setRight(Node n) throws HsqlException {
300
301         if (Trace.DOASSERT) {
302             Trace.doAssert(iBalance != -2);
303         }
304
305         ((CachedRow) rData).setChanged();
306
307         iRight = n == null ? NO_POS
308                            : n.getKey();
309     }
310
311     boolean equals(Node n) {
312
313 /*
314         if (Trace.DOASSERT) {
315             Trace.doAssert(iBalance != -2);
316
317             if (n != this) {
318                 boolean test = (getKey() == NO_POS) || (n == null)
319                                || (n.getKey() != getKey());
320
321                 if (test == false) {
322                     test = iParent == ((DiskNode) n).iParent
323                            && iLeft == ((DiskNode) n).iLeft
324                            && iRight == ((DiskNode) n).iRight;
325
326                     if (test == false) {
327                         int aA = ((CachedRow) getRow()).iLastAccess;
328                         int bA = ((CachedRow) n.getRow()).iLastAccess;
329
330                         Trace.doAssert(test,
331                                        "a: " + aA + ", " + iParent + ", "
332                                        + iLeft + ", " + iRight + " b: " + bA
333                                        + ", " + ((DiskNode) n).iParent + ", "
334                                        + ((DiskNode) n).iLeft + ", "
335                                        + ((DiskNode) n).iRight);
336                     }
337                 }
338             }
339         }
340 */

341         return this == n
342                || (n != null && getKey() == ((DiskNode) n).getKey());
343     }
344
345     void write(RowOutputInterface out) throws IOException JavaDoc {
346
347         if (Trace.DOASSERT) {
348
349             // fredt - assert not correct - row can be deleted from one index but
350
// not yet deleted from other indexes while the process of finding
351
// the node is in progress which may require saving the row
352
// to make way for new rows in the cache
353
// Trace.doAssert(iBalance != -2);
354
}
355
356         out.writeIntData(iBalance);
357         out.writeIntData((iLeft == NO_POS) ? 0
358                                            : iLeft);
359         out.writeIntData((iRight == NO_POS) ? 0
360                                             : iRight);
361         out.writeIntData((iParent == NO_POS) ? 0
362                                              : iParent);
363     }
364
365     Node getUpdatedNode() throws HsqlException {
366
367         Row row = rData.getUpdatedRow();
368
369         return row == null ? null
370                            : row.getNode(iId);
371     }
372
373     void writeTranslate(RowOutputInterface out, IntLookup lookup) {
374
375         out.writeIntData(iBalance);
376         writeTranslatePointer(iLeft, out, lookup);
377         writeTranslatePointer(iRight, out, lookup);
378         writeTranslatePointer(iParent, out, lookup);
379     }
380
381     private void writeTranslatePointer(int pointer, RowOutputInterface out,
382                                        IntLookup lookup) {
383
384         int newPointer = 0;
385
386         if (pointer != Node.NO_POS) {
387             newPointer = lookup.lookupFirstEqual(pointer);
388         }
389
390         out.writeIntData(newPointer);
391     }
392 }
393
Popular Tags