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