1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: RecordSearch.java,v 1.28.2.2 2010/01/04 15:30:48 cwl Exp $
*/
package com.sleepycat.je.util;
import java.io.File;
import java.util.logging.Level;
import com.sleepycat.je.Cursor;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.utilint.CmdUtil;
/**
* KeySearch is a debugging aid that searches the database for a given
* record.
*/
public class RecordSearch {
public static void main(String[] argv) {
try {
int whichArg = 0;
DatabaseEntry searchKey = null;
String dbName = null;
String keyVal = null;
String levelVal = "SEVERE";
boolean dumpAll = false;
boolean searchKeyRange = false;
/*
* Usage: -h <envHomeDir> (optional
* -db <db name>
* -ks <key to search for, as a string>
* -ksr <key to range search for, as a string>
* -a <if true, dump the whole db>
* -l <logging level>
*/
String envHome = "."; // default to current directory
while (whichArg < argv.length) {
String nextArg = argv[whichArg];
if (nextArg.equals("-h")) {
whichArg++;
envHome = CmdUtil.getArg(argv, whichArg);
} else if (nextArg.equals("-db")) {
whichArg++;
dbName = CmdUtil.getArg(argv, whichArg);
} else if (nextArg.equals("-ks")) {
whichArg++;
keyVal = CmdUtil.getArg(argv, whichArg);
searchKey = new DatabaseEntry(keyVal.getBytes());
} else if (nextArg.equals("-ksr")) {
whichArg++;
keyVal = CmdUtil.getArg(argv, whichArg);
searchKey = new DatabaseEntry(keyVal.getBytes());
searchKeyRange = true;
} else if (nextArg.equals("-l")) {
whichArg++;
levelVal = CmdUtil.getArg(argv, whichArg);
Level.parse(levelVal); // sanity check level
} else if (nextArg.equals("-a")) {
whichArg++;
String dumpVal = CmdUtil.getArg(argv, whichArg);
dumpAll = Boolean.valueOf(dumpVal).booleanValue();
} else {
throw new IllegalArgumentException
(nextArg + " is not a supported option.");
}
whichArg++;
}
if (dbName == null) {
usage();
System.exit(1);
}
/* Make a read only environment */
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
// Don't debug log to the database log.
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false");
// Do debug log to the console
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true");
// Set logging level to only show errors
envConfig.setConfigParam
(EnvironmentParams.JE_LOGGING_LEVEL.getName(), levelVal);
envConfig.setReadOnly(true);
Environment envHandle = new Environment(new File(envHome),
envConfig);
/* Open the db. */
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setReadOnly(true);
DbInternal.setUseExistingConfig(dbConfig, true);
Database db = envHandle.openDatabase(null, dbName, dbConfig);
DatabaseEntry foundData = new DatabaseEntry();
if (dumpAll) {
Cursor cursor = db.openCursor(null, null);
DatabaseEntry foundKey = new DatabaseEntry();
int i = 0;
while (cursor.getNext(foundKey, foundData,
LockMode.DEFAULT) == OperationStatus.SUCCESS) {
System.out.println(i + ":key=" +
new String(foundKey.getData()));
i++;
}
cursor.close();
} else if (searchKeyRange) {
/* Range Search for the key. */
Cursor cursor = db.openCursor(null, null);
OperationStatus status = cursor.getSearchKeyRange(searchKey,
foundData,
LockMode.DEFAULT);
cursor.close();
System.out.println("Range Search for key " + keyVal +
" status = " + status + " => " +
new String(searchKey.getData()));
} else {
/* Search for the key. */
OperationStatus status = db.get(null, searchKey, foundData,
LockMode.DEFAULT);
System.out.println("Search for key " + keyVal +
" status = " + status);
}
db.close();
envHandle.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
usage();
System.exit(1);
}
}
private static void usage() {
System.out.println("Usage: RecordSearch");
System.out.println(" -h <environment home> ");
System.out.println(" -a <true if dump all>");
System.out.println(" -db <db name>");
System.out.println(" -l logging level");
System.out.println(" -ks <key to search for, as a string");
System.out.println(" -ksr <key to range search for, as a string");
}
}
|