KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > LogEntry


1 package com.quadcap.sql.file;
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.Externalizable JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.ObjectInput JavaDoc;
44 import java.io.ObjectOutput JavaDoc;
45
46 import java.util.Vector JavaDoc;
47
48 import java.sql.SQLException JavaDoc;
49
50 import com.quadcap.sql.io.Extern;
51 import com.quadcap.sql.io.Externable;
52
53 import com.quadcap.sql.lock.Transaction;
54
55 import com.quadcap.util.Debug;
56
57 /**
58  * An entry in the log. For 'STEP' items, which represent actions
59  * in the database, the first byte is the 'redo state', which is
60  * initially zero (DONE) and is overwritten to one (UNDONE) if the
61  * step is 'undone' as part of a rollback.
62  *
63  * @author Stan Bailes
64  */

65 public class LogEntry implements LogOp, Externable {
66     // codes
67
public static final int BEGIN_TRANSACTION = 1;
68     public static final int BEGIN_STATEMENT = 2;
69     public static final int COMMIT = 3;
70     public static final int ROLLBACK = 4;
71     public static final int CHECKPOINT = 5;
72     public static final int BLOCK_WRITE = 6;
73     public static final int CLOSE = 7;
74     public static final int FLUSH = 8;
75     public static final int STEP = 9;
76     public static final int SYNC = 10;
77     public static final int MAX_CODES = 11;
78
79     // redo states
80
public static final int DONE = 0;
81     public static final int UNDONE = 1;
82     
83     int redoState = DONE;
84     int code;
85     long transId = -1;
86     int stmtId = -1;
87     int prev = -1;
88
89     // my position in the log file, only valid for log entries
90
// which have been read from the file.
91
transient int position;
92
93     public LogEntry() {}
94
95     public LogEntry(int code) { this.code = code; }
96
97     public LogEntry(long transId, int code) {
98         this.transId = transId;
99         this.code = code;
100     }
101
102     public LogEntry(long transId, int stmtId, int code) {
103         this.transId = transId;
104         this.stmtId = stmtId;
105         this.code = code;
106     }
107
108     public final int getCode() { return code; }
109
110     /**
111      * Bind this LogEntry to a transaction
112      */

113     public final void setTransactionId(long transId) {
114         this.transId = transId;
115     }
116
117     /**
118      * Return the transaction ID for this entry
119      */

120     public final long getTransactionId() { return transId; }
121
122     /**
123      * Bind this LogEntry to a statement
124      */

125     public final void setStatementId(int stmtId) {
126         this.stmtId = stmtId;
127     }
128
129     /**
130      * Return the statement ID for this entry
131      */

132     public final int getStatementId() { return stmtId; }
133     
134     public final int getPosition() { return position; }
135
136     final void setPosition(int position) { this.position = position; }
137
138     final void setPrev(int prev) { this.prev = prev; }
139
140     final int getPrev() { return prev; }
141
142     public final int getRedoState() { return redoState; }
143
144     public final void setRedoState(int redo) { redoState = redo; }
145     
146     public void readExternal(ObjectInput JavaDoc in)
147     throws IOException JavaDoc, ClassNotFoundException JavaDoc
148     {
149     redoState = in.read();
150     code = in.read();
151     transId = in.readLong();
152         stmtId = in.readInt();
153         prev = in.readInt();
154     }
155     
156     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
157     out.write(redoState);
158     out.write(code);
159     out.writeLong(transId);
160         out.writeInt(stmtId);
161         out.writeInt(prev);
162     }
163
164     //#ifdef DEBUG
165
public String JavaDoc toString() {
166     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
167     sb.append("[T");
168     sb.append(Long.toString(transId));
169         if (stmtId >= 0) {
170             sb.append(",");
171             sb.append(String.valueOf(stmtId));
172         }
173     sb.append("] (pos=" + position + " prev=" + prev + ") ");
174     switch (code) {
175     case BEGIN_TRANSACTION:
176         sb.append("BEGIN TRANSACTION");
177         break;
178     case BEGIN_STATEMENT:
179         sb.append("BEGIN_STATEMENT");
180         break;
181     case COMMIT:
182         sb.append("COMMIT");
183         break;
184     case ROLLBACK:
185         sb.append("ROLLBACK");
186         break;
187     case CHECKPOINT:
188         sb.append("CHECKPOINT");
189         break;
190     case BLOCK_WRITE:
191         sb.append("BLOCK_WRITE");
192         break;
193     case FLUSH:
194         sb.append("FLUSH");
195         break;
196     case CLOSE:
197         sb.append("CLOSE");
198         break;
199         case SYNC:
200             sb.append("SYNC");
201             break;
202     case STEP:
203         String JavaDoc s = this.getClass().getName();
204         int idx = s.lastIndexOf('.');
205         if (idx > 0) s = s.substring(idx+1);
206             s = s + ((redoState == DONE) ? "" : " [UNDONE]");
207         sb.append("STEP " + s);
208         break;
209     default:
210         sb.append(Integer.toString(code));
211         break;
212     }
213     return sb.toString();
214     }
215     //#endif
216

217     static Extern extern = null;
218     public Extern getExtern() { return extern; }
219     public void setExtern(Extern extern) { LogEntry.extern = extern; }
220
221     public void handle(Log log) throws Exception JavaDoc {
222         log.reallyAddEntry(this);
223     }
224
225     /**
226      * Do the actual work necessary to perform this logged operation.
227      * In this base class, there's nothing to do.
228      */

229     public void redo(Transaction t, Datafile db)
230         throws IOException JavaDoc, DatafileException
231     {
232     }
233      
234     /**
235      * Do the actual work necessary to undo this logged operation.
236      * In this base class, there's nothing to do.
237      */

238     public void undo(Transaction t, Datafile db)
239         throws IOException JavaDoc, DatafileException
240     {
241     }
242
243     public void discard(Datafile db) throws IOException JavaDoc {}
244 }
245
Popular Tags