001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DbStat.java,v 1.21.2.3 2008/01/07 15:14:17 cwl Exp $
007: */
008:
009: package com.sleepycat.je.util;
010:
011: import java.io.File;
012: import java.io.PrintStream;
013: import java.util.logging.Level;
014:
015: import com.sleepycat.je.Database;
016: import com.sleepycat.je.DatabaseConfig;
017: import com.sleepycat.je.DatabaseException;
018: import com.sleepycat.je.DatabaseStats;
019: import com.sleepycat.je.DbInternal;
020: import com.sleepycat.je.Environment;
021: import com.sleepycat.je.JEVersion;
022: import com.sleepycat.je.StatsConfig;
023: import com.sleepycat.je.utilint.CmdUtil;
024: import com.sleepycat.je.utilint.Tracer;
025:
026: public class DbStat extends DbVerify {
027: private String usageString = "usage: "
028: + CmdUtil.getJavaCommand(DbStat.class)
029: + "\n"
030: + " [-V] -s database -h dbEnvHome [-v progressInterval]\n";
031:
032: private int progressInterval = 0;
033:
034: static public void main(String argv[]) throws DatabaseException {
035:
036: DbStat stat = new DbStat();
037: stat.parseArgs(argv);
038:
039: int ret = 0;
040: try {
041: if (!stat.stats(System.err)) {
042: ret = 1;
043: }
044: } catch (Throwable T) {
045: ret = 1;
046: T.printStackTrace(System.err);
047: }
048:
049: try {
050: stat.env.close();
051: } catch (Throwable ignored) {
052:
053: /*
054: * Klockwork - ok
055: * Don't say anything about exceptions here.
056: */
057: }
058: System.exit(ret);
059: }
060:
061: protected DbStat() {
062: }
063:
064: public DbStat(Environment env, String dbName) {
065: super (env, dbName, false);
066: }
067:
068: protected void printUsage(String msg) {
069: System.err.println(msg);
070: System.err.println(usageString);
071: System.exit(-1);
072: }
073:
074: protected void parseArgs(String argv[]) {
075:
076: int argc = 0;
077: int nArgs = argv.length;
078: while (argc < nArgs) {
079: String this Arg = argv[argc++];
080: if (this Arg.equals("-V")) {
081: System.out.println(JEVersion.CURRENT_VERSION);
082: System.exit(0);
083: } else if (this Arg.equals("-h")) {
084: if (argc < nArgs) {
085: envHome = new File(argv[argc++]);
086: } else {
087: printUsage("-h requires an argument");
088: }
089: } else if (this Arg.equals("-s")) {
090: if (argc < nArgs) {
091: dbName = argv[argc++];
092: } else {
093: printUsage("-s requires an argument");
094: }
095: } else if (this Arg.equals("-v")) {
096: if (argc < nArgs) {
097: progressInterval = Integer.parseInt(argv[argc++]);
098: if (progressInterval <= 0) {
099: 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 out) throws DatabaseException {
117:
118: try {
119: openEnv();
120:
121: Tracer.trace(Level.INFO, DbInternal
122: .envGetEnvironmentImpl(env), "DbStat.stats of "
123: + 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
142: .envGetEnvironmentImpl(env), "DbStat.stats of "
143: + dbName + " ending");
144: } catch (DatabaseException DE) {
145: return false;
146: }
147: return true;
148: }
149: }
|