~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to db/java/src/com/sleepycat/examples/AccessExample.java

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

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-2001
 
5
 *      Sleepycat Software.  All rights reserved.
 
6
 *
 
7
 * $Id: AccessExample.java,v 11.7 2001/05/12 21:43:27 dda Exp $
 
8
 */
 
9
 
 
10
package com.sleepycat.examples;
 
11
 
 
12
import com.sleepycat.db.*;
 
13
import java.io.File;
 
14
import java.io.FileNotFoundException;
 
15
import java.io.InputStreamReader;
 
16
import java.io.IOException;
 
17
import java.io.PrintStream;
 
18
 
 
19
class AccessExample
 
20
{
 
21
    private static final String FileName = "access.db";
 
22
 
 
23
    public AccessExample()
 
24
    {
 
25
    }
 
26
 
 
27
    public static void main(String argv[])
 
28
    {
 
29
        try
 
30
        {
 
31
            AccessExample app = new AccessExample();
 
32
            app.run();
 
33
        }
 
34
        catch (DbException dbe)
 
35
        {
 
36
            System.err.println("AccessExample: " + dbe.toString());
 
37
            System.exit(1);
 
38
        }
 
39
        catch (FileNotFoundException fnfe)
 
40
        {
 
41
            System.err.println("AccessExample: " + fnfe.toString());
 
42
            System.exit(1);
 
43
        }
 
44
        System.exit(0);
 
45
    }
 
46
 
 
47
    // Prompts for a line, and keeps prompting until a non blank
 
48
    // line is returned.  Returns null on error.
 
49
    //
 
50
    static public String askForLine(InputStreamReader reader,
 
51
                                    PrintStream out, String prompt)
 
52
    {
 
53
        String result = "";
 
54
        while (result != null && result.length() == 0) {
 
55
            out.print(prompt);
 
56
            out.flush();
 
57
            result = getLine(reader);
 
58
        }
 
59
        return result;
 
60
    }
 
61
 
 
62
    // Not terribly efficient, but does the job.
 
63
    // Works for reading a line from stdin or a file.
 
64
    // Returns null on EOF.  If EOF appears in the middle
 
65
    // of a line, returns that line, then null on next call.
 
66
    //
 
67
    static public String getLine(InputStreamReader reader)
 
68
    {
 
69
        StringBuffer b = new StringBuffer();
 
70
        int c;
 
71
        try {
 
72
            while ((c = reader.read()) != -1 && c != '\n') {
 
73
                if (c != '\r')
 
74
                    b.append((char)c);
 
75
            }
 
76
        }
 
77
        catch (IOException ioe) {
 
78
            c = -1;
 
79
        }
 
80
 
 
81
        if (c == -1 && b.length() == 0)
 
82
            return null;
 
83
        else
 
84
            return b.toString();
 
85
    }
 
86
 
 
87
    public void run()
 
88
         throws DbException, FileNotFoundException
 
89
    {
 
90
        // Remove the previous database.
 
91
        new File(FileName).delete();
 
92
 
 
93
        // Create the database object.
 
94
        // There is no environment for this simple example.
 
95
        Db table = new Db(null, 0);
 
96
        table.set_error_stream(System.err);
 
97
        table.set_errpfx("AccessExample");
 
98
        table.open(FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
 
99
 
 
100
        //
 
101
        // Insert records into the database, where the key is the user
 
102
        // input and the data is the user input in reverse order.
 
103
        //
 
104
        InputStreamReader reader = new InputStreamReader(System.in);
 
105
 
 
106
        for (;;) {
 
107
            String line = askForLine(reader, System.out, "input> ");
 
108
            if (line == null)
 
109
                break;
 
110
 
 
111
            String reversed = (new StringBuffer(line)).reverse().toString();
 
112
 
 
113
            // See definition of StringDbt below
 
114
            //
 
115
            StringDbt key = new StringDbt(line);
 
116
            StringDbt data = new StringDbt(reversed);
 
117
 
 
118
            try
 
119
            {
 
120
                int err;
 
121
                if ((err = table.put(null, 
 
122
                    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
 
123
                        System.out.println("Key " + line + " already exists.");
 
124
                }
 
125
            }
 
126
            catch (DbException dbe)
 
127
            {
 
128
                System.out.println(dbe.toString());
 
129
            }
 
130
            System.out.println("");
 
131
        }
 
132
 
 
133
        // Acquire an iterator for the table.
 
134
        Dbc iterator;
 
135
        iterator = table.cursor(null, 0);
 
136
 
 
137
        // Walk through the table, printing the key/data pairs.
 
138
        // See class StringDbt defined below.
 
139
        //
 
140
        StringDbt key = new StringDbt();
 
141
        StringDbt data = new StringDbt();
 
142
        while (iterator.get(key, data, Db.DB_NEXT) == 0)
 
143
        {
 
144
            System.out.println(key.getString() + " : " + data.getString());
 
145
        }
 
146
        iterator.close();
 
147
        table.close(0);
 
148
    }
 
149
 
 
150
    // Here's an example of how you can extend a Dbt in a straightforward
 
151
    // way to allow easy storage/retrieval of strings, or whatever
 
152
    // kind of data you wish.  We've declared it as a static inner
 
153
    // class, but it need not be.
 
154
    //
 
155
    static /*inner*/
 
156
    class StringDbt extends Dbt
 
157
    {
 
158
        StringDbt()
 
159
        {
 
160
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
 
161
        }
 
162
 
 
163
        StringDbt(String value)
 
164
        {
 
165
            setString(value);
 
166
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
 
167
        }
 
168
 
 
169
        void setString(String value)
 
170
        {
 
171
            set_data(value.getBytes());
 
172
            set_size(value.length());
 
173
        }
 
174
 
 
175
        String getString()
 
176
        {
 
177
            return new String(get_data(), 0, get_size());
 
178
        }
 
179
    }
 
180
}