1 8 9 package com.sleepycat.je.log; 10 11 import java.io.IOException ; 12 import java.nio.ByteBuffer ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import com.sleepycat.je.DatabaseException; 17 import com.sleepycat.je.cleaner.TrackedFileSummary; 18 import com.sleepycat.je.cleaner.UtilizationTracker; 19 import com.sleepycat.je.dbi.DatabaseId; 20 import com.sleepycat.je.dbi.DbTree; 21 import com.sleepycat.je.dbi.EnvironmentImpl; 22 import com.sleepycat.je.log.entry.INContainingEntry; 23 import com.sleepycat.je.log.entry.INLogEntry; 24 import com.sleepycat.je.log.entry.LNLogEntry; 25 import com.sleepycat.je.log.entry.LogEntry; 26 import com.sleepycat.je.log.entry.NodeLogEntry; 27 import com.sleepycat.je.tree.FileSummaryLN; 28 import com.sleepycat.je.tree.IN; 29 import com.sleepycat.je.tree.INDeleteInfo; 30 import com.sleepycat.je.tree.INDupDeleteInfo; 31 import com.sleepycat.je.tree.MapLN; 32 import com.sleepycat.je.utilint.DbLsn; 33 34 45 public class INFileReader extends FileReader { 46 47 48 private boolean lastEntryWasDelete; 49 private boolean lastEntryWasDupDelete; 50 private LogEntryType fromLogType; 51 private boolean isProvisional; 52 53 58 private Map targetEntryMap; 59 private LogEntry targetLogEntry; 60 61 68 private Map dbIdTrackingMap; 69 private LNLogEntry dbIdTrackingEntry; 70 private Map txnIdTrackingMap; 71 private LNLogEntry txnIdTrackingEntry; 72 private Map otherNodeTrackingMap; 73 private NodeLogEntry nodeTrackingEntry; 74 private INLogEntry inTrackingEntry; 75 private LNLogEntry fsTrackingEntry; 76 77 82 private boolean trackIds; 83 private long maxNodeId; 84 private int maxDbId; 85 private long maxTxnId; 86 private boolean mapDbOnly; 87 88 89 private long partialCkptStart; 90 private UtilizationTracker tracker; 91 private Map fileSummaryLsns; 92 93 96 public INFileReader(EnvironmentImpl env, 97 int readBufferSize, 98 long startLsn, 99 long finishLsn, 100 boolean trackIds, 101 boolean mapDbOnly, 102 long partialCkptStart, 103 Map fileSummaryLsns) 104 throws IOException , DatabaseException { 105 106 super(env, readBufferSize, true, startLsn, null, 107 DbLsn.NULL_LSN, finishLsn); 108 109 this.trackIds = trackIds; 110 this.mapDbOnly = mapDbOnly; 111 targetEntryMap = new HashMap (); 112 113 if (trackIds) { 114 maxNodeId = 0; 115 maxDbId = 0; 116 tracker = env.getUtilizationTracker(); 117 this.partialCkptStart = partialCkptStart; 118 this.fileSummaryLsns = fileSummaryLsns; 119 fsTrackingEntry = (LNLogEntry) 120 LogEntryType.LOG_FILESUMMARYLN.getNewLogEntry(); 121 122 dbIdTrackingMap = new HashMap (); 123 txnIdTrackingMap = new HashMap (); 124 otherNodeTrackingMap = new HashMap (); 125 126 dbIdTrackingMap.put(LogEntryType.LOG_MAPLN_TRANSACTIONAL, 127 LogEntryType.LOG_MAPLN_TRANSACTIONAL. 128 getNewLogEntry()); 129 dbIdTrackingMap.put(LogEntryType.LOG_MAPLN, 130 LogEntryType.LOG_MAPLN.getNewLogEntry()); 131 txnIdTrackingMap.put(LogEntryType.LOG_LN_TRANSACTIONAL, 132 LogEntryType.LOG_LN_TRANSACTIONAL. 133 getNewLogEntry()); 134 txnIdTrackingMap.put(LogEntryType.LOG_MAPLN_TRANSACTIONAL, 135 LogEntryType.LOG_MAPLN_TRANSACTIONAL. 136 getNewLogEntry()); 137 txnIdTrackingMap.put(LogEntryType.LOG_NAMELN_TRANSACTIONAL, 138 LogEntryType.LOG_NAMELN_TRANSACTIONAL. 139 getNewLogEntry()); 140 txnIdTrackingMap.put(LogEntryType.LOG_DEL_DUPLN_TRANSACTIONAL, 141 LogEntryType.LOG_DEL_DUPLN_TRANSACTIONAL. 142 getNewLogEntry()); 143 txnIdTrackingMap.put(LogEntryType.LOG_DUPCOUNTLN_TRANSACTIONAL, 144 LogEntryType.LOG_DUPCOUNTLN_TRANSACTIONAL. 145 getNewLogEntry()); 146 } 147 } 148 149 152 public void addTargetType(LogEntryType entryType) 153 throws DatabaseException { 154 155 targetEntryMap.put(entryType, entryType.getNewLogEntry()); 156 } 157 158 163 protected boolean isTargetEntry(byte entryTypeNum, 164 byte entryTypeVersion) 165 throws DatabaseException { 166 167 lastEntryWasDelete = false; 168 lastEntryWasDupDelete = false; 169 targetLogEntry = null; 170 dbIdTrackingEntry = null; 171 txnIdTrackingEntry = null; 172 nodeTrackingEntry = null; 173 inTrackingEntry = null; 174 fsTrackingEntry = null; 175 isProvisional = LogEntryType.isProvisional(entryTypeVersion); 176 177 178 fromLogType = LogEntryType.findType(entryTypeNum, entryTypeVersion); 179 LogEntry possibleTarget = (LogEntry) targetEntryMap.get(fromLogType); 180 181 185 if (!isProvisional) { 186 targetLogEntry = possibleTarget; 187 } 188 189 190 if (LogEntryType.LOG_IN_DELETE_INFO.equals(fromLogType)) { 191 lastEntryWasDelete = true; 192 } 193 194 if (LogEntryType.LOG_IN_DUPDELETE_INFO.equals(fromLogType)) { 195 lastEntryWasDupDelete = true; 196 } 197 198 if (trackIds) { 199 200 204 if (!isProvisional) { 205 dbIdTrackingEntry = (LNLogEntry) 206 dbIdTrackingMap.get(fromLogType); 207 txnIdTrackingEntry = (LNLogEntry) 208 txnIdTrackingMap.get(fromLogType); 209 } 210 211 215 if (fromLogType.isNodeType()) { 216 if (possibleTarget != null) { 217 nodeTrackingEntry = (NodeLogEntry) possibleTarget; 218 } else if (dbIdTrackingEntry != null) { 219 nodeTrackingEntry = dbIdTrackingEntry; 220 } else if (txnIdTrackingEntry != null) { 221 nodeTrackingEntry = txnIdTrackingEntry; 222 } else { 223 nodeTrackingEntry = (NodeLogEntry) 224 otherNodeTrackingMap.get(fromLogType); 225 if (nodeTrackingEntry == null) { 226 nodeTrackingEntry = (NodeLogEntry) 227 fromLogType.getNewLogEntry(); 228 otherNodeTrackingMap.put(fromLogType, 229 nodeTrackingEntry); 230 } 231 } 232 if (nodeTrackingEntry instanceof INLogEntry) { 233 inTrackingEntry = (INLogEntry) nodeTrackingEntry; 234 } 235 if (LogEntryType.LOG_FILESUMMARYLN.equals(fromLogType)) { 236 fsTrackingEntry = (LNLogEntry) nodeTrackingEntry; 237 } 238 } 239 240 244 if (!LogEntryType.LOG_FILE_HEADER.equals(fromLogType)) { 245 tracker.countNewLogEntry(getLastLsn(), fromLogType, 246 LogManager.HEADER_BYTES + 247 currentEntrySize); 248 } 249 250 255 return (targetLogEntry != null) || 256 (dbIdTrackingEntry != null) || 257 (txnIdTrackingEntry != null) || 258 (nodeTrackingEntry != null); 259 } else { 260 261 266 return (targetLogEntry != null); 267 } 268 } 269 270 274 protected boolean processEntry(ByteBuffer entryBuffer) 275 throws DatabaseException { 276 277 boolean useEntry = false; 278 boolean entryLoaded = false; 279 280 281 if (targetLogEntry != null) { 282 targetLogEntry.readEntry(entryBuffer, currentEntrySize, 283 currentEntryTypeVersion, true); 284 DatabaseId dbId = getDatabaseId(); 285 boolean isMapDb = dbId.equals(DbTree.ID_DB_ID); 286 useEntry = (!mapDbOnly || isMapDb); 287 entryLoaded = true; 288 } 289 290 291 if (trackIds) { 292 293 300 LNLogEntry lnEntry = null; 301 if (dbIdTrackingEntry != null) { 302 303 lnEntry = dbIdTrackingEntry; 304 lnEntry.readEntry(entryBuffer, currentEntrySize, 305 currentEntryTypeVersion, 306 true ); 307 entryLoaded = true; 308 MapLN mapLN = (MapLN) lnEntry.getMainItem(); 309 int dbId = mapLN.getDatabase().getId().getId(); 310 if (dbId > maxDbId) { 311 maxDbId = dbId; 312 } 313 } 314 if (txnIdTrackingEntry != null) { 315 316 if (lnEntry == null) { 317 lnEntry = txnIdTrackingEntry; 318 lnEntry.readEntry(entryBuffer, currentEntrySize, 319 currentEntryTypeVersion, 320 true ); 321 entryLoaded = true; 322 } 323 long txnId = lnEntry.getTxnId().longValue(); 324 if (txnId > maxTxnId) { 325 maxTxnId = txnId; 326 } 327 } 328 329 333 if (fsTrackingEntry != null) { 334 335 336 if (!entryLoaded) { 337 nodeTrackingEntry.readEntry(entryBuffer, currentEntrySize, 338 currentEntryTypeVersion, 339 true ); 340 entryLoaded = true; 341 } 342 343 348 byte[] keyBytes = fsTrackingEntry.getKey(); 349 FileSummaryLN fsln = 350 (FileSummaryLN) fsTrackingEntry.getMainItem(); 351 long fileNum = fsln.getFileNumber(keyBytes); 352 TrackedFileSummary trackedLN = tracker.getTrackedFile(fileNum); 353 if (trackedLN != null) { 354 trackedLN.reset(); 355 } 356 357 358 fileSummaryLsns.put(new Long (fileNum), new Long (getLastLsn())); 359 360 365 } 366 367 371 if (nodeTrackingEntry != null) { 372 if (!entryLoaded) { 373 nodeTrackingEntry.readEntry(entryBuffer, currentEntrySize, 374 currentEntryTypeVersion, 375 false ); 376 entryLoaded = true; 377 } 378 379 long nodeId = nodeTrackingEntry.getNodeId(); 380 maxNodeId = (nodeId > maxNodeId) ? nodeId: maxNodeId; 381 } 382 383 if (inTrackingEntry != null) { 384 assert entryLoaded : "All nodes should have been loaded"; 385 386 393 long oldLsn = inTrackingEntry.getObsoleteLsn(); 394 if (oldLsn != DbLsn.NULL_LSN) { 395 long newLsn = getLastLsn(); 396 if (!isObsoleteLsnAlreadyCounted(oldLsn, newLsn)) { 397 tracker.countObsoleteNodeInexact 398 (oldLsn, fromLogType, 0); 399 } 400 } 401 402 413 if (isProvisional && partialCkptStart != DbLsn.NULL_LSN) { 414 oldLsn = getLastLsn(); 415 if (DbLsn.compareTo(partialCkptStart, oldLsn) < 0) { 416 tracker.countObsoleteNodeInexact 417 (oldLsn, fromLogType, 0); 418 } 419 } 420 } 421 } 422 423 424 return useEntry; 425 } 426 427 432 private boolean isObsoleteLsnAlreadyCounted(long oldLsn, long newLsn) { 433 434 435 Long fileNum = new Long (DbLsn.getFileNumber(oldLsn)); 436 long fileSummaryLsn = 437 DbLsn.longToLsn((Long ) fileSummaryLsns.get(fileNum)); 438 int cmpFsLsnToNewLsn = (fileSummaryLsn != DbLsn.NULL_LSN) ? 439 DbLsn.compareTo(fileSummaryLsn, newLsn) : -1; 440 return (cmpFsLsnToNewLsn >= 0); 441 } 442 443 446 public IN getIN() 447 throws DatabaseException { 448 449 return ((INContainingEntry) targetLogEntry).getIN(env); 450 } 451 452 455 public DatabaseId getDatabaseId() { 456 if (lastEntryWasDelete) { 457 return ((INDeleteInfo) targetLogEntry.getMainItem()). 458 getDatabaseId(); 459 } else if (lastEntryWasDupDelete) { 460 return ((INDupDeleteInfo) targetLogEntry.getMainItem()). 461 getDatabaseId(); 462 } else { 463 return ((INContainingEntry) targetLogEntry).getDbId(); 464 } 465 } 466 467 470 public long getMaxNodeId() { 471 return maxNodeId; 472 } 473 474 477 public int getMaxDbId() { 478 return maxDbId; 479 } 480 481 484 public long getMaxTxnId() { 485 return maxTxnId; 486 } 487 488 491 public boolean isDeleteInfo() { 492 return lastEntryWasDelete; 493 } 494 495 498 public boolean isDupDeleteInfo() { 499 return lastEntryWasDupDelete; 500 } 501 502 505 public long getDeletedNodeId() { 506 return ((INDeleteInfo) 507 targetLogEntry.getMainItem()).getDeletedNodeId(); 508 } 509 510 513 public byte[] getDeletedIdKey() { 514 return ((INDeleteInfo) 515 targetLogEntry.getMainItem()).getDeletedIdKey(); 516 } 517 518 521 public long getDupDeletedNodeId() { 522 return ((INDupDeleteInfo) 523 targetLogEntry.getMainItem()).getDeletedNodeId(); 524 } 525 526 529 public byte[] getDupDeletedMainKey() { 530 return ((INDupDeleteInfo) 531 targetLogEntry.getMainItem()).getDeletedMainKey(); 532 } 533 534 537 public byte[] getDupDeletedDupKey() { 538 return ((INDupDeleteInfo) 539 targetLogEntry.getMainItem()).getDeletedDupKey(); 540 } 541 542 547 public long getLsnOfIN() { 548 return ((INContainingEntry) targetLogEntry).getLsnOfIN(getLastLsn()); 549 } 550 551 554 public LogEntryType getLogEntryType() { 555 return LogEntryType.findType(currentEntryTypeNum, 556 currentEntryTypeVersion); 557 } 558 } 559 | Popular Tags |