1 8 9 package com.sleepycat.je.util; 10 11 import java.io.File ; 12 import java.util.logging.Level ; 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 30 public class RecordSearch { 31 32 public static void main(String [] argv) { 33 try { 34 int whichArg = 0; 35 DatabaseEntry searchKey = null; 36 String dbName = null; 37 String keyVal = null; 38 String levelVal = "SEVERE"; 39 boolean dumpAll = false; 40 boolean searchKeyRange = false; 41 42 50 String envHome = "."; while (whichArg < argv.length) { 52 String 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); } else if (nextArg.equals("-a")) { 74 whichArg++; 75 String dumpVal = CmdUtil.getArg(argv, whichArg); 76 dumpAll = Boolean.valueOf(dumpVal).booleanValue(); 77 } else { 78 throw new IllegalArgumentException 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 90 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 91 92 envConfig.setConfigParam 94 (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false"); 95 96 envConfig.setConfigParam 98 (EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true"); 99 100 envConfig.setConfigParam 102 (EnvironmentParams.JE_LOGGING_LEVEL.getName(), levelVal); 103 104 envConfig.setReadOnly(true); 105 106 Environment envHandle = new Environment(new File (envHome), 107 envConfig); 108 109 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 (foundKey.getData())); 124 i++; 125 } 126 cursor.close(); 127 } else if (searchKeyRange) { 128 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 (searchKey.getData())); 137 } else { 138 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 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 |