~ubuntu-branches/ubuntu/natty/evolution-data-server/natty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

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.File;
14
 
import java.io.FileNotFoundException;
15
 
import java.io.InputStreamReader;
16
 
import java.io.IOException;
17
 
import java.io.PrintStream;
18
 
 
19
 
class BulkAccessExample
20
 
{
21
 
    private static final String FileName = "access.db";
22
 
 
23
 
    public BulkAccessExample()
24
 
    {
25
 
    }
26
 
 
27
 
    public static void main(String argv[])
28
 
    {
29
 
        try
30
 
        {
31
 
            BulkAccessExample app = new BulkAccessExample();
32
 
            app.run();
33
 
        }
34
 
        catch (DbException dbe)
35
 
        {
36
 
            System.err.println("BulkAccessExample: " + dbe.toString());
37
 
            System.exit(1);
38
 
        }
39
 
        catch (FileNotFoundException fnfe)
40
 
        {
41
 
            System.err.println("BulkAccessExample: " + 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("BulkAccessExample");
98
 
        table.open(null, 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 a cursor for the table and two Dbts.
134
 
        Dbc dbc = table.cursor(null, 0);
135
 
        Dbt foo = new Dbt();
136
 
        foo.set_flags(Db.DB_DBT_MALLOC);
137
 
 
138
 
        Dbt bulk_data = new Dbt();
139
 
 
140
 
        // Set Db.DB_DBT_USERMEM on the data Dbt;  Db.DB_MULTIPLE_KEY requires
141
 
        // it.  Then allocate a byte array of a reasonable size;  we'll
142
 
        // go through the database in chunks this big.
143
 
        bulk_data.set_flags(Db.DB_DBT_USERMEM);
144
 
        bulk_data.set_data(new byte[1000000]);
145
 
        bulk_data.set_ulen(1000000);
146
 
 
147
 
 
148
 
        // Walk through the table, printing the key/data pairs.
149
 
        //
150
 
        while (dbc.get(foo, bulk_data, Db.DB_NEXT | Db.DB_MULTIPLE_KEY) == 0)
151
 
        {
152
 
            DbMultipleKeyDataIterator iterator;
153
 
            iterator = new DbMultipleKeyDataIterator(bulk_data);
154
 
 
155
 
            StringDbt key, data;
156
 
            key = new StringDbt();
157
 
            data = new StringDbt();
158
 
 
159
 
            while (iterator.next(key, data)) {
160
 
                System.out.println(key.getString() + " : " + data.getString());
161
 
            }
162
 
        }
163
 
        dbc.close();
164
 
        table.close(0);
165
 
    }
166
 
 
167
 
    // Here's an example of how you can extend a Dbt in a straightforward
168
 
    // way to allow easy storage/retrieval of strings, or whatever
169
 
    // kind of data you wish.  We've declared it as a static inner
170
 
    // class, but it need not be.
171
 
    //
172
 
    static /*inner*/
173
 
    class StringDbt extends Dbt
174
 
    {
175
 
        StringDbt()
176
 
        {
177
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
178
 
        }
179
 
 
180
 
        StringDbt(String value)
181
 
        {
182
 
            setString(value);
183
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
184
 
        }
185
 
 
186
 
        void setString(String value)
187
 
        {
188
 
            byte[] data = value.getBytes();
189
 
            set_data(data);
190
 
            set_size(data.length);
191
 
        }
192
 
 
193
 
        String getString()
194
 
        {
195
 
            return new String(get_data(), get_offset(), get_size());
196
 
        }
197
 
    }
198
 
}