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.Externalizable;
042: import java.io.IOException;
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045:
046: import java.util.Vector;
047:
048: import java.sql.SQLException;
049:
050: import com.quadcap.sql.file.Datafile;
051: import com.quadcap.sql.file.DatafileException;
052: import com.quadcap.sql.file.LogEntry;
053:
054: import com.quadcap.sql.lock.Transaction;
055:
056: import com.quadcap.util.Debug;
057:
058: /**
059: * Basic unit of write-ahead logging strategy.
060: *
061: * @author Stan Bailes
062: */
063: public abstract class LogStep extends LogEntry {
064: Session mySession;
065:
066: public LogStep() {
067: super (-1, -1, STEP);
068: }
069:
070: public LogStep(Session session) {
071: super (session.getTransactionId(), session.getStmtId(), STEP);
072: mySession = session;
073: }
074:
075: /**
076: * Undo the effects of this step from the database.
077: */
078: public void undo(Transaction t, Datafile df) throws IOException,
079: DatafileException {
080: try {
081: Session s = mySession;
082: if (s == null) {
083: Database db = (Database) df;
084: s = db.getSession(t);
085: }
086: undo(s);
087: //#ifdef DEBUG
088: if (Trace.bit(15)) {
089: Debug.println("LogStep.UNDO(" + this + ")");
090: }
091: //#endif
092: } catch (SQLException ex) {
093: throw new DatafileException(ex);
094: }
095: }
096:
097: /**
098: * Undo the effects of this step from the database.
099: */
100: public void redo(Transaction t, Datafile df) throws IOException,
101: DatafileException {
102: try {
103: Session s = mySession;
104: if (s == null) {
105: Database db = (Database) df;
106: s = db.getSession(t);
107: }
108: redo(s);
109: //#ifdef DEBUG
110: if (Trace.bit(16)) {
111: Debug.println("LogStep.redo(" + this + ")");
112: }
113: //#endif
114: } catch (SQLException ex) {
115: throw new DatafileException(ex);
116: }
117: }
118:
119: /**
120: * Do/redo this step.
121: */
122: abstract public void undo(Session session) throws IOException,
123: SQLException;
124:
125: /**
126: * Do/redo this step.
127: */
128: abstract public void redo(Session session) throws IOException,
129: SQLException;
130:
131: /**
132: * Perform any actions that are necessary to prepare this log record
133: * for being written to the log. This is called before the log
134: * record is written, and before the first invocation of 'redo'. It
135: * is not called subsequently, or during recovery.
136: */
137: public void prepare(Session session) throws IOException,
138: SQLException {
139: mySession = session;
140: }
141: }
|