001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: RecordSearch.java,v 1.24.2.3 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.util;
010:
011: import java.io.File;
012: import java.util.logging.Level;
013:
014: import com.sleepycat.je.Cursor;
015: import com.sleepycat.je.Database;
016: import com.sleepycat.je.DatabaseConfig;
017: import com.sleepycat.je.DatabaseEntry;
018: import com.sleepycat.je.DbInternal;
019: import com.sleepycat.je.Environment;
020: import com.sleepycat.je.EnvironmentConfig;
021: import com.sleepycat.je.LockMode;
022: import com.sleepycat.je.OperationStatus;
023: import com.sleepycat.je.config.EnvironmentParams;
024: import com.sleepycat.je.utilint.CmdUtil;
025:
026: /**
027: * KeySearch is a debugging aid that searches the database for a given
028: * record.
029: */
030: public class RecordSearch {
031:
032: public static void main(String[] argv) {
033: try {
034: int whichArg = 0;
035: DatabaseEntry searchKey = null;
036: String dbName = null;
037: String keyVal = null;
038: String levelVal = "SEVERE";
039: boolean dumpAll = false;
040: boolean searchKeyRange = false;
041:
042: /*
043: * Usage: -h <envHomeDir> (optional
044: * -db <db name>
045: * -ks <key to search for, as a string>
046: * -ksr <key to range search for, as a string>
047: * -a <if true, dump the whole db>
048: * -l <logging level>
049: */
050: String envHome = "."; // default to current directory
051: while (whichArg < argv.length) {
052: String nextArg = argv[whichArg];
053:
054: if (nextArg.equals("-h")) {
055: whichArg++;
056: envHome = CmdUtil.getArg(argv, whichArg);
057: } else if (nextArg.equals("-db")) {
058: whichArg++;
059: dbName = CmdUtil.getArg(argv, whichArg);
060: } else if (nextArg.equals("-ks")) {
061: whichArg++;
062: keyVal = CmdUtil.getArg(argv, whichArg);
063: searchKey = new DatabaseEntry(keyVal.getBytes());
064: } else if (nextArg.equals("-ksr")) {
065: whichArg++;
066: keyVal = CmdUtil.getArg(argv, whichArg);
067: searchKey = new DatabaseEntry(keyVal.getBytes());
068: searchKeyRange = true;
069: } else if (nextArg.equals("-l")) {
070: whichArg++;
071: levelVal = CmdUtil.getArg(argv, whichArg);
072: Level.parse(levelVal); // sanity check level
073: } else if (nextArg.equals("-a")) {
074: whichArg++;
075: String dumpVal = CmdUtil.getArg(argv, whichArg);
076: dumpAll = Boolean.valueOf(dumpVal).booleanValue();
077: } else {
078: throw new IllegalArgumentException(nextArg
079: + " is not a supported option.");
080: }
081: whichArg++;
082: }
083:
084: if (dbName == null) {
085: usage();
086: System.exit(1);
087: }
088:
089: /* Make a read only environment */
090: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
091:
092: // Don't debug log to the database log.
093: envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_DBLOG
094: .getName(), "false");
095:
096: // Do debug log to the console
097: envConfig.setConfigParam(
098: EnvironmentParams.JE_LOGGING_CONSOLE.getName(),
099: "true");
100:
101: // Set logging level to only show errors
102: envConfig.setConfigParam(EnvironmentParams.JE_LOGGING_LEVEL
103: .getName(), levelVal);
104:
105: envConfig.setReadOnly(true);
106:
107: Environment envHandle = new Environment(new File(envHome),
108: envConfig);
109:
110: /* Open the db. */
111: DatabaseConfig dbConfig = new DatabaseConfig();
112: dbConfig.setReadOnly(true);
113: DbInternal.setUseExistingConfig(dbConfig, true);
114: Database db = envHandle
115: .openDatabase(null, dbName, dbConfig);
116:
117: DatabaseEntry foundData = new DatabaseEntry();
118: if (dumpAll) {
119: Cursor cursor = db.openCursor(null, null);
120: DatabaseEntry foundKey = new DatabaseEntry();
121: int i = 0;
122: while (cursor.getNext(foundKey, foundData,
123: LockMode.DEFAULT) == OperationStatus.SUCCESS) {
124: System.out.println(i + ":key="
125: + new String(foundKey.getData()));
126: i++;
127: }
128: cursor.close();
129: } else if (searchKeyRange) {
130: /* Range Search for the key. */
131: Cursor cursor = db.openCursor(null, null);
132: OperationStatus status = cursor.getSearchKeyRange(
133: searchKey, foundData, LockMode.DEFAULT);
134: cursor.close();
135: System.out.println("Range Search for key " + keyVal
136: + " status = " + status + " => "
137: + new String(searchKey.getData()));
138: } else {
139: /* Search for the key. */
140: OperationStatus status = db.get(null, searchKey,
141: foundData, LockMode.DEFAULT);
142: System.out.println("Search for key " + keyVal
143: + " status = " + status);
144: }
145: db.close();
146: envHandle.close();
147:
148: } catch (Exception e) {
149: e.printStackTrace();
150: System.out.println(e.getMessage());
151: usage();
152: System.exit(1);
153: }
154: }
155:
156: private static void usage() {
157: System.out.println("Usage: RecordSearch");
158: System.out.println(" -h <environment home> ");
159: System.out.println(" -a <true if dump all>");
160: System.out.println(" -db <db name>");
161: System.out.println(" -l logging level");
162: System.out.println(" -ks <key to search for, as a string");
163: System.out
164: .println(" -ksr <key to range search for, as a string");
165: }
166: }
|