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

« back to all changes in this revision

Viewing changes to libdb/test/scr016/TestAssociate.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.Reader;
16
 
import java.io.StringReader;
17
 
import java.io.IOException;
18
 
import java.io.PrintStream;
19
 
import java.util.Hashtable;
20
 
 
21
 
public class TestAssociate
22
 
    implements DbDupCompare
23
 
{
24
 
    private static final String FileName = "access.db";
25
 
    public static Db saveddb1 = null;
26
 
    public static Db saveddb2 = null;
27
 
 
28
 
    public TestAssociate()
29
 
    {
30
 
    }
31
 
 
32
 
    private static void usage()
33
 
    {
34
 
        System.err.println("usage: TestAssociate\n");
35
 
        System.exit(1);
36
 
    }
37
 
 
38
 
    public static void main(String argv[])
39
 
    {
40
 
        try
41
 
        {
42
 
            TestAssociate app = new TestAssociate();
43
 
            app.run();
44
 
        }
45
 
        catch (DbException dbe)
46
 
        {
47
 
            System.err.println("TestAssociate: " + dbe.toString());
48
 
            System.exit(1);
49
 
        }
50
 
        catch (FileNotFoundException fnfe)
51
 
        {
52
 
            System.err.println("TestAssociate: " + fnfe.toString());
53
 
            System.exit(1);
54
 
        }
55
 
        System.exit(0);
56
 
    }
57
 
 
58
 
    public static int counter = 0;
59
 
    public static String results[] = { "abc", "def", "ghi", "JKL", "MNO", null };
60
 
 
61
 
    // Prompts for a line, and keeps prompting until a non blank
62
 
    // line is returned.  Returns null on error.
63
 
    //
64
 
    static public String askForLine(Reader reader,
65
 
                                    PrintStream out, String prompt)
66
 
    {
67
 
        /*
68
 
        String result = "";
69
 
        while (result != null && result.length() == 0) {
70
 
            out.print(prompt);
71
 
            out.flush();
72
 
            result = getLine(reader);
73
 
        }
74
 
        return result;
75
 
        */
76
 
        return results[counter++];
77
 
    }
78
 
 
79
 
    // Not terribly efficient, but does the job.
80
 
    // Works for reading a line from stdin or a file.
81
 
    // Returns null on EOF.  If EOF appears in the middle
82
 
    // of a line, returns that line, then null on next call.
83
 
    //
84
 
    static public String getLine(Reader reader)
85
 
    {
86
 
        StringBuffer b = new StringBuffer();
87
 
        int c;
88
 
        try {
89
 
            while ((c = reader.read()) != -1 && c != '\n') {
90
 
                if (c != '\r')
91
 
                    b.append((char)c);
92
 
            }
93
 
        }
94
 
        catch (IOException ioe) {
95
 
            c = -1;
96
 
        }
97
 
 
98
 
        if (c == -1 && b.length() == 0)
99
 
            return null;
100
 
        else
101
 
            return b.toString();
102
 
    }
103
 
 
104
 
    static public String shownull(Object o)
105
 
    {
106
 
        if (o == null)
107
 
            return "null";
108
 
        else
109
 
            return "not null";
110
 
    }
111
 
 
112
 
    public void run()
113
 
         throws DbException, FileNotFoundException
114
 
    {
115
 
        // Remove the previous database.
116
 
        new File(FileName).delete();
117
 
 
118
 
        // Create the database object.
119
 
        // There is no environment for this simple example.
120
 
        DbEnv dbenv = new DbEnv(0);
121
 
        dbenv.open("./", Db.DB_CREATE|Db.DB_INIT_MPOOL, 0644);
122
 
        (new java.io.File(FileName)).delete();
123
 
        Db table = new Db(dbenv, 0);
124
 
        Db table2 = new Db(dbenv, 0);
125
 
        table2.set_dup_compare(this);
126
 
        table2.set_flags(Db.DB_DUPSORT);
127
 
        table.set_error_stream(System.err);
128
 
        table2.set_error_stream(System.err);
129
 
        table.set_errpfx("TestAssociate");
130
 
        table2.set_errpfx("TestAssociate(table2)");
131
 
        System.out.println("Primary database is " + shownull(table));
132
 
        System.out.println("Secondary database is " + shownull(table2));
133
 
        saveddb1 = table;
134
 
        saveddb2 = table2;
135
 
        table.open(null, FileName, null, Db.DB_BTREE, Db.DB_CREATE, 0644);
136
 
        table2.open(null, FileName + "2", null,
137
 
                    Db.DB_BTREE, Db.DB_CREATE, 0644);
138
 
        table.associate(null, table2, new Capitalize(), 0);
139
 
 
140
 
        //
141
 
        // Insert records into the database, where the key is the user
142
 
        // input and the data is the user input in reverse order.
143
 
        //
144
 
        Reader reader = new StringReader("abc\ndef\njhi");
145
 
 
146
 
        for (;;) {
147
 
            String line = askForLine(reader, System.out, "input> ");
148
 
            if (line == null)
149
 
                break;
150
 
 
151
 
            String reversed = (new StringBuffer(line)).reverse().toString();
152
 
 
153
 
            // See definition of StringDbt below
154
 
            //
155
 
            StringDbt key = new StringDbt(line);
156
 
            StringDbt data = new StringDbt(reversed);
157
 
 
158
 
            try
159
 
            {
160
 
                int err;
161
 
                if ((err = table.put(null,
162
 
                    key, data, Db.DB_NOOVERWRITE)) == Db.DB_KEYEXIST) {
163
 
                        System.out.println("Key " + line + " already exists.");
164
 
                }
165
 
            }
166
 
            catch (DbException dbe)
167
 
            {
168
 
                System.out.println(dbe.toString());
169
 
            }
170
 
            System.out.println("");
171
 
        }
172
 
 
173
 
        // Acquire an iterator for the table.
174
 
        Dbc iterator;
175
 
        iterator = table2.cursor(null, 0);
176
 
 
177
 
        // Walk through the table, printing the key/data pairs.
178
 
        // See class StringDbt defined below.
179
 
        //
180
 
        StringDbt key = new StringDbt();
181
 
        StringDbt data = new StringDbt();
182
 
        StringDbt pkey = new StringDbt();
183
 
 
184
 
        while (iterator.get(key, data, Db.DB_NEXT) == 0)
185
 
        {
186
 
            System.out.println(key.getString() + " : " + data.getString());
187
 
        }
188
 
 
189
 
        key.setString("BC");
190
 
        System.out.println("get BC returns " + table2.get(null, key, data, 0));
191
 
        System.out.println("  values: " + key.getString() + " : " + data.getString());
192
 
        System.out.println("pget BC returns " + table2.pget(null, key, pkey, data, 0));
193
 
        System.out.println("  values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
194
 
        key.setString("KL");
195
 
        System.out.println("get KL returns " + table2.get(null, key, data, 0));
196
 
        System.out.println("  values: " + key.getString() + " : " + data.getString());
197
 
        System.out.println("pget KL returns " + table2.pget(null, key, pkey, data, 0));
198
 
        System.out.println("  values: " + key.getString() + " : " + pkey.getString() + " : " + data.getString());
199
 
 
200
 
        iterator.close();
201
 
        table.close(0);
202
 
    }
203
 
 
204
 
    // Here's an example of how you can extend a Dbt in a straightforward
205
 
    // way to allow easy storage/retrieval of strings, or whatever
206
 
    // kind of data you wish.  We've declared it as a static inner
207
 
    // class, but it need not be.
208
 
    //
209
 
    static /*inner*/
210
 
    class StringDbt extends Dbt
211
 
    {
212
 
        StringDbt()
213
 
        {
214
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
215
 
        }
216
 
 
217
 
        StringDbt(String value)
218
 
        {
219
 
            setString(value);
220
 
            set_flags(Db.DB_DBT_MALLOC); // tell Db to allocate on retrieval
221
 
        }
222
 
 
223
 
        void setString(String value)
224
 
        {
225
 
            set_data(value.getBytes());
226
 
            set_size(value.length());
227
 
        }
228
 
 
229
 
        String getString()
230
 
        {
231
 
            return new String(get_data(), 0, get_size());
232
 
        }
233
 
 
234
 
        public String toString()
235
 
        {
236
 
            return "StringDbt=" + getString();
237
 
        }
238
 
    }
239
 
 
240
 
    /* creates a stupid secondary index as follows:
241
 
     For an N letter key, we use N-1 letters starting at
242
 
     position 1.  If the new letters are already capitalized,
243
 
     we return the old array, but with offset set to 1.
244
 
     If the letters are not capitalized, we create a new,
245
 
     capitalized array.  This is pretty stupid for
246
 
     an application, but it tests all the paths in the runtime.
247
 
     */
248
 
    public static class Capitalize implements DbSecondaryKeyCreate
249
 
    {
250
 
        public int secondary_key_create(Db secondary, Dbt key, Dbt value,
251
 
                                        Dbt result)
252
 
            throws DbException
253
 
        {
254
 
            String which = "unknown db";
255
 
            if (saveddb1.equals(secondary)) {
256
 
                which = "primary";
257
 
            }
258
 
            else if (saveddb2.equals(secondary)) {
259
 
                which = "secondary";
260
 
            }
261
 
            System.out.println("secondary_key_create, Db: " + shownull(secondary) + "(" + which + "), key: " + show_dbt(key) + ", data: " + show_dbt(value));
262
 
            int len = key.get_size();
263
 
            byte[] arr = key.get_data();
264
 
            boolean capped = true;
265
 
 
266
 
            if (len < 1)
267
 
                throw new DbException("bad key");
268
 
 
269
 
            if (len < 2)
270
 
                return Db.DB_DONOTINDEX;
271
 
 
272
 
            result.set_size(len - 1);
273
 
            for (int i=1; capped && i<len; i++) {
274
 
                if (!Character.isUpperCase((char)arr[i]))
275
 
                    capped = false;
276
 
            }
277
 
            if (capped) {
278
 
                System.out.println("  creating key(1): " + new String(arr, 1, len-1));
279
 
                result.set_data(arr);
280
 
                result.set_offset(1);
281
 
            }
282
 
            else {
283
 
                System.out.println("  creating key(2): " + (new String(arr)).substring(1).
284
 
                                   toUpperCase());
285
 
                result.set_data((new String(arr)).substring(1).
286
 
                                toUpperCase().getBytes());
287
 
            }
288
 
            return 0;
289
 
        }
290
 
    }
291
 
 
292
 
    public int dup_compare(Db db, Dbt dbt1, Dbt dbt2)
293
 
    {
294
 
        System.out.println("compare");
295
 
        int sz1 = dbt1.get_size();
296
 
        int sz2 = dbt2.get_size();
297
 
        if (sz1 < sz2)
298
 
            return -1;
299
 
        if (sz1 > sz2)
300
 
            return 1;
301
 
        byte[] data1 = dbt1.get_data();
302
 
        byte[] data2 = dbt2.get_data();
303
 
        for (int i=0; i<sz1; i++)
304
 
            if (data1[i] != data2[i])
305
 
                return (data1[i] < data2[i] ? -1 : 1);
306
 
        return 0;
307
 
    }
308
 
 
309
 
    public static int nseen = 0;
310
 
    public static Hashtable ht = new Hashtable();
311
 
 
312
 
    public static String show_dbt(Dbt dbt)
313
 
    {
314
 
        String name;
315
 
 
316
 
        if (dbt == null)
317
 
            return "null dbt";
318
 
 
319
 
        name = (String)ht.get(dbt);
320
 
        if (name == null) {
321
 
            name = "Dbt" + (nseen++);
322
 
            ht.put(dbt, name);
323
 
        }
324
 
 
325
 
        byte[] value = dbt.get_data();
326
 
        if (value == null)
327
 
            return name + "(null)";
328
 
        else
329
 
            return name + "(\"" + new String(value) + "\")";
330
 
    }
331
 
}
332
 
 
333