00001
00002
00003 package db.txn;
00004
00005 import com.sleepycat.bind.serial.StoredClassCatalog;
00006
00007 import com.sleepycat.db.Database;
00008 import com.sleepycat.db.DatabaseConfig;
00009 import com.sleepycat.db.DatabaseException;
00010 import com.sleepycat.db.DatabaseType;
00011 import com.sleepycat.db.LockDetectMode;
00012
00013 import com.sleepycat.db.Environment;
00014 import com.sleepycat.db.EnvironmentConfig;
00015
00016 import java.io.File;
00017 import java.io.FileNotFoundException;
00018
00019 public class TxnGuide {
00020
00021 private static String myEnvPath = "./";
00022 private static String dbName = "mydb.db";
00023 private static String cdbName = "myclassdb.db";
00024
00025
00026 private static Database myDb = null;
00027 private static Database myClassDb = null;
00028 private static Environment myEnv = null;
00029
00030 private static final int NUMTHREADS = 5;
00031
00032 private static void usage() {
00033 System.out.println("TxnGuide [-h <env directory>]");
00034 System.exit(-1);
00035 }
00036
00037 public static void main(String args[]) {
00038 try {
00039
00040 parseArgs(args);
00041
00042 openEnv();
00043
00044 StoredClassCatalog classCatalog =
00045 new StoredClassCatalog(myClassDb);
00046
00047
00048 DBWriter[] threadArray;
00049 threadArray = new DBWriter[NUMTHREADS];
00050 for (int i = 0; i < NUMTHREADS; i++) {
00051 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog);
00052 threadArray[i].start();
00053 }
00054
00055 for (int i = 0; i < NUMTHREADS; i++) {
00056 threadArray[i].join();
00057 }
00058 } catch (Exception e) {
00059 System.err.println("TxnGuide: " + e.toString());
00060 e.printStackTrace();
00061 } finally {
00062 closeEnv();
00063 }
00064 System.out.println("All done.");
00065 }
00066
00067
00068 private static void openEnv() throws DatabaseException {
00069 System.out.println("opening env");
00070
00071
00072 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
00073 myEnvConfig.setAllowCreate(true);
00074 myEnvConfig.setInitializeCache(true);
00075 myEnvConfig.setInitializeLocking(true);
00076 myEnvConfig.setInitializeLogging(true);
00077 myEnvConfig.setRunRecovery(true);
00078 myEnvConfig.setTransactional(true);
00079
00080
00081
00082
00083
00084
00085
00086
00087 myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
00088
00089
00090 DatabaseConfig myDbConfig = new DatabaseConfig();
00091 myDbConfig.setType(DatabaseType.BTREE);
00092 myDbConfig.setAllowCreate(true);
00093 myDbConfig.setTransactional(true);
00094 myDbConfig.setSortedDuplicates(true);
00095 myDbConfig.setReadUncommitted(true);
00096
00097
00098
00099
00100 try {
00101
00102 myEnv = new Environment(new File(myEnvPath),
00103 myEnvConfig);
00104
00105
00106
00107
00108 myDb = myEnv.openDatabase(null,
00109 dbName,
00110 null,
00111 myDbConfig);
00112
00113
00114
00115 myDbConfig.setSortedDuplicates(false);
00116 myClassDb = myEnv.openDatabase(null,
00117 cdbName,
00118 null,
00119 myDbConfig);
00120 } catch (FileNotFoundException fnfe) {
00121 System.err.println("openEnv: " + fnfe.toString());
00122 System.exit(-1);
00123 }
00124 }
00125
00126 private static void closeEnv() {
00127 System.out.println("Closing env and databases");
00128 if (myDb != null ) {
00129 try {
00130 myDb.close();
00131 } catch (DatabaseException e) {
00132 System.err.println("closeEnv: myDb: " +
00133 e.toString());
00134 e.printStackTrace();
00135 }
00136 }
00137
00138 if (myClassDb != null ) {
00139 try {
00140 myClassDb.close();
00141 } catch (DatabaseException e) {
00142 System.err.println("closeEnv: myClassDb: " +
00143 e.toString());
00144 e.printStackTrace();
00145 }
00146 }
00147
00148 if (myEnv != null ) {
00149 try {
00150 myEnv.close();
00151 } catch (DatabaseException e) {
00152 System.err.println("closeEnv: " + e.toString());
00153 e.printStackTrace();
00154 }
00155 }
00156 }
00157
00158 private TxnGuide() {}
00159
00160 private static void parseArgs(String args[]) {
00161 int nArgs = args.length;
00162 for(int i = 0; i < args.length; ++i) {
00163 if (args[i].startsWith("-")) {
00164 switch(args[i].charAt(1)) {
00165 case 'h':
00166 if (i < nArgs - 1) {
00167 myEnvPath = new String(args[++i]);
00168 }
00169 break;
00170 default:
00171 usage();
00172 }
00173 }
00174 }
00175 }
00176 }