KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > LazyRow


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.sql.SQLException JavaDoc;
45
46 import com.quadcap.sql.io.ObjectInputStream;
47 import com.quadcap.sql.io.ObjectOutputStream;
48
49 import com.quadcap.sql.types.Type;
50 import com.quadcap.sql.types.Value;
51
52 import com.quadcap.sql.file.ByteArrayRandomAccess;
53 import com.quadcap.sql.file.Datafile;
54 import com.quadcap.sql.file.PageManager;
55 import com.quadcap.sql.file.RandomAccess;
56 import com.quadcap.sql.file.RandomAccessOutputStream;
57 import com.quadcap.sql.file.SubPageManager;
58
59 import com.quadcap.util.Debug;
60 import com.quadcap.util.Util;
61
62 /**
63  * A row that we deserialize only as needed to produce values.
64  *
65  * @author Stan Bailes
66  */

67 public class LazyRow extends Row {
68     ByteArrayRandomAccess ra;
69     boolean[] dirty;
70
71     public LazyRow(int size) {
72     super(size);
73     this.dirty = new boolean[size + 1];
74     }
75
76     public void reset(RandomAccess rowBuf, Datafile db)
77         throws IOException JavaDoc, SQLException JavaDoc
78     {
79         resetDirty();
80         if (ra == null) {
81             ra = new ByteArrayRandomAccess((int)rowBuf.size());
82         }
83         ra.resize(rowBuf.size());
84         byte[] buf = ra.getBytes();
85         rowBuf.read(0, buf, 0, (int)rowBuf.size());
86         bulkLoad(db);
87     }
88     
89     public void reset(byte[] buf, Datafile db)
90         throws IOException JavaDoc, SQLException JavaDoc
91     {
92         resetDirty();
93         if (ra == null) {
94             ra = new ByteArrayRandomAccess(buf);
95         } else {
96             ra.reset(buf, buf.length);
97         }
98         bulkLoad(db);
99     }
100
101     final void bulkLoad(Datafile db) throws SQLException JavaDoc {
102         try {
103             ByteArrayInputStream JavaDoc bi = new ByteArrayInputStream JavaDoc(getBytes());
104             ObjectInputStream oi = new ObjectInputStream(bi);
105             int rsize = oi.readInt();
106             if (rsize != size()) {
107                 throw new RuntimeException JavaDoc("Bad row size: " + rsize +
108                                            " vs tuple size: " + size());
109             }
110             this.blobCnt = oi.readInt();
111             for (int i = 1; i <= rsize; i++) {
112                 Value vx = (Value)oi.readObject();
113                 if (db != null) vx.setDatafile(db);
114                 set(i, vx);
115                 dirty[i] = false;
116             }
117         } catch (Throwable JavaDoc e) {
118             throw DbException.wrapThrowable(e);
119         }
120     }
121
122     public final void resetDirty() {
123     for (int i = 1; i <= size(); i++) {
124             dirty[i] = false;
125         }
126     }
127     
128     final boolean isDirty() {
129         boolean d = false;
130         for (int i = 1; i < dirty.length && !d; i++) {
131             d = dirty[i];
132         }
133         return d;
134     }
135     
136     public void set(int i, Value v) throws SQLException JavaDoc {
137     super.set(i, v);
138         dirty[i] = true;
139     }
140
141
142     final byte[] getBytes() throws IOException JavaDoc {
143         byte[] ret = ra.getBytes();
144         return ret;
145     }
146
147     final static byte[] writeRow(Session session, Tuple tuple, Row row)
148     throws IOException JavaDoc, SQLException JavaDoc
149     {
150         if (row instanceof LazyRow) {
151             LazyRow lrow = (LazyRow)row;
152             if (!lrow.isDirty()) {
153                 byte[] buf = lrow.getBytes();
154                 return buf;
155             }
156         }
157         ObjectOutputStream out = session.oos;
158         out.reset(null);
159         writeRow(tuple, row, out);
160         byte[] ret = out.toByteArray();
161         return ret;
162     }
163
164     //#ifdef DEBUG
165
final static String JavaDoc tn(Value v) throws SQLException JavaDoc {
166         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(v.getType().toString());
167         sb.append(":");
168         sb.append(com.quadcap.sql.types.Value.tn(v));
169         sb.append("(=");
170         sb.append(String.valueOf(v));
171         sb.append(')');
172         return sb.toString();
173     }
174     //#endif
175

176     final static void writeRow(Tuple tuple, Row row, ObjectOutputStream out)
177     throws IOException JavaDoc, SQLException JavaDoc
178     {
179         out.writeInt(row.size());
180         out.writeInt(row.blobCnt);
181     for (int i = 1; i <= row.size(); i++) {
182         Value vx = row.item(i);
183         if (tuple != null) {
184                 Type t = tuple.getColumn(i).getType();
185                 Value v1 = t.convert(vx);
186                 vx = v1;
187             }
188         out.writeObject(vx);
189     }
190     }
191 }
192
193
Popular Tags