KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > cleaner > RMWLockingTest


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

8
9 package com.sleepycat.je.cleaner;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.bind.tuple.IntegerBinding;
17 import com.sleepycat.je.CheckpointConfig;
18 import com.sleepycat.je.Database;
19 import com.sleepycat.je.DatabaseConfig;
20 import com.sleepycat.je.DatabaseEntry;
21 import com.sleepycat.je.DatabaseException;
22 import com.sleepycat.je.DbInternal;
23 import com.sleepycat.je.Environment;
24 import com.sleepycat.je.EnvironmentConfig;
25 import com.sleepycat.je.LockMode;
26 import com.sleepycat.je.OperationStatus;
27 import com.sleepycat.je.Transaction;
28 import com.sleepycat.je.log.FileManager;
29 import com.sleepycat.je.util.TestUtils;
30
31 /**
32  * Use LockMode.RMW and verify that the FileSummaryLNs accurately reflect only
33  * those LNs that have been made obsolete.
34  */

35 public class RMWLockingTest extends TestCase {
36     
37     private static final int NUM_RECS = 5;
38
39     private File JavaDoc envHome;
40     private Environment env;
41     private Database db;
42     private DatabaseEntry key;
43     private DatabaseEntry data;
44
45     public RMWLockingTest() {
46         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
47     }
48
49     public void setUp()
50         throws IOException JavaDoc, DatabaseException {
51
52         TestUtils.removeLogFiles("Setup", envHome, false);
53         TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
54     }
55
56     public void tearDown()
57         throws IOException JavaDoc, DatabaseException {
58
59         try {
60             if (db != null) {
61                 db.close();
62             }
63             if (env != null) {
64                 env.close();
65             }
66         } catch (Throwable JavaDoc e) {
67             System.out.println("tearDown: " + e);
68         }
69                 
70         try {
71             TestUtils.removeLogFiles("tearDown", envHome, true);
72             TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX);
73         } catch (Throwable JavaDoc e) {
74             System.out.println("tearDown: " + e);
75         }
76
77         db = null;
78         env = null;
79         envHome = null;
80     }
81
82     public void testBasic()
83         throws DatabaseException {
84
85         init();
86         insertRecords();
87         rmwModify();
88
89         UtilizationProfile up =
90             DbInternal.envGetEnvironmentImpl(env).getUtilizationProfile();
91
92         /*
93          * Checkpoint the environment to flush all utilization tracking
94          * information before verifying.
95          */

96         CheckpointConfig ckptConfig = new CheckpointConfig();
97         ckptConfig.setForce(true);
98         env.checkpoint(ckptConfig);
99
100         assertTrue(up.verifyFileSummaryDatabase());
101     }
102
103     /**
104      * Tests that we can load a log file containing offsets that correspond to
105      * non-obsolete LNs. The bad log file was created using testBasic run
106      * against JE 2.0.54. It contains version 1 FSLNs, one of which has an
107      * offset which is not obsolete.
108      */

109     public void testBadLog()
110         throws DatabaseException, IOException JavaDoc {
111
112         /* Copy a log file with bad offsets to log file zero. */
113         String JavaDoc resName = "rmw_bad_offsets.jdb";
114         TestUtils.loadLog(getClass(), resName, envHome);
115
116         /* Open the log we just copied. */
117         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
118         envConfig.setAllowCreate(false);
119         envConfig.setReadOnly(true);
120         env = new Environment(envHome, envConfig);
121
122         /*
123          * Verify the UP of the bad log. Prior to adding the code in
124          * FileSummaryLN.postFetchInit that discards version 1 offsets, this
125          * assertion failed.
126          */

127         UtilizationProfile up =
128             DbInternal.envGetEnvironmentImpl(env).getUtilizationProfile();
129         assertTrue(up.verifyFileSummaryDatabase());
130
131         env.close();
132         env = null;
133     }
134
135     private void init()
136         throws DatabaseException {
137
138         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
139         envConfig.setAllowCreate(true);
140         envConfig.setTransactional(true);
141         env = new Environment(envHome, envConfig);
142
143         DatabaseConfig dbConfig = new DatabaseConfig();
144         dbConfig.setAllowCreate(true);
145         dbConfig.setTransactional(true);
146         db = env.openDatabase(null, "foo", dbConfig);
147     }
148
149     /* Insert records. */
150     private void insertRecords()
151         throws DatabaseException {
152
153         key = new DatabaseEntry();
154         data = new DatabaseEntry();
155
156         IntegerBinding.intToEntry(100, data);
157
158         for (int i = 0; i < NUM_RECS; i++) {
159             IntegerBinding.intToEntry(i, key);
160             assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
161         }
162     }
163
164     /* lock two records with RMW, only modify one. */
165     private void rmwModify()
166         throws DatabaseException {
167
168         Transaction txn = env.beginTransaction(null, null);
169         IntegerBinding.intToEntry(0, key);
170         assertEquals(OperationStatus.SUCCESS,
171                      db.get(txn, key, data, LockMode.RMW));
172         IntegerBinding.intToEntry(1, key);
173         assertEquals(OperationStatus.SUCCESS,
174                      db.get(txn, key, data, LockMode.RMW));
175
176         IntegerBinding.intToEntry(200, data);
177         assertEquals(OperationStatus.SUCCESS,
178                      db.put(txn, key, data));
179         txn.commit();
180     }
181 }
182
Popular Tags