package persist.txn;
import com.sleepycat.db.DatabaseConfig;
import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.DatabaseType;
import com.sleepycat.db.LockDetectMode;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
import java.io.File;
import java.io.FileNotFoundException;
public class TxnGuideDPL {
private static String myEnvPath = "./";
private static String storeName = "exampleStore";
private static EntityStore myStore = null;
private static Environment myEnv = null;
private static final int NUMTHREADS = 5;
private static void usage() {
System.out.println("TxnGuideDPL [-h <env directory>]");
System.exit(-1);
}
public static void main(String args[]) {
try {
parseArgs(args);
openEnv();
StoreWriter[] threadArray;
threadArray = new StoreWriter[NUMTHREADS];
for (int i = 0; i < NUMTHREADS; i++) {
threadArray[i] = new StoreWriter(myEnv, myStore);
threadArray[i].start();
}
for (int i = 0; i < NUMTHREADS; i++) {
threadArray[i].join();
}
} catch (Exception e) {
System.err.println("TxnGuideDPL: " + e.toString());
e.printStackTrace();
} finally {
closeEnv();
}
System.out.println("All done.");
}
private static void openEnv() throws DatabaseException {
System.out.println("opening env and store");
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
myEnvConfig.setAllowCreate(true);
myEnvConfig.setInitializeCache(true);
myEnvConfig.setInitializeLocking(true);
myEnvConfig.setInitializeLogging(true);
myEnvConfig.setRunRecovery(true);
myEnvConfig.setTransactional(true);
myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
StoreConfig myStoreConfig = new StoreConfig();
myStoreConfig.setAllowCreate(true);
myStoreConfig.setTransactional(true);
DatabaseConfig myDbConfig = new DatabaseConfig();
myDbConfig.setType(DatabaseType.BTREE);
myDbConfig.setAllowCreate(true);
myDbConfig.setTransactional(true);
myDbConfig.setReadUncommitted(true);
try {
myEnv = new Environment(new File(myEnvPath), myEnvConfig);
myStore = new EntityStore(myEnv, storeName, myStoreConfig);
myStore.setPrimaryConfig(PayloadDataEntity.class, myDbConfig);
} catch (FileNotFoundException fnfe) {
System.err.println("openEnv: " + fnfe.toString());
System.exit(-1);
}
}
private static void closeEnv() {
System.out.println("Closing env and store");
if (myStore != null ) {
try {
myStore.close();
} catch (DatabaseException e) {
System.err.println("closeEnv: myStore: " +
e.toString());
e.printStackTrace();
}
}
if (myEnv != null ) {
try {
myEnv.close();
} catch (DatabaseException e) {
System.err.println("closeEnv: " + e.toString());
e.printStackTrace();
}
}
}
private TxnGuideDPL() {}
private static void parseArgs(String args[]) {
int nArgs = args.length;
for(int i = 0; i < args.length; ++i) {
if (args[i].startsWith("-")) {
switch(args[i].charAt(1)) {
case 'h':
if (i < nArgs - 1) {
myEnvPath = new String(args[++i]);
}
break;
default:
usage();
}
}
}
}
}