KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.log;
10
11 import java.nio.ByteBuffer JavaDoc;
12 import java.sql.Timestamp JavaDoc;
13 import java.util.Calendar JavaDoc;
14
15 import com.sleepycat.je.DatabaseException;
16
17 /**
18  * A FileHeader embodies the header information at the beginning of each log
19  * file.
20  */

21 public class FileHeader implements LoggableObject, LogReadable {
22
23     /*
24      * Version 3
25      * ---------
26      * [12328] Add main and dupe tree fanout values for DatabaseImpl.
27      * [12557] Add IN LSN array compression.
28      * [11597] Add a change to FileSummaryLNs: obsolete offset tracking was
29      * added and multiple records are stored for a single file rather than a
30      * single record. Each record contains the offsets that were tracked since
31      * the last record was written.
32      * [11597] Add the full obsolete LSN in LNLogEntry.
33      *
34      * Version 4
35      * ---------
36      * [#14422] Bump MapLN version from 1 to 2. Instead of a String for the
37      * comparator class name, store either a serialized string or Comparator.
38      *
39      * Version 5
40      * ---------
41      * [#15195] FileSummaryLN version 3. Add FileSummary.obsoleteLNSize and
42      * obsoleteLNSizeCounted fields.
43      */

44     private static final int LOG_VERSION = 5;
45
46     /*
47      * fileNum is the number of file, starting at 0. An unsigned int, so stored
48      * in a long in memory, but in 4 bytes on disk
49      */

50     private long fileNum;
51     private long lastEntryInPrevFileOffset;
52     private Timestamp JavaDoc time;
53     private int logVersion;
54
55     FileHeader(long fileNum, long lastEntryInPrevFileOffset) {
56         this.fileNum = fileNum;
57         this.lastEntryInPrevFileOffset = lastEntryInPrevFileOffset;
58         Calendar JavaDoc now = Calendar.getInstance();
59         time = new Timestamp JavaDoc(now.getTimeInMillis());
60         logVersion = LOG_VERSION;
61     }
62
63     /**
64      * For logging only.
65      */

66     public FileHeader() {
67     }
68
69     public int getLogVersion() {
70         return logVersion;
71     }
72
73     /**
74      * @return whether the file header has an old version number.
75      *
76      * @throws DatabaseException if the header isn't valid.
77      */

78     boolean validate(String JavaDoc fileName, long expectedFileNum)
79         throws DatabaseException {
80
81         if (fileNum != expectedFileNum) {
82             throw new LogException
83                 ("Wrong filenum in header for file " +
84                  fileName + " expected " +
85                  expectedFileNum + " got " + fileNum);
86         }
87
88         return logVersion < LOG_VERSION;
89     }
90
91     /**
92      * @return the offset of the last entry in the previous file.
93      */

94     long getLastEntryInPrevFileOffset() {
95         return lastEntryInPrevFileOffset;
96     }
97
98     /*
99      * Logging support
100      */

101
102     /**
103      * @see LoggableObject#getLogType
104      */

105     public LogEntryType getLogType() {
106         return LogEntryType.LOG_FILE_HEADER;
107     }
108
109     /**
110      * @see LoggableObject#marshallOutsideWriteLatch
111      * Can be marshalled outside the log write latch.
112      */

113     public boolean marshallOutsideWriteLatch() {
114         return true;
115     }
116
117     /**
118      * @see LoggableObject#countAsObsoleteWhenLogged
119      */

120     public boolean countAsObsoleteWhenLogged() {
121         return false;
122     }
123
124     /**
125      * @see LoggableObject#postLogWork
126      */

127     public void postLogWork(long justLoggedLsn)
128         throws DatabaseException {
129     }
130
131     /**
132      * A header is always a known size. Is public for unit testing.
133      */

134     public static int entrySize() {
135         return
136             LogUtils.getTimestampLogSize() + // time
137
LogUtils.UNSIGNED_INT_BYTES + // file number
138
LogUtils.LONG_BYTES + // lastEntryInPrevFileOffset
139
LogUtils.INT_BYTES; // logVersion
140
}
141     /**
142      * @see LoggableObject#getLogSize
143      * @return number of bytes used to store this object
144      */

145     public int getLogSize() {
146         return entrySize();
147     }
148
149     /**
150      * @see LoggableObject#writeToLog
151      * Serialize this object into the buffer. Update cksum with all
152      * the bytes used by this object
153      * @param logBuffer is the destination buffer
154      */

155     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
156         LogUtils.writeTimestamp(logBuffer, time);
157         LogUtils.writeUnsignedInt(logBuffer,fileNum);
158         LogUtils.writeLong(logBuffer, lastEntryInPrevFileOffset);
159         LogUtils.writeInt(logBuffer, logVersion);
160     }
161
162     /**
163      * @see LogReadable#readFromLog
164      * Initialize this object from the data in itemBuf.
165      * @param itemBuf the source buffer
166      */

167     public void readFromLog(ByteBuffer JavaDoc logBuffer, byte entryTypeVersion)
168     throws LogException {
169         time = LogUtils.readTimestamp(logBuffer);
170         fileNum = LogUtils.getUnsignedInt(logBuffer);
171         lastEntryInPrevFileOffset = LogUtils.readLong(logBuffer);
172         logVersion = LogUtils.readInt(logBuffer);
173         if (logVersion > LOG_VERSION) {
174             throw new LogException("Expected log version " + LOG_VERSION +
175                                    " or earlier but found " + logVersion +
176                                    " -- this version is not supported.");
177         }
178     }
179
180     /**
181      * @see LogReadable#dumpLog
182      * @param sb destination string buffer
183      * @param verbose if true, dump the full, verbose version
184      */

185     public void dumpLog(StringBuffer JavaDoc sb, boolean verbose) {
186         sb.append("<FileHeader num=\"0x");
187         sb.append(Long.toHexString(fileNum));
188         sb.append("\" lastEntryInPrevFileOffset=\"0x");
189         sb.append(Long.toHexString(lastEntryInPrevFileOffset));
190         sb.append("\" logVersion=\"0x");
191         sb.append(Integer.toHexString(logVersion));
192         sb.append("\" time=\"").append(time);
193         sb.append("\"/>");
194     }
195
196     /**
197      * @see LogReadable#logEntryIsTransactional
198      */

199     public boolean logEntryIsTransactional() {
200     return false;
201     }
202
203     /**
204      * @see LogReadable#getTransactionId
205      */

206     public long getTransactionId() {
207     return 0;
208     }
209
210     /**
211      * Print in xml format
212      */

213     public String JavaDoc toString() {
214         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
215         dumpLog(sb, true);
216         return sb.toString();
217     }
218 }
219
Popular Tags