KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CleanerTestUtils.java,v 1.8 2006/11/17 23:47:28 mark Exp $
7  */

8
9 package com.sleepycat.je.cleaner;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.sleepycat.je.Cursor;
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.DbTestProxy;
20 import com.sleepycat.je.dbi.CursorImpl;
21 import com.sleepycat.je.dbi.EnvironmentImpl;
22 import com.sleepycat.je.log.UtilizationFileReader;
23 import com.sleepycat.je.tree.BIN;
24 import com.sleepycat.je.utilint.DbLsn;
25
26 /**
27  * Package utilities.
28  */

29 public class CleanerTestUtils {
30
31     /**
32      * Gets the file of the LSN at the cursor position, using internal methods.
33      */

34     static long getLogFile(TestCase test, Cursor cursor)
35         throws DatabaseException {
36
37         CursorImpl impl = DbTestProxy.dbcGetCursorImpl(cursor);
38         int index;
39         BIN bin = impl.getDupBIN();
40         if (bin != null) {
41             index = impl.getDupIndex();
42         } else {
43             bin = impl.getBIN();
44             TestCase.assertNotNull(bin);
45             index = impl.getIndex();
46         }
47         TestCase.assertNotNull(bin.getTarget(index));
48         long lsn = bin.getLsn(index);
49         TestCase.assertTrue(lsn != DbLsn.NULL_LSN);
50         long file = DbLsn.getFileNumber(lsn);
51         return file;
52     }
53
54     /**
55      * Compare utilization as calculated by UtilizationProfile to utilization
56      * as calculated by UtilizationFileReader.
57      */

58     static void verifyUtilization(EnvironmentImpl env,
59                                   boolean expectAccurateObsoleteLNCount,
60                                   boolean expectAccurateObsoleteLNSize)
61         throws DatabaseException {
62
63         Map JavaDoc profileMap = env.getCleaner()
64                             .getUtilizationProfile()
65                             .getFileSummaryMap(true);
66         /* Flush the log before reading. */
67         env.getLogManager().flushNoSync();
68         Map JavaDoc recalcMap;
69         try {
70             recalcMap = UtilizationFileReader.calcFileSummaryMap(env);
71         } catch (IOException JavaDoc e) {
72             throw new DatabaseException(e);
73         }
74         Iterator JavaDoc i = profileMap.entrySet().iterator();
75         while (i.hasNext()) {
76             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
77             Long JavaDoc file = (Long JavaDoc) entry.getKey();
78             FileSummary profileSummary = (FileSummary) entry.getValue();
79             FileSummary recalcSummary = (FileSummary) recalcMap.remove(file);
80             TestCase.assertNotNull(recalcSummary);
81             //*
82
if (expectAccurateObsoleteLNCount &&
83                 profileSummary.obsoleteLNCount !=
84                 recalcSummary.obsoleteLNCount) {
85                 System.out.println("file=" + file);
86                 System.out.println("profile=" + profileSummary);
87                 System.out.println("recalc=" + recalcSummary);
88             }
89             //*/
90
TestCase.assertEquals(recalcSummary.totalCount,
91                                  profileSummary.totalCount);
92             TestCase.assertEquals(recalcSummary.totalSize,
93                                  profileSummary.totalSize);
94             TestCase.assertEquals(recalcSummary.totalINCount,
95                                  profileSummary.totalINCount);
96             TestCase.assertEquals(recalcSummary.totalINSize,
97                                  profileSummary.totalINSize);
98             TestCase.assertEquals(recalcSummary.totalLNCount,
99                                  profileSummary.totalLNCount);
100             TestCase.assertEquals(recalcSummary.totalLNSize,
101                                  profileSummary.totalLNSize);
102             /*
103              * Currently we cannot verify obsolete INs because
104              * UtilizationFileReader does not count them accurately.
105              */

106             if (false) {
107                 TestCase.assertEquals(recalcSummary.obsoleteINCount,
108                                      profileSummary.obsoleteINCount);
109             }
110
111             /*
112              * The obsolete LN count/size is not accurate when running recovery
113              * and the IN for the newer LN is already flushed. For example,
114              * it is not accurate in INUtilizationTest when truncating the
115              * log before the FileSummaryLNs that are part of the checkpoint.
116              */

117             if (expectAccurateObsoleteLNCount) {
118                 TestCase.assertEquals(recalcSummary.obsoleteLNCount,
119                                      profileSummary.obsoleteLNCount);
120
121                 /*
122                  * The obsoletely LN size is only accurate when running
123                  * recovery or doing a DB truncate/remove iff the
124                  * je.cleaner.fetchObsoleteSize configuration is set to true.
125                  * For example, it is not accurate in TruncateAndRemoveTest
126                  * when fetchObsoleteSize is not set to true.
127                  */

128                 if (expectAccurateObsoleteLNSize) {
129                     TestCase.assertEquals(recalcSummary.getObsoleteLNSize(),
130                                          profileSummary.obsoleteLNSize);
131                 }
132             }
133         }
134         TestCase.assertTrue(recalcMap.toString(), recalcMap.isEmpty());
135     }
136 }
137
Popular Tags