KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > DbVerify


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbVerify.java,v 1.42 2006/11/28 13:52:06 mark Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.File JavaDoc;
12 import java.io.PrintStream JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.logging.Level JavaDoc;
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 JavaDoc 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 JavaDoc envHome = null;
45     protected Environment env;
46     protected String JavaDoc 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 JavaDoc 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 JavaDoc T) {
64         if (!verifier.quiet) {
65         T.printStackTrace(System.err);
66         }
67     } finally {
68
69         verifier.closeEnv();
70
71             /*
72              * Show the status, only omit if the user asked for a quiet
73              * run and didn't specify a progress interval, in which case
74              * we can assume that they really don't want any status output.
75              *
76              * If the user runs this from the command line, presumably they'd
77              * like to see the status.
78              */

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 JavaDoc 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 JavaDoc msg) {
101     System.err.println(msg);
102     System.err.println(usageString);
103     System.exit(-1);
104     }
105
106     protected void parseArgs(String JavaDoc argv[]) {
107
108     int argc = 0;
109     int nArgs = argv.length;
110     while (argc < nArgs) {
111         String JavaDoc 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 JavaDoc(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                 /*
143                  * Unadvertised option. Open the environment read/write
144                  * so that a checkLsns pass gets an accurate root LSN to
145                  * start from in the event that a recovery split the root.
146                  * A read/only environment open will keep any logging in
147                  * the log buffers, and the LSNs stored in the INs will
148                  * be converted to DbLsn.NULL_LSN.
149                  */

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 JavaDoc 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             /* If no database is specified, verify all. */
197             List JavaDoc dbNameList = null;
198             List JavaDoc 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 JavaDoc();
208                 dbNameList.add(dbName);
209                 internalDbs = new ArrayList JavaDoc();
210             }
211             
212             /* Check application data. */
213             Iterator JavaDoc iter = dbNameList.iterator();
214             while (iter.hasNext()) {
215                 String JavaDoc targetDb = (String JavaDoc) 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             /*
242              * Check internal databases, which don't have to be opened
243              * through a Database handle.
244              */

245             iter = internalDbs.iterator();
246             while (iter.hasNext()) {
247                 String JavaDoc targetDb = (String JavaDoc) 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 JavaDoc ignored) {
272
273         /*
274          * Klockwork - ok
275          * Don't say anything about exceptions here.
276          */

277         }
278         throw DE;
279         }
280
281     return ret;
282     }
283
284     private boolean verifyOneDbImpl(DatabaseImpl dbImpl,
285                                     String JavaDoc name,
286                                     VerifyConfig verifyConfig,
287                                     PrintStream JavaDoc out)
288         throws DatabaseException {
289         boolean status = true;
290         
291         if (verifyConfig.getPrintInfo()) {
292             out.println("Verifying database " + name);
293         }
294
295         /*
296          * First check the tree. Use DatabaseImpl.verify so we can get a status
297          * return.
298          */

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             /*
306              * Intentionally use print, not println, because stats.toString()
307              * puts in a newline too.
308              */

309             out.print(stats);
310         }
311
312         /* Then check the obsolete lsns */
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