KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > DbDumpTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DbDumpTest.java,v 1.44 2006/10/30 21:14:53 bostic Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.BufferedReader JavaDoc;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.PrintStream JavaDoc;
18 import java.util.Hashtable JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import com.sleepycat.je.Cursor;
23 import com.sleepycat.je.Database;
24 import com.sleepycat.je.DatabaseConfig;
25 import com.sleepycat.je.DatabaseEntry;
26 import com.sleepycat.je.DatabaseException;
27 import com.sleepycat.je.DbInternal;
28 import com.sleepycat.je.Environment;
29 import com.sleepycat.je.EnvironmentConfig;
30 import com.sleepycat.je.LockMode;
31 import com.sleepycat.je.OperationStatus;
32 import com.sleepycat.je.config.EnvironmentParams;
33 import com.sleepycat.je.tree.Key;
34
35 public class DbDumpTest extends TestCase {
36
37     private File JavaDoc envHome;
38     
39     private static final int N_KEYS = 100;
40     private static final int N_KEY_BYTES = 1000;
41     private static final String JavaDoc dbName = "testDB";
42
43     private Environment env;
44
45     public DbDumpTest() {
46         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
47     }
48
49     public void setUp()
50     throws IOException JavaDoc {
51
52         TestUtils.removeLogFiles("Setup", envHome, false);
53     }
54     
55     public void tearDown()
56     throws IOException JavaDoc {
57
58         TestUtils.removeLogFiles("TearDown", envHome, false);
59     }
60     
61     /**
62      * A simple test to check if JE's dump format matches Core.
63      */

64     public void testMatchCore()
65         throws Throwable JavaDoc {
66
67         try {
68             /* Set up a new environment. */
69             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
70             envConfig.setAllowCreate(true);
71             env = new Environment(envHome, envConfig);
72         
73             /*
74              * Make a stream holding a small dump in a format known to be
75              * the same as Core DB.
76              */

77             ByteArrayOutputStream JavaDoc dumpInfo = new ByteArrayOutputStream JavaDoc();
78             PrintStream JavaDoc dumpStream = new PrintStream JavaDoc(dumpInfo);
79             dumpStream.println("VERSION=3");
80             dumpStream.println("format=print");
81             dumpStream.println("type=btree");
82             dumpStream.println("dupsort=0");
83             dumpStream.println("HEADER=END");
84             dumpStream.println(" abc");
85             dumpStream.println(" firstLetters");
86             dumpStream.println(" xyz");
87             dumpStream.println(" lastLetters");
88             dumpStream.println("DATA=END");
89
90             /* load it */
91             DbLoad loader = new DbLoad();
92             loader.setEnv(env);
93             loader.setInputReader(new BufferedReader JavaDoc(new InputStreamReader JavaDoc
94                              (new ByteArrayInputStream JavaDoc(dumpInfo.toByteArray()))));
95             loader.setNoOverwrite(false);
96         loader.setDbName("foobar");
97             loader.load();
98
99             /* Make sure we retrieve the expected data. */
100             Database checkDb = env.openDatabase(null, "foobar", null);
101             Cursor cursor = checkDb.openCursor(null, null);
102             DatabaseEntry key = new DatabaseEntry();
103             DatabaseEntry data = new DatabaseEntry();
104             assertEquals(OperationStatus.SUCCESS,
105                          cursor.getNext(key, data, LockMode.DEFAULT));
106             assertEquals("abc", new String JavaDoc(key.getData()));
107             assertEquals("firstLetters", new String JavaDoc(data.getData()));
108             assertEquals(OperationStatus.SUCCESS,
109                          cursor.getNext(key, data, LockMode.DEFAULT));
110             assertEquals("xyz", new String JavaDoc(key.getData()));
111             assertEquals("lastLetters", new String JavaDoc(data.getData()));
112             assertEquals(OperationStatus.NOTFOUND,
113                          cursor.getNext(key, data, LockMode.DEFAULT));
114             cursor.close();
115             checkDb.close();
116
117             /* Check that a dump of the database matches the input file. */
118             ByteArrayOutputStream JavaDoc dump2 = new ByteArrayOutputStream JavaDoc();
119             DbDump dumper2 = new DbDump(env, "foobar",
120                                         new PrintStream JavaDoc(dump2),
121                     null, true);
122             dumper2.dump();
123             assertEquals(dump2.toString(), dumpInfo.toString());
124             
125             env.close();
126         } catch (Throwable JavaDoc t) {
127             t.printStackTrace();
128             throw t;
129         }
130     }
131
132     public void testDumpLoadBinary()
133         throws Throwable JavaDoc {
134
135         try {
136             doDumpLoadTest(false, 1);
137         } catch (Throwable JavaDoc t) {
138             t.printStackTrace();
139             throw t;
140         }
141     }
142
143     public void testDumpLoadPrintable()
144         throws IOException JavaDoc, DatabaseException {
145
146     doDumpLoadTest(true, 1);
147     }
148
149     public void testDumpLoadTwo()
150         throws IOException JavaDoc, DatabaseException {
151
152     doDumpLoadTest(false, 2);
153     }
154
155     public void testDumpLoadThree()
156         throws IOException JavaDoc, DatabaseException {
157
158     doDumpLoadTest(true, 3);
159     }
160
161     private void doDumpLoadTest(boolean printable, int nDumps)
162     throws IOException JavaDoc, DatabaseException {
163
164     Hashtable JavaDoc[] dataMaps = new Hashtable JavaDoc[nDumps];
165         for (int i = 0; i < nDumps; i += 1) {
166             dataMaps[i] = new Hashtable JavaDoc();
167         }
168     initDbs(nDumps, dataMaps);
169     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
170     PrintStream JavaDoc out = new PrintStream JavaDoc(baos);
171         for (int i = 0; i < nDumps; i += 1) {
172             DbDump dumper =
173         new DbDump(env, dbName + i, out, null, printable);
174             dumper.dump();
175         }
176     byte[] baosba = baos.toByteArray();
177         BufferedReader JavaDoc rdr = new BufferedReader JavaDoc
178             (new InputStreamReader JavaDoc(new ByteArrayInputStream JavaDoc(baosba)));
179         for (int i = 0; i < nDumps; i += 1) {
180             DbLoad loader = new DbLoad();
181             loader.setEnv(env);
182             loader.setInputReader(rdr);
183             loader.setNoOverwrite(false);
184         loader.setDbName(dbName + i);
185             loader.load();
186             verifyDb(dataMaps[i], i);
187         }
188
189         ByteArrayOutputStream JavaDoc baos2 = new ByteArrayOutputStream JavaDoc();
190         PrintStream JavaDoc out2 = new PrintStream JavaDoc(baos2);
191         for (int i = 0; i < nDumps; i += 1) {
192             DbDump dumper2 =
193         new DbDump(env, dbName + i, out2, null, printable);
194             dumper2.dump();
195         }
196         assertEquals(0, Key.compareKeys(baosba, baos2.toByteArray(), null));
197         
198     env.close();
199     }
200
201     /**
202      * Set up the environment and db.
203      */

204     private void initDbs(int nDumps, Hashtable JavaDoc[] dataMaps)
205     throws DatabaseException {
206
207     EnvironmentConfig envConfig = TestUtils.initEnvConfig();
208         envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
209         envConfig.setAllowCreate(true);
210         env = new Environment(envHome, envConfig);
211
212         /* Make a db and open it. */
213         for (int i = 0; i < nDumps; i += 1) {
214             DatabaseConfig dbConfig = new DatabaseConfig();
215             dbConfig.setAllowCreate(true);
216             dbConfig.setSortedDuplicates(true);
217             Database myDb = env.openDatabase(null, dbName + i, dbConfig);
218             Cursor cursor = myDb.openCursor(null, null);
219             doLargePut(dataMaps[i], cursor, N_KEYS);
220             cursor.close();
221             myDb.close();
222         }
223     }
224
225     private void verifyDb(Hashtable JavaDoc dataMap, int dumpIndex)
226     throws DatabaseException {
227
228         DatabaseConfig config = new DatabaseConfig();
229         config.setReadOnly(true);
230         DbInternal.setUseExistingConfig(config, true);
231     Database myDb = env.openDatabase(null, dbName + dumpIndex, config);
232     Cursor cursor = myDb.openCursor(null, null);
233     StringDbt foundKey = new StringDbt();
234     StringDbt foundData = new StringDbt();
235     OperationStatus status =
236         cursor.getFirst(foundKey, foundData, LockMode.DEFAULT);
237     while (status == OperationStatus.SUCCESS) {
238         String JavaDoc foundKeyString = foundKey.getString();
239         String JavaDoc foundDataString = foundData.getString();
240         if (dataMap.get(foundKeyString) != null) {
241         assertTrue(((String JavaDoc) dataMap.get(foundKeyString)).
242                equals(foundDataString));
243         dataMap.remove(foundKeyString);
244         } else {
245         fail("didn't find key in either map (" +
246              foundKeyString +
247              ")");
248         }
249         status = cursor.getNext(foundKey, foundData, LockMode.DEFAULT);
250     }
251     assertTrue(dataMap.size() == 0);
252         cursor.close();
253         myDb.close();
254     }
255
256     private void doLargePut(Hashtable JavaDoc dataMap, Cursor cursor, int nKeys)
257     throws DatabaseException {
258
259     for (int i = 0; i < nKeys; i++) {
260         byte[] key = new byte[N_KEY_BYTES];
261         TestUtils.generateRandomAlphaBytes(key);
262         String JavaDoc keyString = new String JavaDoc(key);
263         String JavaDoc dataString = Integer.toString(i);
264         OperationStatus status =
265         cursor.put(new StringDbt(key),
266                            new StringDbt(dataString));
267         assertEquals(OperationStatus.SUCCESS, status);
268         if (dataMap != null) {
269         dataMap.put(keyString, dataString);
270         }
271     }
272     }
273 }
274
Popular Tags