Quadcap Embeddable Database

com/quadcap/sql/DeleteRow.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.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 * Log step to delete a row from a table. 00070 * 00071 * @author Stan Bailes 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 * Default constructor 00083 */ 00084 public DeleteRow() {} 00085 00086 /** 00087 * Explicit constructor specifies table and row id 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 * Return the row id of the deleted row 00098 */ 00099 public long getRowId() { return rowId; } 00100 00101 /** 00102 * Get (lazy lookup) the table 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 * LogStep.redo implementation. Delete the row (and blob refs, if 00113 * any) 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 //#ifdef DEBUG 00126 if (Trace.bit(12)) { 00127 Debug.println("FREE Row " + SubPageManager.toString(rowId)); 00128 } 00129 //#endif 00130 session.incrUpdateCount(); 00131 } 00132 00133 /** 00134 * LogStep.undo: Undo the row deletion by putting the old row 00135 * back! (The row map is used to keep track of the fact that the 00136 * row may have a new row id now...) 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 * Get ready to delete the row 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 //#ifdef DEBUG 00168 if (Trace.bit(12)) { 00169 Debug.println("DeleteRow.prepare(): Row " + 00170 SubPageManager.toString(rowId) + ": " + 00171 Util.hexBytes(rowBytes)); 00172 } 00173 //#endif 00174 } 00175 00176 /** 00177 * Read me from a stream 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 * Write me to a stream 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 * My class's extern object 00207 */ 00208 static Extern extern = null; 00209 public void setExtern(Extern extern) { DeleteRow.extern = extern; } 00210 public Extern getExtern() { return extern; } 00211 00212 //#ifdef DEBUG 00213 /** 00214 * Return displayable string representation 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 //#endif 00226 }