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 TxnGuideInMemory {
00020
00021
00022 private static Database myDb = null;
00023 private static Database myClassDb = null;
00024 private static Environment myEnv = null;
00025
00026 private static int NUMTHREADS = 5;
00027
00028 public static void main(String args[]) {
00029 try {
00030
00031 openEnv();
00032
00033
00034 StoredClassCatalog classCatalog =
00035 new StoredClassCatalog(myClassDb);
00036
00037
00038 DBWriter[] threadArray;
00039 threadArray = new DBWriter[NUMTHREADS];
00040 for (int i = 0; i < NUMTHREADS; i++) {
00041 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog, true);
00042 threadArray[i].start();
00043 }
00044
00045 System.out.println("Threads started.\n");
00046
00047 for (int i = 0; i < NUMTHREADS; i++) {
00048 threadArray[i].join();
00049 }
00050 } catch (Exception e) {
00051 System.err.println("TxnGuideInMemory: " + e.toString());
00052 e.printStackTrace();
00053 } finally {
00054 closeEnv();
00055 }
00056 System.out.println("All done.");
00057 }
00058
00059 private static void openEnv() throws DatabaseException {
00060 System.out.println("opening env");
00061
00062
00063 EnvironmentConfig myEnvConfig = new EnvironmentConfig();
00064
00065
00066
00067 myEnvConfig.setPrivate(true);
00068 myEnvConfig.setAllowCreate(true);
00069 myEnvConfig.setInitializeCache(true);
00070 myEnvConfig.setInitializeLocking(true);
00071 myEnvConfig.setInitializeLogging(true);
00072 myEnvConfig.setThreaded(true);
00073
00074 myEnvConfig.setTransactional(true);
00075
00076
00077
00078
00079
00080
00081
00082
00083 myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
00084
00085
00086 myEnvConfig.setLogInMemory(true);
00087
00088
00089
00090 myEnvConfig.setLogBufferSize(10 * 1024 * 1024);
00091
00092
00093 myEnvConfig.setCacheSize(10 * 1024 * 1024);
00094
00095
00096 DatabaseConfig myDbConfig = new DatabaseConfig();
00097 myDbConfig.setType(DatabaseType.BTREE);
00098 myDbConfig.setAllowCreate(true);
00099 myDbConfig.setTransactional(true);
00100 myDbConfig.setSortedDuplicates(true);
00101
00102
00103
00104
00105 try {
00106
00107 myEnv = new Environment(null,
00108 myEnvConfig);
00109
00110
00111
00112
00113 myDb = myEnv.openDatabase(null,
00114 null,
00115 null,
00116 myDbConfig);
00117
00118
00119
00120 myDbConfig.setSortedDuplicates(false);
00121 myClassDb = myEnv.openDatabase(null,
00122 null,
00123 null,
00124 myDbConfig);
00125 } catch (FileNotFoundException fnfe) {
00126 System.err.println("openEnv: " + fnfe.toString());
00127 System.exit(-1);
00128 }
00129 }
00130
00131 private static void closeEnv() {
00132 System.out.println("Closing env");
00133 if (myDb != null ) {
00134 try {
00135 myDb.close();
00136 } catch (DatabaseException e) {
00137 System.err.println("closeEnv: myDb: " +
00138 e.toString());
00139 e.printStackTrace();
00140 }
00141 }
00142
00143 if (myClassDb != null ) {
00144 try {
00145 myClassDb.close();
00146 } catch (DatabaseException e) {
00147 System.err.println("closeEnv: myClassDb: " +
00148 e.toString());
00149 e.printStackTrace();
00150 }
00151 }
00152
00153 if (myEnv != null ) {
00154 try {
00155 myEnv.close();
00156 } catch (DatabaseException e) {
00157 System.err.println("closeEnv: " + e.toString());
00158 e.printStackTrace();
00159 }
00160 }
00161 }
00162
00163 private TxnGuideInMemory() {}
00164 }