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.*;
14
import java.io.FileNotFoundException;
15
import java.io.InputStreamReader;
16
import java.io.IOException;
17
import java.io.PrintStream;
19
class BulkAccessExample
21
private static final String FileName = "access.db";
23
public BulkAccessExample()
27
public static void main(String argv[])
31
BulkAccessExample app = new BulkAccessExample();
34
catch (DbException dbe)
36
System.err.println("BulkAccessExample: " + dbe.toString());
39
catch (FileNotFoundException fnfe)
41
System.err.println("BulkAccessExample: " + fnfe.toString());
47
// Prompts for a line, and keeps prompting until a non blank
48
// line is returned. Returns null on error.
50
static public String askForLine(InputStreamReader reader,
51
PrintStream out, String prompt)
54
while (result != null && result.length() == 0) {
57
result = getLine(reader);
62
// Not terribly efficient, but does the job.
63
// Works for reading a line from stdin or a file.
64
// Returns null on EOF. If EOF appears in the middle
65
// of a line, returns that line, then null on next call.
67
static public String getLine(InputStreamReader reader)
69
StringBuffer b = new StringBuffer();
72
while ((c = reader.read()) != -1 && c != '\n') {
77
catch (IOException ioe) {
81
if (c == -1 && b.length() == 0)
88
throws DbException, FileNotFoundException
90
// Remove the previous database.
91
new File(FileName).delete();
93
// Create the database object.
94
// There is no environment for this simple example.
95
Db table = new Db(null, 0);
96
table.set_error_stream(System.err);
97
table.set_errpfx("BulkAccessExample");
98
table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
101
// Insert records into the database, where the key is the user
102
// input and the data is the user input in reverse order.
104
InputStreamReader reader = new InputStreamReader(System.in);
107
String line = askForLine(reader, System.out, "input> ");
111
String reversed = (new StringBuffer(line)).reverse().toString();
113
// See definition of StringDbt below
115
StringDbt key = new StringDbt(line);
116
StringDbt data = new StringDbt(reversed);
121
if ((err = table.put(null,
122
key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
123
System.out.println("Key " + line + " already exists.");
126
catch (DbException dbe)
128
System.out.println(dbe.toString());
130
System.out.println("");
133
// Acquire a cursor for the table and two Dbts.
134
Dbc dbc = table.cursor(null, 0);
136
foo.set_flags(Db.DB_DBT_MALLOC);
138
Dbt bulk_data = new Dbt();
140
// Set Db.DB_DBT_USERMEM on the data Dbt; Db.DB_MULTIPLE_KEY requires
141
// it. Then allocate a byte array of a reasonable size; we'll
142
// go through the database in chunks this big.
143
bulk_data.set_flags(Db.DB_DBT_USERMEM);
144
bulk_data.set_data(new byte[1000000]);
145
bulk_data.set_ulen(1000000);
148
// Walk through the table, printing the key/data pairs.
150
while (dbc.get(foo, bulk_data, Db.DB_NEXT | Db.DB_MULTIPLE_KEY) == 0)
152
DbMultipleKeyDataIterator iterator;
153
iterator = new DbMultipleKeyDataIterator(bulk_data);
156
key = new StringDbt();
157
data = new StringDbt();
159
while (iterator.next(key, data)) {
160
System.out.println(key.getString() + " : " + data.getString());
167
// Here's an example of how you can extend a Dbt in a straightforward
168
// way to allow easy storage/retrieval of strings, or whatever
169
// kind of data you wish. We've declared it as a static inner
170
// class, but it need not be.
173
class StringDbt extends Dbt
177
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
180
StringDbt(String value)
183
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
186
void setString(String value)
188
byte[] data = value.getBytes();
190
set_size(data.length);
195
return new String(get_data(), get_offset(), get_size());