1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1997-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package collections.access; 10 11import java.io.File; 12import java.io.FileNotFoundException; 13import java.io.IOException; 14import java.io.InputStreamReader; 15import java.io.PrintStream; 16import java.util.Iterator; 17import java.util.Map; 18import java.util.SortedMap; 19 20import com.sleepycat.bind.ByteArrayBinding; 21import com.sleepycat.collections.StoredSortedMap; 22import com.sleepycat.collections.TransactionRunner; 23import com.sleepycat.collections.TransactionWorker; 24import com.sleepycat.db.Database; 25import com.sleepycat.db.DatabaseConfig; 26import com.sleepycat.db.DatabaseException; 27import com.sleepycat.db.DatabaseType; 28import com.sleepycat.db.Environment; 29import com.sleepycat.db.EnvironmentConfig; 30 31/** 32 * AccesssExample mirrors the functionality of a class by the same name 33 * used to demonstrate the com.sleepycat.je Java API. This version makes 34 * use of the new com.sleepycat.collections.* collections style classes to make 35 * life easier. 36 * 37 *@author Gregory Burd 38 *@created October 22, 2002 39 */ 40public class AccessExample 41 implements Runnable { 42 43 // Class Variables of AccessExample class 44 private static boolean create = true; 45 private static final int EXIT_SUCCESS = 0; 46 private static final int EXIT_FAILURE = 1; 47 48 public static void usage() { 49 50 System.out.println("usage: java " + AccessExample.class.getName() + 51 " [-r] [database]\n"); 52 System.exit(EXIT_FAILURE); 53 } 54 55 /** 56 * The main program for the AccessExample class 57 * 58 *@param argv The command line arguments 59 */ 60 public static void main(String[] argv) { 61 62 boolean removeExistingDatabase = false; 63 String databaseName = "access.db"; 64 65 for (int i = 0; i < argv.length; i++) { 66 if (argv[i].equals("-r")) { 67 removeExistingDatabase = true; 68 } else if (argv[i].equals("-?")) { 69 usage(); 70 } else if (argv[i].startsWith("-")) { 71 usage(); 72 } else { 73 if ((argv.length - i) != 1) 74 usage(); 75 databaseName = argv[i]; 76 break; 77 } 78 } 79 80 try { 81 82 EnvironmentConfig envConfig = new EnvironmentConfig(); 83 envConfig.setTransactional(true); 84 envConfig.setInitializeCache(true); 85 envConfig.setInitializeLocking(true); 86 if (create) { 87 envConfig.setAllowCreate(true); 88 } 89 Environment env = new Environment(new File("."), envConfig); 90 // Remove the previous database. 91 if (removeExistingDatabase) { 92 env.removeDatabase(null, databaseName, null); 93 } 94 95 // create the app and run it 96 AccessExample app = new AccessExample(env, databaseName); 97 app.run(); 98 app.close(); 99 } catch (DatabaseException e) { 100 e.printStackTrace(); 101 System.exit(1); 102 } catch (FileNotFoundException e) { 103 e.printStackTrace(); 104 System.exit(1); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 System.exit(1); 108 } 109 System.exit(0); 110 } 111 112 113 private Database db; 114 private SortedMap map; 115 private Environment env; 116 117 118 /** 119 * Constructor for the AccessExample object 120 * 121 *@param env Description of the Parameter 122 *@exception Exception Description of the Exception 123 */ 124 public AccessExample(Environment env, String databaseName) 125 throws Exception { 126 127 this.env = env; 128 129 // 130 // Lets mimic the db.AccessExample 100% 131 // and use plain old byte arrays to store the key and data strings. 132 // 133 ByteArrayBinding keyBinding = new ByteArrayBinding(); 134 ByteArrayBinding dataBinding = new ByteArrayBinding(); 135 136 // 137 // Open a data store. 138 // 139 DatabaseConfig dbConfig = new DatabaseConfig(); 140 if (create) { 141 dbConfig.setAllowCreate(true); 142 dbConfig.setType(DatabaseType.BTREE); 143 } 144 this.db = env.openDatabase(null, databaseName, null, dbConfig); 145 146 // 147 // Now create a collection style map view of the data store 148 // so that it is easy to work with the data in the database. 149 // 150 this.map = new StoredSortedMap(db, keyBinding, dataBinding, true); 151 } 152 153 /** 154 * Close the database and environment. 155 */ 156 void close() 157 throws DatabaseException { 158 159 db.close(); 160 env.close(); 161 } 162 163 /** 164 * Main processing method for the AccessExample object 165 */ 166 public void run() { 167 // 168 // Insert records into a Stored Sorted Map DatabaseImpl, where 169 // the key is the user input and the data is the user input 170 // in reverse order. 171 // 172 final InputStreamReader reader = new InputStreamReader(System.in); 173 174 for (; ; ) { 175 final String line = askForLine(reader, System.out, "input> "); 176 if (line == null) { 177 break; 178 } 179 180 final String reversed = 181 (new StringBuffer(line)).reverse().toString(); 182 183 log("adding: \"" + 184 line + "\" : \"" + 185 reversed + "\""); 186 187 // Do the work to add the key/data to the HashMap here. 188 TransactionRunner tr = new TransactionRunner(env); 189 try { 190 tr.run( 191 new TransactionWorker() { 192 public void doWork() { 193 if (!map.containsKey(line.getBytes())) 194 map.put(line.getBytes(), 195 reversed.getBytes()); 196 else 197 System.out.println("Key " + line + 198 " already exists."); 199 } 200 }); 201 } catch (com.sleepycat.db.DatabaseException e) { 202 System.err.println("AccessExample: " + e.toString()); 203 System.exit(1); 204 } catch (java.lang.Exception e) { 205 System.err.println("AccessExample: " + e.toString()); 206 System.exit(1); 207 } 208 } 209 System.out.println(""); 210 211 // Do the work to traverse and print the HashMap key/data 212 // pairs here get iterator over map entries. 213 Iterator iter = map.entrySet().iterator(); 214 System.out.println("Reading data"); 215 while (iter.hasNext()) { 216 Map.Entry entry = (Map.Entry) iter.next(); 217 log("found \"" + 218 new String((byte[]) entry.getKey()) + 219 "\" key with data \"" + 220 new String((byte[]) entry.getValue()) + "\""); 221 } 222 } 223 224 225 /** 226 * Prompts for a line, and keeps prompting until a non blank line is 227 * returned. Returns null on error. 228 * 229 *@param reader stream from which to read user input 230 *@param out stream on which to prompt for user input 231 *@param prompt prompt to use to solicit input 232 *@return the string supplied by the user 233 */ 234 String askForLine(InputStreamReader reader, PrintStream out, 235 String prompt) { 236 237 String result = ""; 238 while (result != null && result.length() == 0) { 239 out.print(prompt); 240 out.flush(); 241 result = getLine(reader); 242 } 243 return result; 244 } 245 246 247 /** 248 * Read a single line. Gets the line attribute of the AccessExample object 249 * Not terribly efficient, but does the job. Works for reading a line from 250 * stdin or a file. 251 * 252 *@param reader stream from which to read the line 253 *@return either a String or null on EOF, if EOF appears in the 254 * middle of a line, returns that line, then null on next call. 255 */ 256 String getLine(InputStreamReader reader) { 257 258 StringBuffer b = new StringBuffer(); 259 int c; 260 try { 261 while ((c = reader.read()) != -1 && c != '\n') { 262 if (c != '\r') { 263 b.append((char) c); 264 } 265 } 266 } catch (IOException ioe) { 267 c = -1; 268 } 269 270 if (c == -1 && b.length() == 0) { 271 return null; 272 } else { 273 return b.toString(); 274 } 275 } 276 277 278 /** 279 * A simple log method. 280 * 281 *@param s The string to be logged. 282 */ 283 private void log(String s) { 284 285 System.out.println(s); 286 System.out.flush(); 287 } 288} 289