1 8 9 package com.sleepycat.je.tree; 10 11 import java.io.UnsupportedEncodingException ; 12 import java.nio.ByteBuffer ; 13 14 import com.sleepycat.je.DatabaseException; 15 import com.sleepycat.je.cleaner.FileSummary; 16 import com.sleepycat.je.cleaner.PackedOffsets; 17 import com.sleepycat.je.cleaner.TrackedFileSummary; 18 import com.sleepycat.je.dbi.DatabaseImpl; 19 import com.sleepycat.je.log.LogEntryType; 20 import com.sleepycat.je.log.LogException; 21 import com.sleepycat.je.log.LogUtils; 22 import com.sleepycat.je.log.LoggableObject; 23 24 72 public final class FileSummaryLN extends LN { 73 74 private static final String BEGIN_TAG = "<fileSummaryLN>"; 75 private static final String END_TAG = "</fileSummaryLN>"; 76 77 private FileSummary baseSummary; 78 private TrackedFileSummary trackedSummary; 79 private PackedOffsets obsoleteOffsets; 80 private boolean needOffsets; 81 private byte logVersion; 82 83 86 public FileSummaryLN(FileSummary baseSummary) { 87 super(new byte[0]); 88 assert baseSummary != null; 89 this.baseSummary = baseSummary; 90 obsoleteOffsets = new PackedOffsets(); 91 logVersion = -1; 92 } 93 94 97 public FileSummaryLN() 98 throws DatabaseException { 99 baseSummary = new FileSummary(); 100 obsoleteOffsets = new PackedOffsets(); 101 } 102 103 107 public void setTrackedSummary(TrackedFileSummary trackedSummary) { 108 this.trackedSummary = trackedSummary; 109 needOffsets = true; 110 } 111 112 116 public TrackedFileSummary getTrackedSummary() { 117 return trackedSummary; 118 } 119 120 123 public FileSummary getBaseSummary() { 124 return baseSummary; 125 } 126 127 130 public PackedOffsets getObsoleteOffsets() { 131 return obsoleteOffsets; 132 } 133 134 161 public boolean hasStringKey(byte[] bytes) { 162 163 if (logVersion == 0 || bytes.length != 8) { 164 return true; 165 } else { 166 return (bytes[4] >= '0' && bytes[4] <= '9'); 167 } 168 } 169 170 174 public long getFileNumber(byte[] bytes) { 175 176 if (hasStringKey(bytes)) { 177 try { 178 return Long.valueOf(new String (bytes, "UTF-8")).longValue(); 179 } catch (UnsupportedEncodingException shouldNeverHappen) { 180 assert false: shouldNeverHappen; 181 return 0; 182 } 183 } else { 184 ByteBuffer buf = ByteBuffer.wrap(bytes); 185 return LogUtils.readIntMSB(buf) & 0xFFFFFFFFL; 186 } 187 } 188 189 193 public static byte[] makePartialKey(long fileNum) { 194 195 byte[] bytes = new byte[4]; 196 ByteBuffer buf = ByteBuffer.wrap(bytes); 197 198 LogUtils.writeIntMSB(buf, (int) fileNum); 199 200 return bytes; 201 } 202 203 210 public static byte[] makeFullKey(long fileNum, int sequence) { 211 212 assert sequence >= 0; 213 214 byte[] bytes = new byte[8]; 215 ByteBuffer buf = ByteBuffer.wrap(bytes); 216 217 222 LogUtils.writeIntMSB(buf, (int) fileNum); 223 LogUtils.writeIntMSB(buf, Integer.MAX_VALUE - sequence); 224 225 return bytes; 226 } 227 228 233 public void postFetchInit(DatabaseImpl db, long sourceLsn) 234 throws DatabaseException { 235 236 super.postFetchInit(db, sourceLsn); 237 238 if (logVersion == 1 && 239 db.getDbEnvironment().getUtilizationProfile().isRMWFixEnabled()) { 240 obsoleteOffsets = new PackedOffsets(); 241 } 242 } 243 244 247 248 public String toString() { 249 return dumpString(0, true); 250 } 251 252 public String beginTag() { 253 return BEGIN_TAG; 254 } 255 256 public String endTag() { 257 return END_TAG; 258 } 259 260 public String dumpString(int nSpaces, boolean dumpTags) { 261 StringBuffer sb = new StringBuffer (); 262 sb.append(super.dumpString(nSpaces, dumpTags)); 263 sb.append('\n'); 264 if (!isDeleted()) { 265 sb.append(baseSummary.toString()); 266 sb.append(obsoleteOffsets.toString()); 267 } 268 return sb.toString(); 269 } 270 271 275 protected void dumpLogAdditional(StringBuffer sb, boolean verbose) { 276 if (!isDeleted()) { 277 baseSummary.dumpLog(sb, true); 278 if (verbose) { 279 obsoleteOffsets.dumpLog(sb, true); 280 } 281 } 282 } 283 284 287 288 291 protected LogEntryType getTransactionalLogType() { 292 assert false : "Txnl access to UP db not allowed"; 293 return LogEntryType.LOG_FILESUMMARYLN; 294 } 295 296 299 public LogEntryType getLogType() { 300 return LogEntryType.LOG_FILESUMMARYLN; 301 } 302 303 309 public boolean marshallOutsideWriteLatch() { 310 return false; 311 } 312 313 316 public boolean countAsObsoleteWhenLogged() { 317 return false; 318 } 319 320 323 public int getLogSize() { 324 int size = super.getLogSize(); 325 if (!isDeleted()) { 326 size += baseSummary.getLogSize(); 327 getOffsets(); 328 size += obsoleteOffsets.getLogSize(); 329 } 330 return size; 331 } 332 333 336 public void writeToLog(ByteBuffer logBuffer) { 337 338 343 if (trackedSummary != null) { 344 345 baseSummary.add(trackedSummary); 346 347 if (!isDeleted()) { 348 getOffsets(); 349 } 350 351 352 trackedSummary.reset(); 353 } 354 355 super.writeToLog(logBuffer); 356 357 if (!isDeleted()) { 358 baseSummary.writeToLog(logBuffer); 359 obsoleteOffsets.writeToLog(logBuffer); 360 } 361 } 362 363 366 public void readFromLog(ByteBuffer itemBuffer, byte entryTypeVersion) 367 throws LogException { 368 369 super.readFromLog(itemBuffer, entryTypeVersion); 370 371 logVersion = entryTypeVersion; 372 373 if (!isDeleted()) { 374 baseSummary.readFromLog(itemBuffer, entryTypeVersion); 375 if (entryTypeVersion > 0) { 376 obsoleteOffsets.readFromLog(itemBuffer, entryTypeVersion); 377 } 378 } 379 } 380 381 385 private void getOffsets() { 386 if (needOffsets) { 387 long[] offsets = trackedSummary.getObsoleteOffsets(); 388 if (offsets != null) { 389 obsoleteOffsets.pack(offsets); 390 } 391 needOffsets = false; 392 } 393 } 394 } 395 | Popular Tags |