KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: RecordSearch.java,v 1.24 2006/10/30 21:14:54 bostic Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.File JavaDoc;
12 import java.util.logging.Level JavaDoc;
13
14 import com.sleepycat.je.Cursor;
15 import com.sleepycat.je.Database;
16 import com.sleepycat.je.DatabaseConfig;
17 import com.sleepycat.je.DatabaseEntry;
18 import com.sleepycat.je.DbInternal;
19 import com.sleepycat.je.Environment;
20 import com.sleepycat.je.EnvironmentConfig;
21 import com.sleepycat.je.LockMode;
22 import com.sleepycat.je.OperationStatus;
23 import com.sleepycat.je.config.EnvironmentParams;
24 import com.sleepycat.je.utilint.CmdUtil;
25
26 /**
27  * KeySearch is a debugging aid that searches the database for a given
28  * record.
29  */

30 public class RecordSearch {
31
32     public static void main(String JavaDoc [] argv) {
33         try {
34             int whichArg = 0;
35             DatabaseEntry searchKey = null;
36             String JavaDoc dbName = null;
37             String JavaDoc keyVal = null;
38             String JavaDoc levelVal = "SEVERE";
39             boolean dumpAll = false;
40         boolean searchKeyRange = false;
41
42             /*
43              * Usage: -h <envHomeDir> (optional
44              * -db <db name>
45              * -ks <key to search for, as a string>
46              * -ksr <key to range search for, as a string>
47              * -a <if true, dump the whole db>
48              * -l <logging level>
49              */

50             String JavaDoc envHome = "."; // default to current directory
51
while (whichArg < argv.length) {
52                 String JavaDoc nextArg = argv[whichArg];
53
54                 if (nextArg.equals("-h")) {
55                     whichArg++;
56                     envHome = CmdUtil.getArg(argv, whichArg);
57                 } else if (nextArg.equals("-db")) {
58                     whichArg++;
59                     dbName = CmdUtil.getArg(argv, whichArg);
60                 } else if (nextArg.equals("-ks")) {
61                     whichArg++;
62                     keyVal = CmdUtil.getArg(argv, whichArg);
63                     searchKey = new DatabaseEntry(keyVal.getBytes());
64                 } else if (nextArg.equals("-ksr")) {
65                     whichArg++;
66                     keyVal = CmdUtil.getArg(argv, whichArg);
67                     searchKey = new DatabaseEntry(keyVal.getBytes());
68             searchKeyRange = true;
69                 } else if (nextArg.equals("-l")) {
70                     whichArg++;
71                     levelVal = CmdUtil.getArg(argv, whichArg);
72                     Level.parse(levelVal); // sanity check level
73
} else if (nextArg.equals("-a")) {
74                     whichArg++;
75                     String JavaDoc dumpVal = CmdUtil.getArg(argv, whichArg);
76                     dumpAll = Boolean.valueOf(dumpVal).booleanValue();
77                 } else {
78                     throw new IllegalArgumentException JavaDoc
79                         (nextArg + " is not a supported option.");
80                 }
81                 whichArg++;
82             }
83
84             if (dbName == null) {
85                 usage();
86                 System.exit(1);
87             }
88
89             /* Make a read only environment */
90             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
91
92             // Don't debug log to the database log.
93
envConfig.setConfigParam
94                 (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false");
95
96             // Do debug log to the console
97
envConfig.setConfigParam
98                 (EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true");
99
100             // Set logging level to only show errors
101
envConfig.setConfigParam
102                 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), levelVal);
103
104             envConfig.setReadOnly(true);
105
106             Environment envHandle = new Environment(new File JavaDoc(envHome),
107                             envConfig);
108
109             /* Open the db. */
110             DatabaseConfig dbConfig = new DatabaseConfig();
111             dbConfig.setReadOnly(true);
112             DbInternal.setUseExistingConfig(dbConfig, true);
113             Database db = envHandle.openDatabase(null, dbName, dbConfig);
114
115             DatabaseEntry foundData = new DatabaseEntry();
116             if (dumpAll) {
117                 Cursor cursor = db.openCursor(null, null);
118                 DatabaseEntry foundKey = new DatabaseEntry();
119                 int i = 0;
120                 while (cursor.getNext(foundKey, foundData,
121                                       LockMode.DEFAULT) == OperationStatus.SUCCESS) {
122                     System.out.println(i + ":key=" +
123                                        new String JavaDoc(foundKey.getData()));
124                     i++;
125                 }
126         cursor.close();
127             } else if (searchKeyRange) {
128                 /* Range Search for the key. */
129         Cursor cursor = db.openCursor(null, null);
130         OperationStatus status = cursor.getSearchKeyRange(searchKey,
131                                   foundData,
132                                   LockMode.DEFAULT);
133         cursor.close();
134                 System.out.println("Range Search for key " + keyVal +
135                                    " status = " + status + " => " +
136                    new String JavaDoc(searchKey.getData()));
137             } else {
138                 /* Search for the key. */
139                 OperationStatus status = db.get(null, searchKey, foundData,
140                         LockMode.DEFAULT);
141                 System.out.println("Search for key " + keyVal +
142                                    " status = " + status);
143         }
144             db.close();
145             envHandle.close();
146
147         } catch (Exception JavaDoc e) {
148             e.printStackTrace();
149             System.out.println(e.getMessage());
150             usage();
151             System.exit(1);
152         }
153     }
154
155     private static void usage() {
156         System.out.println("Usage: RecordSearch");
157     System.out.println(" -h <environment home> ");
158         System.out.println(" -a <true if dump all>");
159         System.out.println(" -db <db name>");
160         System.out.println(" -l logging level");
161         System.out.println(" -ks <key to search for, as a string");
162         System.out.println(" -ksr <key to range search for, as a string");
163     }
164 }
165
Popular Tags