1
package com.sleepycat.test;
2
import com.sleepycat.db.*;
4
public class CallbackTest
6
public static void main(String args[])
9
Db db = new Db(null, 0);
10
db.set_bt_compare(new BtreeCompare());
11
db.open(null, "test.db", "", Db.DB_BTREE, Db.DB_CREATE, 0666);
12
StringDbt[] keys = new StringDbt[10];
13
StringDbt[] datas = new StringDbt[10];
14
for (int i = 0; i<10; i++) {
15
int val = (i * 3) % 10;
16
keys[i] = new StringDbt("key" + val);
17
datas[i] = new StringDbt("data" + val);
18
System.out.println("put " + val);
19
db.put(null, keys[i], datas[i], 0);
22
catch (DbException dbe) {
23
System.err.println("FAIL: " + dbe);
25
catch (java.io.FileNotFoundException fnfe) {
26
System.err.println("FAIL: " + fnfe);
35
implements DbBtreeCompare
37
/* A weird comparator, for example.
38
* In fact, it may not be legal, since it's not monotonically increasing.
40
public int bt_compare(Db db, Dbt dbt1, Dbt dbt2)
42
System.out.println("compare function called");
43
byte b1[] = dbt1.get_data();
44
byte b2[] = dbt2.get_data();
45
System.out.println(" " + (new String(b1)) + ", " + (new String(b2)));
49
return (len1 < len2) ? 1 : -1;
51
for (int i=0; i<len1; i++) {
53
return (b1[i] < b2[i]) ? value : -value;
60
class StringDbt extends Dbt
64
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
67
StringDbt(String value)
70
set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
73
void setString(String value)
75
set_data(value.getBytes());
76
set_size(value.length());
81
return new String(get_data(), 0, get_size());