1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: BulkAccessExample.java,v 12.8 2008/01/08 20:58:32 bostic Exp $
7 */
8
9package db;
10
11import com.sleepycat.db.*;
12import java.io.File;
13import java.io.FileNotFoundException;
14import java.io.InputStreamReader;
15import java.io.IOException;
16import java.io.PrintStream;
17
18class BulkAccessExample {
19    private static final String FileName = "access.db";
20
21    public BulkAccessExample() {
22    }
23
24    public static void main(String[] argv) {
25        try {
26            BulkAccessExample app = new BulkAccessExample();
27            app.run();
28        } catch (DatabaseException dbe) {
29            System.err.println("BulkAccessExample: " + dbe.toString());
30            System.exit(1);
31        } catch (FileNotFoundException fnfe) {
32            System.err.println("BulkAccessExample: " + fnfe.toString());
33            System.exit(1);
34        }
35        System.exit(0);
36    }
37
38    // Prompts for a line, and keeps prompting until a non blank
39    // line is returned.  Returns null on error.
40    //
41    public static String askForLine(InputStreamReader reader,
42                                    PrintStream out, String prompt) {
43        String result = "";
44        while (result != null && result.length() == 0) {
45            out.print(prompt);
46            out.flush();
47            result = getLine(reader);
48        }
49        return result;
50    }
51
52    // Not terribly efficient, but does the job.
53    // Works for reading a line from stdin or a file.
54    // Returns null on EOF.  If EOF appears in the middle
55    // of a line, returns that line, then null on next call.
56    //
57    public static String getLine(InputStreamReader reader) {
58        StringBuffer b = new StringBuffer();
59        int c;
60        try {
61            while ((c = reader.read()) != -1 && c != '\n') {
62                if (c != '\r')
63                    b.append((char)c);
64            }
65        } catch (IOException ioe) {
66            c = -1;
67        }
68
69        if (c == -1 && b.length() == 0)
70            return null;
71        else
72            return b.toString();
73    }
74
75    public void run() throws DatabaseException, FileNotFoundException {
76        // Remove the previous database.
77        new File(FileName).delete();
78
79        // Create the database object.
80        // There is no environment for this simple example.
81        DatabaseConfig config = new DatabaseConfig();
82        config.setErrorStream(System.err);
83        config.setErrorPrefix("BulkAccessExample");
84        config.setType(DatabaseType.BTREE);
85        config.setAllowCreate(true);
86        config.setMode(0644);
87        Database table = new Database(FileName, null, config);
88
89        //
90        // Insert records into the database, where the key is the user
91        // input and the data is the user input in reverse order.
92        //
93        InputStreamReader reader = new InputStreamReader(System.in);
94
95        for (;;) {
96            String line = askForLine(reader, System.out, "input> ");
97            if (line == null)
98                break;
99
100            String reversed = (new StringBuffer(line)).reverse().toString();
101
102            // See definition of StringEntry below
103            //
104            StringEntry key = new StringEntry(line);
105            StringEntry data = new StringEntry(reversed);
106
107            try {
108                if (table.putNoOverwrite(null, key, data) == OperationStatus.KEYEXIST)
109                    System.out.println("Key " + line + " already exists.");
110            } catch (DatabaseException dbe) {
111                System.out.println(dbe.toString());
112            }
113            System.out.println("");
114        }
115
116        // Acquire a cursor for the table.
117        Cursor cursor = table.openCursor(null, null);
118        DatabaseEntry foo = new DatabaseEntry();
119
120        MultipleKeyDataEntry bulk_data = new MultipleKeyDataEntry();
121        bulk_data.setData(new byte[1024 * 1024]);
122        bulk_data.setUserBuffer(1024 * 1024, true);
123
124        // Walk through the table, printing the key/data pairs.
125        //
126        while (cursor.getNext(foo, bulk_data, null) == OperationStatus.SUCCESS) {
127            StringEntry key, data;
128            key = new StringEntry();
129            data = new StringEntry();
130
131            while (bulk_data.next(key, data))
132                System.out.println(key.getString() + " : " + data.getString());
133        }
134        cursor.close();
135        table.close();
136    }
137
138    // Here's an example of how you can extend DatabaseEntry in a
139    // straightforward way to allow easy storage/retrieval of strings, or
140    // whatever kind of data you wish.  We've declared it as a static inner
141    // class, but it need not be.
142    //
143    static class StringEntry extends DatabaseEntry {
144        StringEntry() {
145        }
146
147        StringEntry(String value) {
148            setString(value);
149        }
150
151        void setString(String value) {
152            byte[] data = value.getBytes();
153            setData(data);
154            setSize(data.length);
155        }
156
157        String getString() {
158            return new String(getData(), getOffset(), getSize());
159        }
160    }
161}
162