1 21 22 package org.apache.derby.impl.store.raw.log; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import org.apache.derby.iapi.services.io.FormatIdUtil; 26 import org.apache.derby.iapi.services.io.StoredFormatIds; 27 import org.apache.derby.iapi.store.raw.log.LogInstant; 28 import org.apache.derby.iapi.store.access.DatabaseInstant; 29 import org.apache.derby.iapi.services.io.CompressedNumber; 30 31 import java.io.IOException ; 32 import java.io.ObjectInput ; 33 import java.io.ObjectOutput ; 34 35 46 public class LogCounter implements LogInstant { 47 48 61 62 63 public static final long INVALID_LOG_INSTANT = 0; 64 65 public static final long DERBY_10_0_MAX_LOGFILE_NUMBER = (long)0x003FFFFFL; public static final long MAX_LOGFILE_NUMBER = (long)0x7FFFFFFFL; private static final long FILE_NUMBER_SHIFT = 32; 71 72 public static final long MAX_LOGFILE_SIZE = (long)0x0FFFFFFFL; private static final long FILE_POSITION_MASK = (long)0x7FFFFFFFL; 76 77 private long fileNumber; 78 private long filePosition; 79 80 public LogCounter(long value) { 82 fileNumber = getLogFileNumber(value); 83 filePosition = getLogFilePosition(value); 84 } 85 86 public LogCounter(long fileNumber, long position) { 87 88 if (SanityManager.DEBUG) { 89 SanityManager.ASSERT(fileNumber > 0, "illegal fileNumber"); 90 SanityManager.ASSERT(position > 0, "illegal file position"); 91 92 SanityManager.ASSERT(position < MAX_LOGFILE_SIZE, 93 "log file position exceeded max log file size"); 94 SanityManager.ASSERT(fileNumber < MAX_LOGFILE_NUMBER, 95 "log file number exceeded max log file number"); 96 } 97 98 this.fileNumber = fileNumber; 99 this.filePosition = position; 100 } 101 102 105 public LogCounter() {} 106 107 111 112 static public final long makeLogInstantAsLong(long filenum, long filepos) 115 { 116 if (SanityManager.DEBUG) { 117 SanityManager.ASSERT(filenum > 0, "illegal fileNumber"); 118 SanityManager.ASSERT(filepos > 0, "illegal file position"); 119 120 SanityManager.ASSERT(filepos < MAX_LOGFILE_SIZE, 121 "log file position exceeded max log file size"); 122 SanityManager.ASSERT(filenum < MAX_LOGFILE_NUMBER, 123 "log file number exceeded max log file number"); 124 } 125 126 return ((filenum << FILE_NUMBER_SHIFT) | filepos); 127 } 128 129 130 static public final long getLogFilePosition(long valueAsLong) 131 { 132 return valueAsLong & FILE_POSITION_MASK; 133 } 134 135 static public final long getLogFileNumber(long valueAsLong) 136 { 137 return valueAsLong >>> FILE_NUMBER_SHIFT; 138 } 139 140 141 142 public boolean lessThan(DatabaseInstant other) { 143 LogCounter compare = (LogCounter)other; 144 145 return (fileNumber == compare.fileNumber) ? 146 filePosition < compare.filePosition : 147 fileNumber < compare.fileNumber; 148 } 149 150 public boolean equals(Object other) { 151 if (this == other) 152 return true; 153 154 if (!(other instanceof LogCounter)) 155 return false; 156 157 LogCounter compare = (LogCounter)other; 158 159 return fileNumber == compare.fileNumber && 160 filePosition == compare.filePosition; 161 } 162 163 public DatabaseInstant next() { 164 return new LogCounter( makeLogInstantAsLong(fileNumber, filePosition) + 1); 165 } 166 167 public DatabaseInstant prior() { 168 return new LogCounter( makeLogInstantAsLong(fileNumber, filePosition) - 1); 169 } 170 171 public int hashCode() { 172 return (int) (filePosition ^ fileNumber); 173 } 174 175 public String toString() { 176 return "(" + fileNumber + "," + filePosition + ")"; 177 } 178 179 public static String toDebugString(long instant) 180 { 181 if (SanityManager.DEBUG) 182 return "(" + getLogFileNumber(instant) + "," + getLogFilePosition(instant) + ")"; 183 else 184 return null; 185 } 186 187 192 public long getValueAsLong() { 193 return makeLogInstantAsLong(fileNumber, filePosition); 194 } 195 196 public long getLogFilePosition() 197 { 198 return filePosition; 199 } 200 201 public long getLogFileNumber() 202 { 203 return fileNumber; 204 } 205 206 207 210 211 216 public void readExternal(ObjectInput oi) throws IOException , ClassNotFoundException { 217 fileNumber = CompressedNumber.readLong(oi); 218 filePosition = CompressedNumber.readLong(oi); 219 } 220 221 225 public void writeExternal(ObjectOutput oo) throws IOException { 226 CompressedNumber.writeLong(oo,fileNumber); 227 CompressedNumber.writeLong(oo,filePosition); 228 } 229 230 235 public int getTypeFormatId() { return StoredFormatIds.LOG_COUNTER; } 236 237 } 238 | Popular Tags |