Quadcap Embeddable Database

com/quadcap/sql/LazyRow.java

Go to the documentation of this file.
00001 package com.quadcap.sql; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.ByteArrayInputStream; 00042 import java.io.IOException; 00043 00044 import java.sql.SQLException; 00045 00046 import com.quadcap.sql.io.ObjectInputStream; 00047 import com.quadcap.sql.io.ObjectOutputStream; 00048 00049 import com.quadcap.sql.types.Type; 00050 import com.quadcap.sql.types.Value; 00051 00052 import com.quadcap.sql.file.ByteArrayRandomAccess; 00053 import com.quadcap.sql.file.Datafile; 00054 import com.quadcap.sql.file.PageManager; 00055 import com.quadcap.sql.file.RandomAccess; 00056 import com.quadcap.sql.file.RandomAccessOutputStream; 00057 import com.quadcap.sql.file.SubPageManager; 00058 00059 import com.quadcap.util.Debug; 00060 import com.quadcap.util.Util; 00061 00062 /** 00063 * A row that we deserialize only as needed to produce values. 00064 * 00065 * @author Stan Bailes 00066 */ 00067 public class LazyRow extends Row { 00068 ByteArrayRandomAccess ra; 00069 boolean[] dirty; 00070 00071 public LazyRow(int size) { 00072 super(size); 00073 this.dirty = new boolean[size + 1]; 00074 } 00075 00076 public void reset(RandomAccess rowBuf, Datafile db) 00077 throws IOException, SQLException 00078 { 00079 resetDirty(); 00080 if (ra == null) { 00081 ra = new ByteArrayRandomAccess((int)rowBuf.size()); 00082 } 00083 ra.resize(rowBuf.size()); 00084 byte[] buf = ra.getBytes(); 00085 rowBuf.read(0, buf, 0, (int)rowBuf.size()); 00086 bulkLoad(db); 00087 } 00088 00089 public void reset(byte[] buf, Datafile db) 00090 throws IOException, SQLException 00091 { 00092 resetDirty(); 00093 if (ra == null) { 00094 ra = new ByteArrayRandomAccess(buf); 00095 } else { 00096 ra.reset(buf, buf.length); 00097 } 00098 bulkLoad(db); 00099 } 00100 00101 final void bulkLoad(Datafile db) throws SQLException { 00102 try { 00103 ByteArrayInputStream bi = new ByteArrayInputStream(getBytes()); 00104 ObjectInputStream oi = new ObjectInputStream(bi); 00105 int rsize = oi.readInt(); 00106 if (rsize != size()) { 00107 throw new RuntimeException("Bad row size: " + rsize + 00108 " vs tuple size: " + size()); 00109 } 00110 this.blobCnt = oi.readInt(); 00111 for (int i = 1; i <= rsize; i++) { 00112 Value vx = (Value)oi.readObject(); 00113 if (db != null) vx.setDatafile(db); 00114 set(i, vx); 00115 dirty[i] = false; 00116 } 00117 } catch (Throwable e) { 00118 throw DbException.wrapThrowable(e); 00119 } 00120 } 00121 00122 public final void resetDirty() { 00123 for (int i = 1; i <= size(); i++) { 00124 dirty[i] = false; 00125 } 00126 } 00127 00128 final boolean isDirty() { 00129 boolean d = false; 00130 for (int i = 1; i < dirty.length && !d; i++) { 00131 d = dirty[i]; 00132 } 00133 return d; 00134 } 00135 00136 public void set(int i, Value v) throws SQLException { 00137 super.set(i, v); 00138 dirty[i] = true; 00139 } 00140 00141 00142 final byte[] getBytes() throws IOException { 00143 byte[] ret = ra.getBytes(); 00144 return ret; 00145 } 00146 00147 final static byte[] writeRow(Session session, Tuple tuple, Row row) 00148 throws IOException, SQLException 00149 { 00150 if (row instanceof LazyRow) { 00151 LazyRow lrow = (LazyRow)row; 00152 if (!lrow.isDirty()) { 00153 byte[] buf = lrow.getBytes(); 00154 return buf; 00155 } 00156 } 00157 ObjectOutputStream out = session.oos; 00158 out.reset(null); 00159 writeRow(tuple, row, out); 00160 byte[] ret = out.toByteArray(); 00161 return ret; 00162 } 00163 00164 //#ifdef DEBUG 00165 final static String tn(Value v) throws SQLException { 00166 StringBuffer sb = new StringBuffer(v.getType().toString()); 00167 sb.append(":"); 00168 sb.append(com.quadcap.sql.types.Value.tn(v)); 00169 sb.append("(="); 00170 sb.append(String.valueOf(v)); 00171 sb.append(')'); 00172 return sb.toString(); 00173 } 00174 //#endif 00175 00176 final static void writeRow(Tuple tuple, Row row, ObjectOutputStream out) 00177 throws IOException, SQLException 00178 { 00179 out.writeInt(row.size()); 00180 out.writeInt(row.blobCnt); 00181 for (int i = 1; i <= row.size(); i++) { 00182 Value vx = row.item(i); 00183 if (tuple != null) { 00184 Type t = tuple.getColumn(i).getType(); 00185 Value v1 = t.convert(vx); 00186 vx = v1; 00187 } 00188 out.writeObject(vx); 00189 } 00190 } 00191 } 00192