2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
package com.sleepycat.test;
12
import com.sleepycat.db.*;
14
import java.io.FileNotFoundException;
15
import java.io.Reader;
16
import java.io.StringReader;
17
import java.io.IOException;
18
import java.io.PrintStream;
19
import java.util.Hashtable;
21
public class TestAssociate
22
implements DbDupCompare
24
private static final String FileName = "access.db";
25
public static Db saveddb1 = null;
26
public static Db saveddb2 = null;
28
public TestAssociate()
32
private static void usage()
34
System.err.println("usage: TestAssociate\n");
38
public static void main(String argv[])
42
TestAssociate app = new TestAssociate();
45
catch (DbException dbe)
47
System.err.println("TestAssociate: " + dbe.toString());
50
catch (FileNotFoundException fnfe)
52
System.err.println("TestAssociate: " + fnfe.toString());
58
public static int counter = 0;
59
public static String results[] = { "abc", "def", "ghi", "JKL", "MNO", null };
61
// Prompts for a line, and keeps prompting until a non blank
62
// line is returned. Returns null on error.
64
static public String askForLine(Reader reader,
65
PrintStream out, String prompt)
69
while (result != null && result.length() == 0) {
72
result = getLine(reader);
76
return results[counter++];
79
// Not terribly efficient, but does the job.
80
// Works for reading a line from stdin or a file.
81
// Returns null on EOF. If EOF appears in the middle
82
// of a line, returns that line, then null on next call.
84
static public String getLine(Reader reader)
86
StringBuffer b = new StringBuffer();
89
while ((c = reader.read()) != -1 && c != '\n') {
94
catch (IOException ioe) {
98
if (c == -1 && b.length() == 0)
104
static public String shownull(Object o)
113
throws DbException, FileNotFoundException
115
// Remove the previous database.
116
new File(FileName).delete();
118
// Create the database object.
119
// There is no environment for this simple example.
120
DbEnv dbenv = new DbEnv(0);
121
dbenv.open("./", Db.DB_CREATE|Db.DB_INIT_MPOOL, 0644);
122
(new java.io.File(FileName)).delete();
123
Db table = new Db(dbenv, 0);
124
Db table2 = new Db(dbenv, 0);
125
table2.set_dup_compare(this);
126
table2.set_flags(Db.DB_DUPSORT);
127
table.set_error_stream(System.err);
128
table2.set_error_stream(System.err);
129
table.set_errpfx("TestAssociate");
130
table2.set_errpfx("TestAssociate(table2)");
131
System.out.println("Primary database is " + shownull(table));
132
System.out.println("Secondary database is " + shownull(table2));
135
table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
136
table2.open(null, FileName + "2", null,
137
Db.DB_BTREE, Db.DB_CREATE, 0644);
138
table.associate(null, table2, new Capitalize(), 0);
141
// Insert records into the database, where the key is the user
142
// input and the data is the user input in reverse order.
144
Reader reader = new StringReader("abc\ndef\njhi");
147
String line = askForLine(reader, System.out, "input> ");
151
String reversed = (new StringBuffer(line)).reverse().toString();
153
// See definition of StringDbt below
155
StringDbt key = new StringDbt(line);
156
StringDbt data = new StringDbt(reversed);
161
if ((err = table.put(null,
162
key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
163
System.out.println("Key " + line + " already exists.");
166
catch (DbException dbe)
168
System.out.println(dbe.toString());
170
System.out.println("");
173
// Acquire an iterator for the table.
175
iterator = table2.cursor(null, 0);
177
// Walk through the table, printing the key/data pairs.
178
// See class StringDbt defined below.
180
StringDbt key = new StringDbt();
181
StringDbt data = new StringDbt();
182
StringDbt pkey = new StringDbt();
184
while (iterator.get(key, data, Db.DB_NEXT) == 0)
186
System.out.println(key.getString() + " : " + data.getString());
190
System.out.println("get BC returns " + table2.get(null, key, data, 0));
191
System.out.println(" values: " + key.getString() + " : " + data.getString());
192
System.out.println("pget BC returns " + table2.pget(null, key, pkey, data, 0));
193
System.out.println(" values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
195
System.out.println("get KL returns " + table2.get(null, key, data, 0));
196
System.out.println(" values: " + key.getString() + " : " + data.getString());
197
System.out.println("pget KL returns " + table2.pget(null, key, pkey, data, 0));
198
System.out.println(" values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
204
// Here's an example of how you can extend a Dbt in a straightforward
205
// way to allow easy storage/retrieval of strings, or whatever
206
// kind of data you wish. We've declared it as a static inner
207
// class, but it need not be.
210
class StringDbt extends Dbt
214
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
217
StringDbt(String value)
220
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
223
void setString(String value)
225
set_data(value.getBytes());
226
set_size(value.length());
231
return new String(get_data(), 0, get_size());
234
public String toString()
236
return "StringDbt=" + getString();
240
/* creates a stupid secondary index as follows:
241
For an N letter key, we use N-1 letters starting at
242
position 1. If the new letters are already capitalized,
243
we return the old array, but with offset set to 1.
244
If the letters are not capitalized, we create a new,
245
capitalized array. This is pretty stupid for
246
an application, but it tests all the paths in the runtime.
248
public static class Capitalize implements DbSecondaryKeyCreate
250
public int secondary_key_create(Db secondary, Dbt key, Dbt value,
254
String which = "unknown db";
255
if (saveddb1.equals(secondary)) {
258
else if (saveddb2.equals(secondary)) {
261
System.out.println("secondary_key_create, Db: " + shownull(secondary) + "(" + which + "), key: " + show_dbt(key) + ", data: " + show_dbt(value));
262
int len = key.get_size();
263
byte[] arr = key.get_data();
264
boolean capped = true;
267
throw new DbException("bad key");
270
return Db.DB_DONOTINDEX;
272
result.set_size(len - 1);
273
for (int i=1; capped && i<len; i++) {
274
if (!Character.isUpperCase((char)arr[i]))
278
System.out.println(" creating key(1): " + new String(arr, 1, len-1));
279
result.set_data(arr);
280
result.set_offset(1);
283
System.out.println(" creating key(2): " + (new String(arr)).substring(1).
285
result.set_data((new String(arr)).substring(1).
286
toUpperCase().getBytes());
292
public int dup_compare(Db db, Dbt dbt1, Dbt dbt2)
294
System.out.println("compare");
295
int sz1 = dbt1.get_size();
296
int sz2 = dbt2.get_size();
301
byte[] data1 = dbt1.get_data();
302
byte[] data2 = dbt2.get_data();
303
for (int i=0; i<sz1; i++)
304
if (data1[i] != data2[i])
305
return (data1[i] < data2[i] ? -1 : 1);
309
public static int nseen = 0;
310
public static Hashtable ht = new Hashtable();
312
public static String show_dbt(Dbt dbt)
319
name = (String)ht.get(dbt);
321
name = "Dbt" + (nseen++);
325
byte[] value = dbt.get_data();
327
return name + "(null)";
329
return name + "(\"" + new String(value) + "\")";