1 3 package je.txn; 4 5 import java.io.File ; 6 7 import com.sleepycat.bind.serial.StoredClassCatalog; 8 import com.sleepycat.je.Database; 9 import com.sleepycat.je.DatabaseConfig; 10 import com.sleepycat.je.DatabaseException; 11 import com.sleepycat.je.Environment; 12 import com.sleepycat.je.EnvironmentConfig; 13 14 public class TxnGuide { 15 16 private static String myEnvPath = "./"; 17 private static String dbName = "mydb.db"; 18 private static String cdbName = "myclassdb.db"; 19 20 private static Database myDb = null; 22 private static Database myClassDb = null; 23 private static Environment myEnv = null; 24 25 private static int NUMTHREADS = 5; 26 27 private static void usage() { 28 System.out.println("TxnGuide [-h <env directory>]"); 29 System.exit(-1); 30 } 31 32 public static void main(String args[]) { 33 try { 34 parseArgs(args); 36 openEnv(); 38 StoredClassCatalog classCatalog = 40 new StoredClassCatalog(myClassDb); 41 42 DBWriter[] threadArray; 44 threadArray = new DBWriter[NUMTHREADS]; 45 for (int i = 0; i < NUMTHREADS; i++) { 46 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog); 47 threadArray[i].start(); 48 } 49 50 for (int i = 0; i < NUMTHREADS; i++) { 51 threadArray[i].join(); 52 } 53 } catch (Exception e) { 54 System.err.println("TxnGuide: " + e.toString()); 55 e.printStackTrace(); 56 } finally { 57 closeEnv(); 58 } 59 System.out.println("All done."); 60 } 61 62 63 private static void openEnv() throws DatabaseException { 64 System.out.println("opening env"); 65 66 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 68 myEnvConfig.setAllowCreate(true); 69 myEnvConfig.setTransactional(true); 70 74 DatabaseConfig myDbConfig = new DatabaseConfig(); 76 myDbConfig.setAllowCreate(true); 77 myDbConfig.setTransactional(true); 78 myDbConfig.setSortedDuplicates(true); 79 83 myEnv = new Environment(new File (myEnvPath), myEnvConfig); 86 87 myDb = myEnv.openDatabase(null, dbName, myDbConfig); 93 94 myDbConfig.setSortedDuplicates(false); 97 myClassDb = myEnv.openDatabase(null, cdbName, myDbConfig); 100 } 101 102 private static void closeEnv() { 103 System.out.println("Closing env and databases"); 104 if (myDb != null ) { 105 try { 106 myDb.close(); 107 } catch (DatabaseException e) { 108 System.err.println("closeEnv: myDb: " + 109 e.toString()); 110 e.printStackTrace(); 111 } 112 } 113 114 if (myClassDb != null ) { 115 try { 116 myClassDb.close(); 117 } catch (DatabaseException e) { 118 System.err.println("closeEnv: myClassDb: " + 119 e.toString()); 120 e.printStackTrace(); 121 } 122 } 123 124 if (myEnv != null ) { 125 try { 126 myEnv.close(); 127 } catch (DatabaseException e) { 128 System.err.println("closeEnv: " + e.toString()); 129 e.printStackTrace(); 130 } 131 } 132 } 133 134 private TxnGuide() {} 135 136 private static void parseArgs(String args[]) { 137 for(int i = 0; i < args.length; ++i) { 138 if (args[i].startsWith("-")) { 139 switch(args[i].charAt(1)) { 140 case 'h': 141 myEnvPath = new String (args[++i]); 142 break; 143 default: 144 usage(); 145 } 146 } 147 } 148 } 149 } 150 | Popular Tags |