• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/db-4.8.30/docs/programmer_reference/
1/*-
2 * Copyright (c) 2002,2009 Oracle.  All rights reserved.
3 *
4 */
5package db;
6
7import com.sleepycat.db.*;
8import java.io.File;
9import java.io.FileNotFoundException;
10import java.io.InputStreamReader;
11import java.io.IOException;
12import java.io.PrintStream;
13
14class SecondaryExample
15{
16	private static final String progname = "SecondaryExample";
17	private static final String DATABASE_HOME = "TESTDIR";
18
19	public static void main(String[] args)
20	{
21		try {
22			SecondaryExample app = new SecondaryExample();
23			app.run();
24		} catch(Exception e) {
25			System.err.println(progname + ": " + e);
26			e.printStackTrace(System.err);
27			System.exit(1);
28		}
29	}
30
31	void run() throws DbException, FileNotFoundException
32	{
33		DbEnv dbenv = new DbEnv(0);
34
35		/* Open the environment. */
36		dbenv.open(DATABASE_HOME,
37		    Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_LOG |
38		    Db.DB_INIT_MPOOL | Db.DB_INIT_TXN, 0);
39
40		try {
41			run_app(dbenv);
42		} finally {
43			dbenv.close(0);
44		}
45	}
46
47	private void run_app(DbEnv dbenv)
48		throws DbException, FileNotFoundException
49	{
50		Db dbp, sdbp;
51		Dbt key, pkey, skey, data;
52		StudentRecord srec;
53
54		/* Open/create primary */
55		dbp = new Db(dbenv, 0);
56		dbp.open(null, "students.db", null, Db.DB_BTREE, Db.DB_CREATE,
57		    0600);
58
59		/*
60		 * Open/create secondary.  Note that it supports duplicate data
61		 * items, since last names might not be unique.
62		 */
63		sdbp = new Db(dbenv, 0);
64		sdbp.set_flags(Db.DB_DUP | Db.DB_DUPSORT);
65		sdbp.open(null, "lastname.db", null, Db.DB_BTREE, Db.DB_CREATE,
66		    0600);
67
68		try {
69			/* Associate the secondary with the primary. */
70			dbp.associate(sdbp, new GetName(), 0);
71
72			/* Add a new record */
73			key = new Dbt();
74			key.set_data("WC42".getBytes());
75			key.set_size(4);
76			srec = new StudentRecord();
77			srec.student_id = "WC42";
78			srec.last_name = "Churchill      ";
79			srec.first_name = "Winston        ";
80			data = new Dbt();
81			srec.encode(data);
82
83			System.out.println("Adding a record with primary key " +
84			    new String(key.get_data()) + " and secondary key " +
85			    srec.last_name);
86			dbp.put(null, key, data, 0);
87
88			/* Now do a lookup */
89			skey = new Dbt();
90			pkey = new Dbt();
91			data = new Dbt();
92			skey.set_data("Churchill      ".getBytes());
93			skey.set_size(15);
94			System.out.println("Searching with secondary key " +
95			    new String(skey.get_data()));
96			sdbp.pget(null, skey, pkey, data, 0);
97
98			System.out.println("Found a record with primary key " +
99			    new String(pkey.get_data()));
100		} finally {
101			dbp.close(0);
102			sdbp.close(0);
103		}
104	}
105
106	/*
107	 * getname -- extracts a secondary key (the last name) from a primary
108	 * 	key/data pair
109	 */
110	class GetName implements DbSecondaryKeyCreate {
111		public int secondary_key_create(Db secondary,
112		    Dbt pkey, Dbt pdata, Dbt skey) {
113 			StudentRecord srec = new StudentRecord();
114			srec.decode(pdata);
115
116			// Make a fixed-length array of last_name
117			byte[] last_name_data = srec.last_name.getBytes();
118			byte[] last_name_raw = new byte[15];
119			System.arraycopy(last_name_data, 0, last_name_raw, 0,
120			    last_name_data.length);
121
122			skey.set_data(last_name_raw);
123			skey.set_size(last_name_raw.length);
124			return (0);
125		}
126	}
127
128	class StudentRecord
129	{
130		String student_id;	// assumed to be 4 bytes long
131		String last_name;	// assumed to be 15 bytes long
132		String first_name;	// assumed to be 15 bytes long
133
134		void decode(Dbt dbt) {
135			byte[] data = dbt.get_data();
136			student_id = new String(data, 0, 4);
137			last_name = new String(data, 4, 15);
138			first_name = new String(data, 19, 15);
139		}
140
141		void encode(Dbt dbt) {
142			byte[] data = new byte[34];
143			System.arraycopy(student_id.getBytes(), 0, data, 0, 4);
144			byte[] last_name_raw = last_name.getBytes();
145			System.arraycopy(last_name_raw, 0, data, 4,
146			    last_name_raw.length);
147			byte[] first_name_raw = first_name.getBytes();
148			System.arraycopy(first_name_raw, 0, data, 19,
149			    first_name_raw.length);
150			dbt.set_data(data);
151			dbt.set_size(data.length);
152		}
153	}
154}
155