1 package com.quadcap.sql; 2 3 40 41 import java.io.ByteArrayInputStream ; 42 import java.io.IOException ; 43 44 import java.sql.SQLException ; 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 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 , SQLException 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 , SQLException 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 { 102 try { 103 ByteArrayInputStream bi = new ByteArrayInputStream (getBytes()); 104 ObjectInputStream oi = new ObjectInputStream(bi); 105 int rsize = oi.readInt(); 106 if (rsize != size()) { 107 throw new RuntimeException ("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 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 { 137 super.set(i, v); 138 dirty[i] = true; 139 } 140 141 142 final byte[] getBytes() throws IOException { 143 byte[] ret = ra.getBytes(); 144 return ret; 145 } 146 147 final static byte[] writeRow(Session session, Tuple tuple, Row row) 148 throws IOException , SQLException 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 final static String tn(Value v) throws SQLException { 166 StringBuffer sb = new StringBuffer (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 176 final static void writeRow(Tuple tuple, Row row, ObjectOutputStream out) 177 throws IOException , SQLException 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 |