KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > LogEntryType


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: LogEntryType.java,v 1.75 2006/11/03 03:07:50 mark Exp $
7  */

8
9 package com.sleepycat.je.log;
10
11 import java.util.HashSet JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import com.sleepycat.je.DatabaseException;
15 import com.sleepycat.je.log.entry.BINDeltaLogEntry;
16 import com.sleepycat.je.log.entry.DeletedDupLNLogEntry;
17 import com.sleepycat.je.log.entry.INLogEntry;
18 import com.sleepycat.je.log.entry.LNLogEntry;
19 import com.sleepycat.je.log.entry.LogEntry;
20 import com.sleepycat.je.log.entry.SingleItemLogEntry;
21
22 /**
23  * LogEntryType is a type safe enumeration of all log entry types.
24  *
25  * <p>When adding a new version of a log entry, make sure the owning Loggable
26  * object is capable of reading in older versions from the log. The Loggable
27  * object must be sure that older versions are converted in memory into a
28  * correct instance of the newest version, so when that Loggable object is
29  * written again as the result of migration, eviction, the resulting new log
30  * entry conforms to the requirements of the new version. If context objects
31  * are required for data conversion, the conversion can be done in the
32  * Node.postFetchInit method.</p>
33  */

34 public class LogEntryType {
35
36     /*
37      * Collection of log entry type classes, used to read the log. Note that
38      * this must be declared before any instances of LogEntryType, since the
39      * constructor uses this map. Each statically defined LogEntryType should
40      * register itself with this collection.
41      */

42
43     private static final int MAX_TYPE_NUM = 27;
44
45     private static LogEntryType[] LOG_TYPES = new LogEntryType[MAX_TYPE_NUM];
46
47     /*
48      * Enumeration of log entry types. The log entry value represents the 2
49      * byte field that starts every log entry. The top byte is the log type,
50      * the bottom byte holds the version value and provisional bit.
51      *
52      * Logtype (8 bits) (Provisional (1 bit) Version (7 bits)
53      *
54      * The provisional bit can be set for any log type in the log. It's an
55      * indication to recovery that the entry shouldn't be processed when
56      * rebuilding the tree. It's used to ensure the atomic logging of multiple
57      * entries.
58      */

59
60     /* Node types */
61     public static final LogEntryType LOG_LN_TRANSACTIONAL =
62         new LogEntryType((byte) 1, (byte) 0, "LN_TX",
63              new LNLogEntry(com.sleepycat.je.tree.LN.class,
64                     true),
65                          true /* isReplicated */);
66
67     public static final LogEntryType LOG_LN =
68         new LogEntryType((byte) 2, (byte) 0, "LN",
69              new LNLogEntry(com.sleepycat.je.tree.LN.class,
70                     false),
71                          false /* isReplicated */);
72
73     public static final LogEntryType LOG_MAPLN_TRANSACTIONAL =
74         new LogEntryType((byte) 3, (byte) 2, "MapLN_TX",
75              new LNLogEntry(com.sleepycat.je.tree.MapLN.class,
76                     true),
77                          true /* isReplicated */);
78
79     public static final LogEntryType LOG_MAPLN =
80         new LogEntryType((byte) 4, (byte) 2, "MapLN",
81              new LNLogEntry(com.sleepycat.je.tree.MapLN.class,
82                     false),
83                          false /* isReplicated */);
84
85     public static final LogEntryType LOG_NAMELN_TRANSACTIONAL =
86         new LogEntryType((byte) 5, (byte) 0, "NameLN_TX",
87              new LNLogEntry(com.sleepycat.je.tree.NameLN.class,
88                     true),
89                          true /* isReplicated */);
90
91     public static final LogEntryType LOG_NAMELN =
92         new LogEntryType((byte) 6, (byte) 0, "NameLN",
93              new LNLogEntry(com.sleepycat.je.tree.NameLN.class,
94                     false),
95                          false /* isReplicated */);
96
97     public static final LogEntryType LOG_DEL_DUPLN_TRANSACTIONAL =
98         new LogEntryType((byte) 7, (byte) 0, "DelDupLN_TX",
99              new DeletedDupLNLogEntry(true),
100                          true /* isReplicated */);
101
102     public static final LogEntryType LOG_DEL_DUPLN =
103         new LogEntryType((byte) 8, (byte) 0, "DelDupLN",
104              new DeletedDupLNLogEntry(false),
105                          false /* isReplicated */);
106
107     public static final LogEntryType LOG_DUPCOUNTLN_TRANSACTIONAL =
108         new LogEntryType
109     ((byte) 9, (byte) 0, "DupCountLN_TX",
110      new LNLogEntry(com.sleepycat.je.tree.DupCountLN.class, true),
111          true /* isReplicated */);
112
113     public static final LogEntryType LOG_DUPCOUNTLN =
114         new LogEntryType
115     ((byte) 10, (byte) 0, "DupCountLN",
116      new LNLogEntry(com.sleepycat.je.tree.DupCountLN.class, false),
117          false /* isReplicated */);
118
119     public static final LogEntryType LOG_FILESUMMARYLN =
120         new LogEntryType
121     ((byte) 11, (byte) 3, "FileSummaryLN",
122      new LNLogEntry(com.sleepycat.je.tree.FileSummaryLN.class, false),
123          false /* isReplicated */);
124
125     public static final LogEntryType LOG_IN =
126         new LogEntryType
127     ((byte) 12, (byte) 2, "IN",
128      new INLogEntry(com.sleepycat.je.tree.IN.class),
129          false /* isReplicated */);
130
131     public static final LogEntryType LOG_BIN =
132         new LogEntryType
133     ((byte) 13, (byte) 2, "BIN",
134      new INLogEntry(com.sleepycat.je.tree.BIN.class),
135          false /* isReplicated */);
136
137     public static final LogEntryType LOG_DIN =
138         new LogEntryType
139     ((byte) 14, (byte) 2, "DIN",
140      new INLogEntry(com.sleepycat.je.tree.DIN.class),
141          false /* isReplicated */);
142
143     public static final LogEntryType LOG_DBIN =
144         new LogEntryType
145     ((byte) 15, (byte) 2, "DBIN",
146      new INLogEntry(com.sleepycat.je.tree.DBIN.class),
147          false /* isReplicated */);
148
149     public static final LogEntryType[] IN_TYPES = {
150         LogEntryType.LOG_IN,
151         LogEntryType.LOG_BIN,
152         LogEntryType.LOG_DIN,
153         LogEntryType.LOG_DBIN,
154     };
155
156     /*** If you add new types, be sure to update MAX_TYPE_NUM at the top.***/
157
158     private static final int MAX_NODE_TYPE_NUM = 15;
159
160     public static boolean isNodeType(byte typeNum, byte version) {
161         return (typeNum <= MAX_NODE_TYPE_NUM);
162     }
163
164     /* Root */
165     public static final LogEntryType LOG_ROOT =
166     new LogEntryType((byte) 16, (byte) 1, "Root",
167              new SingleItemLogEntry
168                  (com.sleepycat.je.dbi.DbTree.class),
169                          false /* isReplicated */);
170
171     /* Transactional entries */
172     public static final LogEntryType LOG_TXN_COMMIT =
173         new LogEntryType((byte) 17, (byte) 0, "Commit",
174              new SingleItemLogEntry
175                  (com.sleepycat.je.txn.TxnCommit.class),
176                          true /* isReplicated */);
177
178     public static final LogEntryType LOG_TXN_ABORT =
179         new LogEntryType((byte) 18, (byte) 0, "Abort",
180              new SingleItemLogEntry
181                  (com.sleepycat.je.txn.TxnAbort.class),
182                          true /* isReplicated */);
183
184     public static final LogEntryType LOG_CKPT_START =
185         new LogEntryType
186     ((byte) 19, (byte) 0, "CkptStart",
187      new SingleItemLogEntry
188          (com.sleepycat.je.recovery.CheckpointStart.class),
189          false /* isReplicated */);
190
191     public static final LogEntryType LOG_CKPT_END =
192         new LogEntryType((byte) 20, (byte) 0, "CkptEnd",
193              new SingleItemLogEntry
194                  (com.sleepycat.je.recovery.CheckpointEnd.class),
195                          false /* isReplicated */);
196
197     public static final LogEntryType LOG_IN_DELETE_INFO =
198         new LogEntryType((byte) 21, (byte) 0, "INDelete",
199              new SingleItemLogEntry
200                  (com.sleepycat.je.tree.INDeleteInfo.class),
201                          false /* isReplicated */);
202
203     public static final LogEntryType LOG_BIN_DELTA =
204         new LogEntryType((byte) 22, (byte) 0, "BINDelta",
205              new BINDeltaLogEntry
206                  (com.sleepycat.je.tree.BINDelta.class),
207                          false /* isReplicated */);
208             
209     public static final LogEntryType LOG_DUP_BIN_DELTA =
210         new LogEntryType((byte) 23, (byte) 0, "DupBINDelta",
211              new BINDeltaLogEntry
212                  (com.sleepycat.je.tree.BINDelta.class),
213                          false /* isReplicated */);
214
215     /* Administrative entries */
216     public static final LogEntryType LOG_TRACE =
217         new LogEntryType((byte) 24, (byte) 0, "Trace",
218              new SingleItemLogEntry
219                  (com.sleepycat.je.utilint.Tracer.class),
220                          false /* isReplicated */);
221
222     /* File header */
223     public static final LogEntryType LOG_FILE_HEADER =
224         new LogEntryType((byte) 25, (byte) 0, "FileHeader",
225              new SingleItemLogEntry
226                  (com.sleepycat.je.log.FileHeader.class),
227                          false /* isReplicated */);
228
229     public static final LogEntryType LOG_IN_DUPDELETE_INFO =
230         new LogEntryType((byte) 26, (byte) 0, "INDupDelete",
231              new SingleItemLogEntry
232                  (com.sleepycat.je.tree.INDupDeleteInfo.class),
233                          false /* isReplicated */);
234
235     public static final LogEntryType LOG_TXN_PREPARE =
236         new LogEntryType((byte) 27, (byte) 0, "Prepare",
237              new SingleItemLogEntry
238                  (com.sleepycat.je.txn.TxnPrepare.class),
239                          false /* isReplicated */);
240
241     /*** If you add new types, be sure to update MAX_TYPE_NUM at the top.***/
242
243     /* For validity checking */
244
245     private static final byte PROVISIONAL_MASK = (byte) 0x80;
246     private static final byte IGNORE_PROVISIONAL = ~PROVISIONAL_MASK;
247
248     /*
249      * Implementation of a log entry.
250      */

251     private byte typeNum; // persistent value for this entry type
252
private byte version; // for upgrades
253
private String JavaDoc displayName;
254     private LogEntry logEntry;
255     private boolean isReplicated; /* If true, replicate before logging. */
256
257     /*
258      * Constructors
259      */

260
261     /**
262      * For base class support.
263      */

264
265     /* No log types can be defined outside this package. */
266     LogEntryType(byte typeNum, byte version) {
267         this.typeNum = typeNum;
268         this.version = version;
269     }
270
271     /**
272      * Create the static log types.
273      */

274     private LogEntryType(byte typeNum,
275              byte version,
276              String JavaDoc displayName,
277              LogEntry logEntry,
278                          boolean isReplicated) {
279
280         this.typeNum = typeNum;
281         this.version = version;
282         this.logEntry = logEntry;
283         this.displayName = displayName;
284         this.isReplicated = isReplicated;
285     LOG_TYPES[typeNum - 1] = this;
286     }
287
288     public boolean isNodeType() {
289         return (typeNum <= MAX_NODE_TYPE_NUM);
290     }
291
292     /**
293      * @return the static version of this type
294      */

295     public static LogEntryType findType(byte typeNum, byte version) {
296     if (typeNum <= 0 || typeNum > MAX_TYPE_NUM) {
297         return null;
298     }
299     return (LogEntryType) LOG_TYPES[typeNum - 1];
300     }
301
302     /**
303      * Get a copy of all types for unit testing.
304      */

305     public static Set JavaDoc getAllTypes() {
306     HashSet JavaDoc ret = new HashSet JavaDoc();
307
308     for (int i = 0; i < MAX_TYPE_NUM; i++) {
309         ret.add(LOG_TYPES[i]);
310     }
311     return ret;
312     }
313
314     /**
315      * @return the log entry type owned by the shared, static version
316      */

317     public LogEntry getSharedLogEntry() {
318         return logEntry;
319     }
320
321     /**
322      * @return a clone of the log entry type for a given log type.
323      */

324     LogEntry getNewLogEntry()
325         throws DatabaseException {
326
327         try {
328             return (LogEntry) logEntry.clone();
329         } catch (CloneNotSupportedException JavaDoc e) {
330             throw new DatabaseException(e);
331         }
332     }
333
334     /**
335      * Set the provisional bit.
336      */

337     static byte setProvisional(byte version) {
338         return (byte) (version | PROVISIONAL_MASK);
339     }
340
341     /**
342      * Clear the provisional bit.
343      */

344     public static byte clearProvisional(byte version) {
345         return (byte) (version & IGNORE_PROVISIONAL);
346     }
347
348     /**
349      * @return true if the provisional bit is set.
350      */

351     static boolean isProvisional(byte version) {
352         return ((version & PROVISIONAL_MASK) != 0);
353     }
354
355     byte getTypeNum() {
356         return typeNum;
357     }
358
359     byte getVersion() {
360         return version;
361     }
362
363     /**
364      * @return true if type number is valid.
365      */

366     static boolean isValidType(byte typeNum) {
367         return typeNum > 0 && typeNum <= MAX_TYPE_NUM;
368     }
369
370     public String JavaDoc toString() {
371         return displayName + "/" + version;
372     }
373
374     /**
375      * Check for equality without making a new object.
376      */

377     boolean equalsType(byte typeNum, byte version) {
378         return (this.typeNum == typeNum);
379     }
380
381     public boolean equalsType(byte typeNum) {
382         return (this.typeNum == typeNum);
383     }
384
385     /*
386      * Override Object.equals. Ignore provisional bit when checking for
387      * equality.
388      */

389     public boolean equals(Object JavaDoc obj) {
390         // Same instance?
391
if (this == obj) {
392             return true;
393         }
394
395         // Is it the right type of object?
396
if (!(obj instanceof LogEntryType)) {
397             return false;
398         }
399
400         return typeNum == ((LogEntryType) obj).typeNum;
401     }
402
403     /**
404      * This is used as a hash key.
405      */

406     public int hashCode() {
407         return typeNum;
408     }
409
410     /**
411      * Return true if this log entry should be transmitted to other
412      * sites if the environment is part of a replication group.
413      */

414     public boolean isReplicated() {
415         return isReplicated;
416     }
417 }
418
Popular Tags