001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: CleanerTestUtils.java,v 1.8.2.3 2008/01/07 15:14:25 cwl Exp $
007: */
008:
009: package com.sleepycat.je.cleaner;
010:
011: import java.io.IOException;
012: import java.util.Iterator;
013: import java.util.Map;
014:
015: import junit.framework.TestCase;
016:
017: import com.sleepycat.je.Cursor;
018: import com.sleepycat.je.DatabaseException;
019: import com.sleepycat.je.DbTestProxy;
020: import com.sleepycat.je.dbi.CursorImpl;
021: import com.sleepycat.je.dbi.EnvironmentImpl;
022: import com.sleepycat.je.log.UtilizationFileReader;
023: import com.sleepycat.je.tree.BIN;
024: import com.sleepycat.je.utilint.DbLsn;
025:
026: /**
027: * Package utilities.
028: */
029: public class CleanerTestUtils {
030:
031: /**
032: * Gets the file of the LSN at the cursor position, using internal methods.
033: */
034: static long getLogFile(TestCase test, Cursor cursor)
035: throws DatabaseException {
036:
037: CursorImpl impl = DbTestProxy.dbcGetCursorImpl(cursor);
038: int index;
039: BIN bin = impl.getDupBIN();
040: if (bin != null) {
041: index = impl.getDupIndex();
042: } else {
043: bin = impl.getBIN();
044: TestCase.assertNotNull(bin);
045: index = impl.getIndex();
046: }
047: TestCase.assertNotNull(bin.getTarget(index));
048: long lsn = bin.getLsn(index);
049: TestCase.assertTrue(lsn != DbLsn.NULL_LSN);
050: long file = DbLsn.getFileNumber(lsn);
051: return file;
052: }
053:
054: /**
055: * Compare utilization as calculated by UtilizationProfile to utilization
056: * as calculated by UtilizationFileReader.
057: */
058: static void verifyUtilization(EnvironmentImpl env,
059: boolean expectAccurateObsoleteLNCount,
060: boolean expectAccurateObsoleteLNSize)
061: throws DatabaseException {
062:
063: Map profileMap = env.getCleaner().getUtilizationProfile()
064: .getFileSummaryMap(true);
065: /* Flush the log before reading. */
066: env.getLogManager().flushNoSync();
067: Map recalcMap;
068: try {
069: recalcMap = UtilizationFileReader.calcFileSummaryMap(env);
070: } catch (IOException e) {
071: throw new DatabaseException(e);
072: }
073: Iterator i = profileMap.entrySet().iterator();
074: while (i.hasNext()) {
075: Map.Entry entry = (Map.Entry) i.next();
076: Long file = (Long) entry.getKey();
077: FileSummary profileSummary = (FileSummary) entry.getValue();
078: FileSummary recalcSummary = (FileSummary) recalcMap
079: .remove(file);
080: TestCase.assertNotNull(recalcSummary);
081: //*
082: if (expectAccurateObsoleteLNCount
083: && profileSummary.obsoleteLNCount != recalcSummary.obsoleteLNCount) {
084: System.out.println("file=" + file);
085: System.out.println("profile=" + profileSummary);
086: System.out.println("recalc=" + recalcSummary);
087: }
088: //*/
089: TestCase.assertEquals(recalcSummary.totalCount,
090: profileSummary.totalCount);
091: TestCase.assertEquals(recalcSummary.totalSize,
092: profileSummary.totalSize);
093: TestCase.assertEquals(recalcSummary.totalINCount,
094: profileSummary.totalINCount);
095: TestCase.assertEquals(recalcSummary.totalINSize,
096: profileSummary.totalINSize);
097: TestCase.assertEquals(recalcSummary.totalLNCount,
098: profileSummary.totalLNCount);
099: TestCase.assertEquals(recalcSummary.totalLNSize,
100: profileSummary.totalLNSize);
101: /*
102: * Currently we cannot verify obsolete INs because
103: * UtilizationFileReader does not count them accurately.
104: */
105: if (false) {
106: TestCase.assertEquals(recalcSummary.obsoleteINCount,
107: profileSummary.obsoleteINCount);
108: }
109:
110: /*
111: * The obsolete LN count/size is not accurate when running recovery
112: * and the IN for the newer LN is already flushed. For example,
113: * it is not accurate in INUtilizationTest when truncating the
114: * log before the FileSummaryLNs that are part of the checkpoint.
115: */
116: if (expectAccurateObsoleteLNCount) {
117: TestCase.assertEquals("file=" + file,
118: 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("file=" + file, recalcSummary
130: .getObsoleteLNSize(),
131: profileSummary.obsoleteLNSize);
132: }
133: }
134: }
135: TestCase.assertTrue(recalcMap.toString(), recalcMap.isEmpty());
136: }
137: }
|