KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > logversion > MakeLogEntryVersionData


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: MakeLogEntryVersionData.java,v 1.11 2006/11/03 02:38:54 mark Exp $
7  */

8
9 package com.sleepycat.je.logversion;
10
11 import java.io.BufferedOutputStream JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.transaction.xa.XAResource JavaDoc;
18
19 import com.sleepycat.je.CheckpointConfig;
20 import com.sleepycat.je.Cursor;
21 import com.sleepycat.je.Database;
22 import com.sleepycat.je.DatabaseConfig;
23 import com.sleepycat.je.DatabaseEntry;
24 import com.sleepycat.je.DbInternal;
25 import com.sleepycat.je.Environment;
26 import com.sleepycat.je.EnvironmentConfig;
27 import com.sleepycat.je.JEVersion;
28 import com.sleepycat.je.OperationStatus;
29 import com.sleepycat.je.Transaction;
30 import com.sleepycat.je.XAEnvironment;
31 import com.sleepycat.je.config.EnvironmentParams;
32 import com.sleepycat.je.log.LogEntryType;
33 import com.sleepycat.je.log.TestUtilLogReader;
34 import com.sleepycat.je.log.LogUtils.XidImpl;
35 import com.sleepycat.je.util.TestUtils;
36
37 /**
38  * This standalone command line program generates log files named je-x.y.z.jdb
39  * and je-x.y.z.txt, where x.y.z is the version of JE used to run the program.
40  * This program needs to be run for the current version of JE when we release
41  * a new major version of JE. It does not need to be run again for older
42  * versions of JE, unless it is changed to generate new types of log entries
43  * and we need to verify those log entries for all versions of JE. In that
44  * the LogEntryVersionTest may also need to be changed.
45  *
46  * <p>Run this program with the desired version of JE in the classpath and pass
47  * a home directory as the single command line argument. After running this
48  * program move the je-x.y.z.* files to the directory of this source package.
49  * When adding je-x.y.z.jdb to CVS make sure to use -kb since it is a binary
50  * file.</p>
51  *
52  * <p>This program can be run using the logversiondata ant target.</p>
53  *
54  * @see LogEntryVersionTest
55  */

56 public class MakeLogEntryVersionData {
57
58     /* Minimum child entries per BIN. */
59     private static int N_ENTRIES = 4;
60
61     private MakeLogEntryVersionData() {
62     }
63
64     public static void main(String JavaDoc[] args)
65         throws Exception JavaDoc {
66
67         if (args.length != 1) {
68             throw new Exception JavaDoc("Home directory arg is required.");
69         }
70
71         File JavaDoc homeDir = new File JavaDoc(args[0]);
72         File JavaDoc logFile = new File JavaDoc(homeDir, TestUtils.LOG_FILE_NAME);
73         File JavaDoc renamedLogFile = new File JavaDoc(homeDir, "je-" +
74             JEVersion.CURRENT_VERSION.getNumericVersionString() + ".jdb");
75         File JavaDoc summaryFile = new File JavaDoc(homeDir, "je-" +
76             JEVersion.CURRENT_VERSION.getNumericVersionString() + ".txt");
77
78         if (logFile.exists()) {
79             throw new Exception JavaDoc("Home directory must be empty of log files.");
80         }
81
82         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
83     DbInternal.disableParameterValidation(envConfig);
84         envConfig.setAllowCreate(true);
85         envConfig.setTransactional(true);
86         /* Make as small a log as possible to save space in CVS. */
87         envConfig.setConfigParam
88             (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
89         envConfig.setConfigParam
90             (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
91         envConfig.setConfigParam
92             (EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
93         envConfig.setConfigParam
94             (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
95     /* force trace messages at recovery. */
96         envConfig.setConfigParam
97             (EnvironmentParams.JE_LOGGING_LEVEL.getName(), "CONFIG");
98         /* Use a 100 MB log file size to ensure only one file is written. */
99         envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(),
100                                  Integer.toString(100 * (1 << 20)));
101         /* Force BINDelta. */
102         envConfig.setConfigParam
103             (EnvironmentParams.BIN_DELTA_PERCENT.getName(),
104              Integer.toString(75));
105         /* Force INDelete -- only used when the root is purged. */
106         envConfig.setConfigParam
107             (EnvironmentParams.COMPRESSOR_PURGE_ROOT.getName(), "true");
108         /* Ensure that we create two BINs with N_ENTRIES LNs. */
109         envConfig.setConfigParam
110             (EnvironmentParams.NODE_MAX.getName(),
111              Integer.toString(N_ENTRIES));
112
113         CheckpointConfig forceCheckpoint = new CheckpointConfig();
114         forceCheckpoint.setForce(true);
115
116         XAEnvironment env = new XAEnvironment(homeDir, envConfig);
117
118         for (int i = 0; i < 2; i += 1) {
119             boolean transactional = (i == 0);
120             String JavaDoc dbName = transactional ? Utils.DB1_NAME : Utils.DB2_NAME;
121             
122             DatabaseConfig dbConfig = new DatabaseConfig();
123             dbConfig.setAllowCreate(true);
124             dbConfig.setTransactional(transactional);
125             dbConfig.setSortedDuplicates(true);
126             Database db = env.openDatabase(null, dbName, dbConfig);
127
128             Transaction txn = null;
129             if (transactional) {
130                 txn = env.beginTransaction(null, null);
131             }
132
133             for (int j = 0; j < N_ENTRIES; j += 1) {
134                 db.put(txn, Utils.entry(j), Utils.entry(0));
135             }
136             db.put(txn, Utils.entry(0), Utils.entry(1));
137
138             /* Must checkpoint to generate BINDeltas. */
139             env.checkpoint(forceCheckpoint);
140
141             /* Delete everything but the last LN to cause IN deletion. */
142             for (int j = 0; j < N_ENTRIES - 1; j += 1) {
143                 db.delete(txn, Utils.entry(j));
144             }
145
146             if (transactional) {
147                 txn.abort();
148             }
149
150             db.close();
151         }
152
153         /* Compress twice to delete DBIN, DIN, BIN, IN. */
154         env.compress();
155         env.compress();
156
157         /* DB2 was not aborted and will contain: {3, 0} */
158         DatabaseConfig dbConfig = new DatabaseConfig();
159         dbConfig.setAllowCreate(false);
160         dbConfig.setReadOnly(true);
161         dbConfig.setSortedDuplicates(true);
162         Database db = env.openDatabase(null, Utils.DB2_NAME, dbConfig);
163         Cursor cursor = db.openCursor(null, null);
164         try {
165             DatabaseEntry key = new DatabaseEntry();
166             DatabaseEntry data = new DatabaseEntry();
167             OperationStatus status = cursor.getFirst(key, data, null);
168             if (status != OperationStatus.SUCCESS) {
169                 throw new Exception JavaDoc("Expected SUCCESS but got: " + status);
170             }
171             if (Utils.value(key) != 3 || Utils.value(data) != 0) {
172                 throw new Exception JavaDoc("Expected {3,0} but got: {" +
173                                     Utils.value(key) + ',' +
174                                     Utils.value(data) + '}');
175             }
176         } finally {
177             cursor.close();
178         }
179         db.close();
180
181     XidImpl xid =
182         new XidImpl(1, "MakeLogEntryVersionData".getBytes(), null);
183     env.start(xid, XAResource.TMNOFLAGS);
184     env.prepare(xid);
185     env.rollback(xid);
186
187         env.close();
188
189         /*
190          * Get the set of all log entry types we expect to output. We punt on
191          * one type -- MapLN_TX -- because MapLN (non-transactional) is now
192          * used instead.
193          */

194         Set JavaDoc expectedTypes = LogEntryType.getAllTypes();
195         expectedTypes.remove(LogEntryType.LOG_MAPLN_TRANSACTIONAL);
196
197         /* Open read-only and write all LogEntryType names to a text file. */
198         envConfig.setReadOnly(true);
199         Environment env2 = new Environment(homeDir, envConfig);
200         PrintWriter JavaDoc writer = new PrintWriter JavaDoc
201             (new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(summaryFile)));
202         TestUtilLogReader reader = new TestUtilLogReader
203             (DbInternal.envGetEnvironmentImpl(env2));
204         while (reader.readNextEntry()) {
205             LogEntryType type = reader.getEntryType();
206             writer.println(type.toString());
207             expectedTypes.remove(type);
208         }
209         writer.close();
210         env2.close();
211
212         if (expectedTypes.size() > 0) {
213             throw new Exception JavaDoc("Types not output: " + expectedTypes);
214         }
215
216         if (!logFile.exists()) {
217             throw new Exception JavaDoc("What happened to: " + logFile);
218         }
219
220         if (!logFile.renameTo(renamedLogFile)) {
221             throw new Exception JavaDoc
222                 ("Could not rename: " + logFile + " to " + renamedLogFile);
223         }
224
225         System.out.println("Created: " + renamedLogFile);
226         System.out.println("Created: " + summaryFile);
227     }
228 }
229
Popular Tags