KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbStat.java,v 1.21 2006/10/30 21:14:28 bostic 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.logging.Level JavaDoc;
14
15 import com.sleepycat.je.Database;
16 import com.sleepycat.je.DatabaseConfig;
17 import com.sleepycat.je.DatabaseException;
18 import com.sleepycat.je.DatabaseStats;
19 import com.sleepycat.je.DbInternal;
20 import com.sleepycat.je.Environment;
21 import com.sleepycat.je.JEVersion;
22 import com.sleepycat.je.StatsConfig;
23 import com.sleepycat.je.utilint.CmdUtil;
24 import com.sleepycat.je.utilint.Tracer;
25
26 public class DbStat extends DbVerify {
27     private String JavaDoc usageString =
28     "usage: " + CmdUtil.getJavaCommand(DbStat.class) + "\n" +
29     " [-V] -s database -h dbEnvHome [-v progressInterval]\n";
30
31     private int progressInterval = 0;
32
33     static public void main(String JavaDoc argv[])
34     throws DatabaseException {
35
36     DbStat stat = new DbStat();
37     stat.parseArgs(argv);
38
39     int ret = 0;
40     try {
41         if (!stat.stats(System.err)) {
42         ret = 1;
43         }
44     } catch (Throwable JavaDoc T) {
45         ret = 1;
46         T.printStackTrace(System.err);
47     }
48
49     try {
50         stat.env.close();
51     } catch (Throwable JavaDoc ignored) {
52
53         /*
54          * Klockwork - ok
55          * Don't say anything about exceptions here.
56          */

57     }
58     System.exit(ret);
59     }
60
61     protected DbStat() {
62     }
63
64     public DbStat(Environment env, String JavaDoc dbName) {
65     super(env, dbName, false);
66     }
67
68     protected void printUsage(String JavaDoc msg) {
69     System.err.println(msg);
70     System.err.println(usageString);
71     System.exit(-1);
72     }
73
74     protected void parseArgs(String JavaDoc argv[]) {
75
76     int argc = 0;
77     int nArgs = argv.length;
78     while (argc < nArgs) {
79         String JavaDoc thisArg = argv[argc++];
80         if (thisArg.equals("-V")) {
81         System.out.println(JEVersion.CURRENT_VERSION);
82         System.exit(0);
83         } else if (thisArg.equals("-h")) {
84         if (argc < nArgs) {
85             envHome = new File JavaDoc(argv[argc++]);
86         } else {
87             printUsage("-h requires an argument");
88         }
89         } else if (thisArg.equals("-s")) {
90         if (argc < nArgs) {
91             dbName = argv[argc++];
92         } else {
93             printUsage("-s requires an argument");
94         }
95         } else if (thisArg.equals("-v")) {
96         if (argc < nArgs) {
97             progressInterval = Integer.parseInt(argv[argc++]);
98             if (progressInterval <= 0) {
99             printUsage("-v requires a positive argument");
100             }
101         } else {
102             printUsage("-v requires an argument");
103         }
104         }
105     }
106
107     if (envHome == null) {
108         printUsage("-h is a required argument");
109     }
110
111     if (dbName == null) {
112         printUsage("-s is a required argument");
113     }
114     }
115
116     public boolean stats(PrintStream JavaDoc out)
117     throws DatabaseException {
118
119     try {
120         openEnv();
121
122         Tracer.trace(Level.INFO, DbInternal.envGetEnvironmentImpl(env),
123              "DbStat.stats of " + dbName + " starting");
124
125         DatabaseConfig dbConfig = new DatabaseConfig();
126         dbConfig.setReadOnly(true);
127         dbConfig.setAllowCreate(false);
128         DbInternal.setUseExistingConfig(dbConfig, true);
129         Database db = env.openDatabase(null, dbName, dbConfig);
130
131         StatsConfig statsConfig = new StatsConfig();
132         if (progressInterval > 0) {
133         statsConfig.setShowProgressInterval(progressInterval);
134         statsConfig.setShowProgressStream(out);
135         }
136
137         DatabaseStats stats = db.getStats(statsConfig);
138         out.println(stats);
139
140         db.close();
141         Tracer.trace(Level.INFO, DbInternal.envGetEnvironmentImpl(env),
142              "DbStat.stats of " + dbName + " ending");
143     } catch (DatabaseException DE) {
144         return false;
145     }
146     return true;
147     }
148 }
149
Popular Tags