| 1 package com.quadcap.sql; 2 3 40 41 import java.io.BufferedOutputStream ; 42 import java.io.Externalizable ; 43 import java.io.IOException ; 44 import java.io.ObjectInput ; 45 import java.io.ObjectOutput ; 46 import java.io.OutputStream ; 47 48 import java.sql.SQLException ; 49 50 import com.quadcap.sql.io.ObjectInputStream; 51 import com.quadcap.sql.io.ObjectOutputStream; 52 import com.quadcap.sql.io.Extern; 53 54 import com.quadcap.sql.file.BlockAccess; 55 import com.quadcap.sql.file.ByteArrayRandomAccess; 56 import com.quadcap.sql.file.BlockFile; 57 import com.quadcap.sql.file.Datafile; 58 import com.quadcap.sql.file.Log; 59 import com.quadcap.sql.file.RandomAccess; 60 import com.quadcap.sql.file.SubPageManager; 61 62 import com.quadcap.sql.types.Value; 63 import com.quadcap.sql.types.ValueBlob; 64 65 import com.quadcap.util.Debug; 66 import com.quadcap.util.Util; 67 68 73 public class DeleteRow extends LogStep implements Externalizable { 74 transient Table table; 75 transient LazyRow row = null; 76 77 byte[] rowBytes; 78 String tableName = null; 79 long rowId = -1; 80 81 84 public DeleteRow() {} 85 86 89 public DeleteRow(Session session, Table table, long rowId) { 90 super(session); 91 this.table = table; 92 this.rowId = rowId; 93 this.tableName = table.getName(); 94 } 95 96 99 public long getRowId() { return rowId; } 100 101 104 final Table getTable(Database db) throws IOException { 105 if (table == null) { 106 table = (Table)db.getRelation(tableName); 107 } 108 return table; 109 } 110 111 115 public void redo(Session session) throws IOException , SQLException { 116 Database db = session.getDatabase(); 117 BlockFile file = db.getFile(); 118 getTable(db); 119 120 if (session.getConnection().inRecovery()) { 121 rowId = session.getLog().getRowMap(rowId); 122 } 123 124 db.removeRow(rowId); 125 if (Trace.bit(12)) { 127 Debug.println("FREE Row " + SubPageManager.toString(rowId)); 128 } 129 session.incrUpdateCount(); 131 } 132 133 138 public void undo(Session session) throws IOException , SQLException { 139 Database db = session.getDatabase(); 140 Log log = session.getLog(); 141 getTable(db); 142 143 BlockFile file = db.getFile(); 144 long newRowId; 145 if (db.inMemory()) { 146 newRowId = db.putRow(session, file, getTable(db), row); 147 } else { 148 newRowId = file.putBytes(rowBytes); 149 } 150 log.putRowMap(this.rowId, newRowId); 151 session.decrUpdateCount(); 152 } 153 154 157 public void prepare(Session session) throws IOException , SQLException { 158 Database db = session.getDatabase(); 159 if (db.inMemory()) { 160 Table t = getTable(db); 161 row = new LazyRow(t.getColumnCount()); 162 db.getRow(rowId, row, false); 163 } else { 164 BlockFile file = db.getFile(); 165 this.rowBytes = file.getBytes(rowId); 166 } 167 if (Trace.bit(12)) { 169 Debug.println("DeleteRow.prepare(): Row " + 170 SubPageManager.toString(rowId) + ": " + 171 Util.hexBytes(rowBytes)); 172 } 173 } 175 176 179 public void readExternal(ObjectInput in) 180 throws IOException , ClassNotFoundException  181 { 182 super.readExternal(in); 183 rowId = in.readLong(); 184 tableName = (String )in.readObject(); 185 int size = in.readInt(); 186 rowBytes = new byte[size]; 187 in.read(rowBytes); 188 } 189 190 193 public void writeExternal(ObjectOutput out) throws IOException { 194 if (rowId == 0) { 195 throw new IOException ("rowId not set"); 196 } 197 super.writeExternal(out); 198 out.writeLong(rowId); 199 out.writeObject(tableName); 200 out.writeInt(rowBytes.length); 201 out.write(rowBytes); 202 } 203 204 205 208 static Extern extern = null; 209 public void setExtern(Extern extern) { DeleteRow.extern = extern; } 210 public Extern getExtern() { return extern; } 211 212 216 public String toString() { 217 StringBuffer sb = new StringBuffer (super.toString()); 218 sb.append(" DeleteRow("); 219 sb.append(tableName); 220 sb.append(','); 221 sb.append(SubPageManager.toString(rowId)); 222 sb.append(')'); 223 return sb.toString(); 224 } 225 } 227 | Popular Tags |