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

« back to all changes in this revision

Viewing changes to libdb/test/scr016/TestLockVec.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
 
/*
11
 
 * test of DbEnv.lock_vec()
12
 
 */
13
 
 
14
 
package com.sleepycat.test;
15
 
 
16
 
import com.sleepycat.db.*;
17
 
import java.io.FileNotFoundException;
18
 
 
19
 
public class TestLockVec
20
 
{
21
 
    public static int locker1;
22
 
    public static int locker2;
23
 
 
24
 
    public static void gdb_pause()
25
 
    {
26
 
        try {
27
 
            System.err.println("attach gdb and type return...");
28
 
            System.in.read(new byte[10]);
29
 
        }
30
 
        catch (java.io.IOException ie) {
31
 
        }
32
 
    }
33
 
 
34
 
    public static void main(String[] args)
35
 
    {
36
 
        try {
37
 
            DbEnv dbenv1 = new DbEnv(0);
38
 
            DbEnv dbenv2 = new DbEnv(0);
39
 
            dbenv1.open(".",
40
 
                       Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL, 0);
41
 
            dbenv2.open(".",
42
 
                       Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL, 0);
43
 
            locker1 = dbenv1.lock_id();
44
 
            locker2 = dbenv1.lock_id();
45
 
            Db db1 = new Db(dbenv1, 0);
46
 
            db1.open(null, "my.db", null, Db.DB_BTREE, Db.DB_CREATE, 0);
47
 
            Db db2 = new Db(dbenv2, 0);
48
 
            db2.open(null, "my.db", null, Db.DB_BTREE, 0, 0);
49
 
 
50
 
            // populate our database, just two elements.
51
 
            Dbt Akey = new Dbt("A".getBytes());
52
 
            Dbt Adata = new Dbt("Adata".getBytes());
53
 
            Dbt Bkey = new Dbt("B".getBytes());
54
 
            Dbt Bdata = new Dbt("Bdata".getBytes());
55
 
 
56
 
            // We don't allow Dbts to be reused within the
57
 
            // same method call, so we need some duplicates.
58
 
            Dbt Akeyagain = new Dbt("A".getBytes());
59
 
            Dbt Bkeyagain = new Dbt("B".getBytes());
60
 
 
61
 
            db1.put(null, Akey, Adata, 0);
62
 
            db1.put(null, Bkey, Bdata, 0);
63
 
 
64
 
            Dbt notInDatabase = new Dbt("C".getBytes());
65
 
 
66
 
            /* make sure our check mechanisms work */
67
 
            int expectedErrs = 0;
68
 
 
69
 
            lock_check_free(dbenv2, Akey);
70
 
            try {
71
 
                lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
72
 
            }
73
 
            catch (DbException dbe1) {
74
 
                expectedErrs += 1;
75
 
            }
76
 
            DbLock tmplock = dbenv1.lock_get(locker1, Db.DB_LOCK_NOWAIT,
77
 
                                            Akey, Db.DB_LOCK_READ);
78
 
            lock_check_held(dbenv2, Akey, Db.DB_LOCK_READ);
79
 
            try {
80
 
                lock_check_free(dbenv2, Akey);
81
 
            }
82
 
            catch (DbException dbe2) {
83
 
                expectedErrs += 2;
84
 
            }
85
 
            if (expectedErrs != 1+2) {
86
 
                System.err.println("lock check mechanism is broken");
87
 
                System.exit(1);
88
 
            }
89
 
            dbenv1.lock_put(tmplock);
90
 
 
91
 
            /* Now on with the test, a series of lock_vec requests,
92
 
             * with checks between each call.
93
 
             */
94
 
 
95
 
            System.out.println("get a few");
96
 
            /* Request: get A(W), B(R), B(R) */
97
 
            DbLockRequest[] reqs = new DbLockRequest[3];
98
 
 
99
 
            reqs[0] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_WRITE,
100
 
                                        Akey, null);
101
 
            reqs[1] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
102
 
                                        Bkey, null);
103
 
            reqs[2] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
104
 
                                        Bkeyagain, null);
105
 
 
106
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 3);
107
 
 
108
 
            /* Locks held: A(W), B(R), B(R) */
109
 
            lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
110
 
            lock_check_held(dbenv2, Akey, Db.DB_LOCK_WRITE);
111
 
 
112
 
            System.out.println("put a couple");
113
 
            /* Request: put A, B(first) */
114
 
            reqs[0].set_op(Db.DB_LOCK_PUT);
115
 
            reqs[1].set_op(Db.DB_LOCK_PUT);
116
 
 
117
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 2);
118
 
 
119
 
            /* Locks held: B(R) */
120
 
            lock_check_free(dbenv2, Akey);
121
 
            lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
122
 
 
123
 
            System.out.println("put one more, test index offset");
124
 
            /* Request: put B(second) */
125
 
            reqs[2].set_op(Db.DB_LOCK_PUT);
126
 
 
127
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 2, 1);
128
 
 
129
 
            /* Locks held: <none> */
130
 
            lock_check_free(dbenv2, Akey);
131
 
            lock_check_free(dbenv2, Bkey);
132
 
 
133
 
            System.out.println("get a few");
134
 
            /* Request: get A(R), A(R), B(R) */
135
 
            reqs[0] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
136
 
                                        Akey, null);
137
 
            reqs[1] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
138
 
                                        Akeyagain, null);
139
 
            reqs[2] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
140
 
                                        Bkey, null);
141
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 3);
142
 
 
143
 
            /* Locks held: A(R), B(R), B(R) */
144
 
            lock_check_held(dbenv2, Akey, Db.DB_LOCK_READ);
145
 
            lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
146
 
 
147
 
            System.out.println("try putobj");
148
 
            /* Request: get B(R), putobj A */
149
 
            reqs[1] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
150
 
                                        Bkey, null);
151
 
            reqs[2] = new DbLockRequest(Db.DB_LOCK_PUT_OBJ, 0,
152
 
                                        Akey, null);
153
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 1, 2);
154
 
 
155
 
            /* Locks held: B(R), B(R) */
156
 
            lock_check_free(dbenv2, Akey);
157
 
            lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
158
 
 
159
 
            System.out.println("get one more");
160
 
            /* Request: get A(W) */
161
 
            reqs[0] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_WRITE,
162
 
                                        Akey, null);
163
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 1);
164
 
 
165
 
            /* Locks held: A(W), B(R), B(R) */
166
 
            lock_check_held(dbenv2, Akey, Db.DB_LOCK_WRITE);
167
 
            lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
168
 
 
169
 
            System.out.println("putall");
170
 
            /* Request: putall */
171
 
            reqs[0] = new DbLockRequest(Db.DB_LOCK_PUT_ALL, 0,
172
 
                                        null, null);
173
 
            dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 1);
174
 
 
175
 
            lock_check_free(dbenv2, Akey);
176
 
            lock_check_free(dbenv2, Bkey);
177
 
            db1.close(0);
178
 
            dbenv1.close(0);
179
 
            db2.close(0);
180
 
            dbenv2.close(0);
181
 
            System.out.println("done");
182
 
        }
183
 
        catch (DbLockNotGrantedException nge) {
184
 
            System.err.println("Db Exception: " + nge);
185
 
        }
186
 
        catch (DbException dbe) {
187
 
            System.err.println("Db Exception: " + dbe);
188
 
        }
189
 
        catch (FileNotFoundException fnfe) {
190
 
            System.err.println("FileNotFoundException: " + fnfe);
191
 
        }
192
 
 
193
 
    }
194
 
 
195
 
    /* Verify that the lock is free, throw an exception if not.
196
 
     * We do this by trying to grab a write lock (no wait).
197
 
     */
198
 
    static void lock_check_free(DbEnv dbenv, Dbt dbt)
199
 
        throws DbException
200
 
    {
201
 
        DbLock tmplock = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
202
 
                                        dbt, Db.DB_LOCK_WRITE);
203
 
        dbenv.lock_put(tmplock);
204
 
    }
205
 
 
206
 
    /* Verify that the lock is held with the mode, throw an exception if not.
207
 
     * If we have a write lock, we should not be able to get the lock
208
 
     * for reading.  If we have a read lock, we should be able to get
209
 
     * it for reading, but not writing.
210
 
     */
211
 
    static void lock_check_held(DbEnv dbenv, Dbt dbt, int mode)
212
 
        throws DbException
213
 
    {
214
 
        DbLock never = null;
215
 
 
216
 
        try {
217
 
            if (mode == Db.DB_LOCK_WRITE) {
218
 
                never = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
219
 
                                       dbt, Db.DB_LOCK_READ);
220
 
            }
221
 
            else if (mode == Db.DB_LOCK_READ) {
222
 
                DbLock rlock = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
223
 
                                              dbt, Db.DB_LOCK_READ);
224
 
                dbenv.lock_put(rlock);
225
 
                never = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
226
 
                                              dbt, Db.DB_LOCK_WRITE);
227
 
            }
228
 
            else {
229
 
                throw new DbException("lock_check_held bad mode");
230
 
            }
231
 
        }
232
 
        catch (DbLockNotGrantedException nge) {
233
 
            /* We expect this on our last lock_get call */
234
 
        }
235
 
 
236
 
        /* make sure we failed */
237
 
        if (never != null) {
238
 
          try {
239
 
            dbenv.lock_put(never);
240
 
          }
241
 
          catch (DbException dbe2) {
242
 
            System.err.println("Got some real troubles now");
243
 
            System.exit(1);
244
 
          }
245
 
          throw new DbException("lock_check_held: lock was not held");
246
 
        }
247
 
    }
248
 
 
249
 
}