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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997-2002
* Sleepycat Software. All rights reserved.
*
* $Id: TestAssociate.java,v 1.4 2002/08/16 19:35:54 dda Exp $
*/
package com.sleepycat.test;
import com.sleepycat.db.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Reader;
import java.io.StringReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Hashtable;
public class TestAssociate
implements DbDupCompare
{
private static final String FileName = "access.db";
public static Db saveddb1 = null;
public static Db saveddb2 = null;
public TestAssociate()
{
}
private static void usage()
{
System.err.println("usage: TestAssociate\n");
System.exit(1);
}
public static void main(String argv[])
{
try
{
TestAssociate app = new TestAssociate();
app.run();
}
catch (DbException dbe)
{
System.err.println("TestAssociate: " + dbe.toString());
System.exit(1);
}
catch (FileNotFoundException fnfe)
{
System.err.println("TestAssociate: " + fnfe.toString());
System.exit(1);
}
System.exit(0);
}
public static int counter = 0;
public static String results[] = { "abc", "def", "ghi", "JKL", "MNO", null };
// Prompts for a line, and keeps prompting until a non blank
// line is returned. Returns null on error.
//
static public String askForLine(Reader reader,
PrintStream out, String prompt)
{
/*
String result = "";
while (result != null && result.length() == 0) {
out.print(prompt);
out.flush();
result = getLine(reader);
}
return result;
*/
return results[counter++];
}
// Not terribly efficient, but does the job.
// Works for reading a line from stdin or a file.
// Returns null on EOF. If EOF appears in the middle
// of a line, returns that line, then null on next call.
//
static public String getLine(Reader 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();
}
static public String shownull(Object o)
{
if (o == null)
return "null";
else
return "not null";
}
public void run()
throws DbException, FileNotFoundException
{
// Remove the previous database.
new File(FileName).delete();
// Create the database object.
// There is no environment for this simple example.
DbEnv dbenv = new DbEnv(0);
dbenv.open("./", Db.DB_CREATE|Db.DB_INIT_MPOOL, 0644);
(new java.io.File(FileName)).delete();
Db table = new Db(dbenv, 0);
Db table2 = new Db(dbenv, 0);
table2.set_dup_compare(this);
table2.set_flags(Db.DB_DUPSORT);
table.set_error_stream(System.err);
table2.set_error_stream(System.err);
table.set_errpfx("TestAssociate");
table2.set_errpfx("TestAssociate(table2)");
System.out.println("Primary database is " + shownull(table));
System.out.println("Secondary database is " + shownull(table2));
saveddb1 = table;
saveddb2 = table2;
table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
table2.open(null, FileName + "2", null,
Db.DB_BTREE, Db.DB_CREATE, 0644);
table.associate(null, table2, new Capitalize(), 0);
//
// Insert records into the database, where the key is the user
// input and the data is the user input in reverse order.
//
Reader reader = new StringReader("abc\ndef\njhi");
for (;;) {
String line = askForLine(reader, System.out, "input> ");
if (line == null)
break;
String reversed = (new StringBuffer(line)).reverse().toString();
// See definition of StringDbt below
//
StringDbt key = new StringDbt(line);
StringDbt data = new StringDbt(reversed);
try
{
int err;
if ((err = table.put(null,
key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
System.out.println("Key " + line + " already exists.");
}
}
catch (DbException dbe)
{
System.out.println(dbe.toString());
}
System.out.println("");
}
// Acquire an iterator for the table.
Dbc iterator;
iterator = table2.cursor(null, 0);
// Walk through the table, printing the key/data pairs.
// See class StringDbt defined below.
//
StringDbt key = new StringDbt();
StringDbt data = new StringDbt();
StringDbt pkey = new StringDbt();
while (iterator.get(key, data, Db.DB_NEXT) == 0)
{
System.out.println(key.getString() + " : " + data.getString());
}
key.setString("BC");
System.out.println("get BC returns " + table2.get(null, key, data, 0));
System.out.println(" values: " + key.getString() + " : " + data.getString());
System.out.println("pget BC returns " + table2.pget(null, key, pkey, data, 0));
System.out.println(" values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
key.setString("KL");
System.out.println("get KL returns " + table2.get(null, key, data, 0));
System.out.println(" values: " + key.getString() + " : " + data.getString());
System.out.println("pget KL returns " + table2.pget(null, key, pkey, data, 0));
System.out.println(" values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
iterator.close();
table.close(0);
}
// Here's an example of how you can extend a Dbt in a straightforward
// way to allow easy storage/retrieval of strings, or whatever
// kind of data you wish. We've declared it as a static inner
// class, but it need not be.
//
static /*inner*/
class StringDbt extends Dbt
{
StringDbt()
{
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
}
StringDbt(String value)
{
setString(value);
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
}
void setString(String value)
{
set_data(value.getBytes());
set_size(value.length());
}
String getString()
{
return new String(get_data(), 0, get_size());
}
public String toString()
{
return "StringDbt=" + getString();
}
}
/* creates a stupid secondary index as follows:
For an N letter key, we use N-1 letters starting at
position 1. If the new letters are already capitalized,
we return the old array, but with offset set to 1.
If the letters are not capitalized, we create a new,
capitalized array. This is pretty stupid for
an application, but it tests all the paths in the runtime.
*/
public static class Capitalize implements DbSecondaryKeyCreate
{
public int secondary_key_create(Db secondary, Dbt key, Dbt value,
Dbt result)
throws DbException
{
String which = "unknown db";
if (saveddb1.equals(secondary)) {
which = "primary";
}
else if (saveddb2.equals(secondary)) {
which = "secondary";
}
System.out.println("secondary_key_create, Db: " + shownull(secondary) + "(" + which + "), key: " + show_dbt(key) + ", data: " + show_dbt(value));
int len = key.get_size();
byte[] arr = key.get_data();
boolean capped = true;
if (len < 1)
throw new DbException("bad key");
if (len < 2)
return Db.DB_DONOTINDEX;
result.set_size(len - 1);
for (int i=1; capped && i<len; i++) {
if (!Character.isUpperCase((char)arr[i]))
capped = false;
}
if (capped) {
System.out.println(" creating key(1): " + new String(arr, 1, len-1));
result.set_data(arr);
result.set_offset(1);
}
else {
System.out.println(" creating key(2): " + (new String(arr)).substring(1).
toUpperCase());
result.set_data((new String(arr)).substring(1).
toUpperCase().getBytes());
}
return 0;
}
}
public int dup_compare(Db db, Dbt dbt1, Dbt dbt2)
{
System.out.println("compare");
int sz1 = dbt1.get_size();
int sz2 = dbt2.get_size();
if (sz1 < sz2)
return -1;
if (sz1 > sz2)
return 1;
byte[] data1 = dbt1.get_data();
byte[] data2 = dbt2.get_data();
for (int i=0; i<sz1; i++)
if (data1[i] != data2[i])
return (data1[i] < data2[i] ? -1 : 1);
return 0;
}
public static int nseen = 0;
public static Hashtable ht = new Hashtable();
public static String show_dbt(Dbt dbt)
{
String name;
if (dbt == null)
return "null dbt";
name = (String)ht.get(dbt);
if (name == null) {
name = "Dbt" + (nseen++);
ht.put(dbt, name);
}
byte[] value = dbt.get_data();
if (value == null)
return name + "(null)";
else
return name + "(\"" + new String(value) + "\")";
}
}
|