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

« back to all changes in this revision

Viewing changes to libdb/test/scr016/TestDbtFlags.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.test;
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
 
public class TestDbtFlags
20
 
{
21
 
    private static final String FileName = "access.db";
22
 
    private int flag_value;
23
 
    private int buf_size;
24
 
    private int cur_input_line = 0;
25
 
 
26
 
    /*zippy quotes for test input*/
27
 
    static final String[] input_lines = {
28
 
        "If we shadows have offended", 
29
 
        "Think but this, and all is mended",
30
 
        "That you have but slumber'd here",
31
 
        "While these visions did appear",
32
 
        "And this weak and idle theme",
33
 
        "No more yielding but a dream",
34
 
        "Gentles, do not reprehend",
35
 
        "if you pardon, we will mend",
36
 
        "And, as I am an honest Puck, if we have unearned luck",
37
 
        "Now to 'scape the serpent's tongue, we will make amends ere long;",
38
 
        "Else the Puck a liar call; so, good night unto you all.",
39
 
        "Give me your hands, if we be friends, and Robin shall restore amends."
40
 
    };
41
 
 
42
 
    public TestDbtFlags(int flag_value, int buf_size)
43
 
    {
44
 
        this.flag_value = flag_value;
45
 
        this.buf_size = buf_size;
46
 
    }
47
 
 
48
 
    public static void runWithFlags(int flag_value, int size)
49
 
    {
50
 
        String msg = "=-=-=-= Test with DBT flags " + flag_value +
51
 
            " bufsize " + size;
52
 
        System.out.println(msg);
53
 
        System.err.println(msg);
54
 
 
55
 
        try
56
 
        {
57
 
            TestDbtFlags app = new TestDbtFlags(flag_value, size);
58
 
            app.run();
59
 
        }
60
 
        catch (DbException dbe)
61
 
        {
62
 
            System.err.println("TestDbtFlags: " + dbe.toString());
63
 
            System.exit(1);
64
 
        }
65
 
        catch (FileNotFoundException fnfe)
66
 
        {
67
 
            System.err.println("TestDbtFlags: " + fnfe.toString());
68
 
            System.exit(1);
69
 
        }
70
 
    }
71
 
 
72
 
    public static void main(String argv[])
73
 
    {
74
 
        runWithFlags(Db.DB_DBT_MALLOC, -1);
75
 
        runWithFlags(Db.DB_DBT_REALLOC, -1);
76
 
        runWithFlags(Db.DB_DBT_USERMEM, 20);
77
 
        runWithFlags(Db.DB_DBT_USERMEM, 50);
78
 
        runWithFlags(Db.DB_DBT_USERMEM, 200);
79
 
        runWithFlags(0, -1);
80
 
 
81
 
        System.exit(0);
82
 
    }
83
 
 
84
 
    String get_input_line()
85
 
    {
86
 
        if (cur_input_line >= input_lines.length)
87
 
            return null;
88
 
        return input_lines[cur_input_line++];
89
 
    }
90
 
 
91
 
    public void run()
92
 
         throws DbException, FileNotFoundException
93
 
    {
94
 
        // Remove the previous database.
95
 
        new File(FileName).delete();
96
 
 
97
 
        // Create the database object.
98
 
        // There is no environment for this simple example.
99
 
        Db table = new Db(null, 0);
100
 
        table.set_error_stream(System.err);
101
 
        table.set_errpfx("TestDbtFlags");
102
 
        table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
103
 
 
104
 
        //
105
 
        // Insert records into the database, where the key is the user
106
 
        // input and the data is the user input in reverse order.
107
 
        //
108
 
        for (;;) {
109
 
            //System.err.println("input line " + cur_input_line);
110
 
            String line = get_input_line();
111
 
            if (line == null)
112
 
                break;
113
 
 
114
 
            String reversed = (new StringBuffer(line)).reverse().toString();
115
 
 
116
 
            // See definition of StringDbt below
117
 
            //
118
 
            StringDbt key = new StringDbt(line, flag_value);
119
 
            StringDbt data = new StringDbt(reversed, flag_value);
120
 
 
121
 
            try
122
 
            {
123
 
                int err;
124
 
                if ((err = table.put(null,
125
 
                    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
126
 
                        System.out.println("Key " + line + " already exists.");
127
 
                }
128
 
                key.check_flags();
129
 
                data.check_flags();
130
 
            }
131
 
            catch (DbException dbe)
132
 
            {
133
 
                System.out.println(dbe.toString());
134
 
            }
135
 
        }
136
 
 
137
 
        // Acquire an iterator for the table.
138
 
        Dbc iterator;
139
 
        iterator = table.cursor(null, 0);
140
 
 
141
 
        // Walk through the table, printing the key/data pairs.
142
 
        // See class StringDbt defined below.
143
 
        //
144
 
        StringDbt key = new StringDbt(flag_value, buf_size);
145
 
        StringDbt data = new StringDbt(flag_value, buf_size);
146
 
 
147
 
        int iteration_count = 0;
148
 
        int dbreturn = 0;
149
 
 
150
 
        while (dbreturn == 0) {
151
 
            //System.err.println("iteration " + iteration_count);
152
 
            try {
153
 
                if ((dbreturn = iterator.get(key, data, Db.DB_NEXT)) == 0) {
154
 
                    System.out.println(key.get_string() + " : " + data.get_string());
155
 
                }
156
 
            }
157
 
            catch (DbMemoryException dme) {
158
 
                /* In a real application, we'd normally increase
159
 
                 * the size of the buffer.  Since we've created
160
 
                 * this error condition for testing, we'll just report it.
161
 
                 * We still need to skip over this record, and we don't
162
 
                 * want to mess with our original Dbt's, since we want
163
 
                 * to see more errors.  So create some temporary
164
 
                 * mallocing Dbts to get this record.
165
 
                 */
166
 
                System.err.println("exception, iteration " + iteration_count +
167
 
                                   ": " + dme);
168
 
                System.err.println("  key size: " + key.get_size() +
169
 
                                   " ulen: " + key.get_ulen());
170
 
                System.err.println("  data size: " + key.get_size() +
171
 
                                   " ulen: " + key.get_ulen());
172
 
 
173
 
                dme.get_dbt().set_size(buf_size);
174
 
                StringDbt tempkey = new StringDbt(Db.DB_DBT_MALLOC, -1);
175
 
                StringDbt tempdata = new StringDbt(Db.DB_DBT_MALLOC, -1);
176
 
                if ((dbreturn = iterator.get(tempkey, tempdata, Db.DB_NEXT)) != 0) {
177
 
                    System.err.println("cannot get expected next record");
178
 
                    return;
179
 
                }
180
 
                System.out.println(tempkey.get_string() + " : " +
181
 
                                   tempdata.get_string());
182
 
            }
183
 
            iteration_count++;
184
 
        }
185
 
        key.check_flags();
186
 
        data.check_flags();
187
 
 
188
 
        iterator.close();
189
 
        table.close(0);
190
 
    }
191
 
 
192
 
    // Here's an example of how you can extend a Dbt in a straightforward
193
 
    // way to allow easy storage/retrieval of strings, or whatever
194
 
    // kind of data you wish.  We've declared it as a static inner
195
 
    // class, but it need not be.
196
 
    //
197
 
    static /*inner*/
198
 
    class StringDbt extends Dbt
199
 
    {
200
 
        int saved_flags;
201
 
 
202
 
        StringDbt(int flags, int buf_size)
203
 
        {
204
 
            this.saved_flags = flags;
205
 
            set_flags(saved_flags);
206
 
            if (buf_size != -1) {
207
 
                set_data(new byte[buf_size]);
208
 
                set_ulen(buf_size);
209
 
            }
210
 
        }
211
 
 
212
 
        StringDbt(String value, int flags)
213
 
        {
214
 
            this.saved_flags = flags;
215
 
            set_flags(saved_flags);
216
 
            set_string(value);
217
 
        }
218
 
 
219
 
        void set_string(String value)
220
 
        {
221
 
            set_data(value.getBytes());
222
 
            set_size(value.length());
223
 
            check_flags();
224
 
        }
225
 
 
226
 
        String get_string()
227
 
        {
228
 
            check_flags();
229
 
            return new String(get_data(), 0, get_size());
230
 
        }
231
 
 
232
 
        void check_flags()
233
 
        {
234
 
            int actual_flags = get_flags();
235
 
            if (actual_flags != saved_flags) {
236
 
                System.err.println("flags botch: expected " + saved_flags +
237
 
                                   ", got " + actual_flags);
238
 
            }
239
 
        }
240
 
    }
241
 
}