001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: FileReaderBufferingTest.java,v 1.12.2.3 2008/01/07 15:14:29 cwl Exp $
007: */
008:
009: package com.sleepycat.je.log;
010:
011: import java.io.File;
012: import java.io.IOException;
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.Iterator;
016:
017: import junit.framework.TestCase;
018:
019: import com.sleepycat.je.DatabaseException;
020: import com.sleepycat.je.DbInternal;
021: import com.sleepycat.je.Environment;
022: import com.sleepycat.je.EnvironmentConfig;
023: import com.sleepycat.je.config.EnvironmentParams;
024: import com.sleepycat.je.dbi.EnvironmentImpl;
025: import com.sleepycat.je.util.TestUtils;
026: import com.sleepycat.je.utilint.DbLsn;
027: import com.sleepycat.je.utilint.Tracer;
028:
029: /**
030: * Check our ability to adjust the file reader buffer size.
031: */
032: public class FileReaderBufferingTest extends TestCase {
033:
034: private File envHome;
035: private Environment env;
036: private EnvironmentImpl envImpl;
037: private ArrayList expectedLsns;
038: private ArrayList expectedVals;
039:
040: public FileReaderBufferingTest() {
041: super ();
042: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
043: }
044:
045: public void setUp() throws IOException, DatabaseException {
046:
047: TestUtils.removeLogFiles("Setup", envHome, false);
048: }
049:
050: public void tearDown() throws IOException, DatabaseException {
051:
052: TestUtils.removeFiles("TearDown", envHome,
053: FileManager.JE_SUFFIX);
054: }
055:
056: /**
057: * Should overflow once and then grow.
058: */
059: public void testBasic() throws Exception {
060: readLog(1050, // starting size of object in entry
061: 0, // object growth increment
062: 100, // starting read buffer size
063: "3000", // max read buffer size
064: 0); // expected number of overflows.
065: }
066:
067: /**
068: * Should overflow once and then grow.
069: */
070: public void testCantGrow() throws Exception {
071: readLog(2000, // starting size of object in entry
072: 0, // object growth increment
073: 100, // starting read buffer size
074: "1000", // max read buffer size
075: 11); // expected number of overflows. This comes out to
076: // an odd number because one of the entries hits it
077: // in just the right place so as to overflow twice.
078: }
079:
080: /**
081: * Should overflow, grow, and then reach the max.
082: */
083: public void testReachMax() throws Exception {
084: readLog(1000, // size of object in entry
085: 1000, // object growth increment
086: 100, // starting read buffer size
087: "3500", // max read buffer size
088: 8); // expected number of overflows.
089: }
090:
091: /**
092: *
093: */
094: private void readLog(int entrySize, int entrySizeIncrement,
095: int readBufferSize, String bufferMaxSize,
096: int expectedOverflows) throws Exception {
097:
098: try {
099:
100: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
101: envConfig.setAllowCreate(true);
102: envConfig.setConfigParam(
103: EnvironmentParams.LOG_ITERATOR_MAX_SIZE.getName(),
104: bufferMaxSize);
105: env = new Environment(envHome, envConfig);
106:
107: envImpl = DbInternal.envGetEnvironmentImpl(env);
108:
109: /* Make a log file */
110: createLogFile(10, entrySize, entrySizeIncrement);
111: SearchFileReader reader = new SearchFileReader(envImpl,
112: readBufferSize, true, DbLsn
113: .longToLsn((Long) expectedLsns.get(0)),
114: DbLsn.NULL_LSN, LogEntryType.LOG_TRACE);
115:
116: Iterator lsnIter = expectedLsns.iterator();
117: Iterator valIter = expectedVals.iterator();
118: while (reader.readNextEntry()) {
119: Tracer rec = (Tracer) reader.getLastObject();
120: assertTrue(lsnIter.hasNext());
121: assertEquals(reader.getLastLsn(), DbLsn
122: .longToLsn((Long) lsnIter.next()));
123: assertEquals((String) valIter.next(), rec.getMessage());
124: }
125: assertEquals(10, reader.getNumRead());
126: assertEquals(expectedOverflows, reader
127: .getNRepeatIteratorReads());
128:
129: } catch (Exception e) {
130: e.printStackTrace();
131: throw e;
132: } finally {
133: env.close();
134: }
135: }
136:
137: /**
138: * Write a logfile of entries, put the entries that we expect to
139: * read into a list for later verification.
140: * @return end of file LSN.
141: */
142: private void createLogFile(int numItems, int size, int sizeIncrement)
143: throws IOException, DatabaseException {
144:
145: LogManager logManager = envImpl.getLogManager();
146: expectedLsns = new ArrayList();
147: expectedVals = new ArrayList();
148:
149: for (int i = 0; i < numItems; i++) {
150: /* Add a debug record just to be filler. */
151: int recordSize = size + (i * sizeIncrement);
152: byte[] filler = new byte[recordSize];
153: Arrays.fill(filler, (byte) i);
154: String val = new String(filler);
155:
156: Tracer rec = new Tracer(val);
157: long lsn = rec.log(logManager);
158: expectedLsns.add(new Long(lsn));
159: expectedVals.add(val);
160: }
161:
162: logManager.flush();
163: envImpl.getFileManager().clear();
164: }
165: }
|