2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
package com.sleepycat.examples;
12
import com.sleepycat.db.*;
13
import java.io.BufferedReader;
15
import java.io.FileNotFoundException;
16
import java.io.FileReader;
17
import java.io.FileWriter;
18
import java.io.InputStreamReader;
19
import java.io.IOException;
20
import java.io.PrintStream;
22
public class BtRecExample
24
static final String progname = "BtRecExample"; // Program name.
25
static final String database = "access.db";
26
static final String wordlist = "../test/wordlist";
28
BtRecExample(BufferedReader reader)
29
throws DbException, IOException, FileNotFoundException
33
// Remove the previous database.
34
File f = new File(database);
37
dbp = new Db(null, 0);
39
dbp.set_error_stream(System.err);
40
dbp.set_errpfx(progname);
41
dbp.set_pagesize(1024); // 1K page sizes.
43
dbp.set_flags(Db.DB_RECNUM); // Record numbers.
44
dbp.open(null, database, null, Db.DB_BTREE, Db.DB_CREATE, 0664);
47
// Insert records into the database, where the key is the word
48
// preceded by its record number, and the data is the same, but
52
for (int cnt = 1; cnt <= 1000; ++cnt) {
53
String numstr = String.valueOf(cnt);
54
while (numstr.length() < 4)
55
numstr = "0" + numstr;
56
String buf = numstr + '_' + reader.readLine();
57
StringBuffer rbuf = new StringBuffer(buf).reverse();
59
StringDbt key = new StringDbt(buf);
60
StringDbt data = new StringDbt(rbuf.toString());
62
if ((ret = dbp.put(null, key, data, Db.DB_NOOVERWRITE)) != 0) {
63
if (ret != Db.DB_KEYEXIST)
64
throw new DbException("Db.put failed" + ret);
75
// Acquire a cursor for the database.
76
dbcp = dbp.cursor(null, 0);
79
// Prompt the user for a record number, then retrieve and display
82
InputStreamReader reader = new InputStreamReader(System.in);
85
// Get a record number.
86
String line = askForLine(reader, System.out, "recno #> ");
91
recno = Integer.parseInt(line);
93
catch (NumberFormatException nfe) {
94
System.err.println("Bad record number: " + nfe);
99
// Start with a fresh key each time, the dbp.get() routine returns
100
// the key and data pair, not just the key!
102
RecnoStringDbt key = new RecnoStringDbt(recno, 100);
103
RecnoStringDbt data = new RecnoStringDbt(100);
105
if ((ret = dbcp.get(key, data, Db.DB_SET_RECNO)) != 0) {
106
throw new DbException("Dbc.get failed", ret);
109
// Display the key and data.
110
show("k/d\t", key, data);
112
// Move the cursor a record forward.
113
if ((ret = dbcp.get(key, data, Db.DB_NEXT)) != 0) {
114
throw new DbException("Dbc.get failed", ret);
117
// Display the key and data.
118
show("next\t", key, data);
120
RecnoStringDbt datano = new RecnoStringDbt(100);
123
// Retrieve the record number for the following record into
126
if ((ret = dbcp.get(key, datano, Db.DB_GET_RECNO)) != 0) {
127
if (ret != Db.DB_NOTFOUND && ret != Db.DB_KEYEMPTY) {
128
throw new DbException("Dbc.get failed", ret);
132
recno = datano.getRecno();
133
System.out.println("retrieved recno: " + recno);
142
// Print out the number of records in the database.
149
statp = (DbBtreeStat)dbp.stat(0);
150
System.out.println(progname + ": database contains " +
151
statp.bt_ndata + " records");
154
void show(String msg, RecnoStringDbt key, RecnoStringDbt data)
157
System.out.println(msg + key.getString() + ": " + data.getString());
160
public void shutdown()
173
public static void main(String argv[])
177
// Open the word database.
178
FileReader freader = new FileReader(wordlist);
180
BtRecExample app = new BtRecExample(new BufferedReader(freader));
182
// Close the word database.
188
} catch (FileNotFoundException fnfe) {
189
System.err.println(progname + ": unexpected open error " + fnfe);
191
} catch (IOException ioe) {
192
System.err.println(progname + ": open " + wordlist + ": " + ioe);
194
} catch (DbException dbe) {
195
System.err.println("Exception: " + dbe);
196
System.exit(dbe.get_errno());
202
// Prompts for a line, and keeps prompting until a non blank
203
// line is returned. Returns null on error.
205
static public String askForLine(InputStreamReader reader,
206
PrintStream out, String prompt)
209
while (result != null && result.length() == 0) {
212
result = getLine(reader);
217
// Not terribly efficient, but does the job.
218
// Works for reading a line from stdin or a file.
219
// Returns null on EOF. If EOF appears in the middle
220
// of a line, returns that line, then null on next call.
222
static public String getLine(InputStreamReader reader)
224
StringBuffer b = new StringBuffer();
227
while ((c = reader.read()) != -1 && c != '\n') {
232
catch (IOException ioe) {
236
if (c == -1 && b.length() == 0)
245
// Here's an example of how you can extend a Dbt in a straightforward
246
// way to allow easy storage/retrieval of strings.
247
// We've declared it as a static inner class, but it need not be.
250
class StringDbt extends Dbt
252
StringDbt(byte[] arr)
254
set_flags(Db.DB_DBT_USERMEM);
256
set_size(arr.length);
261
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
264
StringDbt(String value)
267
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
270
void setString(String value)
272
byte[] data = value.getBytes();
274
set_size(data.length);
275
// must set ulen because sometimes a string is returned
276
set_ulen(data.length);
281
return new String(get_data(), 0, get_size());
285
// Here's an example of how you can extend a Dbt to store
286
// (potentially) both recno's and strings in the same
290
class RecnoStringDbt extends Dbt
292
RecnoStringDbt(int maxsize)
294
this(0, maxsize); // let other constructor do most of the work
297
RecnoStringDbt(int value, int maxsize)
299
set_flags(Db.DB_DBT_USERMEM); // do not allocate on retrieval
300
arr = new byte[maxsize];
301
set_data(arr); // use our local array for data
302
set_ulen(maxsize); // size of return storage
306
RecnoStringDbt(String value, int maxsize)
308
set_flags(Db.DB_DBT_USERMEM); // do not allocate on retrieval
309
arr = new byte[maxsize];
310
set_data(arr); // use our local array for data
311
set_ulen(maxsize); // size of return storage
315
void setRecno(int value)
317
set_recno_key_data(value);
318
set_size(arr.length);
321
void setString(String value)
323
byte[] data = value.getBytes();
325
set_size(data.length);
330
return get_recno_key_data();
335
return new String(get_data(), 0, get_size());