001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: DbDumpTest.java,v 1.44.2.3 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.util;
010:
011: import java.io.BufferedReader;
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.File;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017: import java.io.PrintStream;
018: import java.util.Hashtable;
019:
020: import junit.framework.TestCase;
021:
022: import com.sleepycat.je.Cursor;
023: import com.sleepycat.je.Database;
024: import com.sleepycat.je.DatabaseConfig;
025: import com.sleepycat.je.DatabaseEntry;
026: import com.sleepycat.je.DatabaseException;
027: import com.sleepycat.je.DbInternal;
028: import com.sleepycat.je.Environment;
029: import com.sleepycat.je.EnvironmentConfig;
030: import com.sleepycat.je.LockMode;
031: import com.sleepycat.je.OperationStatus;
032: import com.sleepycat.je.config.EnvironmentParams;
033: import com.sleepycat.je.tree.Key;
034:
035: public class DbDumpTest extends TestCase {
036:
037: private File envHome;
038:
039: private static final int N_KEYS = 100;
040: private static final int N_KEY_BYTES = 1000;
041: private static final String dbName = "testDB";
042:
043: private Environment env;
044:
045: public DbDumpTest() {
046: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
047: }
048:
049: public void setUp() throws IOException {
050:
051: TestUtils.removeLogFiles("Setup", envHome, false);
052: }
053:
054: public void tearDown() throws IOException {
055:
056: TestUtils.removeLogFiles("TearDown", envHome, false);
057: }
058:
059: /**
060: * A simple test to check if JE's dump format matches Core.
061: */
062: public void testMatchCore() throws Throwable {
063:
064: try {
065: /* Set up a new environment. */
066: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
067: envConfig.setAllowCreate(true);
068: env = new Environment(envHome, envConfig);
069:
070: /*
071: * Make a stream holding a small dump in a format known to be
072: * the same as Core DB.
073: */
074: ByteArrayOutputStream dumpInfo = new ByteArrayOutputStream();
075: PrintStream dumpStream = new PrintStream(dumpInfo);
076: dumpStream.println("VERSION=3");
077: dumpStream.println("format=print");
078: dumpStream.println("type=btree");
079: dumpStream.println("dupsort=0");
080: dumpStream.println("HEADER=END");
081: dumpStream.println(" abc");
082: dumpStream.println(" firstLetters");
083: dumpStream.println(" xyz");
084: dumpStream.println(" lastLetters");
085: dumpStream.println("DATA=END");
086:
087: /* load it */
088: DbLoad loader = new DbLoad();
089: loader.setEnv(env);
090: loader.setInputReader(new BufferedReader(
091: new InputStreamReader(new ByteArrayInputStream(
092: dumpInfo.toByteArray()))));
093: loader.setNoOverwrite(false);
094: loader.setDbName("foobar");
095: loader.load();
096:
097: /* Make sure we retrieve the expected data. */
098: Database checkDb = env.openDatabase(null, "foobar", null);
099: Cursor cursor = checkDb.openCursor(null, null);
100: DatabaseEntry key = new DatabaseEntry();
101: DatabaseEntry data = new DatabaseEntry();
102: assertEquals(OperationStatus.SUCCESS, cursor.getNext(key,
103: data, LockMode.DEFAULT));
104: assertEquals("abc", new String(key.getData()));
105: assertEquals("firstLetters", new String(data.getData()));
106: assertEquals(OperationStatus.SUCCESS, cursor.getNext(key,
107: data, LockMode.DEFAULT));
108: assertEquals("xyz", new String(key.getData()));
109: assertEquals("lastLetters", new String(data.getData()));
110: assertEquals(OperationStatus.NOTFOUND, cursor.getNext(key,
111: data, LockMode.DEFAULT));
112: cursor.close();
113: checkDb.close();
114:
115: /* Check that a dump of the database matches the input file. */
116: ByteArrayOutputStream dump2 = new ByteArrayOutputStream();
117: DbDump dumper2 = new DbDump(env, "foobar", new PrintStream(
118: dump2), null, true);
119: dumper2.dump();
120: assertEquals(dump2.toString(), dumpInfo.toString());
121:
122: env.close();
123: } catch (Throwable t) {
124: t.printStackTrace();
125: throw t;
126: }
127: }
128:
129: public void testDumpLoadBinary() throws Throwable {
130:
131: try {
132: doDumpLoadTest(false, 1);
133: } catch (Throwable t) {
134: t.printStackTrace();
135: throw t;
136: }
137: }
138:
139: public void testDumpLoadPrintable() throws IOException,
140: DatabaseException {
141:
142: doDumpLoadTest(true, 1);
143: }
144:
145: public void testDumpLoadTwo() throws IOException, DatabaseException {
146:
147: doDumpLoadTest(false, 2);
148: }
149:
150: public void testDumpLoadThree() throws IOException,
151: DatabaseException {
152:
153: doDumpLoadTest(true, 3);
154: }
155:
156: private void doDumpLoadTest(boolean printable, int nDumps)
157: throws IOException, DatabaseException {
158:
159: Hashtable[] dataMaps = new Hashtable[nDumps];
160: for (int i = 0; i < nDumps; i += 1) {
161: dataMaps[i] = new Hashtable();
162: }
163: initDbs(nDumps, dataMaps);
164: ByteArrayOutputStream baos = new ByteArrayOutputStream();
165: PrintStream out = new PrintStream(baos);
166: for (int i = 0; i < nDumps; i += 1) {
167: DbDump dumper = new DbDump(env, dbName + i, out, null,
168: printable);
169: dumper.dump();
170: }
171: byte[] baosba = baos.toByteArray();
172: BufferedReader rdr = new BufferedReader(new InputStreamReader(
173: new ByteArrayInputStream(baosba)));
174: for (int i = 0; i < nDumps; i += 1) {
175: DbLoad loader = new DbLoad();
176: loader.setEnv(env);
177: loader.setInputReader(rdr);
178: loader.setNoOverwrite(false);
179: loader.setDbName(dbName + i);
180: loader.load();
181: verifyDb(dataMaps[i], i);
182: }
183:
184: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
185: PrintStream out2 = new PrintStream(baos2);
186: for (int i = 0; i < nDumps; i += 1) {
187: DbDump dumper2 = new DbDump(env, dbName + i, out2, null,
188: printable);
189: dumper2.dump();
190: }
191: assertEquals(0, Key.compareKeys(baosba, baos2.toByteArray(),
192: null));
193:
194: env.close();
195: }
196:
197: /**
198: * Set up the environment and db.
199: */
200: private void initDbs(int nDumps, Hashtable[] dataMaps)
201: throws DatabaseException {
202:
203: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
204: envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
205: "6");
206: envConfig.setAllowCreate(true);
207: env = new Environment(envHome, envConfig);
208:
209: /* Make a db and open it. */
210: for (int i = 0; i < nDumps; i += 1) {
211: DatabaseConfig dbConfig = new DatabaseConfig();
212: dbConfig.setAllowCreate(true);
213: dbConfig.setSortedDuplicates(true);
214: Database myDb = env
215: .openDatabase(null, dbName + i, dbConfig);
216: Cursor cursor = myDb.openCursor(null, null);
217: doLargePut(dataMaps[i], cursor, N_KEYS);
218: cursor.close();
219: myDb.close();
220: }
221: }
222:
223: private void verifyDb(Hashtable dataMap, int dumpIndex)
224: throws DatabaseException {
225:
226: DatabaseConfig config = new DatabaseConfig();
227: config.setReadOnly(true);
228: DbInternal.setUseExistingConfig(config, true);
229: Database myDb = env.openDatabase(null, dbName + dumpIndex,
230: config);
231: Cursor cursor = myDb.openCursor(null, null);
232: StringDbt foundKey = new StringDbt();
233: StringDbt foundData = new StringDbt();
234: OperationStatus status = cursor.getFirst(foundKey, foundData,
235: LockMode.DEFAULT);
236: while (status == OperationStatus.SUCCESS) {
237: String foundKeyString = foundKey.getString();
238: String foundDataString = foundData.getString();
239: if (dataMap.get(foundKeyString) != null) {
240: assertTrue(((String) dataMap.get(foundKeyString))
241: .equals(foundDataString));
242: dataMap.remove(foundKeyString);
243: } else {
244: fail("didn't find key in either map (" + foundKeyString
245: + ")");
246: }
247: status = cursor.getNext(foundKey, foundData,
248: LockMode.DEFAULT);
249: }
250: assertTrue(dataMap.size() == 0);
251: cursor.close();
252: myDb.close();
253: }
254:
255: private void doLargePut(Hashtable dataMap, Cursor cursor, int nKeys)
256: throws DatabaseException {
257:
258: for (int i = 0; i < nKeys; i++) {
259: byte[] key = new byte[N_KEY_BYTES];
260: TestUtils.generateRandomAlphaBytes(key);
261: String keyString = new String(key);
262: String dataString = Integer.toString(i);
263: OperationStatus status = cursor.put(new StringDbt(key),
264: new StringDbt(dataString));
265: assertEquals(OperationStatus.SUCCESS, status);
266: if (dataMap != null) {
267: dataMap.put(keyString, dataString);
268: }
269: }
270: }
271: }
|