1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: HelloDatabaseWorld.java,v 1.27.2.2 2010/01/04 15:30:24 cwl Exp $
*/
package collections.hello;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import com.sleepycat.bind.serial.ClassCatalog;
import com.sleepycat.bind.serial.SerialBinding;
import com.sleepycat.bind.serial.StoredClassCatalog;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.collections.StoredSortedMap;
import com.sleepycat.collections.TransactionRunner;
import com.sleepycat.collections.TransactionWorker;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
/**
* @author Mark Hayes
*/
public class HelloDatabaseWorld implements TransactionWorker {
private static final String[] INT_NAMES = {
"Hello", "Database", "World",
};
private static boolean create = true;
private Environment env;
private ClassCatalog catalog;
private Database db;
private SortedMap<Integer, String> map;
/** Creates the environment and runs a transaction */
public static void main(String[] argv)
throws Exception {
String dir = "./tmp";
// environment is transactional
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setTransactional(true);
if (create) {
envConfig.setAllowCreate(true);
}
Environment env = new Environment(new File(dir), envConfig);
// create the application and run a transaction
HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
TransactionRunner runner = new TransactionRunner(env);
try {
// open and access the database within a transaction
runner.run(worker);
} finally {
// close the database outside the transaction
worker.close();
}
}
/** Creates the database for this application */
private HelloDatabaseWorld(Environment env)
throws Exception {
this.env = env;
open();
}
/** Performs work within a transaction. */
public void doWork()
throws Exception {
writeAndRead();
}
/** Opens the database and creates the Map. */
private void open()
throws Exception {
// use a generic database configuration
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
if (create) {
dbConfig.setAllowCreate(true);
}
// catalog is needed for serial bindings (java serialization)
Database catalogDb = env.openDatabase(null, "catalog", dbConfig);
catalog = new StoredClassCatalog(catalogDb);
// use Integer tuple binding for key entries
TupleBinding<Integer> keyBinding =
TupleBinding.getPrimitiveBinding(Integer.class);
// use String serial binding for data entries
SerialBinding<String> dataBinding =
new SerialBinding<String>(catalog, String.class);
this.db = env.openDatabase(null, "helloworld", dbConfig);
// create a map view of the database
this.map = new StoredSortedMap<Integer, String>
(db, keyBinding, dataBinding, true);
}
/** Closes the database. */
private void close()
throws Exception {
if (catalog != null) {
catalog.close();
catalog = null;
}
if (db != null) {
db.close();
db = null;
}
if (env != null) {
env.close();
env = null;
}
}
/** Writes and reads the database via the Map. */
private void writeAndRead() {
// check for existing data
Integer key = new Integer(0);
String val = map.get(key);
if (val == null) {
System.out.println("Writing data");
// write in reverse order to show that keys are sorted
for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) {
map.put(new Integer(i), INT_NAMES[i]);
}
}
// get iterator over map entries
Iterator<Map.Entry<Integer, String>> iter = map.entrySet().iterator();
System.out.println("Reading data");
while (iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
System.out.println(entry.getKey().toString() + ' ' +
entry.getValue());
}
}
}
|