KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: SR10553Test.java,v 1.10 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 SR10553Test 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 SR10553Test() {
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         /* Use a small memory size to force eviction. */
99         config.setConfigParam(EnvironmentParams.MAX_MEMORY.getName(),
100                               Integer.toString(1024 * 96));
101         /* Don't track detail with a tiny cache size. */
102         config.setConfigParam
103             (EnvironmentParams.CLEANER_TRACK_DETAIL.getName(), "false");
104         config.setConfigParam(EnvironmentParams.NUM_LOG_BUFFERS.getName(),
105                               Integer.toString(2));
106         /* Set log buffers large enough for trace messages. */
107         config.setConfigParam(EnvironmentParams.LOG_MEM_SIZE.getName(),
108                               Integer.toString(7000));
109         
110         env = new Environment(envHome, config);
111
112         openDb();
113     }
114
115     /**
116      * Opens that database.
117      */

118     private void openDb()
119         throws DatabaseException {
120
121         DatabaseConfig dbConfig = new DatabaseConfig();
122         dbConfig.setAllowCreate(true);
123         dbConfig.setSortedDuplicates(true);
124         db = env.openDatabase(null, DB_NAME, dbConfig);
125     }
126
127     /**
128      * Closes the environment and database.
129      */

130     private void closeEnv()
131         throws DatabaseException {
132
133         if (db != null) {
134             db.close();
135             db = null;
136         }
137         if (env != null) {
138             env.close();
139             env = null;
140         }
141     }
142
143     /**
144      */

145     public void testSR10553()
146         throws DatabaseException {
147
148         openEnv();
149
150         /* Put some duplicates, enough to fill a log file. */
151         final int COUNT = 10;
152         DatabaseEntry key = new DatabaseEntry(TestUtils.getTestArray(0));
153         DatabaseEntry data = new DatabaseEntry();
154         for (int i = 0; i < COUNT; i += 1) {
155             data.setData(TestUtils.getTestArray(i));
156             db.put(null, key, data);
157         }
158         Cursor cursor = db.openCursor(null, null);
159         assertEquals(OperationStatus.SUCCESS,
160                      cursor.getSearchKey(key, data, null));
161         assertEquals(COUNT, cursor.count());
162         cursor.close();
163
164         /* Delete everything. Do not compress. */
165         db.delete(null, key);
166
167         /* Checkpoint and clean. */
168         env.checkpoint(forceConfig);
169         int cleaned = env.cleanLog();
170         assertTrue("cleaned=" + cleaned, cleaned > 0);
171
172         /* Force eviction. */
173         env.evictMemory();
174
175         /* Scan all values. */
176         cursor = db.openCursor(null, null);
177         for (OperationStatus status = cursor.getFirst(key, data, null);
178                              status == OperationStatus.SUCCESS;
179                              status = cursor.getNext(key, data, null)) {
180         }
181         cursor.close();
182
183         /*
184          * Before the fix to 10553, while scanning over deleted records, a
185          * LogFileNotFoundException would occur when faulting in a deleted
186          * record, if the log file had been cleaned. This was because the
187          * cleaner was not setting knownDeleted for deleted records.
188          */

189         closeEnv();
190     }
191 }
192
Popular Tags