KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 import com.quadcap.util.collections.LongMap;
49
50 import com.quadcap.sql.lock.Transaction;
51
52 import com.quadcap.util.Debug;
53
54 /**
55  * Interface to logging subsystem.
56  *
57  * @author Stan Bailes
58  */

59 public class Log3 implements Log {
60     Datafile db;
61     Properties JavaDoc props;
62     LongMap tt = new LongMap(222);
63     LongMap rowMap = new LongMap(3333);
64     
65     /**
66      * Initialize the logging subsystem
67      */

68     public void init(Datafile db, boolean create, Properties JavaDoc props)
69     throws IOException JavaDoc {
70         this.db = db;
71         this.props = (Properties JavaDoc)props.clone();
72     }
73
74     /**
75      * Start the logging subsystem
76      */

77     public void start() {
78     }
79
80     /**
81      * Return the database that we're logging for
82      */

83     public Datafile getDatafile() {
84         return db;
85     }
86
87     /**
88      * Add a transaction's log record to the end of the open log file
89      */

90     public void addEntry(LogEntry entry) throws IOException JavaDoc {
91         int code = entry.getCode();
92         if (code == LogEntry.BEGIN_TRANSACTION ||
93             code == LogEntry.COMMIT) {
94             tt.remove(entry.getTransactionId());
95         } else if (code == LogEntry.BLOCK_WRITE) {
96             // blank
97
} else {
98             List JavaDoc list = (List JavaDoc)tt.get(entry.getTransactionId());
99             if (list == null) {
100                 list = new ArrayList JavaDoc();
101                 tt.put(entry.getTransactionId(), list);
102             }
103             list.add(entry);
104         }
105     }
106
107     /**
108      * XXX why public?
109      */

110     public void reallyAddEntry(LogEntry entry) throws IOException JavaDoc {
111         addEntry(entry);
112     }
113     
114     /**
115      * Flush and close the log file.
116      */

117     public void close() throws IOException JavaDoc {
118         tt = new LongMap(tt.buckets());
119     }
120     
121     /**
122      * Flush all log records to disk.
123      */

124     public void flushLog() throws IOException JavaDoc {
125     }
126
127     /**
128      * Perform a checkpoint operation.
129      */

130     public void checkpoint() throws IOException JavaDoc {
131     }
132
133     /**
134      * Wait for all queue ops to be processed by the log sync thread
135      */

136     public void sync() throws IOException JavaDoc {
137     }
138
139     /**
140      * Transaction rollback.
141      */

142     public void rollbackTransaction(Transaction trans) throws IOException JavaDoc {
143         List JavaDoc list = (List JavaDoc)tt.get(trans.getTransactionId());
144         if (list != null) {
145             for (int i = list.size()-1; i >= 0; i--) {
146                 LogEntry entry = (LogEntry)list.get(i);
147                 switch (entry.getCode()) {
148                 case LogEntry.STEP:
149                     if (entry.getRedoState() == LogEntry.DONE) {
150                         try {
151                             entry.undo(trans, db);
152                         } catch (Throwable JavaDoc th) {
153                             //#ifdef DEBUG
154
Debug.println("Exception in rollback transaction");
155                             Debug.print(th);
156                             //#endif
157
}
158                         entry.setRedoState(LogEntry.UNDONE);
159                     }
160                     break;
161                 default:
162                     break;
163                 }
164             }
165         }
166     }
167         
168     /**
169      * Statement rollback.
170      */

171     public void rollbackStatement(Transaction trans, int stmtId)
172     throws IOException JavaDoc
173     {
174         List JavaDoc list = (List JavaDoc)tt.get(trans.getTransactionId());
175         if (list != null) {
176             for (int i = list.size()-1; i >= 0; i--) {
177                 LogEntry entry = (LogEntry)list.get(i);
178                 if (entry.getStatementId() != stmtId) continue;
179                 
180                 switch (entry.getCode()) {
181                 case LogEntry.STEP:
182                     if (entry.getRedoState() == LogEntry.DONE) {
183                         try {
184                             entry.undo(trans, db);
185                         } catch (Throwable JavaDoc th) {
186                             //#ifdef DEBUG
187
Debug.println("Exception in rollback transaction");
188                             Debug.print(th);
189                             //#endif
190
}
191                         entry.setRedoState(LogEntry.UNDONE);
192                     }
193                     break;
194                 case LogEntry.BEGIN_STATEMENT:
195                     return;
196                 default:
197                     break;
198                 }
199             }
200         }
201     }
202
203     /**
204      * Restart from a previous state
205      */

206     public void restart() throws Exception JavaDoc {
207     }
208
209     /**
210      * Retrieve a row mapping.
211      */

212     public long getRowMap(long rowId) {
213         long ret = rowId;
214         Long JavaDoc l = (Long JavaDoc)rowMap.get(rowId);
215         if (l != null) ret = l.longValue();
216         return ret;
217     }
218
219     /**
220      * Remember a row mapping {old,new} The old row (logRow) is now stored
221      * in a new place (fileRow), so any stored log entries that refer to
222      * the old row need to be translated to use the new row.
223      *
224      * @param logRow the "old" row
225      * @param fileRow the "new" row
226      */

227     public void putRowMap(long logRow, long fileRow) {
228         rowMap.put(logRow, new Long JavaDoc(fileRow));
229     }
230
231     /**
232      * Discard a row mapping
233      */

234     public void removeRowMap(long row) {
235         rowMap.remove(row);
236     }
237
238     /**
239      * Are you logging?
240      */

241     public boolean isLogging() {
242         return true;
243     }
244
245     /**
246      * Save a "before" image
247      */

248     public void saveBlock(long b) throws IOException JavaDoc {
249     }
250
251     /**
252      * Restore all the "before" images
253      */

254     public void restoreBlocks() throws IOException JavaDoc {
255     }
256
257     /**
258      * Reset the "before" list to be empty
259      */

260     public void resetBlocks() throws IOException JavaDoc {
261     }
262
263     /**
264      * Remove the log
265      */

266     public void remove() throws IOException JavaDoc {
267     }
268
269     /**
270      * Are we currently performing restart log recovery?
271      */

272     public boolean inRecovery() throws IOException JavaDoc {
273         return false;
274     }
275 }
276
Popular Tags