00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 package db;
00012
00013 import com.sleepycat.db.*;
00014 import java.io.File;
00015 import java.io.FileNotFoundException;
00016 import java.io.InputStreamReader;
00017 import java.io.IOException;
00018 import java.io.PrintStream;
00019
00020 class AccessExample {
00021 private static final int EXIT_SUCCESS = 0;
00022 private static final int EXIT_FAILURE = 1;
00023
00024 public AccessExample() {
00025 }
00026
00027 public static void usage() {
00028 System.out.println("usage: java " +
00029 "db.AccessExample [-r] [database]\n");
00030 System.exit(EXIT_FAILURE);
00031 }
00032
00033 public static void main(String[] argv) {
00034 boolean removeExistingDatabase = false;
00035 String databaseName = "access.db";
00036
00037 for (int i = 0; i < argv.length; i++) {
00038 if (argv[i].equals("-r"))
00039 removeExistingDatabase = true;
00040 else if (argv[i].equals("-?"))
00041 usage();
00042 else if (argv[i].startsWith("-"))
00043 usage();
00044 else {
00045 if ((argv.length - i) != 1)
00046 usage();
00047 databaseName = argv[i];
00048 break;
00049 }
00050 }
00051
00052 try {
00053 AccessExample app = new AccessExample();
00054 app.run(removeExistingDatabase, databaseName);
00055 } catch (DatabaseException dbe) {
00056 System.err.println("AccessExample: " + dbe.toString());
00057 System.exit(EXIT_FAILURE);
00058 } catch (FileNotFoundException fnfe) {
00059 System.err.println("AccessExample: " + fnfe.toString());
00060 System.exit(EXIT_FAILURE);
00061 }
00062 System.exit(EXIT_SUCCESS);
00063 }
00064
00065
00066
00067
00068 public static String askForLine(InputStreamReader reader,
00069 PrintStream out, String prompt) {
00070 String result = "";
00071 while (result != null && result.length() == 0) {
00072 out.print(prompt);
00073 out.flush();
00074 result = getLine(reader);
00075 }
00076 return result;
00077 }
00078
00079
00080
00081
00082
00083
00084 public static String getLine(InputStreamReader reader) {
00085 StringBuffer b = new StringBuffer();
00086 int c;
00087 try {
00088 while ((c = reader.read()) != -1 && c != '\n') {
00089 if (c != '\r')
00090 b.append((char)c);
00091 }
00092 } catch (IOException ioe) {
00093 c = -1;
00094 }
00095
00096 if (c == -1 && b.length() == 0)
00097 return null;
00098 else
00099 return b.toString();
00100 }
00101
00102 public void run(boolean removeExistingDatabase, String databaseName)
00103 throws DatabaseException, FileNotFoundException {
00104
00105
00106 if (removeExistingDatabase)
00107 new File(databaseName).delete();
00108
00109
00110
00111 DatabaseConfig dbConfig = new DatabaseConfig();
00112 dbConfig.setErrorStream(System.err);
00113 dbConfig.setErrorPrefix("AccessExample");
00114 dbConfig.setType(DatabaseType.BTREE);
00115 dbConfig.setAllowCreate(true);
00116 Database table = new Database(databaseName, null, dbConfig);
00117
00118
00119
00120
00121
00122 InputStreamReader reader = new InputStreamReader(System.in);
00123
00124 for (;;) {
00125 String line = askForLine(reader, System.out, "input> ");
00126 if (line == null)
00127 break;
00128
00129 String reversed = (new StringBuffer(line)).reverse().toString();
00130
00131
00132
00133 StringEntry key = new StringEntry(line);
00134 StringEntry data = new StringEntry(reversed);
00135
00136 try {
00137 if (table.putNoOverwrite(null, key, data) == OperationStatus.KEYEXIST)
00138 System.out.println("Key " + line + " already exists.");
00139 } catch (DatabaseException dbe) {
00140 System.out.println(dbe.toString());
00141 }
00142 }
00143
00144
00145 Cursor cursor;
00146 cursor = table.openCursor(null, null);
00147
00148
00149
00150
00151 StringEntry key = new StringEntry();
00152 StringEntry data = new StringEntry();
00153 while (cursor.getNext(key, data, null) == OperationStatus.SUCCESS)
00154 System.out.println(key.getString() + " : " + data.getString());
00155 cursor.close();
00156 table.close();
00157 }
00158
00159
00160
00161
00162
00163
00164 static
00165 class StringEntry extends DatabaseEntry {
00166 StringEntry() {
00167 }
00168
00169 StringEntry(String value) {
00170 setString(value);
00171 }
00172
00173 void setString(String value) {
00174 byte[] data = value.getBytes();
00175 setData(data);
00176 setSize(data.length);
00177 }
00178
00179 String getString() {
00180 return new String(getData(), getOffset(), getSize());
00181 }
00182 }
00183 }