KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > DeleteRow


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedOutputStream JavaDoc;
42 import java.io.Externalizable JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.ObjectInput JavaDoc;
45 import java.io.ObjectOutput JavaDoc;
46 import java.io.OutputStream JavaDoc;
47
48 import java.sql.SQLException JavaDoc;
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 /**
69  * Log step to delete a row from a table.
70  *
71  * @author Stan Bailes
72  */

73 public class DeleteRow extends LogStep implements Externalizable JavaDoc {
74     transient Table table;
75     transient LazyRow row = null;
76
77     byte[] rowBytes;
78     String JavaDoc tableName = null;
79     long rowId = -1;
80
81     /**
82      * Default constructor
83      */

84     public DeleteRow() {}
85
86     /**
87      * Explicit constructor specifies table and row id
88      */

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     /**
97      * Return the row id of the deleted row
98      */

99     public long getRowId() { return rowId; }
100
101     /**
102      * Get (lazy lookup) the table
103      */

104     final Table getTable(Database db) throws IOException JavaDoc {
105     if (table == null) {
106         table = (Table)db.getRelation(tableName);
107     }
108     return table;
109     }
110
111     /**
112      * LogStep.redo implementation. Delete the row (and blob refs, if
113      * any)
114      */

115     public void redo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
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         //#ifdef DEBUG
126
if (Trace.bit(12)) {
127             Debug.println("FREE Row " + SubPageManager.toString(rowId));
128         }
129         //#endif
130
session.incrUpdateCount();
131     }
132
133     /**
134      * LogStep.undo: Undo the row deletion by putting the old row
135      * back! (The row map is used to keep track of the fact that the
136      * row may have a new row id now...)
137      */

138     public void undo(Session session) throws IOException JavaDoc, SQLException JavaDoc {
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     /**
155      * Get ready to delete the row
156      */

157     public void prepare(Session session) throws IOException JavaDoc, SQLException JavaDoc {
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         //#ifdef DEBUG
168
if (Trace.bit(12)) {
169             Debug.println("DeleteRow.prepare(): Row " +
170                           SubPageManager.toString(rowId) + ": " +
171                           Util.hexBytes(rowBytes));
172         }
173         //#endif
174
}
175
176     /**
177      * Read me from a stream
178      */

179     public void readExternal(ObjectInput JavaDoc in)
180     throws IOException JavaDoc, ClassNotFoundException JavaDoc
181     {
182     super.readExternal(in);
183     rowId = in.readLong();
184     tableName = (String JavaDoc)in.readObject();
185     int size = in.readInt();
186     rowBytes = new byte[size];
187     in.read(rowBytes);
188     }
189
190     /**
191      * Write me to a stream
192      */

193     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
194     if (rowId == 0) {
195         throw new IOException JavaDoc("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     /**
206      * My class's extern object
207      */

208     static Extern extern = null;
209     public void setExtern(Extern extern) { DeleteRow.extern = extern; }
210     public Extern getExtern() { return extern; }
211
212     //#ifdef DEBUG
213
/**
214      * Return displayable string representation
215      */

216     public String JavaDoc toString() {
217     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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     //#endif
226
}
227
Popular Tags