1 8 9 package com.sleepycat.je.util; 10 11 import java.io.BufferedReader ; 12 import java.io.ByteArrayInputStream ; 13 import java.io.ByteArrayOutputStream ; 14 import java.io.File ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 import java.io.PrintStream ; 18 import java.util.Hashtable ; 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 envHome; 38 39 private static final int N_KEYS = 100; 40 private static final int N_KEY_BYTES = 1000; 41 private static final String dbName = "testDB"; 42 43 private Environment env; 44 45 public DbDumpTest() { 46 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 47 } 48 49 public void setUp() 50 throws IOException { 51 52 TestUtils.removeLogFiles("Setup", envHome, false); 53 } 54 55 public void tearDown() 56 throws IOException { 57 58 TestUtils.removeLogFiles("TearDown", envHome, false); 59 } 60 61 64 public void testMatchCore() 65 throws Throwable { 66 67 try { 68 69 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 70 envConfig.setAllowCreate(true); 71 env = new Environment(envHome, envConfig); 72 73 77 ByteArrayOutputStream dumpInfo = new ByteArrayOutputStream (); 78 PrintStream dumpStream = new PrintStream (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 91 DbLoad loader = new DbLoad(); 92 loader.setEnv(env); 93 loader.setInputReader(new BufferedReader (new InputStreamReader 94 (new ByteArrayInputStream (dumpInfo.toByteArray())))); 95 loader.setNoOverwrite(false); 96 loader.setDbName("foobar"); 97 loader.load(); 98 99 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 (key.getData())); 107 assertEquals("firstLetters", new String (data.getData())); 108 assertEquals(OperationStatus.SUCCESS, 109 cursor.getNext(key, data, LockMode.DEFAULT)); 110 assertEquals("xyz", new String (key.getData())); 111 assertEquals("lastLetters", new String (data.getData())); 112 assertEquals(OperationStatus.NOTFOUND, 113 cursor.getNext(key, data, LockMode.DEFAULT)); 114 cursor.close(); 115 checkDb.close(); 116 117 118 ByteArrayOutputStream dump2 = new ByteArrayOutputStream (); 119 DbDump dumper2 = new DbDump(env, "foobar", 120 new PrintStream (dump2), 121 null, true); 122 dumper2.dump(); 123 assertEquals(dump2.toString(), dumpInfo.toString()); 124 125 env.close(); 126 } catch (Throwable t) { 127 t.printStackTrace(); 128 throw t; 129 } 130 } 131 132 public void testDumpLoadBinary() 133 throws Throwable { 134 135 try { 136 doDumpLoadTest(false, 1); 137 } catch (Throwable t) { 138 t.printStackTrace(); 139 throw t; 140 } 141 } 142 143 public void testDumpLoadPrintable() 144 throws IOException , DatabaseException { 145 146 doDumpLoadTest(true, 1); 147 } 148 149 public void testDumpLoadTwo() 150 throws IOException , DatabaseException { 151 152 doDumpLoadTest(false, 2); 153 } 154 155 public void testDumpLoadThree() 156 throws IOException , DatabaseException { 157 158 doDumpLoadTest(true, 3); 159 } 160 161 private void doDumpLoadTest(boolean printable, int nDumps) 162 throws IOException , DatabaseException { 163 164 Hashtable [] dataMaps = new Hashtable [nDumps]; 165 for (int i = 0; i < nDumps; i += 1) { 166 dataMaps[i] = new Hashtable (); 167 } 168 initDbs(nDumps, dataMaps); 169 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 170 PrintStream out = new PrintStream (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 rdr = new BufferedReader 178 (new InputStreamReader (new ByteArrayInputStream (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 baos2 = new ByteArrayOutputStream (); 190 PrintStream out2 = new PrintStream (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 204 private void initDbs(int nDumps, Hashtable [] 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 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 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 foundKeyString = foundKey.getString(); 239 String foundDataString = foundData.getString(); 240 if (dataMap.get(foundKeyString) != null) { 241 assertTrue(((String ) 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 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 keyString = new String (key); 263 String 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 |