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.BufferedOutputStream;
00042
import java.io.Externalizable;
00043
import java.io.IOException;
00044
import java.io.ObjectInput;
00045
import java.io.ObjectOutput;
00046
import java.io.OutputStream;
00047
00048
import java.sql.SQLException;
00049
00050
import com.quadcap.sql.io.ObjectInputStream;
00051
import com.quadcap.sql.io.ObjectOutputStream;
00052
import com.quadcap.sql.io.Extern;
00053
00054
import com.quadcap.sql.file.BlockAccess;
00055
import com.quadcap.sql.file.ByteArrayRandomAccess;
00056
import com.quadcap.sql.file.BlockFile;
00057
import com.quadcap.sql.file.Datafile;
00058
import com.quadcap.sql.file.Log;
00059
import com.quadcap.sql.file.RandomAccess;
00060
import com.quadcap.sql.file.SubPageManager;
00061
00062
import com.quadcap.sql.types.Value;
00063
import com.quadcap.sql.types.ValueBlob;
00064
00065
import com.quadcap.util.Debug;
00066
import com.quadcap.util.Util;
00067
00068
00069
00070
00071
00072
00073 public class DeleteRow extends LogStep implements
Externalizable {
00074 transient Table table;
00075 transient LazyRow row = null;
00076
00077 byte[]
rowBytes;
00078 String
tableName = null;
00079 long rowId = -1;
00080
00081
00082
00083
00084 public DeleteRow() {}
00085
00086
00087
00088
00089 public DeleteRow(
Session session,
Table table,
long rowId) {
00090 super(session);
00091
this.table = table;
00092
this.rowId = rowId;
00093
this.tableName = table.
getName();
00094 }
00095
00096
00097
00098
00099 public long getRowId() {
return rowId; }
00100
00101
00102
00103
00104 final Table getTable(
Database db)
throws IOException {
00105
if (
table == null) {
00106
table = (
Table)db.getRelation(
tableName);
00107 }
00108
return table;
00109 }
00110
00111
00112
00113
00114
00115 public void redo(
Session session)
throws IOException, SQLException {
00116
Database db = session.getDatabase();
00117
BlockFile file = db.getFile();
00118 getTable(db);
00119
00120
if (session.getConnection().inRecovery()) {
00121
rowId = session.getLog().getRowMap(
rowId);
00122 }
00123
00124 db.
removeRow(
rowId);
00125
00126
if (
Trace.bit(12)) {
00127
Debug.println(
"FREE Row " +
SubPageManager.toString(
rowId));
00128 }
00129
00130 session.incrUpdateCount();
00131 }
00132
00133
00134
00135
00136
00137
00138 public void undo(
Session session)
throws IOException, SQLException {
00139
Database db = session.getDatabase();
00140
Log log = session.getLog();
00141 getTable(db);
00142
00143
BlockFile file = db.getFile();
00144
long newRowId;
00145
if (db.
inMemory()) {
00146 newRowId = db.
putRow(session, file, getTable(db),
row);
00147 }
else {
00148 newRowId = file.
putBytes(
rowBytes);
00149 }
00150 log.
putRowMap(
this.
rowId, newRowId);
00151 session.decrUpdateCount();
00152 }
00153
00154
00155
00156
00157 public void prepare(
Session session)
throws IOException, SQLException {
00158
Database db = session.getDatabase();
00159
if (db.
inMemory()) {
00160
Table t = getTable(db);
00161
row =
new LazyRow(t.
getColumnCount());
00162 db.
getRow(
rowId,
row,
false);
00163 }
else {
00164
BlockFile file = db.getFile();
00165
this.rowBytes = file.
getBytes(
rowId);
00166 }
00167
00168
if (
Trace.bit(12)) {
00169
Debug.println(
"DeleteRow.prepare(): Row " +
00170
SubPageManager.toString(
rowId) +
": " +
00171
Util.hexBytes(
rowBytes));
00172 }
00173
00174 }
00175
00176
00177
00178
00179 public void readExternal(ObjectInput in)
00180
throws IOException, ClassNotFoundException
00181 {
00182 super.readExternal(in);
00183
rowId = in.readLong();
00184
tableName = (String)in.readObject();
00185
int size = in.readInt();
00186
rowBytes =
new byte[size];
00187 in.read(
rowBytes);
00188 }
00189
00190
00191
00192
00193 public void writeExternal(ObjectOutput out)
throws IOException {
00194
if (
rowId == 0) {
00195
throw new IOException(
"rowId not set");
00196 }
00197 super.writeExternal(out);
00198 out.writeLong(
rowId);
00199 out.writeObject(
tableName);
00200 out.writeInt(
rowBytes.length);
00201 out.write(
rowBytes);
00202 }
00203
00204
00205
00206
00207
00208 static Extern extern = null;
00209 public void setExtern(
Extern extern) {
DeleteRow.extern =
extern; }
00210 public Extern getExtern() {
return extern; }
00211
00212
00213
00214
00215
00216 public String
toString() {
00217 StringBuffer sb =
new StringBuffer(super.toString());
00218 sb.append(
" DeleteRow(");
00219 sb.append(
tableName);
00220 sb.append(
',');
00221 sb.append(
SubPageManager.toString(
rowId));
00222 sb.append(
')');
00223
return sb.toString();
00224 }
00225
00226 }