001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedOutputStream;
042: import java.io.Externalizable;
043: import java.io.IOException;
044: import java.io.ObjectInput;
045: import java.io.ObjectOutput;
046: import java.io.OutputStream;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.io.ObjectInputStream;
051: import com.quadcap.sql.io.ObjectOutputStream;
052: import com.quadcap.sql.io.Extern;
053:
054: import com.quadcap.sql.file.BlockAccess;
055: import com.quadcap.sql.file.ByteArrayRandomAccess;
056: import com.quadcap.sql.file.BlockFile;
057: import com.quadcap.sql.file.Datafile;
058: import com.quadcap.sql.file.Log;
059: import com.quadcap.sql.file.RandomAccess;
060: import com.quadcap.sql.file.SubPageManager;
061:
062: import com.quadcap.sql.types.Value;
063: import com.quadcap.sql.types.ValueBlob;
064:
065: import com.quadcap.util.Debug;
066: import com.quadcap.util.Util;
067:
068: /**
069: * Log step to delete a row from a table.
070: *
071: * @author Stan Bailes
072: */
073: public class DeleteRow extends LogStep implements Externalizable {
074: transient Table table;
075: transient LazyRow row = null;
076:
077: byte[] rowBytes;
078: String tableName = null;
079: long rowId = -1;
080:
081: /**
082: * Default constructor
083: */
084: public DeleteRow() {
085: }
086:
087: /**
088: * Explicit constructor specifies table and row id
089: */
090: public DeleteRow(Session session, Table table, long rowId) {
091: super (session);
092: this .table = table;
093: this .rowId = rowId;
094: this .tableName = table.getName();
095: }
096:
097: /**
098: * Return the row id of the deleted row
099: */
100: public long getRowId() {
101: return rowId;
102: }
103:
104: /**
105: * Get (lazy lookup) the table
106: */
107: final Table getTable(Database db) throws IOException {
108: if (table == null) {
109: table = (Table) db.getRelation(tableName);
110: }
111: return table;
112: }
113:
114: /**
115: * LogStep.redo implementation. Delete the row (and blob refs, if
116: * any)
117: */
118: public void redo(Session session) throws IOException, SQLException {
119: Database db = session.getDatabase();
120: BlockFile file = db.getFile();
121: getTable(db);
122:
123: if (session.getConnection().inRecovery()) {
124: rowId = session.getLog().getRowMap(rowId);
125: }
126:
127: db.removeRow(rowId);
128: //#ifdef DEBUG
129: if (Trace.bit(12)) {
130: Debug.println("FREE Row " + SubPageManager.toString(rowId));
131: }
132: //#endif
133: session.incrUpdateCount();
134: }
135:
136: /**
137: * LogStep.undo: Undo the row deletion by putting the old row
138: * back! (The row map is used to keep track of the fact that the
139: * row may have a new row id now...)
140: */
141: public void undo(Session session) throws IOException, SQLException {
142: Database db = session.getDatabase();
143: Log log = session.getLog();
144: getTable(db);
145:
146: BlockFile file = db.getFile();
147: long newRowId;
148: if (db.inMemory()) {
149: newRowId = db.putRow(session, file, getTable(db), row);
150: } else {
151: newRowId = file.putBytes(rowBytes);
152: }
153: log.putRowMap(this .rowId, newRowId);
154: session.decrUpdateCount();
155: }
156:
157: /**
158: * Get ready to delete the row
159: */
160: public void prepare(Session session) throws IOException,
161: SQLException {
162: Database db = session.getDatabase();
163: if (db.inMemory()) {
164: Table t = getTable(db);
165: row = new LazyRow(t.getColumnCount());
166: db.getRow(rowId, row, false);
167: } else {
168: BlockFile file = db.getFile();
169: this .rowBytes = file.getBytes(rowId);
170: }
171: //#ifdef DEBUG
172: if (Trace.bit(12)) {
173: Debug.println("DeleteRow.prepare(): Row "
174: + SubPageManager.toString(rowId) + ": "
175: + Util.hexBytes(rowBytes));
176: }
177: //#endif
178: }
179:
180: /**
181: * Read me from a stream
182: */
183: public void readExternal(ObjectInput in) throws IOException,
184: ClassNotFoundException {
185: super .readExternal(in);
186: rowId = in.readLong();
187: tableName = (String) in.readObject();
188: int size = in.readInt();
189: rowBytes = new byte[size];
190: in.read(rowBytes);
191: }
192:
193: /**
194: * Write me to a stream
195: */
196: public void writeExternal(ObjectOutput out) throws IOException {
197: if (rowId == 0) {
198: throw new IOException("rowId not set");
199: }
200: super .writeExternal(out);
201: out.writeLong(rowId);
202: out.writeObject(tableName);
203: out.writeInt(rowBytes.length);
204: out.write(rowBytes);
205: }
206:
207: /**
208: * My class's extern object
209: */
210: static Extern extern = null;
211:
212: public void setExtern(Extern extern) {
213: DeleteRow.extern = extern;
214: }
215:
216: public Extern getExtern() {
217: return extern;
218: }
219:
220: //#ifdef DEBUG
221: /**
222: * Return displayable string representation
223: */
224: public String toString() {
225: StringBuffer sb = new StringBuffer(super .toString());
226: sb.append(" DeleteRow(");
227: sb.append(tableName);
228: sb.append(',');
229: sb.append(SubPageManager.toString(rowId));
230: sb.append(')');
231: return sb.toString();
232: }
233: //#endif
234: }
|