1 8 9 package com.sleepycat.je.util; 10 11 import java.io.File ; 12 import java.io.PrintStream ; 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.logging.Level ; 17 18 import com.sleepycat.je.Database; 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.DatabaseStats; 22 import com.sleepycat.je.DbInternal; 23 import com.sleepycat.je.Environment; 24 import com.sleepycat.je.EnvironmentConfig; 25 import com.sleepycat.je.JEVersion; 26 import com.sleepycat.je.VerifyConfig; 27 import com.sleepycat.je.cleaner.VerifyUtils; 28 import com.sleepycat.je.dbi.DatabaseImpl; 29 import com.sleepycat.je.dbi.DbTree; 30 import com.sleepycat.je.dbi.EnvironmentImpl; 31 import com.sleepycat.je.utilint.CmdUtil; 32 import com.sleepycat.je.utilint.Tracer; 33 34 public class DbVerify { 35 private static final String usageString = 36 "usage: " + CmdUtil.getJavaCommand(DbVerify.class) + "\n" + 37 " -h <dir> # environment home directory\n" + 38 " [-c ] # check cleaner metadata\n" + 39 " [-q ] # quiet, exit with success or failure\n" + 40 " [-s <databaseName> ] # database to verify\n" + 41 " [-v <interval>] # progress notification interval\n" + 42 " [-V] # print JE version number"; 43 44 protected File envHome = null; 45 protected Environment env; 46 protected String dbName = null; 47 protected boolean quiet = false; 48 protected boolean checkLsns = false; 49 protected boolean openReadOnly = true; 50 private boolean doClose; 51 52 private int progressInterval = 0; 53 54 static public void main(String argv[]) 55 throws DatabaseException { 56 57 DbVerify verifier = new DbVerify(); 58 verifier.parseArgs(argv); 59 60 boolean ret = false; 61 try { 62 ret = verifier.verify(System.err); 63 } catch (Throwable T) { 64 if (!verifier.quiet) { 65 T.printStackTrace(System.err); 66 } 67 } finally { 68 69 verifier.closeEnv(); 70 71 79 if ((!verifier.quiet) || (verifier.progressInterval > 0)) { 80 System.err.println("Exit status = " + ret); 81 } 82 83 System.exit(ret ? 0 : -1); 84 } 85 } 86 87 DbVerify() { 88 doClose = true; 89 } 90 91 public DbVerify(Environment env, 92 String dbName, 93 boolean quiet) { 94 this.env = env; 95 this.dbName = dbName; 96 this.quiet = quiet; 97 doClose = false; 98 } 99 100 protected void printUsage(String msg) { 101 System.err.println(msg); 102 System.err.println(usageString); 103 System.exit(-1); 104 } 105 106 protected void parseArgs(String argv[]) { 107 108 int argc = 0; 109 int nArgs = argv.length; 110 while (argc < nArgs) { 111 String thisArg = argv[argc++]; 112 if (thisArg.equals("-q")) { 113 quiet = true; 114 } else if (thisArg.equals("-V")) { 115 System.out.println(JEVersion.CURRENT_VERSION); 116 System.exit(0); 117 } else if (thisArg.equals("-h")) { 118 if (argc < nArgs) { 119 envHome = new File (argv[argc++]); 120 } else { 121 printUsage("-h requires an argument"); 122 } 123 } else if (thisArg.equals("-s")) { 124 if (argc < nArgs) { 125 dbName = argv[argc++]; 126 } else { 127 printUsage("-s requires an argument"); 128 } 129 } else if (thisArg.equals("-v")) { 130 if (argc < nArgs) { 131 progressInterval = Integer.parseInt(argv[argc++]); 132 if (progressInterval <= 0) { 133 printUsage("-v requires a positive argument"); 134 } 135 } else { 136 printUsage("-v requires an argument"); 137 } 138 } else if (thisArg.equals("-c")) { 139 checkLsns = true; 140 } else if (thisArg.equals("-rw")) { 141 142 150 openReadOnly = false; 151 } 152 } 153 154 if (envHome == null) { 155 printUsage("-h is a required argument"); 156 } 157 } 158 159 protected void openEnv() 160 throws DatabaseException { 161 162 if (env == null) { 163 EnvironmentConfig envConfig = new EnvironmentConfig(); 164 envConfig.setReadOnly(openReadOnly); 165 env = new Environment(envHome, envConfig); 166 } 167 } 168 169 void closeEnv() 170 throws DatabaseException { 171 172 try { 173 if (env != null) { 174 env.close(); 175 } 176 } finally { 177 env = null; 178 } 179 } 180 181 public boolean verify(PrintStream out) 182 throws DatabaseException { 183 184 boolean ret = true; 185 try { 186 VerifyConfig verifyConfig = new VerifyConfig(); 187 verifyConfig.setPrintInfo(!quiet); 188 if (progressInterval > 0) { 189 verifyConfig.setShowProgressInterval(progressInterval); 190 verifyConfig.setShowProgressStream(out); 191 } 192 193 openEnv(); 194 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 195 196 197 List dbNameList = null; 198 List internalDbs = null; 199 DbTree dbMapTree = envImpl.getDbMapTree(); 200 201 if (dbName == null) { 202 dbNameList = env.getDatabaseNames(); 203 204 dbNameList.addAll(dbMapTree.getInternalDbNames()); 205 internalDbs = dbMapTree.getInternalNoLookupDbNames(); 206 } else { 207 dbNameList = new ArrayList (); 208 dbNameList.add(dbName); 209 internalDbs = new ArrayList (); 210 } 211 212 213 Iterator iter = dbNameList.iterator(); 214 while (iter.hasNext()) { 215 String targetDb = (String ) iter.next(); 216 Tracer.trace(Level.INFO, envImpl, 217 "DbVerify.verify of " + targetDb + " starting"); 218 219 DatabaseConfig dbConfig = new DatabaseConfig(); 220 dbConfig.setReadOnly(true); 221 dbConfig.setAllowCreate(false); 222 DbInternal.setUseExistingConfig(dbConfig, true); 223 Database db = env.openDatabase(null, targetDb, dbConfig); 224 225 try { 226 if (!verifyOneDbImpl(DbInternal.dbGetDatabaseImpl(db), 227 targetDb, 228 verifyConfig, 229 out)) { 230 ret = false; 231 } 232 } finally { 233 if (db != null) { 234 db.close(); 235 } 236 Tracer.trace(Level.INFO, envImpl, 237 "DbVerify.verify of " + targetDb + " ending"); 238 } 239 } 240 241 245 iter = internalDbs.iterator(); 246 while (iter.hasNext()) { 247 String targetDb = (String ) iter.next(); 248 Tracer.trace(Level.INFO, envImpl, 249 "DbVerify.verify of " + targetDb + " starting"); 250 251 try { 252 DatabaseImpl dbImpl = dbMapTree.getDb(null, targetDb, 253 null); 254 if (!verifyOneDbImpl(dbImpl, targetDb, 255 verifyConfig, out)) { 256 ret = false; 257 } 258 } finally { 259 Tracer.trace(Level.INFO, envImpl, 260 "DbVerify.verify of " + targetDb + " ending"); 261 } 262 } 263 264 if (doClose) { 265 closeEnv(); 266 } 267 } catch (DatabaseException DE) { 268 ret = false; 269 try { 270 closeEnv(); 271 } catch (Throwable ignored) { 272 273 277 } 278 throw DE; 279 } 280 281 return ret; 282 } 283 284 private boolean verifyOneDbImpl(DatabaseImpl dbImpl, 285 String name, 286 VerifyConfig verifyConfig, 287 PrintStream out) 288 throws DatabaseException { 289 boolean status = true; 290 291 if (verifyConfig.getPrintInfo()) { 292 out.println("Verifying database " + name); 293 } 294 295 299 if (verifyConfig.getPrintInfo()) { 300 out.println("Checking tree for " + name); 301 } 302 DatabaseStats stats = dbImpl.getEmptyStats(); 303 status = dbImpl.verify(verifyConfig, stats); 304 if (verifyConfig.getPrintInfo()) { 305 309 out.print(stats); 310 } 311 312 313 if (verifyConfig.getPrintInfo()) { 314 out.println("Checking obsolete offsets for " + name); 315 } 316 try { 317 VerifyUtils.checkLsns(dbImpl, out); 318 } catch (DatabaseException e) { 319 if (verifyConfig.getPrintInfo()) { 320 out.println("Problem from checkLsns:" + e); 321 } 322 status = false; 323 } 324 if (verifyConfig.getPrintInfo()) { 325 out.println(); 326 } 327 return status; 328 } 329 } 330 | Popular Tags |