diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
commit | 54df2afaa61c6a03cbb4a33c9b90fa572b6d07b8 (patch) | |
tree | 18147b92b969d25ffbe61935fb63035cac820dd0 /db-4.8.30/examples_java/src/db/txn |
Berkeley DB 4.8 with rust build script for linux.
Diffstat (limited to 'db-4.8.30/examples_java/src/db/txn')
-rw-r--r-- | db-4.8.30/examples_java/src/db/txn/DBWriter.java | 213 | ||||
-rw-r--r-- | db-4.8.30/examples_java/src/db/txn/PayloadData.java | 27 | ||||
-rw-r--r-- | db-4.8.30/examples_java/src/db/txn/TxnGuide.java | 181 | ||||
-rw-r--r-- | db-4.8.30/examples_java/src/db/txn/TxnGuideInMemory.java | 172 |
4 files changed, 593 insertions, 0 deletions
diff --git a/db-4.8.30/examples_java/src/db/txn/DBWriter.java b/db-4.8.30/examples_java/src/db/txn/DBWriter.java new file mode 100644 index 0000000..bb60fa4 --- /dev/null +++ b/db-4.8.30/examples_java/src/db/txn/DBWriter.java @@ -0,0 +1,213 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2005-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package db.txn; + +import com.sleepycat.bind.EntryBinding; +import com.sleepycat.bind.serial.StoredClassCatalog; +import com.sleepycat.bind.serial.SerialBinding; +import com.sleepycat.bind.tuple.StringBinding; + +import com.sleepycat.db.Cursor; +import com.sleepycat.db.CursorConfig; +import com.sleepycat.db.Database; +import com.sleepycat.db.DatabaseEntry; +import com.sleepycat.db.DatabaseException; +import com.sleepycat.db.DeadlockException; +import com.sleepycat.db.Environment; +import com.sleepycat.db.LockMode; +import com.sleepycat.db.OperationStatus; +import com.sleepycat.db.Transaction; + +import java.io.UnsupportedEncodingException; +import java.util.Random; + +public class DBWriter extends Thread +{ + private Database myDb = null; + private Environment myEnv = null; + private EntryBinding dataBinding = null; + private Random generator = new Random(); + private boolean passTxn = false; + + + private static final int MAX_RETRY = 20; + + private static String[] keys = {"key 1", "key 2", "key 3", + "key 4", "key 5", "key 6", + "key 7", "key 8", "key 9", + "key 10"}; + + + // Constructor. Get our DB handles from here + // This consturctor allows us to indicate whether the + // txn handle should be handed to countRecords() + DBWriter(Environment env, Database db, StoredClassCatalog scc, + boolean passtxn) + + throws DatabaseException { + myDb = db; + myEnv = env; + dataBinding = new SerialBinding(scc, PayloadData.class); + + passTxn = passtxn; + } + + // Constructor. Get our DB handles from here + DBWriter(Environment env, Database db, StoredClassCatalog scc) + + throws DatabaseException { + myDb = db; + myEnv = env; + dataBinding = new SerialBinding(scc, PayloadData.class); + } + + // Thread method that writes a series of records + // to the database using transaction protection. + // Deadlock handling is demonstrated here. + public void run () { + Transaction txn = null; + + // Perform 50 transactions + for (int i=0; i<50; i++) { + + boolean retry = true; + int retry_count = 0; + // while loop is used for deadlock retries + while (retry) { + // try block used for deadlock detection and + // general db exception handling + try { + + // Get a transaction + txn = myEnv.beginTransaction(null, null); + + // Write 10 records to the db + // for each transaction + for (int j = 0; j < 10; j++) { + // Get the key + DatabaseEntry key = new DatabaseEntry(); + StringBinding.stringToEntry(keys[j], key); + + // Get the data + PayloadData pd = new PayloadData(i+j, getName(), + generator.nextDouble()); + DatabaseEntry data = new DatabaseEntry(); + dataBinding.objectToEntry(pd, data); + + // Do the put + myDb.put(txn, key, data); + } + + // commit + System.out.println(getName() + " : committing txn : " + i); + + // This code block allows us to decide if txn handle is + // passed to countRecords() + // + // TxnGuideInMemory requires a txn handle be handed to + // countRecords(). The code self deadlocks if you don't. + // TxnGuide has no such requirement because it supports + // uncommitted reads. + Transaction txnHandle = null; + if (passTxn) { txnHandle = txn; } + + System.out.println(getName() + " : Found " + + countRecords(txnHandle) + " records in the database."); + try { + txn.commit(); + txn = null; + } catch (DatabaseException e) { + System.err.println("Error on txn commit: " + + e.toString()); + } + retry = false; + + } catch (DeadlockException de) { + System.out.println("################# " + getName() + + " : caught deadlock"); + // retry if necessary + if (retry_count < MAX_RETRY) { + System.err.println(getName() + + " : Retrying operation."); + retry = true; + retry_count++; + } else { + System.err.println(getName() + + " : out of retries. Giving up."); + retry = false; + } + } catch (DatabaseException e) { + // abort and don't retry + retry = false; + System.err.println(getName() + + " : caught exception: " + e.toString()); + System.err.println(getName() + + " : errno: " + e.getErrno()); + e.printStackTrace(); + } finally { + if (txn != null) { + try { + txn.abort(); + } catch (Exception e) { + System.err.println("Error aborting transaction: " + + e.toString()); + e.printStackTrace(); + } + } + } + } + } + } + + // This simply counts the number of records contained in the + // database and returns the result. You can use this method + // in three ways: + // + // First call it with an active txn handle. + // + // Secondly, configure the cursor for dirty reads + // + // Third, call countRecords AFTER the writer has committed + // its transaction. + // + // If you do none of these things, the writer thread will + // self-deadlock. + // + // Note that this method exists only for illustrative purposes. + // A more straight-forward way to count the number of records in + // a database is to use the Database.getStats() method. + private int countRecords(Transaction txn) throws DatabaseException { + DatabaseEntry key = new DatabaseEntry(); + DatabaseEntry data = new DatabaseEntry(); + int count = 0; + Cursor cursor = null; + + try { + // Get the cursor + CursorConfig cc = new CursorConfig(); + // setReadUncommitted is ignored if the database was not + // opened for uncommitted read support. TxnGuide opens + // its database in this way, TxnGuideInMemory does not. + cc.setReadUncommitted(true); + cursor = myDb.openCursor(txn, cc); + while (cursor.getNext(key, data, LockMode.DEFAULT) == + OperationStatus.SUCCESS) { + + count++; + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return count; + + } +} diff --git a/db-4.8.30/examples_java/src/db/txn/PayloadData.java b/db-4.8.30/examples_java/src/db/txn/PayloadData.java new file mode 100644 index 0000000..9151dd1 --- /dev/null +++ b/db-4.8.30/examples_java/src/db/txn/PayloadData.java @@ -0,0 +1,27 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2005-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package db.txn; + +import java.io.Serializable; + +public class PayloadData implements Serializable { + private int oID; + private String threadName; + private double doubleData; + + PayloadData(int id, String name, double data) { + oID = id; + threadName = name; + doubleData = data; + } + + public double getDoubleData() { return doubleData; } + public int getID() { return oID; } + public String getThreadName() { return threadName; } +} diff --git a/db-4.8.30/examples_java/src/db/txn/TxnGuide.java b/db-4.8.30/examples_java/src/db/txn/TxnGuide.java new file mode 100644 index 0000000..73d172f --- /dev/null +++ b/db-4.8.30/examples_java/src/db/txn/TxnGuide.java @@ -0,0 +1,181 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2005-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +// File TxnGuide.java + +package db.txn; + +import com.sleepycat.bind.serial.StoredClassCatalog; + +import com.sleepycat.db.Database; +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 java.io.File; +import java.io.FileNotFoundException; + +public class TxnGuide { + + private static String myEnvPath = "./"; + private static String dbName = "mydb.db"; + private static String cdbName = "myclassdb.db"; + + // DB handles + private static Database myDb = null; + private static Database myClassDb = null; + private static Environment myEnv = null; + + private static final int NUMTHREADS = 5; + + private static void usage() { + System.out.println("TxnGuide [-h <env directory>]"); + System.exit(-1); + } + + public static void main(String args[]) { + try { + // Parse the arguments list + parseArgs(args); + // Open the environment and databases + openEnv(); + // Get our class catalog (used to serialize objects) + StoredClassCatalog classCatalog = + new StoredClassCatalog(myClassDb); + + // Start the threads + DBWriter[] threadArray; + threadArray = new DBWriter[NUMTHREADS]; + for (int i = 0; i < NUMTHREADS; i++) { + threadArray[i] = new DBWriter(myEnv, myDb, classCatalog); + threadArray[i].start(); + } + + for (int i = 0; i < NUMTHREADS; i++) { + threadArray[i].join(); + } + } catch (Exception e) { + System.err.println("TxnGuide: " + e.toString()); + e.printStackTrace(); + } finally { + closeEnv(); + } + System.out.println("All done."); + } + + + private static void openEnv() throws DatabaseException { + System.out.println("opening env"); + + // Set up the environment. + EnvironmentConfig myEnvConfig = new EnvironmentConfig(); + myEnvConfig.setAllowCreate(true); + myEnvConfig.setInitializeCache(true); + myEnvConfig.setInitializeLocking(true); + myEnvConfig.setInitializeLogging(true); + myEnvConfig.setRunRecovery(true); + myEnvConfig.setTransactional(true); + // EnvironmentConfig.setThreaded(true) is the default behavior + // in Java, so we do not have to do anything to cause the + // environment handle to be free-threaded. + + // Indicate that we want db to internally perform deadlock + // detection. Also indicate that the transaction that has + // performed the least amount of write activity to + // receive the deadlock notification, if any. + myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE); + + // Set up the database + DatabaseConfig myDbConfig = new DatabaseConfig(); + myDbConfig.setType(DatabaseType.BTREE); + myDbConfig.setAllowCreate(true); + myDbConfig.setTransactional(true); + myDbConfig.setSortedDuplicates(true); + myDbConfig.setReadUncommitted(true); + // no DatabaseConfig.setThreaded() method available. + // db handles in java are free-threaded so long as the + // env is also free-threaded. + + try { + // Open the environment + myEnv = new Environment(new File(myEnvPath), // Env home + myEnvConfig); + + // Open the database. Do not provide a txn handle. This open + // is autocommitted because DatabaseConfig.setTransactional() + // is true. + myDb = myEnv.openDatabase(null, // txn handle + dbName, // Database file name + null, // Database name + myDbConfig); + + // Used by the bind API for serializing objects + // Class database must not support duplicates + myDbConfig.setSortedDuplicates(false); + myClassDb = myEnv.openDatabase(null, // txn handle + cdbName, // Database file name + null, // Database name, + myDbConfig); + } catch (FileNotFoundException fnfe) { + System.err.println("openEnv: " + fnfe.toString()); + System.exit(-1); + } + } + + private static void closeEnv() { + System.out.println("Closing env and databases"); + if (myDb != null ) { + try { + myDb.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: myDb: " + + e.toString()); + e.printStackTrace(); + } + } + + if (myClassDb != null ) { + try { + myClassDb.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: myClassDb: " + + e.toString()); + e.printStackTrace(); + } + } + + if (myEnv != null ) { + try { + myEnv.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: " + e.toString()); + e.printStackTrace(); + } + } + } + + private TxnGuide() {} + + private static void parseArgs(String args[]) { + for(int i = 0; i < args.length; ++i) { + if (args[i].startsWith("-")) { + switch(args[i].charAt(1)) { + case 'h': + myEnvPath = new String(args[++i]); + break; + default: + usage(); + } + } + } + } +} diff --git a/db-4.8.30/examples_java/src/db/txn/TxnGuideInMemory.java b/db-4.8.30/examples_java/src/db/txn/TxnGuideInMemory.java new file mode 100644 index 0000000..1955a8f --- /dev/null +++ b/db-4.8.30/examples_java/src/db/txn/TxnGuideInMemory.java @@ -0,0 +1,172 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2005-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +// File TxnGuideInMemory.java + +package db.txn; + +import com.sleepycat.bind.serial.StoredClassCatalog; + +import com.sleepycat.db.Database; +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 java.io.File; +import java.io.FileNotFoundException; + +public class TxnGuideInMemory { + + // DB handles + private static Database myDb = null; + private static Database myClassDb = null; + private static Environment myEnv = null; + + private static int NUMTHREADS = 5; + + public static void main(String args[]) { + try { + // Open the environment and databases + openEnv(); + + // Get our class catalog (used to serialize objects) + StoredClassCatalog classCatalog = + new StoredClassCatalog(myClassDb); + + // Start the threads + DBWriter[] threadArray; + threadArray = new DBWriter[NUMTHREADS]; + for (int i = 0; i < NUMTHREADS; i++) { + threadArray[i] = new DBWriter(myEnv, myDb, classCatalog, true); + threadArray[i].start(); + } + + System.out.println("Threads started.\n"); + + for (int i = 0; i < NUMTHREADS; i++) { + threadArray[i].join(); + } + } catch (Exception e) { + System.err.println("TxnGuideInMemory: " + e.toString()); + e.printStackTrace(); + } finally { + closeEnv(); + } + System.out.println("All done."); + } + + private static void openEnv() throws DatabaseException { + System.out.println("opening env"); + + // Set up the environment. + EnvironmentConfig myEnvConfig = new EnvironmentConfig(); + + // Region files are not backed by the filesystem, they are + // backed by heap memory. + myEnvConfig.setPrivate(true); + myEnvConfig.setAllowCreate(true); + myEnvConfig.setInitializeCache(true); + myEnvConfig.setInitializeLocking(true); + myEnvConfig.setInitializeLogging(true); + myEnvConfig.setThreaded(true); + + myEnvConfig.setTransactional(true); + // EnvironmentConfig.setThreaded(true) is the default behavior + // in Java, so we do not have to do anything to cause the + // environment handle to be free-threaded. + + // Indicate that we want db to internally perform deadlock + // detection. Also indicate that the transaction that has + // performed the least amount of write activity to + // receive the deadlock notification, if any. + myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE); + + // Specify in-memory logging + myEnvConfig.setLogInMemory(true); + // Specify the size of the in-memory log buffer + // Must be large enough to handle the log data created by + // the largest transaction. + myEnvConfig.setLogBufferSize(10 * 1024 * 1024); + // Specify the size of the in-memory cache + // Set it large enough so that it won't page. + myEnvConfig.setCacheSize(10 * 1024 * 1024); + + // Set up the database + DatabaseConfig myDbConfig = new DatabaseConfig(); + myDbConfig.setType(DatabaseType.BTREE); + myDbConfig.setAllowCreate(true); + myDbConfig.setTransactional(true); + myDbConfig.setSortedDuplicates(true); + // no DatabaseConfig.setThreaded() method available. + // db handles in java are free-threaded so long as the + // env is also free-threaded. + + try { + // Open the environment + myEnv = new Environment(null, // Env home + myEnvConfig); + + // Open the database. Do not provide a txn handle. This open + // is autocommitted because DatabaseConfig.setTransactional() + // is true. + myDb = myEnv.openDatabase(null, // txn handle + null, // Database file name + null, // Database name + myDbConfig); + + // Used by the bind API for serializing objects + // Class database must not support duplicates + myDbConfig.setSortedDuplicates(false); + myClassDb = myEnv.openDatabase(null, // txn handle + null, // Database file name + null, // Database name, + myDbConfig); + } catch (FileNotFoundException fnfe) { + System.err.println("openEnv: " + fnfe.toString()); + System.exit(-1); + } + } + + private static void closeEnv() { + System.out.println("Closing env"); + if (myDb != null ) { + try { + myDb.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: myDb: " + + e.toString()); + e.printStackTrace(); + } + } + + if (myClassDb != null ) { + try { + myClassDb.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: myClassDb: " + + e.toString()); + e.printStackTrace(); + } + } + + if (myEnv != null ) { + try { + myEnv.close(); + } catch (DatabaseException e) { + System.err.println("closeEnv: " + e.toString()); + e.printStackTrace(); + } + } + } + + private TxnGuideInMemory() {} +} |