1 8 9 package com.sleepycat.je.cleaner; 10 11 import java.io.PrintStream ; 12 import java.util.ArrayList ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.DbInternal; 21 import com.sleepycat.je.dbi.DatabaseImpl; 22 import com.sleepycat.je.dbi.SortedLSNTreeWalker; 23 import com.sleepycat.je.dbi.SortedLSNTreeWalker.TreeNodeProcessor; 24 import com.sleepycat.je.log.LogEntryType; 25 import com.sleepycat.je.tree.Node; 26 import com.sleepycat.je.utilint.DbLsn; 27 28 31 public class VerifyUtils { 32 33 private static final boolean DEBUG = false; 34 35 41 public static void checkLsns(Database db) 42 throws DatabaseException { 43 checkLsns(DbInternal.dbGetDatabaseImpl(db), System.out); 44 } 45 46 52 public static void checkLsns(DatabaseImpl dbImpl, 53 PrintStream out) 54 throws DatabaseException { 55 56 57 GatherLSNs gatherLsns = new GatherLSNs(); 58 long rootLsn = dbImpl.getTree().getRootLsn(); 59 List savedExceptions = new ArrayList (); 60 61 SortedLSNTreeWalker walker = 62 new SortedLSNTreeWalker(dbImpl, 63 false, false, rootLsn, 66 gatherLsns, 67 savedExceptions, 68 null); 69 walker.walk(); 70 71 72 if (savedExceptions.size() > 0) { 73 out.println(savedExceptions.size() + 74 " problems seen during tree walk for checkLsns"); 75 Iterator iter = savedExceptions.iterator(); 76 while (iter.hasNext()) { 77 Exception e = (Exception ) iter.next(); 78 out.println(" " + e); 79 } 80 } 81 82 Set lsnsInTree = gatherLsns.getLsns(); 83 lsnsInTree.add(new Long (rootLsn)); 84 85 86 Iterator iter = lsnsInTree.iterator(); 87 Set fileNums = new HashSet (); 88 89 while (iter.hasNext()) { 90 long lsn = ((Long ) iter.next()).longValue(); 91 fileNums.add(new Long (DbLsn.getFileNumber(lsn))); 92 } 93 94 95 iter = fileNums.iterator(); 96 Set obsoleteLsns = new HashSet (); 97 UtilizationProfile profile = 98 dbImpl.getDbEnvironment().getUtilizationProfile(); 99 100 while (iter.hasNext()) { 101 Long fileNum = (Long ) iter.next(); 102 103 PackedOffsets obsoleteOffsets = new PackedOffsets(); 104 TrackedFileSummary tfs = 105 profile.getObsoleteDetail(fileNum, 106 obsoleteOffsets, 107 false ); 108 PackedOffsets.Iterator obsoleteIter = obsoleteOffsets.iterator(); 109 while (obsoleteIter.hasNext()) { 110 long offset = obsoleteIter.next(); 111 Long oneLsn = new Long (DbLsn.makeLsn(fileNum.longValue(), 112 offset)); 113 obsoleteLsns.add(oneLsn); 114 if (DEBUG) { 115 out.println("Adding 0x" + 116 Long.toHexString(oneLsn.longValue())); 117 } 118 } 119 } 120 121 122 boolean error = false; 123 iter = lsnsInTree.iterator(); 124 while (iter.hasNext()) { 125 Long lsn = (Long ) iter.next(); 126 if (obsoleteLsns.contains(lsn)) { 127 out.println("Obsolete LSN set contains valid LSN " + 128 DbLsn.getNoFormatString(lsn.longValue())); 129 error = true; 130 } 131 } 132 133 137 iter = obsoleteLsns.iterator(); 138 while (iter.hasNext()) { 139 Long lsn = (Long ) iter.next(); 140 if (lsnsInTree.contains(lsn)) { 141 out.println("Tree contains obsolete LSN " + 142 DbLsn.getNoFormatString(lsn.longValue())); 143 error = true; 144 } 145 } 146 147 if (error) { 148 throw new DatabaseException("Lsn mismatch"); 149 } 150 151 if (savedExceptions.size() > 0) { 152 throw new DatabaseException("Sorted LSN Walk problem"); 153 } 154 } 155 156 private static class GatherLSNs implements TreeNodeProcessor { 157 private Set lsns = new HashSet (); 158 159 public void processLSN(long childLSN, 160 LogEntryType childType, 161 Node ignore, 162 byte[] ignore2) 163 throws DatabaseException { 164 165 lsns.add(new Long (childLSN)); 166 } 167 168 169 public void processDupCount(long ignore) { 170 } 171 172 public Set getLsns() { 173 return lsns; 174 } 175 } 176 } 177 | Popular Tags |