2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
11
* test of DbEnv.lock_vec()
14
package com.sleepycat.test;
16
import com.sleepycat.db.*;
17
import java.io.FileNotFoundException;
19
public class TestLockVec
21
public static int locker1;
22
public static int locker2;
24
public static void gdb_pause()
27
System.err.println("attach gdb and type return...");
28
System.in.read(new byte[10]);
30
catch (java.io.IOException ie) {
34
public static void main(String[] args)
37
DbEnv dbenv1 = new DbEnv(0);
38
DbEnv dbenv2 = new DbEnv(0);
40
Db.DB_CREATE | Db.DB_INIT_LOCK | Db.DB_INIT_MPOOL, 0);
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);
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());
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());
61
db1.put(null, Akey, Adata, 0);
62
db1.put(null, Bkey, Bdata, 0);
64
Dbt notInDatabase = new Dbt("C".getBytes());
66
/* make sure our check mechanisms work */
69
lock_check_free(dbenv2, Akey);
71
lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
73
catch (DbException dbe1) {
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);
80
lock_check_free(dbenv2, Akey);
82
catch (DbException dbe2) {
85
if (expectedErrs != 1+2) {
86
System.err.println("lock check mechanism is broken");
89
dbenv1.lock_put(tmplock);
91
/* Now on with the test, a series of lock_vec requests,
92
* with checks between each call.
95
System.out.println("get a few");
96
/* Request: get A(W), B(R), B(R) */
97
DbLockRequest[] reqs = new DbLockRequest[3];
99
reqs[0] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_WRITE,
101
reqs[1] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
103
reqs[2] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
106
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 3);
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);
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);
117
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 2);
119
/* Locks held: B(R) */
120
lock_check_free(dbenv2, Akey);
121
lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
123
System.out.println("put one more, test index offset");
124
/* Request: put B(second) */
125
reqs[2].set_op(Db.DB_LOCK_PUT);
127
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 2, 1);
129
/* Locks held: <none> */
130
lock_check_free(dbenv2, Akey);
131
lock_check_free(dbenv2, Bkey);
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,
137
reqs[1] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
139
reqs[2] = new DbLockRequest(Db.DB_LOCK_GET, Db.DB_LOCK_READ,
141
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 3);
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);
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,
151
reqs[2] = new DbLockRequest(Db.DB_LOCK_PUT_OBJ, 0,
153
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 1, 2);
155
/* Locks held: B(R), B(R) */
156
lock_check_free(dbenv2, Akey);
157
lock_check_held(dbenv2, Bkey, Db.DB_LOCK_READ);
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,
163
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 1);
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);
169
System.out.println("putall");
170
/* Request: putall */
171
reqs[0] = new DbLockRequest(Db.DB_LOCK_PUT_ALL, 0,
173
dbenv1.lock_vec(locker1, Db.DB_LOCK_NOWAIT, reqs, 0, 1);
175
lock_check_free(dbenv2, Akey);
176
lock_check_free(dbenv2, Bkey);
181
System.out.println("done");
183
catch (DbLockNotGrantedException nge) {
184
System.err.println("Db Exception: " + nge);
186
catch (DbException dbe) {
187
System.err.println("Db Exception: " + dbe);
189
catch (FileNotFoundException fnfe) {
190
System.err.println("FileNotFoundException: " + fnfe);
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).
198
static void lock_check_free(DbEnv dbenv, Dbt dbt)
201
DbLock tmplock = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
202
dbt, Db.DB_LOCK_WRITE);
203
dbenv.lock_put(tmplock);
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.
211
static void lock_check_held(DbEnv dbenv, Dbt dbt, int mode)
217
if (mode == Db.DB_LOCK_WRITE) {
218
never = dbenv.lock_get(locker2, Db.DB_LOCK_NOWAIT,
219
dbt, Db.DB_LOCK_READ);
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);
229
throw new DbException("lock_check_held bad mode");
232
catch (DbLockNotGrantedException nge) {
233
/* We expect this on our last lock_get call */
236
/* make sure we failed */
239
dbenv.lock_put(never);
241
catch (DbException dbe2) {
242
System.err.println("Got some real troubles now");
245
throw new DbException("lock_check_held: lock was not held");