KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > CachedDataRow


1 /* Copyright (c) 2001-2005, The HSQL Development 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 HSQL Development 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 HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
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
31
32 package org.hsqldb;
33
34 import java.io.IOException JavaDoc;
35
36 import org.hsqldb.rowio.RowInputInterface;
37 import org.hsqldb.rowio.RowOutputInterface;
38
39 // fredt@users 20021205 - path 1.7.2 - enhancements
40
// fredt@users 20021215 - doc 1.7.2 - javadoc comments
41

42 /**
43  * Implementation of rows for tables with memory resident indexes and
44  * disk-based data, such as TEXT tables.
45  *
46  * @version 1.7.2
47  */

48 class CachedDataRow extends CachedRow {
49
50     /**
51      * Constructor for new rows.
52      */

53     CachedDataRow(Table t, Object JavaDoc[] o) throws HsqlException {
54
55         super(t, o);
56
57         hasDataChanged = true;
58     }
59
60     /**
61      * Constructor when read from the disk into the Cache. The link with
62      * the Nodes is made separetly.
63      */

64     CachedDataRow(Table t,
65                   RowInputInterface in) throws IOException JavaDoc, HsqlException {
66
67         tTable = t;
68         iPos = in.getPos();
69         storageSize = in.getSize();
70         oData = in.readData(tTable.getColumnTypes());
71         hasDataChanged = false;
72     }
73
74     /**
75      * As the indexes are in-memory, this passes the existing primary node
76      * for the construction of the new Row
77      */

78     Row getUpdatedRow() throws HsqlException {
79         return tTable.getRow(iPos, nPrimaryNode);
80     }
81
82     /**
83      * Used when data is read from the disk into the Cache the first time.
84      * New Nodes are created which are then indexed.
85      */

86     void setNewNodes() {
87
88         int index = tTable.getIndexCount();
89
90         nPrimaryNode = Node.newNode(this, 0, tTable);
91
92         Node n = nPrimaryNode;
93
94         for (int i = 1; i < index; i++) {
95             n.nNext = Node.newNode(this, i, tTable);
96             n = n.nNext;
97         }
98     }
99
100     /**
101      * Used when data is re-read from the disk into the Cache. The Row is
102      * already indexed so it is linked with the Node in the primary index.
103      * the Nodes is made separetly.
104      */

105     void setPrimaryNode(Node primary) {
106         nPrimaryNode = primary;
107     }
108
109     /**
110      * returned size does not include the row size written at the beginning
111      */

112     public int getRealSize(RowOutputInterface out) {
113         return out.getSize(this);
114     }
115
116     /**
117      * Writes the data to disk. Unlike CachedRow, hasChanged is never set
118      * to true when changes are made to the Nodes. (Nodes are in-memory).
119      * The only time this is used is when a new Row is added to the Caches.
120      */

121     public void write(RowOutputInterface out) {
122
123         out.writeSize(storageSize);
124         out.writeData(oData, tTable);
125         out.writeEnd();
126
127         hasDataChanged = false;
128     }
129
130     public boolean hasChanged() {
131         return hasDataChanged;
132     }
133
134     /**
135      * Sets the file position for the row and registers the row with
136      * the table.
137      *
138      * @param pos position in data file
139      */

140     public void setPos(int pos) {
141
142         iPos = pos;
143
144         Node n = nPrimaryNode;
145
146         while (n != null) {
147             ((PointerNode) n).iData = iPos;
148             n = n.nNext;
149         }
150     }
151
152     /**
153      * With the current implementation of TEXT table updates and inserts,
154      * the lifetime scope of this method extends until redefinition of table
155      * data source or shutdown.
156      *
157      * @param obj the reference object with which to compare.
158      * @return <code>true</code> if this object is the same as the obj argument;
159      * <code>false</code> otherwise.
160      */

161     public boolean equals(Object JavaDoc obj) {
162
163         if (obj == this) {
164             return true;
165         }
166
167         if (obj instanceof CachedDataRow) {
168             return ((CachedDataRow) obj).iPos == iPos
169                    && ((CachedDataRow) obj).tTable == tTable;
170         }
171
172         return false;
173     }
174 }
175
Popular Tags