summaryrefslogtreecommitdiff
path: root/db-4.8.30/examples_java/src/db/BtRecExample.java
blob: 71beda010102c05924095e1448816f3157c68745 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */


package db;

import com.sleepycat.db.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;

public class BtRecExample {
    static final String progname =  "BtRecExample"; // Program name.
    static final String database =  "access.db";
    static final String wordlist =  "../test/wordlist";

    BtRecExample(BufferedReader reader)
        throws DatabaseException, IOException, FileNotFoundException {

        OperationStatus status;

        // Remove the previous database.
        File f = new File(database);
        f.delete();

        DatabaseConfig config = new DatabaseConfig();

        config.setErrorStream(System.err);
        config.setErrorPrefix(progname);
        config.setPageSize(1024);           // 1K page sizes.

        config.setBtreeRecordNumbers(true);
        config.setType(DatabaseType.BTREE);
        config.setAllowCreate(true);
        db = new Database(database, null, config);

        //
        // Insert records into the database, where the key is the word
        // preceded by its record number, and the data is the same, but
        // in reverse order.
        //

        for (int cnt = 1; cnt <= 1000; ++cnt) {
            String numstr = String.valueOf(cnt);
            while (numstr.length() < 4)
                numstr = "0" + numstr;
            String buf = numstr + '_' + reader.readLine();
            StringBuffer rbuf = new StringBuffer(buf).reverse();

            StringEntry key = new StringEntry(buf);
            StringEntry data = new StringEntry(rbuf.toString());

            status = db.putNoOverwrite(null, key, data);
            if (status != OperationStatus.SUCCESS &&
                status!= OperationStatus.KEYEXIST)
                throw new DatabaseException("Database.put failed " + status);
        }
    }

    void run() throws DatabaseException {
        int recno;
        OperationStatus status;

        // Acquire a cursor for the database.
        cursor = db.openCursor(null, null);

        //
        // Prompt the user for a record number, then retrieve and display
        // that record.
        //
        InputStreamReader reader = new InputStreamReader(System.in);

        for (;;) {
            // Get a record number.
            String line = askForLine(reader, System.out, "recno #> ");
            if (line == null)
                break;

            try {
                recno = Integer.parseInt(line);
            } catch (NumberFormatException nfe) {
                System.err.println("Bad record number: " + nfe);
                continue;
            }

            //
            // Start with a fresh key each time, the db.get() routine returns
            // the key and data pair, not just the key!
            //
            RecnoStringEntry key = new RecnoStringEntry(recno, 4);
            RecnoStringEntry data = new RecnoStringEntry(4);

            status = cursor.getSearchRecordNumber(key, data, null);
            if (status != OperationStatus.SUCCESS)
                throw new DatabaseException("Cursor.setRecno failed: " + status);

            // Display the key and data.
            show("k/d\t", key, data);

            // Move the cursor a record forward.
            status = cursor.getNext(key, data, null);
            if (status != OperationStatus.SUCCESS)
                throw new DatabaseException("Cursor.getNext failed: " + status);

            // Display the key and data.
            show("next\t", key, data);

            RecnoStringEntry datano = new RecnoStringEntry(4);

            //
            // Retrieve the record number for the following record into
            // local memory.
            //
            status = cursor.getRecordNumber(datano, null);
            if (status != OperationStatus.SUCCESS &&
                status != OperationStatus.NOTFOUND &&
                status != OperationStatus.KEYEMPTY)
                throw new DatabaseException("Cursor.get failed: " + status);
            else {
                recno = datano.getRecordNumber();
                System.out.println("retrieved recno: " + recno);
            }
        }

        cursor.close();
        cursor = null;
    }

    //
    // Print out the number of records in the database.
    //
    void stats() throws DatabaseException {
        BtreeStats stats;

        stats = (BtreeStats)db.getStats(null, null);
        System.out.println(progname + ": database contains " +
               stats.getNumData() + " records");
    }

    void show(String msg, RecnoStringEntry key, RecnoStringEntry data)
        throws DatabaseException {

        System.out.println(msg + key.getString() + ": " + data.getString());
    }

    public void shutdown() throws DatabaseException {
        if (cursor != null) {
            cursor.close();
            cursor = null;
        }
        if (db != null) {
            db.close();
            db = null;
        }
    }

    public static void main(String[] argv) {
        try {
            // Open the word database.
            FileReader freader = new FileReader(wordlist);

            BtRecExample app = new BtRecExample(new BufferedReader(freader));

            // Close the word database.
            freader.close();
            freader = null;

            app.stats();
            app.run();
        } catch (FileNotFoundException fnfe) {
            System.err.println(progname + ": unexpected open error " + fnfe);
            System.exit (1);
        } catch (IOException ioe) {
            System.err.println(progname + ": open " + wordlist + ": " + ioe);
            System.exit (1);
        } catch (DatabaseException dbe) {
            System.err.println("Exception: " + dbe);
            System.exit(dbe.getErrno());
        }

        System.exit(0);
    }

    // Prompts for a line, and keeps prompting until a non blank
    // line is returned.  Returns null on error.
    //
    public static String askForLine(InputStreamReader reader,
                                    PrintStream out, String prompt) {
        String result = "";
        while (result != null && result.length() == 0) {
            out.print(prompt);
            out.flush();
            result = getLine(reader);
        }
        return result;
    }

    // Not terribly efficient, but does the job.
    // Works for reading a line from stdin or a file.
    // Returns null on EOF.  If EOF appears in the middle
    // of a line, returns that line, then null on next call.
    //
    public static String getLine(InputStreamReader reader) {
        StringBuffer b = new StringBuffer();
        int c;
        try {
            while ((c = reader.read()) != -1 && c != '\n') {
                if (c != '\r')
                    b.append((char)c);
            }
        } catch (IOException ioe) {
            c = -1;
        }

        if (c == -1 && b.length() == 0)
            return null;
        else
            return b.toString();
    }

    private Cursor cursor;
    private Database db;

    // Here's an example of how you can extend DatabaseEntry in a
    // straightforward way to allow easy storage/retrieval of strings.
    // We've declared it as a static inner class, but it need not be.
    //
    static class StringEntry extends DatabaseEntry {
        StringEntry() {}

        StringEntry(String value) {
            setString(value);
        }

        void setString(String value) {
            byte[] data = value.getBytes();
            setData(data);
            setSize(data.length);
        }

        String getString() {
            return new String(getData(), 0, getSize());
        }
    }

    // Here's an example of how you can extend DatabaseEntry to store
    // (potentially) both recno's and strings in the same structure.
    //
    static class RecnoStringEntry extends DatabaseEntry {
        RecnoStringEntry(int maxsize) {
            this(0, maxsize);     // let other constructor do most of the work
        }

        RecnoStringEntry(int value, int maxsize) {
            arr = new byte[maxsize];
            setRecordNumber(value);
            setSize(arr.length);
        }

        RecnoStringEntry(String value) {
            byte[] data = value.getBytes();
            setData(data);                // use our local array for data
            setUserBuffer(data.length, true);
        }

        void setString(String value) {
            byte[] data = value.getBytes();
            setData(data);
            setSize(data.length);
        }

        String getString() {
            return new String(getData(), getOffset(), getSize());
        }

        byte[] arr;
    }
}