~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/java/src/com/sleepycat/examples/BtRecExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.10.5 upstream)
  • mto: This revision was merged to the branch mainline in revision 128.
  • Revision ID: james.westby@ubuntu.com-20100517170206-xu1wmjuy40nt2sk0
Tags: upstream-2.30.1
ImportĀ upstreamĀ versionĀ 2.30.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1997-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
 
10
 
package com.sleepycat.examples;
11
 
 
12
 
import com.sleepycat.db.*;
13
 
import java.io.BufferedReader;
14
 
import java.io.File;
15
 
import java.io.FileNotFoundException;
16
 
import java.io.FileReader;
17
 
import java.io.FileWriter;
18
 
import java.io.InputStreamReader;
19
 
import java.io.IOException;
20
 
import java.io.PrintStream;
21
 
 
22
 
public class BtRecExample
23
 
{
24
 
    static final String progname =  "BtRecExample";     // Program name.
25
 
    static final String database =  "access.db";
26
 
    static final String wordlist =  "../test/wordlist";
27
 
 
28
 
    BtRecExample(BufferedReader reader)
29
 
        throws DbException, IOException, FileNotFoundException
30
 
    {
31
 
        int ret;
32
 
 
33
 
        // Remove the previous database.
34
 
        File f = new File(database);
35
 
        f.delete();
36
 
 
37
 
        dbp = new Db(null, 0);
38
 
 
39
 
        dbp.set_error_stream(System.err);
40
 
        dbp.set_errpfx(progname);
41
 
        dbp.set_pagesize(1024);                 // 1K page sizes.
42
 
 
43
 
        dbp.set_flags(Db.DB_RECNUM);                    // Record numbers.
44
 
        dbp.open(null, database, null, Db.DB_BTREE, Db.DB_CREATE, 0664);
45
 
 
46
 
        //
47
 
        // Insert records into the database, where the key is the word
48
 
        // preceded by its record number, and the data is the same, but
49
 
        // in reverse order.
50
 
        //
51
 
 
52
 
        for (int cnt = 1; cnt <= 1000; ++cnt) {
53
 
            String numstr = String.valueOf(cnt);
54
 
            while (numstr.length() < 4)
55
 
                numstr = "0" + numstr;
56
 
            String buf = numstr + '_' + reader.readLine();
57
 
            StringBuffer rbuf = new StringBuffer(buf).reverse();
58
 
 
59
 
            StringDbt key = new StringDbt(buf);
60
 
            StringDbt data = new StringDbt(rbuf.toString());
61
 
 
62
 
            if ((ret = dbp.put(null, key, data, Db.DB_NOOVERWRITE)) != 0) {
63
 
                if (ret != Db.DB_KEYEXIST)
64
 
                    throw new DbException("Db.put failed" + ret);
65
 
            }
66
 
        }
67
 
    }
68
 
 
69
 
    void run()
70
 
        throws DbException
71
 
    {
72
 
        int recno;
73
 
        int ret;
74
 
 
75
 
        // Acquire a cursor for the database.
76
 
        dbcp = dbp.cursor(null, 0);
77
 
 
78
 
        //
79
 
        // Prompt the user for a record number, then retrieve and display
80
 
        // that record.
81
 
        //
82
 
        InputStreamReader reader = new InputStreamReader(System.in);
83
 
 
84
 
        for (;;) {
85
 
            // Get a record number.
86
 
            String line = askForLine(reader, System.out, "recno #> ");
87
 
            if (line == null)
88
 
                break;
89
 
 
90
 
            try {
91
 
                recno = Integer.parseInt(line);
92
 
            }
93
 
            catch (NumberFormatException nfe) {
94
 
                System.err.println("Bad record number: " + nfe);
95
 
                continue;
96
 
            }
97
 
 
98
 
            //
99
 
            // Start with a fresh key each time, the dbp.get() routine returns
100
 
            // the key and data pair, not just the key!
101
 
            //
102
 
            RecnoStringDbt key = new RecnoStringDbt(recno, 100);
103
 
            RecnoStringDbt data = new RecnoStringDbt(100);
104
 
 
105
 
            if ((ret = dbcp.get(key, data, Db.DB_SET_RECNO)) != 0) {
106
 
                throw new DbException("Dbc.get failed", ret);
107
 
            }
108
 
 
109
 
            // Display the key and data.
110
 
            show("k/d\t", key, data);
111
 
 
112
 
            // Move the cursor a record forward.
113
 
            if ((ret = dbcp.get(key, data, Db.DB_NEXT)) != 0) {
114
 
                throw new DbException("Dbc.get failed", ret);
115
 
            }
116
 
 
117
 
            // Display the key and data.
118
 
            show("next\t", key, data);
119
 
 
120
 
            RecnoStringDbt datano = new RecnoStringDbt(100);
121
 
 
122
 
            //
123
 
            // Retrieve the record number for the following record into
124
 
            // local memory.
125
 
            //
126
 
            if ((ret = dbcp.get(key, datano, Db.DB_GET_RECNO)) != 0) {
127
 
                if (ret != Db.DB_NOTFOUND && ret != Db.DB_KEYEMPTY) {
128
 
                    throw new DbException("Dbc.get failed", ret);
129
 
                }
130
 
            }
131
 
            else {
132
 
                recno = datano.getRecno();
133
 
                System.out.println("retrieved recno: " + recno);
134
 
            }
135
 
        }
136
 
 
137
 
        dbcp.close();
138
 
        dbcp = null;
139
 
    }
140
 
 
141
 
    //
142
 
    // Print out the number of records in the database.
143
 
    //
144
 
    void stats()
145
 
        throws DbException
146
 
    {
147
 
        DbBtreeStat statp;
148
 
 
149
 
        statp = (DbBtreeStat)dbp.stat(0);
150
 
        System.out.println(progname + ": database contains " +
151
 
                          statp.bt_ndata + " records");
152
 
    }
153
 
 
154
 
    void show(String msg, RecnoStringDbt key, RecnoStringDbt data)
155
 
        throws DbException
156
 
    {
157
 
        System.out.println(msg + key.getString() + ": " + data.getString());
158
 
    }
159
 
 
160
 
    public void shutdown()
161
 
        throws DbException
162
 
    {
163
 
        if (dbcp != null) {
164
 
            dbcp.close();
165
 
            dbcp = null;
166
 
        }
167
 
        if (dbp != null) {
168
 
            dbp.close(0);
169
 
            dbp = null;
170
 
        }
171
 
    }
172
 
 
173
 
    public static void main(String argv[])
174
 
    {
175
 
 
176
 
        try {
177
 
            // Open the word database.
178
 
            FileReader freader = new FileReader(wordlist);
179
 
 
180
 
            BtRecExample app = new BtRecExample(new BufferedReader(freader));
181
 
 
182
 
            // Close the word database.
183
 
            freader.close();
184
 
            freader = null;
185
 
 
186
 
            app.stats();
187
 
            app.run();
188
 
        } catch (FileNotFoundException fnfe) {
189
 
            System.err.println(progname + ": unexpected open error " + fnfe);
190
 
            System.exit (1);
191
 
        } catch (IOException ioe) {
192
 
            System.err.println(progname + ": open " + wordlist + ": " + ioe);
193
 
            System.exit (1);
194
 
        } catch (DbException dbe) {
195
 
            System.err.println("Exception: " + dbe);
196
 
            System.exit(dbe.get_errno());
197
 
        }
198
 
 
199
 
        System.exit(0);
200
 
    }
201
 
 
202
 
    // Prompts for a line, and keeps prompting until a non blank
203
 
    // line is returned.  Returns null on error.
204
 
    //
205
 
    static public String askForLine(InputStreamReader reader,
206
 
                                    PrintStream out, String prompt)
207
 
    {
208
 
        String result = "";
209
 
        while (result != null && result.length() == 0) {
210
 
            out.print(prompt);
211
 
            out.flush();
212
 
            result = getLine(reader);
213
 
        }
214
 
        return result;
215
 
    }
216
 
 
217
 
    // Not terribly efficient, but does the job.
218
 
    // Works for reading a line from stdin or a file.
219
 
    // Returns null on EOF.  If EOF appears in the middle
220
 
    // of a line, returns that line, then null on next call.
221
 
    //
222
 
    static public String getLine(InputStreamReader reader)
223
 
    {
224
 
        StringBuffer b = new StringBuffer();
225
 
        int c;
226
 
        try {
227
 
            while ((c = reader.read()) != -1 && c != '\n') {
228
 
                if (c != '\r')
229
 
                    b.append((char)c);
230
 
            }
231
 
        }
232
 
        catch (IOException ioe) {
233
 
            c = -1;
234
 
        }
235
 
 
236
 
        if (c == -1 && b.length() == 0)
237
 
            return null;
238
 
        else
239
 
            return b.toString();
240
 
    }
241
 
 
242
 
    private Dbc dbcp;
243
 
    private Db dbp;
244
 
 
245
 
    // Here's an example of how you can extend a Dbt in a straightforward
246
 
    // way to allow easy storage/retrieval of strings.
247
 
    // We've declared it as a static inner class, but it need not be.
248
 
    //
249
 
    static /*inner*/
250
 
    class StringDbt extends Dbt
251
 
    {
252
 
        StringDbt(byte[] arr)
253
 
        {
254
 
            set_flags(Db.DB_DBT_USERMEM);
255
 
            set_data(arr);
256
 
            set_size(arr.length);
257
 
        }
258
 
 
259
 
        StringDbt()
260
 
        {
261
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
262
 
        }
263
 
 
264
 
        StringDbt(String value)
265
 
        {
266
 
            setString(value);
267
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
268
 
        }
269
 
 
270
 
        void setString(String value)
271
 
        {
272
 
            byte[] data = value.getBytes();
273
 
            set_data(data);
274
 
            set_size(data.length);
275
 
            // must set ulen because sometimes a string is returned
276
 
            set_ulen(data.length);
277
 
        }
278
 
 
279
 
        String getString()
280
 
        {
281
 
            return new String(get_data(), 0, get_size());
282
 
        }
283
 
    }
284
 
 
285
 
    // Here's an example of how you can extend a Dbt to store
286
 
    // (potentially) both recno's and strings in the same
287
 
    // structure.
288
 
    //
289
 
    static /*inner*/
290
 
    class RecnoStringDbt extends Dbt
291
 
    {
292
 
        RecnoStringDbt(int maxsize)
293
 
        {
294
 
            this(0, maxsize);     // let other constructor do most of the work
295
 
        }
296
 
 
297
 
        RecnoStringDbt(int value, int maxsize)
298
 
        {
299
 
            set_flags(Db.DB_DBT_USERMEM); // do not allocate on retrieval
300
 
            arr = new byte[maxsize];
301
 
            set_data(arr);                // use our local array for data
302
 
            set_ulen(maxsize);           // size of return storage
303
 
            setRecno(value);
304
 
        }
305
 
 
306
 
        RecnoStringDbt(String value, int maxsize)
307
 
        {
308
 
            set_flags(Db.DB_DBT_USERMEM); // do not allocate on retrieval
309
 
            arr = new byte[maxsize];
310
 
            set_data(arr);                // use our local array for data
311
 
            set_ulen(maxsize);           // size of return storage
312
 
            setString(value);
313
 
        }
314
 
 
315
 
        void setRecno(int value)
316
 
        {
317
 
            set_recno_key_data(value);
318
 
            set_size(arr.length);
319
 
        }
320
 
 
321
 
        void setString(String value)
322
 
        {
323
 
            byte[] data = value.getBytes();
324
 
            set_data(data);
325
 
            set_size(data.length);
326
 
        }
327
 
 
328
 
        int getRecno()
329
 
        {
330
 
            return get_recno_key_data();
331
 
        }
332
 
 
333
 
        String getString()
334
 
        {
335
 
            return new String(get_data(), 0, get_size());
336
 
        }
337
 
 
338
 
        byte arr[];
339
 
    }
340
 
}