00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00064
00065
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
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
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