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