KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbDump.java,v 1.48 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.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintStream JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.logging.Level JavaDoc;
18
19 import com.sleepycat.je.Cursor;
20 import com.sleepycat.je.Database;
21 import com.sleepycat.je.DatabaseConfig;
22 import com.sleepycat.je.DatabaseEntry;
23 import com.sleepycat.je.DatabaseException;
24 import com.sleepycat.je.DbInternal;
25 import com.sleepycat.je.Environment;
26 import com.sleepycat.je.EnvironmentConfig;
27 import com.sleepycat.je.JEVersion;
28 import com.sleepycat.je.LockMode;
29 import com.sleepycat.je.OperationStatus;
30 import com.sleepycat.je.config.EnvironmentParams;
31 import com.sleepycat.je.utilint.CmdUtil;
32 import com.sleepycat.je.utilint.DbScavenger;
33 import com.sleepycat.je.utilint.Tracer;
34
35 public class DbDump {
36     private static final int VERSION = 3;
37
38     protected File JavaDoc envHome = null;
39     protected Environment env;
40     protected String JavaDoc dbName = null;
41     protected boolean formatUsingPrintable;
42     private boolean dupSort;
43     private String JavaDoc outputFileName = null;
44     protected String JavaDoc outputDirectory = null;
45     protected PrintStream JavaDoc outputFile = null;
46     protected boolean doScavengerRun = false;
47     protected boolean doAggressiveScavengerRun = false;
48     protected boolean verbose = false;
49
50     private static final String JavaDoc usageString =
51     "usage: " + CmdUtil.getJavaCommand(DbDump.class) + "\n" +
52         " -h <dir> # environment home directory\n" +
53         " [-f <fileName>] # output file, for non -rR dumps\n" +
54         " [-l] # list databases in the environment\n" +
55         " [-p] # output printable characters\n" +
56         " [-r] # salvage mode\n" +
57         " [-R] # aggressive salvage mode\n" +
58         " [-d] <directory> # directory for *.dump files (salvage mode)\n" +
59         " [-s <databaseName>] # database to dump\n" +
60         " [-v] # verbose in salvage mode\n" +
61         " [-V] # print JE version number\n";
62
63     private DbDump() {
64     }
65
66     public DbDump(Environment env,
67           String JavaDoc dbName,
68           PrintStream JavaDoc outputFile,
69           String JavaDoc outputDirectory,
70           boolean formatUsingPrintable) {
71         try {
72             this.envHome = env.getHome();
73         } catch (DatabaseException e) {
74             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
75             iae.initCause(e);
76             throw iae;
77         }
78     this.env = env;
79     this.dbName = dbName;
80     this.outputFile = outputFile;
81     this.outputDirectory = outputDirectory;
82     this.formatUsingPrintable = formatUsingPrintable;
83     }
84
85     public static void main(String JavaDoc argv[])
86     throws DatabaseException, IOException JavaDoc {
87
88     DbDump dumper = new DbDump();
89     boolean listDbs = dumper.parseArgs(argv);
90     if (dumper.doScavengerRun) {
91         dumper.openEnv(false);
92         dumper = new DbScavenger(dumper.env,
93                                      dumper.outputFile,
94                      dumper.outputDirectory,
95                                      dumper.formatUsingPrintable,
96                                      dumper.doAggressiveScavengerRun,
97                                      dumper.verbose);
98         ((DbScavenger) dumper).setDumpCorruptedBounds(true);
99     }
100
101     if (listDbs) {
102         dumper.listDbs();
103         System.exit(0);
104     }
105
106     try {
107         dumper.dump();
108     } catch (Throwable JavaDoc T) {
109         T.printStackTrace();
110     } finally {
111         dumper.env.close();
112         if (dumper.outputFile != null &&
113         dumper.outputFile != System.out) {
114         dumper.outputFile.close();
115         }
116     }
117     }
118
119     private void listDbs()
120     throws DatabaseException {
121
122     openEnv(true);
123
124     List JavaDoc dbNames = env.getDatabaseNames();
125     Iterator JavaDoc iter = dbNames.iterator();
126     while (iter.hasNext()) {
127         String JavaDoc name = (String JavaDoc) iter.next();
128         System.out.println(name);
129     }
130     }
131
132     protected void printUsage(String JavaDoc msg) {
133     System.err.println(msg);
134     System.err.println(usageString);
135     System.exit(-1);
136     }
137
138     protected boolean parseArgs(String JavaDoc argv[])
139     throws IOException JavaDoc {
140
141     int argc = 0;
142     int nArgs = argv.length;
143     boolean listDbs = false;
144     while (argc < nArgs) {
145         String JavaDoc thisArg = argv[argc++];
146         if (thisArg.equals("-p")) {
147         formatUsingPrintable = true;
148         } else if (thisArg.equals("-V")) {
149         System.out.println(JEVersion.CURRENT_VERSION);
150         System.exit(0);
151         } else if (thisArg.equals("-l")) {
152         listDbs = true;
153         } else if (thisArg.equals("-r")) {
154         doScavengerRun = true;
155         } else if (thisArg.equals("-R")) {
156         doScavengerRun = true;
157         doAggressiveScavengerRun = true;
158         } else if (thisArg.equals("-f")) {
159         if (argc < nArgs) {
160             outputFileName = argv[argc++];
161         } else {
162             printUsage("-f requires an argument");
163         }
164         } else if (thisArg.equals("-h")) {
165         if (argc < nArgs) {
166             String JavaDoc envDir = argv[argc++];
167                     envHome = new File JavaDoc(envDir);
168         } else {
169             printUsage("-h requires an argument");
170         }
171         } else if (thisArg.equals("-d")) {
172         if (argc < nArgs) {
173             outputDirectory = argv[argc++];
174         } else {
175             printUsage("-d requires an argument");
176         }
177         } else if (thisArg.equals("-s")) {
178         if (argc < nArgs) {
179             dbName = argv[argc++];
180         } else {
181             printUsage("-s requires an argument");
182         }
183         } else if (thisArg.equals("-v")) {
184                 verbose = true;
185             } else {
186                 printUsage(thisArg + " is not a valid option.");
187         }
188     }
189
190     if (envHome == null) {
191         printUsage("-h is a required argument");
192     }
193
194     if (!listDbs &&
195         !doScavengerRun) {
196         if (dbName == null) {
197         printUsage("Must supply a database name if -l not supplied.");
198         }
199     }
200
201     if (outputFileName == null) {
202         outputFile = System.out;
203     } else {
204         outputFile = new PrintStream JavaDoc(new FileOutputStream JavaDoc(outputFileName));
205     }
206
207     return listDbs;
208     }
209
210     /*
211      * Begin DbDump API. From here on there should be no calls to printUsage,
212      * System.xxx.print, or System.exit.
213      */

214     protected void openEnv(boolean doRecovery)
215     throws DatabaseException {
216
217     if (env == null) {
218             EnvironmentConfig envConfiguration = new EnvironmentConfig();
219             envConfiguration.setReadOnly(true);
220         /* Don't run recovery. */
221         envConfiguration.setConfigParam
222         (EnvironmentParams.ENV_RECOVERY.getName(),
223          doRecovery ? "true" : "false");
224         env = new Environment(envHome, envConfiguration);
225     }
226     }
227
228     public void dump()
229     throws IOException JavaDoc, DatabaseException {
230
231     openEnv(true);
232
233     Tracer.trace(Level.INFO, DbInternal.envGetEnvironmentImpl(env),
234              "DbDump.dump of " + dbName + " starting");
235
236     DatabaseEntry foundKey = new DatabaseEntry();
237     DatabaseEntry foundData = new DatabaseEntry();
238
239         DatabaseConfig dbConfig = new DatabaseConfig();
240         dbConfig.setReadOnly(true);
241         DbInternal.setUseExistingConfig(dbConfig, true);
242         Database db = env.openDatabase(null, dbName, dbConfig);
243     dupSort = db.getConfig().getSortedDuplicates();
244
245     printHeader(outputFile, dupSort, formatUsingPrintable);
246
247     Cursor cursor = db.openCursor(null, null);
248     while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) ==
249                OperationStatus.SUCCESS) {
250         dumpOne(outputFile, foundKey.getData(), formatUsingPrintable);
251         dumpOne(outputFile, foundData.getData(), formatUsingPrintable);
252     }
253     cursor.close();
254     db.close();
255     outputFile.println("DATA=END");
256
257     Tracer.trace(Level.INFO, DbInternal.envGetEnvironmentImpl(env),
258              "DbDump.dump of " + dbName + " ending");
259     }
260
261     protected void printHeader(PrintStream JavaDoc o,
262                    boolean dupSort,
263                    boolean formatUsingPrintable) {
264     o.println("VERSION=" + VERSION);
265     if (formatUsingPrintable) {
266         o.println("format=print");
267     } else {
268         o.println("format=bytevalue");
269     }
270     o.println("type=btree");
271     o.println("dupsort=" + (dupSort ? "1" : "0"));
272     o.println("HEADER=END");
273     }
274
275     protected void dumpOne(PrintStream JavaDoc o, byte[] ba,
276                boolean formatUsingPrintable) {
277         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
278         sb.append(' ');
279         CmdUtil.formatEntry(sb, ba, formatUsingPrintable);
280         o.println(sb.toString());
281     }
282 }
283
Popular Tags