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
|
/*-
* Copyright (c) 2002,2009 Oracle. All rights reserved.
*
*/
package db;
import com.sleepycat.db.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
class SecondaryExample
{
private static final String progname = "SecondaryExample";
private static final String DATABASE_HOME = "TESTDIR";
public static void main(String[] args)
{
try {
SecondaryExample app = new SecondaryExample();
app.run();
} catch(Exception e) {
System.err.println(progname + ": " + e);
e.printStackTrace(System.err);
System.exit(1);
}
}
void run() throws DbException, FileNotFoundException
{
DbEnv dbenv = new DbEnv(0);
/* Open the environment. */
dbenv.open(DATABASE_HOME,
Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_LOG |
Db.DB_INIT_MPOOL | Db.DB_INIT_TXN, 0);
try {
run_app(dbenv);
} finally {
dbenv.close(0);
}
}
private void run_app(DbEnv dbenv)
throws DbException, FileNotFoundException
{
Db dbp, sdbp;
Dbt key, pkey, skey, data;
StudentRecord srec;
/* Open/create primary */
dbp = new Db(dbenv, 0);
dbp.open(null, "students.db", null, Db.DB_BTREE, Db.DB_CREATE,
0600);
/*
* Open/create secondary. Note that it supports duplicate data
* items, since last names might not be unique.
*/
sdbp = new Db(dbenv, 0);
sdbp.set_flags(Db.DB_DUP | Db.DB_DUPSORT);
sdbp.open(null, "lastname.db", null, Db.DB_BTREE, Db.DB_CREATE,
0600);
try {
/* Associate the secondary with the primary. */
dbp.associate(sdbp, new GetName(), 0);
/* Add a new record */
key = new Dbt();
key.set_data("WC42".getBytes());
key.set_size(4);
srec = new StudentRecord();
srec.student_id = "WC42";
srec.last_name = "Churchill ";
srec.first_name = "Winston ";
data = new Dbt();
srec.encode(data);
System.out.println("Adding a record with primary key " +
new String(key.get_data()) + " and secondary key " +
srec.last_name);
dbp.put(null, key, data, 0);
/* Now do a lookup */
skey = new Dbt();
pkey = new Dbt();
data = new Dbt();
skey.set_data("Churchill ".getBytes());
skey.set_size(15);
System.out.println("Searching with secondary key " +
new String(skey.get_data()));
sdbp.pget(null, skey, pkey, data, 0);
System.out.println("Found a record with primary key " +
new String(pkey.get_data()));
} finally {
dbp.close(0);
sdbp.close(0);
}
}
/*
* getname -- extracts a secondary key (the last name) from a primary
* key/data pair
*/
class GetName implements DbSecondaryKeyCreate {
public int secondary_key_create(Db secondary,
Dbt pkey, Dbt pdata, Dbt skey) {
StudentRecord srec = new StudentRecord();
srec.decode(pdata);
// Make a fixed-length array of last_name
byte[] last_name_data = srec.last_name.getBytes();
byte[] last_name_raw = new byte[15];
System.arraycopy(last_name_data, 0, last_name_raw, 0,
last_name_data.length);
skey.set_data(last_name_raw);
skey.set_size(last_name_raw.length);
return (0);
}
}
class StudentRecord
{
String student_id; // assumed to be 4 bytes long
String last_name; // assumed to be 15 bytes long
String first_name; // assumed to be 15 bytes long
void decode(Dbt dbt) {
byte[] data = dbt.get_data();
student_id = new String(data, 0, 4);
last_name = new String(data, 4, 15);
first_name = new String(data, 19, 15);
}
void encode(Dbt dbt) {
byte[] data = new byte[34];
System.arraycopy(student_id.getBytes(), 0, data, 0, 4);
byte[] last_name_raw = last_name.getBytes();
System.arraycopy(last_name_raw, 0, data, 4,
last_name_raw.length);
byte[] first_name_raw = first_name.getBytes();
System.arraycopy(first_name_raw, 0, data, 19,
first_name_raw.length);
dbt.set_data(data);
dbt.set_size(data.length);
}
}
}
|