KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > log > LoggableTest


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

8
9 package com.sleepycat.je.log;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.nio.ByteBuffer JavaDoc;
14
15 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 /**
48  * Check that every loggable object can be read in and out of a buffer
49  */

50 public class LoggableTest extends TestCase {
51
52     static private final boolean DEBUG = false;
53
54     // private DocumentBuilder builder;
55
private EnvironmentImpl env;
56     private File JavaDoc envHome;
57     private DatabaseImpl database;
58
59     public LoggableTest()
60         throws Exception JavaDoc {
61
62         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
63
64         /*
65          * Attempt to validate that the db print log output is valid
66          * xml -- isn't quite working
67          */

68         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
69     }
70
71     public void setUp()
72         throws IOException JavaDoc, 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 JavaDoc, DatabaseException {
84
85         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
86         env.close();
87     }
88
89     public void testEntryData()
90         throws Throwable JavaDoc {
91
92         try {
93             ByteBuffer JavaDoc buffer = ByteBuffer.allocate(1000);
94             database = new DatabaseImpl("foo", new DatabaseId(1),
95                                         env, new DatabaseConfig());
96
97             /*
98              * For each loggable object, can we write the entry data out?
99              */

100
101             /*
102              * Tracer records.
103              */

104             Tracer dMsg = new Tracer("Hello there");
105             writeAndRead(buffer, dMsg, new Tracer());
106
107             /*
108              * LNs
109              */

110             String JavaDoc 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             /*
123              * INs
124              */

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             /* Write it. */
140             IN inFromLog = new IN();
141         inFromLog.latch();
142             writeAndRead(buffer, in, inFromLog);
143         inFromLog.releaseLatch();
144         in.releaseLatch();
145
146         /*
147          * IN - long form
148          */

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             /* Write it. */
167             inFromLog = new IN();
168         inFromLog.latch();
169             writeAndRead(buffer, in, inFromLog);
170         inFromLog.releaseLatch();
171         in.releaseLatch();
172
173             /*
174              * BINs
175              */

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             /*
197              * DINs
198              */

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             /* Write it. */
219             DIN dinFromLog = new DIN();
220         dinFromLog.latch();
221             writeAndRead(buffer, din, dinFromLog);
222         din.releaseLatch();
223         dinFromLog.releaseLatch();
224
225             /*
226              * DBINs
227              */

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             /*
251              * Root
252              */

253             DbTree dbTree = new DbTree(env);
254             DbTree dbTreeFromLog = new DbTree();
255             writeAndRead(buffer, dbTree, dbTreeFromLog);
256
257             /*
258              * MapLN
259              */

260             MapLN mapLn = new MapLN(database);
261             MapLN mapLnFromLog = new MapLN();
262             writeAndRead(buffer, mapLn, mapLnFromLog);
263
264             /*
265              * UserTxn
266              */

267
268             /*
269          * Disabled for now because these txns don't compare equal,
270              * because one has a name of "main" and the other has a name of
271              * null because it was read from the log.
272
273          Txn txn = new Txn(env, new TransactionConfig());
274          Txn txnFromLog = new Txn();
275          writeAndRead(buffer, txn, txnFromLog);
276          txn.commit();
277             */

278
279
280             /*
281              * TxnCommit
282              */

283             TxnCommit commit = new TxnCommit(111, DbLsn.makeLsn(10, 10));
284             TxnCommit commitFromLog = new TxnCommit();
285             writeAndRead(buffer, commit, commitFromLog);
286
287             /*
288              * TxnAbort
289              */

290             TxnAbort abort = new TxnAbort(111, DbLsn.makeLsn(11, 11));
291             TxnAbort abortFromLog = new TxnAbort();
292             writeAndRead(buffer, abort, abortFromLog);
293
294             /*
295              * TxnPrepare
296              */

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             /*
315              * IN delete info
316              */

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             /*
323              * Checkpoint start
324              */

325             CheckpointStart start = new CheckpointStart(177, "test");
326             CheckpointStart startFromLog = new CheckpointStart();
327             writeAndRead(buffer, start, startFromLog);
328
329             /*
330              * Checkpoint end
331              */

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 JavaDoc t) {
344             t.printStackTrace();
345             throw t;
346         }
347     }
348
349     /**
350      * Helper which takes a dbLoggable, writes it, reads it back and
351      * checks for equality and size
352      */

353     private void writeAndRead(ByteBuffer JavaDoc buffer,
354                   LogWritable orig,
355                               LogReadable fromLog)
356         throws Exception JavaDoc {
357
358     byte entryTypeVersion =
359         ((LoggableObject) orig).getLogType().getVersion();
360
361         /* Write it. */
362
363         buffer.clear();
364         orig.writeToLog(buffer);
365
366         /* Check the log size. */
367         buffer.flip();
368         assertEquals(buffer.limit(), orig.getLogSize());
369
370         /*
371      * Read it and compare sizes. Note that we assume we're testing
372      * objects that are readable and writable to the log.
373      */

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         /* Compare contents. */
382         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
383         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc();
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         /* Validate that the dump string is valid XML. */
396         // builder = factory.newDocumentBuilder();
397
// builder.parse("<?xml version=\"1.0\" ?>");
398
// sb1.toString()+
399
}
400 }
401
Popular Tags