KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SR10597Test.java,v 1.7 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.je.CheckpointConfig;
17 import com.sleepycat.je.Cursor;
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.OperationStatus;
26 import com.sleepycat.je.config.EnvironmentParams;
27 import com.sleepycat.je.log.FileManager;
28 import com.sleepycat.je.util.TestUtils;
29
30 public class SR10597Test extends TestCase {
31
32     private static final String JavaDoc DB_NAME = "foo";
33
34     private static final CheckpointConfig forceConfig = new CheckpointConfig();
35     static {
36         forceConfig.setForce(true);
37     }
38
39     private File JavaDoc envHome;
40     private Environment env;
41     private Database db;
42
43     public SR10597Test() {
44         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
45     }
46
47     public void setUp()
48         throws IOException JavaDoc, DatabaseException {
49
50         TestUtils.removeLogFiles("Setup", envHome, false);
51         TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
52     }
53
54     public void tearDown()
55         throws IOException JavaDoc, DatabaseException {
56
57         try {
58             if (env != null) {
59                 env.close();
60             }
61         } catch (Throwable JavaDoc e) {
62             System.out.println("tearDown: " + e);
63         }
64                 
65         try {
66             TestUtils.removeLogFiles("tearDown", envHome, true);
67             TestUtils.removeFiles("tearDown", envHome, FileManager.DEL_SUFFIX);
68         } catch (Throwable JavaDoc e) {
69             System.out.println("tearDown: " + e);
70         }
71
72         db = null;
73         env = null;
74         envHome = null;
75     }
76
77     /**
78      * Opens the environment and database.
79      */

80     private void openEnv()
81         throws DatabaseException {
82
83         EnvironmentConfig config = TestUtils.initEnvConfig();
84     DbInternal.disableParameterValidation(config);
85         config.setAllowCreate(true);
86         /* Do not run the daemons. */
87         config.setConfigParam
88             (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
89         config.setConfigParam
90             (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
91         config.setConfigParam
92         (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
93         config.setConfigParam
94             (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
95         /* Use a small log file size to make cleaning more frequent. */
96         config.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
97                               Integer.toString(1024));
98         env = new Environment(envHome, config);
99
100         openDb();
101     }
102
103     /**
104      * Opens that database.
105      */

106     private void openDb()
107         throws DatabaseException {
108
109         DatabaseConfig dbConfig = new DatabaseConfig();
110         dbConfig.setAllowCreate(true);
111         dbConfig.setSortedDuplicates(true);
112         db = env.openDatabase(null, DB_NAME, dbConfig);
113     }
114
115     /**
116      * Closes the environment and database.
117      */

118     private void closeEnv()
119         throws DatabaseException {
120
121         if (db != null) {
122             db.close();
123             db = null;
124         }
125         if (env != null) {
126             env.close();
127             env = null;
128         }
129     }
130
131     /**
132      */

133     public void testSR10597()
134         throws DatabaseException {
135
136         openEnv();
137
138         /* Put some duplicates, enough to fill a log file. */
139         final int COUNT = 10;
140         DatabaseEntry key = new DatabaseEntry(TestUtils.getTestArray(0));
141         DatabaseEntry data = new DatabaseEntry();
142         for (int i = 0; i < COUNT; i += 1) {
143             data.setData(TestUtils.getTestArray(i));
144             db.put(null, key, data);
145         }
146         Cursor cursor = db.openCursor(null, null);
147         assertEquals(OperationStatus.SUCCESS,
148                      cursor.getSearchKey(key, data, null));
149         assertEquals(COUNT, cursor.count());
150         cursor.close();
151
152         /* Delete everything, then compress to delete the DIN. */
153         db.delete(null, key);
154         env.compress();
155         data.setData(TestUtils.getTestArray(0));
156
157         /* Add a single record, which will not create a DIN. */
158         db.put(null, key, data);
159
160         /* Checkpoint and clean. */
161         env.checkpoint(forceConfig);
162         int cleaned = env.cleanLog();
163         assertTrue("cleaned=" + cleaned, cleaned > 0);
164
165         /*
166          * Before the fix to 10597, when cleaning the log we would be looking
167          * for an LN with containsDuplicates=true. We assumed that when we
168          * found the BIN entry, it must point to a DIN. But because we
169          * deleted and compressed above, the entry is actually an LN. This
170          * caused a ClassCastException at the bottom of
171          * Tree.getParentBINForChildLN.
172          */

173         closeEnv();
174     }
175 }
176
Popular Tags