1 8 9 package collections.access; 10 11 import java.io.File ; 12 import java.io.FileNotFoundException ; 13 import java.io.IOException ; 14 import java.io.InputStreamReader ; 15 import java.io.PrintStream ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.SortedMap ; 19 20 import com.sleepycat.bind.ByteArrayBinding; 21 import com.sleepycat.collections.StoredSortedMap; 22 import com.sleepycat.collections.TransactionRunner; 23 import com.sleepycat.collections.TransactionWorker; 24 import com.sleepycat.je.Database; 25 import com.sleepycat.je.DatabaseConfig; 26 import com.sleepycat.je.DatabaseException; 27 import com.sleepycat.je.Environment; 28 import com.sleepycat.je.EnvironmentConfig; 29 30 39 public class AccessExample 40 implements Runnable { 41 42 private static boolean create = true; 44 private static final int EXIT_FAILURE = 1; 45 46 public static void usage() { 47 48 System.out.println("usage: java " + AccessExample.class.getName() + 49 " [-r] [database]\n"); 50 System.exit(EXIT_FAILURE); 51 } 52 53 58 public static void main(String [] argv) { 59 60 boolean removeExistingDatabase = false; 61 String databaseName = "access.db"; 62 63 for (int i = 0; i < argv.length; i++) { 64 if (argv[i].equals("-r")) { 65 removeExistingDatabase = true; 66 } else if (argv[i].equals("-?")) { 67 usage(); 68 } else if (argv[i].startsWith("-")) { 69 usage(); 70 } else { 71 if ((argv.length - i) != 1) 72 usage(); 73 databaseName = argv[i]; 74 break; 75 } 76 } 77 78 try { 79 80 EnvironmentConfig envConfig = new EnvironmentConfig(); 81 envConfig.setTransactional(true); 82 if (create) { 83 envConfig.setAllowCreate(true); 84 } 85 Environment env = new Environment(new File ("."), envConfig); 86 if (removeExistingDatabase) { 88 env.removeDatabase(null, databaseName); 89 } 90 91 AccessExample app = new AccessExample(env, databaseName); 93 app.run(); 94 } catch (DatabaseException e) { 95 e.printStackTrace(); 96 System.exit(1); 97 } catch (FileNotFoundException e) { 98 e.printStackTrace(); 99 System.exit(1); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 System.exit(1); 103 } 104 System.exit(0); 105 } 106 107 108 private Database db; 109 private SortedMap map; 110 private Environment env; 111 112 113 119 public AccessExample(Environment env, String databaseName) 120 throws Exception { 121 122 this.env = env; 123 124 ByteArrayBinding keyBinding = new ByteArrayBinding(); 129 ByteArrayBinding dataBinding = new ByteArrayBinding(); 130 131 DatabaseConfig dbConfig = new DatabaseConfig(); 135 if (create) { 136 dbConfig.setAllowCreate(true); 137 } 138 this.db = env.openDatabase(null, databaseName, dbConfig); 139 140 this.map = new StoredSortedMap(db, keyBinding, dataBinding, true); 145 } 146 147 148 151 public void run() { 152 final InputStreamReader reader = new InputStreamReader (System.in); 158 159 for (; ; ) { 160 final String line = askForLine(reader, System.out, "input> "); 161 if (line == null) { 162 break; 163 } 164 165 final String reversed = 166 (new StringBuffer (line)).reverse().toString(); 167 168 log("adding: \"" + 169 line + "\" : \"" + 170 reversed + "\""); 171 172 TransactionRunner tr = new TransactionRunner(env); 174 try { 175 tr.run( 176 new TransactionWorker() { 177 public void doWork() { 178 try { 179 if (!map.containsKey(line.getBytes("UTF-8"))) 180 map.put(line.getBytes("UTF-8"), 181 reversed.getBytes("UTF-8")); 182 else 183 System.out.println("Key " + line + 184 " already exists."); 185 } catch (Exception e) { 186 System.err.println("doWork: " + e); 187 } 188 } 189 }); 190 } catch (com.sleepycat.je.DatabaseException e) { 191 System.err.println("AccessExample: " + e); 192 System.exit(1); 193 } catch (java.lang.Exception e) { 194 System.err.println("AccessExample: " + e); 195 System.exit(1); 196 } 197 } 198 System.out.println(""); 199 200 Iterator iter = map.entrySet().iterator(); 203 System.out.println("Reading data"); 204 while (iter.hasNext()) { 205 Map.Entry entry = (Map.Entry ) iter.next(); 206 log("found \"" + 207 new String ((byte[]) entry.getKey()) + 208 "\" key with data \"" + 209 new String ((byte[]) entry.getValue()) + "\""); 210 } 211 } 212 213 214 223 String askForLine(InputStreamReader reader, PrintStream out, 224 String prompt) { 225 226 String result = ""; 227 while (result != null && result.length() == 0) { 228 out.print(prompt); 229 out.flush(); 230 result = getLine(reader); 231 } 232 return result; 233 } 234 235 236 245 String getLine(InputStreamReader reader) { 246 247 StringBuffer b = new StringBuffer (); 248 int c; 249 try { 250 while ((c = reader.read()) != -1 && c != '\n') { 251 if (c != '\r') { 252 b.append((char) c); 253 } 254 } 255 } catch (IOException ioe) { 256 c = -1; 257 } 258 259 if (c == -1 && b.length() == 0) { 260 return null; 261 } else { 262 return b.toString(); 263 } 264 } 265 266 267 272 private void log(String s) { 273 274 System.out.println(s); 275 System.out.flush(); 276 } 277 } 278 | Popular Tags |