1 8 9 package com.sleepycat.je.log; 10 11 import java.nio.ByteBuffer ; 12 import java.sql.Timestamp ; 13 import java.util.Calendar ; 14 15 import com.sleepycat.je.DatabaseException; 16 17 21 public class FileHeader implements LoggableObject, LogReadable { 22 23 44 private static final int LOG_VERSION = 5; 45 46 50 private long fileNum; 51 private long lastEntryInPrevFileOffset; 52 private Timestamp time; 53 private int logVersion; 54 55 FileHeader(long fileNum, long lastEntryInPrevFileOffset) { 56 this.fileNum = fileNum; 57 this.lastEntryInPrevFileOffset = lastEntryInPrevFileOffset; 58 Calendar now = Calendar.getInstance(); 59 time = new Timestamp (now.getTimeInMillis()); 60 logVersion = LOG_VERSION; 61 } 62 63 66 public FileHeader() { 67 } 68 69 public int getLogVersion() { 70 return logVersion; 71 } 72 73 78 boolean validate(String 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 94 long getLastEntryInPrevFileOffset() { 95 return lastEntryInPrevFileOffset; 96 } 97 98 101 102 105 public LogEntryType getLogType() { 106 return LogEntryType.LOG_FILE_HEADER; 107 } 108 109 113 public boolean marshallOutsideWriteLatch() { 114 return true; 115 } 116 117 120 public boolean countAsObsoleteWhenLogged() { 121 return false; 122 } 123 124 127 public void postLogWork(long justLoggedLsn) 128 throws DatabaseException { 129 } 130 131 134 public static int entrySize() { 135 return 136 LogUtils.getTimestampLogSize() + LogUtils.UNSIGNED_INT_BYTES + LogUtils.LONG_BYTES + LogUtils.INT_BYTES; } 141 145 public int getLogSize() { 146 return entrySize(); 147 } 148 149 155 public void writeToLog(ByteBuffer logBuffer) { 156 LogUtils.writeTimestamp(logBuffer, time); 157 LogUtils.writeUnsignedInt(logBuffer,fileNum); 158 LogUtils.writeLong(logBuffer, lastEntryInPrevFileOffset); 159 LogUtils.writeInt(logBuffer, logVersion); 160 } 161 162 167 public void readFromLog(ByteBuffer 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 185 public void dumpLog(StringBuffer 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 199 public boolean logEntryIsTransactional() { 200 return false; 201 } 202 203 206 public long getTransactionId() { 207 return 0; 208 } 209 210 213 public String toString() { 214 StringBuffer sb = new StringBuffer (); 215 dumpLog(sb, true); 216 return sb.toString(); 217 } 218 } 219 | Popular Tags |