KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > recovery > RecoveryTest


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

8
9 package com.sleepycat.je.recovery;
10
11 import java.util.Comparator JavaDoc;
12 import java.util.Hashtable JavaDoc;
13
14 import com.sleepycat.je.Cursor;
15 import com.sleepycat.je.Database;
16 import com.sleepycat.je.DatabaseConfig;
17 import com.sleepycat.je.DatabaseEntry;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.Environment;
20 import com.sleepycat.je.EnvironmentConfig;
21 import com.sleepycat.je.LockMode;
22 import com.sleepycat.je.OperationStatus;
23 import com.sleepycat.je.Transaction;
24 import com.sleepycat.je.config.EnvironmentParams;
25 import com.sleepycat.je.util.TestUtils;
26
27 public class RecoveryTest extends RecoveryTestBase {
28
29     /**
30      * Basic insert, delete data.
31      */

32     public void testBasic()
33         throws Throwable JavaDoc {
34
35         doBasic(true);
36     }
37
38     /**
39      * Basic insert, delete data with BtreeComparator
40      */

41     public void testBasicRecoveryWithBtreeComparator()
42         throws Throwable JavaDoc {
43
44     btreeComparisonFunction = new BtreeComparator(true);
45         doBasic(true);
46     }
47
48     /**
49      * Test that put(OVERWRITE) works correctly with duplicates.
50      */

51     public void testDuplicateOverwrite()
52     throws Throwable JavaDoc {
53
54         createEnvAndDbs(1 << 10, false, NUM_DBS);
55         try {
56             Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
57
58         Transaction txn = env.beginTransaction(null, null);
59         DatabaseEntry key = new DatabaseEntry("aaaaa".getBytes());
60         DatabaseEntry data1 = new DatabaseEntry("dddddddddd".getBytes());
61         DatabaseEntry data2 = new DatabaseEntry("eeeeeeeeee".getBytes());
62         DatabaseEntry data3 = new DatabaseEntry("ffffffffff".getBytes());
63         Database db = dbs[0];
64         assertEquals(OperationStatus.SUCCESS,
65              db.put(null, key, data1));
66         addExpectedData(expectedData, 0, key, data1, true);
67         assertEquals(OperationStatus.SUCCESS,
68              db.put(null, key, data2));
69         addExpectedData(expectedData, 0, key, data2, true);
70         assertEquals(OperationStatus.SUCCESS,
71              db.put(null, key, data3));
72         addExpectedData(expectedData, 0, key, data3, true);
73         assertEquals(OperationStatus.SUCCESS,
74              db.put(null, key, data3));
75         txn.commit();
76         closeEnv();
77
78         recoverAndVerify(expectedData, NUM_DBS);
79         } catch (Throwable JavaDoc t) {
80             t.printStackTrace();
81             throw t;
82         }
83     }
84
85     /**
86      * Basic insert, delete data.
87      */

88     public void testBasicFewerCheckpoints()
89         throws Throwable JavaDoc {
90
91         doBasic(false);
92     }
93
94     public void testSR8984Part1()
95         throws Throwable JavaDoc {
96
97     doTestSR8984Work(true);
98     }
99
100     public void testSR8984Part2()
101         throws Throwable JavaDoc {
102
103     doTestSR8984Work(false);
104     }
105
106     private void doTestSR8984Work(boolean sameKey)
107     throws DatabaseException {
108
109     final int NUM_EXTRA_DUPS = 150;
110     EnvironmentConfig envConfig = TestUtils.initEnvConfig();
111         /* Make an environment and open it */
112         envConfig.setTransactional(false);
113         envConfig.setAllowCreate(true);
114         envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(),
115                  "false");
116         envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
117     envConfig.setConfigParam(EnvironmentParams.ENV_RUN_CLEANER.getName(),
118                  "false");
119
120     envConfig.setConfigParam
121         (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
122         env = new Environment(envHome, envConfig);
123
124         DatabaseConfig dbConfig = new DatabaseConfig();
125         dbConfig.setTransactional(false);
126         dbConfig.setAllowCreate(true);
127         dbConfig.setSortedDuplicates(true);
128     Database db = env.openDatabase(null, "testSR8984db", dbConfig);
129
130     DatabaseEntry key = new DatabaseEntry("k1".getBytes());
131     DatabaseEntry data = new DatabaseEntry("d1".getBytes());
132     assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
133     assertEquals(OperationStatus.SUCCESS, db.delete(null, key));
134         
135     if (!sameKey) {
136         data.setData("d2".getBytes());
137     }
138     /* Cause a dup tree of some depth to be created. */
139     assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
140     for (int i = 3; i < NUM_EXTRA_DUPS; i++) {
141         data.setData(("d" + i).getBytes());
142         assertEquals(OperationStatus.SUCCESS, db.put(null, key, data));
143     }
144
145     data.setData("d1".getBytes());
146
147     Cursor c = db.openCursor(null, null);
148     assertEquals(OperationStatus.SUCCESS,
149              c.getFirst(key, data, LockMode.DEFAULT));
150
151     c.close();
152     db.close();
153         
154         /* Force an abrupt close so there is no checkpoint at the end. */
155         closeEnv();
156         env = new Environment(envHome, envConfig);
157     db = env.openDatabase(null, "testSR8984db", dbConfig);
158     c = db.openCursor(null, null);
159     assertEquals(OperationStatus.SUCCESS,
160              c.getFirst(key, data, LockMode.DEFAULT));
161     assertEquals(NUM_EXTRA_DUPS - 2, c.count());
162     c.close();
163     db.close();
164     env.close();
165     }
166
167     /**
168      * Insert data, delete data into several dbs.
169      */

170     public void doBasic(boolean runCheckpointerDaemon)
171         throws Throwable JavaDoc {
172
173         createEnvAndDbs(1 << 20, runCheckpointerDaemon, NUM_DBS);
174         int numRecs = NUM_RECS;
175
176         try {
177             // Set up an repository of expected data
178
Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
179             
180             // insert all the data
181
Transaction txn = env.beginTransaction(null, null);
182             insertData(txn, 0, numRecs - 1, expectedData, 1, true, NUM_DBS);
183             txn.commit();
184
185             // delete all the even records
186
txn = env.beginTransaction(null, null);
187             deleteData(txn, expectedData, false, true, NUM_DBS);
188             txn.commit();
189
190             // modify all the records
191
txn = env.beginTransaction(null, null);
192             modifyData(txn, NUM_RECS/2, expectedData, 1, true, NUM_DBS);
193             txn.commit();
194
195             closeEnv();
196             recoverAndVerify(expectedData, NUM_DBS);
197         } catch (Throwable JavaDoc t) {
198             // print stacktrace before trying to clean up files
199
t.printStackTrace();
200             throw t;
201         }
202     }
203
204     /**
205      * Insert data, delete all data into several dbs.
206      */

207     public void testBasicDeleteAll()
208         throws Throwable JavaDoc {
209
210         createEnvAndDbs(1024, true, NUM_DBS);
211         int numRecs = NUM_RECS;
212         try {
213             // Set up an repository of expected data
214
Hashtable JavaDoc expectedData = new Hashtable JavaDoc();
215
216             // insert all the data
217
Transaction txn = env.beginTransaction(null, null);
218             insertData(txn, 0, numRecs - 1, expectedData, 1, true, NUM_DBS);
219             txn.commit();
220
221             // modify half the records
222
txn = env.beginTransaction(null, null);
223             modifyData(txn, numRecs/2, expectedData, 1, true, NUM_DBS);
224             txn.commit();
225
226             // delete all the records
227
txn = env.beginTransaction(null, null);
228             deleteData(txn, expectedData, true, true, NUM_DBS);
229             txn.commit();
230
231             closeEnv();
232
233             recoverAndVerify(expectedData, NUM_DBS);
234         } catch (Throwable JavaDoc t) {
235             // print stacktrace before trying to clean up files
236
t.printStackTrace();
237             throw t;
238         }
239     }
240
241     protected static class BtreeComparator implements Comparator JavaDoc {
242     protected boolean ascendingComparison = true;
243
244     public BtreeComparator() {
245     }
246
247     protected BtreeComparator(boolean ascendingComparison) {
248         this.ascendingComparison = ascendingComparison;
249     }
250
251     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
252         byte[] arg1;
253         byte[] arg2;
254         if (ascendingComparison) {
255         arg1 = (byte[]) o1;
256         arg2 = (byte[]) o2;
257         } else {
258         arg1 = (byte[]) o2;
259         arg2 = (byte[]) o1;
260         }
261         int a1Len = arg1.length;
262         int a2Len = arg2.length;
263
264         int limit = Math.min(a1Len, a2Len);
265
266         for (int i = 0; i < limit; i++) {
267         byte b1 = arg1[i];
268         byte b2 = arg2[i];
269         if (b1 == b2) {
270             continue;
271         } else {
272             /* Remember, bytes are signed, so convert to shorts so that
273                we effectively do an unsigned byte comparison. */

274             short s1 = (short) (b1 & 0x7F);
275             short s2 = (short) (b2 & 0x7F);
276             if (b1 < 0) {
277             s1 |= 0x80;
278             }
279             if (b2 < 0) {
280             s2 |= 0x80;
281             }
282             return (s1 - s2);
283         }
284         }
285
286         return (a1Len - a2Len);
287     }
288     }
289 }
290
Popular Tags