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.InputStreamReader;
16
import java.io.IOException;
17
import java.io.PrintStream;
19
public class TestDbtFlags
21
private static final String FileName = "access.db";
22
private int flag_value;
24
private int cur_input_line = 0;
26
/*zippy quotes for test input*/
27
static final String[] input_lines = {
28
"If we shadows have offended",
29
"Think but this, and all is mended",
30
"That you have but slumber'd here",
31
"While these visions did appear",
32
"And this weak and idle theme",
33
"No more yielding but a dream",
34
"Gentles, do not reprehend",
35
"if you pardon, we will mend",
36
"And, as I am an honest Puck, if we have unearned luck",
37
"Now to 'scape the serpent's tongue, we will make amends ere long;",
38
"Else the Puck a liar call; so, good night unto you all.",
39
"Give me your hands, if we be friends, and Robin shall restore amends."
42
public TestDbtFlags(int flag_value, int buf_size)
44
this.flag_value = flag_value;
45
this.buf_size = buf_size;
48
public static void runWithFlags(int flag_value, int size)
50
String msg = "=-=-=-= Test with DBT flags " + flag_value +
52
System.out.println(msg);
53
System.err.println(msg);
57
TestDbtFlags app = new TestDbtFlags(flag_value, size);
60
catch (DbException dbe)
62
System.err.println("TestDbtFlags: " + dbe.toString());
65
catch (FileNotFoundException fnfe)
67
System.err.println("TestDbtFlags: " + fnfe.toString());
72
public static void main(String argv[])
74
runWithFlags(Db.DB_DBT_MALLOC, -1);
75
runWithFlags(Db.DB_DBT_REALLOC, -1);
76
runWithFlags(Db.DB_DBT_USERMEM, 20);
77
runWithFlags(Db.DB_DBT_USERMEM, 50);
78
runWithFlags(Db.DB_DBT_USERMEM, 200);
84
String get_input_line()
86
if (cur_input_line >= input_lines.length)
88
return input_lines[cur_input_line++];
92
throws DbException, FileNotFoundException
94
// Remove the previous database.
95
new File(FileName).delete();
97
// Create the database object.
98
// There is no environment for this simple example.
99
Db table = new Db(null, 0);
100
table.set_error_stream(System.err);
101
table.set_errpfx("TestDbtFlags");
102
table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
105
// Insert records into the database, where the key is the user
106
// input and the data is the user input in reverse order.
109
//System.err.println("input line " + cur_input_line);
110
String line = get_input_line();
114
String reversed = (new StringBuffer(line)).reverse().toString();
116
// See definition of StringDbt below
118
StringDbt key = new StringDbt(line, flag_value);
119
StringDbt data = new StringDbt(reversed, flag_value);
124
if ((err = table.put(null,
125
key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
126
System.out.println("Key " + line + " already exists.");
131
catch (DbException dbe)
133
System.out.println(dbe.toString());
137
// Acquire an iterator for the table.
139
iterator = table.cursor(null, 0);
141
// Walk through the table, printing the key/data pairs.
142
// See class StringDbt defined below.
144
StringDbt key = new StringDbt(flag_value, buf_size);
145
StringDbt data = new StringDbt(flag_value, buf_size);
147
int iteration_count = 0;
150
while (dbreturn == 0) {
151
//System.err.println("iteration " + iteration_count);
153
if ((dbreturn = iterator.get(key, data, Db.DB_NEXT)) == 0) {
154
System.out.println(key.get_string() + " : " + data.get_string());
157
catch (DbMemoryException dme) {
158
/* In a real application, we'd normally increase
159
* the size of the buffer. Since we've created
160
* this error condition for testing, we'll just report it.
161
* We still need to skip over this record, and we don't
162
* want to mess with our original Dbt's, since we want
163
* to see more errors. So create some temporary
164
* mallocing Dbts to get this record.
166
System.err.println("exception, iteration " + iteration_count +
168
System.err.println(" key size: " + key.get_size() +
169
" ulen: " + key.get_ulen());
170
System.err.println(" data size: " + key.get_size() +
171
" ulen: " + key.get_ulen());
173
dme.get_dbt().set_size(buf_size);
174
StringDbt tempkey = new StringDbt(Db.DB_DBT_MALLOC, -1);
175
StringDbt tempdata = new StringDbt(Db.DB_DBT_MALLOC, -1);
176
if ((dbreturn = iterator.get(tempkey, tempdata, Db.DB_NEXT)) != 0) {
177
System.err.println("cannot get expected next record");
180
System.out.println(tempkey.get_string() + " : " +
181
tempdata.get_string());
192
// Here's an example of how you can extend a Dbt in a straightforward
193
// way to allow easy storage/retrieval of strings, or whatever
194
// kind of data you wish. We've declared it as a static inner
195
// class, but it need not be.
198
class StringDbt extends Dbt
202
StringDbt(int flags, int buf_size)
204
this.saved_flags = flags;
205
set_flags(saved_flags);
206
if (buf_size != -1) {
207
set_data(new byte[buf_size]);
212
StringDbt(String value, int flags)
214
this.saved_flags = flags;
215
set_flags(saved_flags);
219
void set_string(String value)
221
set_data(value.getBytes());
222
set_size(value.length());
229
return new String(get_data(), 0, get_size());
234
int actual_flags = get_flags();
235
if (actual_flags != saved_flags) {
236
System.err.println("flags botch: expected " + saved_flags +
237
", got " + actual_flags);