1 8 9 package com.sleepycat.je.log; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.nio.ByteBuffer ; 14 15 import javax.xml.parsers.DocumentBuilderFactory ; 16 17 import junit.framework.TestCase; 18 19 import com.sleepycat.je.DatabaseConfig; 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.EnvironmentConfig; 22 import com.sleepycat.je.cleaner.FileSummary; 23 import com.sleepycat.je.config.EnvironmentParams; 24 import com.sleepycat.je.dbi.DatabaseId; 25 import com.sleepycat.je.dbi.DatabaseImpl; 26 import com.sleepycat.je.dbi.DbTree; 27 import com.sleepycat.je.dbi.EnvironmentImpl; 28 import com.sleepycat.je.recovery.CheckpointEnd; 29 import com.sleepycat.je.recovery.CheckpointStart; 30 import com.sleepycat.je.tree.BIN; 31 import com.sleepycat.je.tree.ChildReference; 32 import com.sleepycat.je.tree.DBIN; 33 import com.sleepycat.je.tree.DIN; 34 import com.sleepycat.je.tree.FileSummaryLN; 35 import com.sleepycat.je.tree.IN; 36 import com.sleepycat.je.tree.INDeleteInfo; 37 import com.sleepycat.je.tree.LN; 38 import com.sleepycat.je.tree.MapLN; 39 import com.sleepycat.je.tree.Node; 40 import com.sleepycat.je.txn.TxnAbort; 41 import com.sleepycat.je.txn.TxnCommit; 42 import com.sleepycat.je.txn.TxnPrepare; 43 import com.sleepycat.je.util.TestUtils; 44 import com.sleepycat.je.utilint.DbLsn; 45 import com.sleepycat.je.utilint.Tracer; 46 47 50 public class LoggableTest extends TestCase { 51 52 static private final boolean DEBUG = false; 53 54 private EnvironmentImpl env; 56 private File envHome; 57 private DatabaseImpl database; 58 59 public LoggableTest() 60 throws Exception { 61 62 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 63 64 68 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 69 } 70 71 public void setUp() 72 throws IOException , DatabaseException { 73 74 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 75 76 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 77 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6"); 78 envConfig.setAllowCreate(true); 79 env = new EnvironmentImpl(envHome, envConfig); 80 } 81 82 public void tearDown() 83 throws IOException , DatabaseException { 84 85 TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX); 86 env.close(); 87 } 88 89 public void testEntryData() 90 throws Throwable { 91 92 try { 93 ByteBuffer buffer = ByteBuffer.allocate(1000); 94 database = new DatabaseImpl("foo", new DatabaseId(1), 95 env, new DatabaseConfig()); 96 97 100 101 104 Tracer dMsg = new Tracer("Hello there"); 105 writeAndRead(buffer, dMsg, new Tracer()); 106 107 110 String data = "abcdef"; 111 LN ln = new LN(data.getBytes()); 112 LN lnFromLog = new LN(); 113 writeAndRead(buffer, ln, lnFromLog); 114 lnFromLog.verify(null); 115 assertTrue(ln.marshallOutsideWriteLatch()); 116 117 FileSummaryLN fsLN = new FileSummaryLN(new FileSummary()); 118 FileSummaryLN fsLNFromLog = new FileSummaryLN(); 119 writeAndRead(buffer, fsLN, fsLNFromLog); 120 assertFalse(fsLN.marshallOutsideWriteLatch()); 121 122 125 IN in = new IN(database, 126 new byte[] {1,0,1,0}, 127 7, 5); 128 in.latch(); 129 in.insertEntry(new ChildReference(null, 130 new byte[] {1,0,1,0}, 131 DbLsn.makeLsn(12, 200))); 132 in.insertEntry(new ChildReference(null, 133 new byte[] {1,1,1,0}, 134 DbLsn.makeLsn(29, 300))); 135 in.insertEntry(new ChildReference(null, 136 new byte[] {0,0,1,0}, 137 DbLsn.makeLsn(35, 400))); 138 139 140 IN inFromLog = new IN(); 141 inFromLog.latch(); 142 writeAndRead(buffer, in, inFromLog); 143 inFromLog.releaseLatch(); 144 in.releaseLatch(); 145 146 149 in = new IN(database, 150 new byte[] {1,0,1,0}, 151 7, 5); 152 in.latch(); 153 in.insertEntry(new ChildReference(null, 154 new byte[] {1,0,1,0}, 155 DbLsn.makeLsn(12, 200))); 156 in.insertEntry(new ChildReference(null, 157 new byte[] {1,1,1,0}, 158 DbLsn.makeLsn(29, 300))); 159 in.insertEntry(new ChildReference(null, 160 new byte[] {0,0,1,0}, 161 DbLsn.makeLsn(1235, 400))); 162 in.insertEntry(new ChildReference(null, 163 new byte[] {0,0,1,0}, 164 DbLsn.makeLsn(0xFFFFFFF0L, 400))); 165 166 167 inFromLog = new IN(); 168 inFromLog.latch(); 169 writeAndRead(buffer, in, inFromLog); 170 inFromLog.releaseLatch(); 171 in.releaseLatch(); 172 173 176 BIN bin = new BIN(database, 177 new byte[] {3,2,1}, 178 8, 5); 179 bin.latch(); 180 bin.insertEntry(new ChildReference(null, 181 new byte[] {1,0,1,0}, 182 DbLsn.makeLsn(212, 200))); 183 bin.insertEntry(new ChildReference(null, 184 new byte[] {1,1,1,0}, 185 DbLsn.makeLsn(229, 300))); 186 bin.insertEntry(new ChildReference(null, 187 new byte[] {0,0,1,0}, 188 DbLsn.makeLsn(235, 400))); 189 BIN binFromLog = new BIN(); 190 binFromLog.latch(); 191 writeAndRead(buffer, bin, binFromLog); 192 binFromLog.verify(null); 193 binFromLog.releaseLatch(); 194 bin.releaseLatch(); 195 196 199 DIN din = new DIN(database, 200 new byte[] {1,0,0,1}, 201 7, 202 new byte[] {0,1,1,0}, 203 new ChildReference(null, 204 new byte[] {1,0,0,1}, 205 DbLsn.makeLsn(10, 100)), 206 5); 207 din.latch(); 208 din.insertEntry(new ChildReference(null, 209 new byte[] {1,0,1,0}, 210 DbLsn.makeLsn(12, 200))); 211 din.insertEntry(new ChildReference(null, 212 new byte[] {1,1,1,0}, 213 DbLsn.makeLsn(29, 300))); 214 din.insertEntry(new ChildReference(null, 215 new byte[] {0,0,1,0}, 216 DbLsn.makeLsn(35, 400))); 217 218 219 DIN dinFromLog = new DIN(); 220 dinFromLog.latch(); 221 writeAndRead(buffer, din, dinFromLog); 222 din.releaseLatch(); 223 dinFromLog.releaseLatch(); 224 225 228 DBIN dbin = new DBIN(database, 229 new byte[] {3,2,1}, 230 8, 231 new byte[] {1,2,3}, 232 5); 233 dbin.latch(); 234 dbin.insertEntry(new ChildReference(null, 235 new byte[] {1,0,1,0}, 236 DbLsn.makeLsn(212, 200))); 237 dbin.insertEntry(new ChildReference(null, 238 new byte[] {1,1,1,0}, 239 DbLsn.makeLsn(229, 300))); 240 dbin.insertEntry(new ChildReference(null, 241 new byte[] {0,0,1,0}, 242 DbLsn.makeLsn(235, 400))); 243 DBIN dbinFromLog = new DBIN(); 244 dbinFromLog.latch(); 245 writeAndRead(buffer, dbin, dbinFromLog); 246 dbinFromLog.verify(null); 247 dbin.releaseLatch(); 248 dbinFromLog.releaseLatch(); 249 250 253 DbTree dbTree = new DbTree(env); 254 DbTree dbTreeFromLog = new DbTree(); 255 writeAndRead(buffer, dbTree, dbTreeFromLog); 256 257 260 MapLN mapLn = new MapLN(database); 261 MapLN mapLnFromLog = new MapLN(); 262 writeAndRead(buffer, mapLn, mapLnFromLog); 263 264 267 268 278 279 280 283 TxnCommit commit = new TxnCommit(111, DbLsn.makeLsn(10, 10)); 284 TxnCommit commitFromLog = new TxnCommit(); 285 writeAndRead(buffer, commit, commitFromLog); 286 287 290 TxnAbort abort = new TxnAbort(111, DbLsn.makeLsn(11, 11)); 291 TxnAbort abortFromLog = new TxnAbort(); 292 writeAndRead(buffer, abort, abortFromLog); 293 294 297 byte[] gid = new byte[64]; 298 byte[] bqual = new byte[64]; 299 TxnPrepare prepare = 300 new TxnPrepare(111, new LogUtils.XidImpl(1, gid, bqual)); 301 TxnPrepare prepareFromLog = new TxnPrepare(); 302 writeAndRead(buffer, prepare, prepareFromLog); 303 304 prepare = 305 new TxnPrepare(111, new LogUtils.XidImpl(1, null, bqual)); 306 prepareFromLog = new TxnPrepare(); 307 writeAndRead(buffer, prepare, prepareFromLog); 308 309 prepare = 310 new TxnPrepare(111, new LogUtils.XidImpl(1, gid, null)); 311 prepareFromLog = new TxnPrepare(); 312 writeAndRead(buffer, prepare, prepareFromLog); 313 314 317 INDeleteInfo info = new INDeleteInfo(77, new byte[1], 318 new DatabaseId(100)); 319 INDeleteInfo infoFromLog = new INDeleteInfo(); 320 writeAndRead(buffer, info, infoFromLog); 321 322 325 CheckpointStart start = new CheckpointStart(177, "test"); 326 CheckpointStart startFromLog = new CheckpointStart(); 327 writeAndRead(buffer, start, startFromLog); 328 329 332 CheckpointEnd end = 333 new CheckpointEnd("test", 334 DbLsn.makeLsn(20, 55), 335 env.getRootLsn(), 336 env.getTxnManager().getFirstActiveLsn(), 337 Node.getLastId(), 338 env.getDbMapTree().getLastDbId(), 339 env.getTxnManager().getLastTxnId(), 340 177); 341 CheckpointEnd endFromLog = new CheckpointEnd(); 342 writeAndRead(buffer, end, endFromLog); 343 } catch (Throwable t) { 344 t.printStackTrace(); 345 throw t; 346 } 347 } 348 349 353 private void writeAndRead(ByteBuffer buffer, 354 LogWritable orig, 355 LogReadable fromLog) 356 throws Exception { 357 358 byte entryTypeVersion = 359 ((LoggableObject) orig).getLogType().getVersion(); 360 361 362 363 buffer.clear(); 364 orig.writeToLog(buffer); 365 366 367 buffer.flip(); 368 assertEquals(buffer.limit(), orig.getLogSize()); 369 370 374 fromLog.readFromLog(buffer, entryTypeVersion); 375 assertEquals(orig.getLogSize(), ((LogWritable) fromLog).getLogSize()); 376 377 assertEquals("We should have read the whole buffer for " + 378 fromLog.getClass().getName(), 379 buffer.limit(), buffer.position()); 380 381 382 StringBuffer sb1 = new StringBuffer (); 383 StringBuffer sb2 = new StringBuffer (); 384 ((LogReadable) orig).dumpLog(sb1, true); 385 fromLog.dumpLog(sb2, true); 386 387 if (DEBUG) { 388 System.out.println("sb1 = " + sb1.toString()); 389 System.out.println("sb2 = " + sb2.toString()); 390 } 391 assertEquals("Not equals for " + 392 fromLog.getClass().getName(), 393 sb1.toString(), sb2.toString()); 394 395 396 } 400 } 401 | Popular Tags |