KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > CachedRow


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.lib.java.JavaSystem;
73 import org.hsqldb.rowio.RowInputInterface;
74 import org.hsqldb.rowio.RowOutputInterface;
75
76 // fredt@users 20020221 - patch 513005 by sqlbob@users (RMP)
77
// fredt@users 20020920 - patch 1.7.1 - refactoring to cut memory footprint
78
// fredt@users 20021205 - patch 1.7.2 - enhancements
79
// fredt@users 20021215 - doc 1.7.2 - javadoc comments
80
// boucherb@users - 20040411 - doc 1.7.2 - javadoc comments
81

82 /**
83  * In-memory representation of a disk-based database row object with methods
84  * for serialization and de-serialization. <p>
85  *
86  * A CachedRow is normally part of a circular double linked list which
87  * contains all of the Rows currently in the Cache for the database. It is
88  * unlinked from this list when it is freed from the Cache to make way for
89  * other rows.
90  *
91  * New class from the Hypersonic Original
92  *
93  * @author Thomas Mueller (Hypersonic SQL Group)
94  * @version 1.7.2
95  * @since Hypersonic SQL
96  */

97 public class CachedRow extends Row {
98
99     static final int NO_POS = -1;
100
101     //
102
protected Table tTable;
103     int storageSize;
104
105     /**
106      * Flag indicating unwritten data.
107      */

108     protected boolean hasDataChanged;
109
110     /**
111      * Flag indicating Node data has changed.
112      */

113     boolean hasNodesChanged;
114
115     /**
116      * Default constructor used only in subclasses.
117      */

118     CachedRow() {}
119
120     public static CachedRow newCachedRow(Table t,
121                                          Object JavaDoc[] o) throws HsqlException {
122
123         if (t.isText) {
124             return new CachedDataRow(t, o);
125         } else {
126             return new CachedRow(t, o);
127         }
128     }
129
130     /**
131      * Constructor for new Rows. Variable hasDataChanged is set to true in
132      * order to indicate the data needs saving.
133      *
134      * @param t table
135      * @param o row data
136      * @throws HsqlException if a database access error occurs
137      */

138     CachedRow(Table t, Object JavaDoc[] o) throws HsqlException {
139
140         tTable = t;
141
142         int indexcount = t.getIndexCount();
143
144         nPrimaryNode = Node.newNode(this, 0, t);
145
146         Node n = nPrimaryNode;
147
148         for (int i = 1; i < indexcount; i++) {
149             n.nNext = Node.newNode(this, i, t);
150             n = n.nNext;
151         }
152
153         oData = o;
154         hasDataChanged = hasNodesChanged = true;
155     }
156
157     /**
158      * Constructor when read from the disk into the Cache.
159      *
160      * @param t table
161      * @param in data source
162      * @throws IOException
163      * @throws HsqlException
164      */

165     public CachedRow(Table t,
166                      RowInputInterface in) throws IOException JavaDoc, HsqlException {
167
168         tTable = t;
169         iPos = in.getPos();
170         storageSize = in.getSize();
171
172         int indexcount = t.getIndexCount();
173
174         nPrimaryNode = Node.newNode(this, in, 0, t);
175
176         Node n = nPrimaryNode;
177
178         for (int i = 1; i < indexcount; i++) {
179             n.nNext = Node.newNode(this, in, i, t);
180             n = n.nNext;
181         }
182
183         oData = in.readData(tTable.getColumnTypes());
184     }
185
186     private void readRowInfo(RowInputInterface in)
187     throws IOException JavaDoc, HsqlException {
188
189         // for use when additional transaction info is attached to rows
190
}
191
192     /**
193      * This method is called only when the Row is deleted from the database
194      * table. The links with all the other objects apart from the data
195      * are removed.
196      *
197      * @throws HsqlException
198      */

199     public void delete() throws HsqlException {
200
201         super.delete();
202
203         hasNodesChanged = hasDataChanged = false;
204         tTable = null;
205     }
206
207     public int getStorageSize() {
208         return storageSize;
209     }
210
211     /**
212      * Sets the file position for the row
213      *
214      * @param pos position in data file
215      */

216     public void setPos(int pos) {
217         iPos = pos;
218     }
219
220     /**
221      * Sets flag for Node data change.
222      */

223     void setChanged() {
224         hasNodesChanged = true;
225     }
226
227     /**
228      * Returns true if Node data has changed.
229      *
230      * @return boolean
231      */

232     public boolean hasChanged() {
233         return hasNodesChanged;
234     }
235
236     /**
237      * Returns the Table to which this Row belongs.
238      *
239      * @return Table
240      */

241     public Table getTable() {
242         return tTable;
243     }
244
245     /**
246      * returned size does not include the row size written at the beginning
247      */

248     public int getRealSize(RowOutputInterface out) {
249         return tTable.getIndexCount() * DiskNode.SIZE_IN_BYTE
250                + out.getSize(this);
251     }
252
253     public void setStorageSize(int size) {
254         storageSize = size;
255     }
256
257     /**
258      * Returns true if any of the Nodes for this row is a root node.
259      * Used only in Cache.java to avoid removing the row from the cache.
260      *
261      * @return boolean
262      * @throws HsqlException
263      */

264     synchronized public boolean isKeepInMemory() {
265
266         Node n = nPrimaryNode;
267
268         while (n != null) {
269             if (n.isRoot()) {
270                 return true;
271             }
272
273             n = n.nNext;
274         }
275
276         return false;
277     }
278
279     /**
280      * Using the internal reference to the Table, returns the current cached
281      * Row. Valid for deleted rows only before any subsequent insert or
282      * update on any cached table.<p>
283      *
284      * Access to tables while performing the internal operations for an
285      * SQL statement result in CachedRow objects to be cleared from the cache.
286      * This method returns the CachedRow, loading it to the cache if it is not
287      * there.
288      * @return the current Row in Cache for this Object
289      * @throws HsqlException
290      */

291     synchronized Row getUpdatedRow() throws HsqlException {
292         return tTable == null ? null
293                               : (CachedRow) tTable.rowStore.get(iPos);
294     }
295
296     /**
297      * used in CachedDataRow
298      */

299     void setNewNodes() {}
300
301     /**
302      * Used exclusively by Cache to save the row to disk. New implementation
303      * in 1.7.2 writes out only the Node data if the table row data has not
304      * changed. This situation accounts for the majority of invocations as
305      * for each row deleted or inserted, the Nodes for several other rows
306      * will change.
307      *
308      * @param output data source
309      * @throws IOException
310      * @throws HsqlException
311      */

312     public void write(RowOutputInterface out) {
313
314         try {
315             writeNodes(out);
316
317             if (hasDataChanged) {
318                 out.writeData(oData, tTable);
319                 out.writeEnd();
320
321                 hasDataChanged = false;
322             }
323         } catch (IOException JavaDoc e) {}
324     }
325
326     private void writeRowInfo(RowOutputInterface out) {
327
328         // for use when additional transaction info is attached to rows
329
}
330
331     public void write(RowOutputInterface out, IntLookup lookup) {
332
333         out.writeSize(storageSize);
334
335         Node rownode = nPrimaryNode;
336
337         while (rownode != null) {
338             ((DiskNode) rownode).writeTranslate(out, lookup);
339
340             rownode = rownode.nNext;
341         }
342
343         out.writeData(getData(), getTable());
344         out.writeEnd();
345     }
346
347     /**
348      * Writes the Nodes, immediately after the row size.
349      *
350      * @param out
351      *
352      * @throws IOException
353      * @throws HsqlException
354      */

355     private void writeNodes(RowOutputInterface out) throws IOException JavaDoc {
356
357         out.writeSize(storageSize);
358
359         Node n = nPrimaryNode;
360
361         while (n != null) {
362             n.write(out);
363
364             n = n.nNext;
365         }
366
367         hasNodesChanged = false;
368     }
369
370     /**
371      * With CACHED tables there may possibly exist two copies of the row.
372      * All copies will have the same iPos.
373      *
374      * @param obj row to compare
375      * @return boolean
376      */

377     public boolean equals(Object JavaDoc obj) {
378
379         if (obj == this) {
380             return true;
381         }
382
383         if (obj instanceof CachedRow) {
384             return ((CachedRow) obj).iPos == iPos;
385         }
386
387         return false;
388     }
389
390     /**
391      * Hash code is valid only until a modification to the cache
392      *
393      * @return file position of row
394      */

395     public int hashCode() {
396         return iPos;
397     }
398 }
399
Popular Tags