KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > txn > CursorTxnTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CursorTxnTest.java,v 1.40 2006/10/30 21:14:52 bostic Exp $
7  */

8
9 package com.sleepycat.je.txn;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.je.Cursor;
17 import com.sleepycat.je.Database;
18 import com.sleepycat.je.DatabaseConfig;
19 import com.sleepycat.je.DatabaseEntry;
20 import com.sleepycat.je.DatabaseException;
21 import com.sleepycat.je.DbInternal;
22 import com.sleepycat.je.DbTestProxy;
23 import com.sleepycat.je.Environment;
24 import com.sleepycat.je.EnvironmentConfig;
25 import com.sleepycat.je.LockMode;
26 import com.sleepycat.je.LockStats;
27 import com.sleepycat.je.OperationStatus;
28 import com.sleepycat.je.config.EnvironmentParams;
29 import com.sleepycat.je.dbi.CursorImpl;
30 import com.sleepycat.je.dbi.DbEnvPool;
31 import com.sleepycat.je.log.FileManager;
32 import com.sleepycat.je.util.TestUtils;
33
34 public class CursorTxnTest extends TestCase {
35     private File JavaDoc envHome;
36     private Environment env;
37     private Database myDb;
38     private int initialEnvReadLocks;
39     private int initialEnvWriteLocks;
40     private boolean noLocking;
41
42     public CursorTxnTest() {
43         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
44         DbEnvPool.getInstance().clear();
45     }
46
47     public void setUp()
48     throws IOException JavaDoc, DatabaseException {
49
50         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
51
52         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
53         DbInternal.setLoadPropertyFile(envConfig, false);
54         envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
55         envConfig.setConfigParam
56             (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
57         envConfig.setConfigParam
58             (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
59         envConfig.setConfigParam
60             (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
61         envConfig.setConfigParam
62             (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
63         envConfig.setAllowCreate(true);
64         env = new Environment(envHome, envConfig);
65
66     EnvironmentConfig envConfigAsSet = env.getConfig();
67     noLocking = !(envConfigAsSet.getLocking());
68
69         DatabaseConfig dbConfig = new DatabaseConfig();
70         dbConfig.setAllowCreate(true);
71         dbConfig.setSortedDuplicates(true);
72         myDb = env.openDatabase(null, "test", dbConfig);
73     }
74     
75     public void tearDown()
76     throws IOException JavaDoc, DatabaseException {
77
78         try {
79             myDb.close();
80         } catch (DatabaseException ignored) {}
81         try {
82             env.close();
83         } catch (DatabaseException ignored) {}
84         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
85     }
86
87     /**
88      * Create a cursor with a null transaction.
89      */

90     public void testNullTxnLockRelease()
91         throws DatabaseException {
92
93         getInitialEnvStats();
94         Cursor cursor = myDb.openCursor(null, null);
95         
96         /* First put() holds a write lock on the non-duplicate entry. */
97         insertData(cursor, 10, 1);
98         checkReadWriteLockCounts(cursor, 0, 1);
99
100         // Check that count does not add more locks
101
int count = cursor.count();
102         assertEquals(1, count);
103         checkReadWriteLockCounts(cursor, 0, 1);
104
105         /*
106      * Second put() holds a write lock on first record (since it
107      * was write locked to discover that the duplicate tree is not
108      * present yet) and a write lock on the duplicate entry and a
109      * write lock on the DupCountLN.
110      */

111         insertData(cursor, 10, 2);
112         checkReadWriteLockCounts(cursor, 0, 3);
113
114         /* Check that count does not add more locks. */
115         count = cursor.count();
116         assertEquals(2, count);
117         checkReadWriteLockCounts(cursor, 0, 3);
118         
119         /* Third put() holds a write lock on the duplicate entry and a write
120          * lock on the DupCountLN. */

121         insertData(cursor, 10, 3);
122         checkReadWriteLockCounts(cursor, 0, 2);
123
124         DatabaseEntry foundKey = new DatabaseEntry();
125         DatabaseEntry foundData = new DatabaseEntry();
126
127         /* Check that read locks are held on forward traversal. */
128         OperationStatus status =
129         cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
130         checkReadWriteLockCounts(cursor, 1, 0);
131         int numSeen = 0;
132         while (status == OperationStatus.SUCCESS) {
133             numSeen++;
134             status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
135             checkReadWriteLockCounts(cursor, 1, 0);
136         if (status != OperationStatus.SUCCESS) {
137         break;
138         }
139
140             status = cursor.getCurrent(foundKey, foundData,
141                                        LockMode.DEFAULT);
142             checkReadWriteLockCounts(cursor, 1, 0);
143         }
144         assertEquals(30, numSeen);
145
146         /* Check that read locks are held on backwards traversal and count. */
147         status = cursor.getLast(foundKey, foundData, LockMode.DEFAULT);
148         checkReadWriteLockCounts(cursor, 1, 0);
149
150         while (status == OperationStatus.SUCCESS) {
151             count = cursor.count();
152             assertEquals("For key " +
153                          TestUtils.dumpByteArray(foundKey.getData()),
154                          3, count);
155             status = cursor.getPrev(foundKey, foundData, LockMode.DEFAULT);
156             checkReadWriteLockCounts(cursor, 1, 0);
157         }
158
159         /* Check that delete holds a write lock. */
160         status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
161         while (status == OperationStatus.SUCCESS) {
162             assertEquals("For key " +
163                          TestUtils.dumpByteArray(foundKey.getData()),
164                          OperationStatus.SUCCESS, cursor.delete());
165             checkReadWriteLockCounts(cursor, 0, 2);
166             status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
167             if (status == OperationStatus.SUCCESS) {
168                 checkReadWriteLockCounts(cursor, 1, 0);
169             } else {
170                 checkReadWriteLockCounts(cursor, 0, 2);
171             }
172         }
173
174         /* Check that count does not add more locks. */
175         count = cursor.count();
176         assertEquals(0, count);
177         checkReadWriteLockCounts(cursor, 0, 2);
178         
179     cursor.close();
180     }
181
182     private void checkReadWriteLockCounts(Cursor cursor,
183                                           int expectReadLocks,
184                                           int expectWriteLocks)
185         throws DatabaseException {
186
187     if (noLocking) {
188         expectReadLocks = expectWriteLocks = 0;
189     }
190
191         CursorImpl cursorImpl = DbTestProxy.dbcGetCursorImpl(cursor);
192         LockStats lockStats = cursorImpl.getLockStats();
193         assertEquals(expectReadLocks, lockStats.getNReadLocks());
194         assertEquals(expectWriteLocks, lockStats.getNWriteLocks());
195         lockStats = env.getLockStats(null);
196         assertEquals(initialEnvReadLocks + expectReadLocks,
197                      lockStats.getNReadLocks());
198         assertEquals(initialEnvWriteLocks + expectWriteLocks,
199                      lockStats.getNWriteLocks());
200     }
201
202     private void getInitialEnvStats()
203         throws DatabaseException {
204
205         LockStats lockStats = env.getLockStats(null);
206         initialEnvReadLocks = lockStats.getNReadLocks();
207         initialEnvWriteLocks = lockStats.getNWriteLocks();
208     }
209     
210     private void insertData(Cursor cursor, int numRecords, int dataVal)
211         throws DatabaseException {
212
213         DatabaseEntry key = new DatabaseEntry();
214         DatabaseEntry data = new DatabaseEntry();
215
216         for (int i = 0; i < numRecords; i++) {
217             byte[] keyData = TestUtils.getTestArray(i);
218             byte[] dataData = new byte[1];
219             dataData[0] = (byte) dataVal;
220             key.setData(keyData);
221             data.setData(dataData);
222             OperationStatus status = cursor.putNoDupData(key, data);
223             assertEquals(OperationStatus.SUCCESS, status);
224         }
225     }
226 }
227
Popular Tags