001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: HelloDatabaseWorld.java,v 1.23.2.2 2008/01/07 15:13:59 cwl Exp $
007: */
008:
009: package collections.hello;
010:
011: import java.io.File;
012: import java.util.Iterator;
013: import java.util.Map;
014: import java.util.SortedMap;
015:
016: import com.sleepycat.bind.serial.ClassCatalog;
017: import com.sleepycat.bind.serial.SerialBinding;
018: import com.sleepycat.bind.serial.StoredClassCatalog;
019: import com.sleepycat.bind.tuple.TupleBinding;
020: import com.sleepycat.collections.StoredSortedMap;
021: import com.sleepycat.collections.TransactionRunner;
022: import com.sleepycat.collections.TransactionWorker;
023: import com.sleepycat.je.Database;
024: import com.sleepycat.je.DatabaseConfig;
025: import com.sleepycat.je.Environment;
026: import com.sleepycat.je.EnvironmentConfig;
027:
028: /**
029: * @author Mark Hayes
030: */
031: public class HelloDatabaseWorld implements TransactionWorker {
032:
033: private static final String[] INT_NAMES = { "Hello", "Database",
034: "World", };
035: private static boolean create = true;
036:
037: private Environment env;
038: private ClassCatalog catalog;
039: private Database db;
040: private SortedMap map;
041:
042: /** Creates the environment and runs a transaction */
043: public static void main(String[] argv) throws Exception {
044:
045: String dir = "./tmp";
046:
047: // environment is transactional
048: EnvironmentConfig envConfig = new EnvironmentConfig();
049: envConfig.setTransactional(true);
050: if (create) {
051: envConfig.setAllowCreate(true);
052: }
053: Environment env = new Environment(new File(dir), envConfig);
054:
055: // create the application and run a transaction
056: HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
057: TransactionRunner runner = new TransactionRunner(env);
058: try {
059: // open and access the database within a transaction
060: runner.run(worker);
061: } finally {
062: // close the database outside the transaction
063: worker.close();
064: }
065: }
066:
067: /** Creates the database for this application */
068: private HelloDatabaseWorld(Environment env) throws Exception {
069:
070: this .env = env;
071: open();
072: }
073:
074: /** Performs work within a transaction. */
075: public void doWork() throws Exception {
076:
077: writeAndRead();
078: }
079:
080: /** Opens the database and creates the Map. */
081: private void open() throws Exception {
082:
083: // use a generic database configuration
084: DatabaseConfig dbConfig = new DatabaseConfig();
085: dbConfig.setTransactional(true);
086: if (create) {
087: dbConfig.setAllowCreate(true);
088: }
089:
090: // catalog is needed for serial bindings (java serialization)
091: Database catalogDb = env
092: .openDatabase(null, "catalog", dbConfig);
093: catalog = new StoredClassCatalog(catalogDb);
094:
095: // use Integer tuple binding for key entries
096: TupleBinding keyBinding = TupleBinding
097: .getPrimitiveBinding(Integer.class);
098:
099: // use String serial binding for data entries
100: SerialBinding dataBinding = new SerialBinding(catalog,
101: String.class);
102:
103: this .db = env.openDatabase(null, "helloworld", dbConfig);
104:
105: // create a map view of the database
106: this .map = new StoredSortedMap(db, keyBinding, dataBinding,
107: true);
108: }
109:
110: /** Closes the database. */
111: private void close() throws Exception {
112:
113: if (catalog != null) {
114: catalog.close();
115: catalog = null;
116: }
117: if (db != null) {
118: db.close();
119: db = null;
120: }
121: if (env != null) {
122: env.close();
123: env = null;
124: }
125: }
126:
127: /** Writes and reads the database via the Map. */
128: private void writeAndRead() {
129:
130: // check for existing data
131: Integer key = new Integer(0);
132: String val = (String) map.get(key);
133: if (val == null) {
134: System.out.println("Writing data");
135: // write in reverse order to show that keys are sorted
136: for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) {
137: map.put(new Integer(i), INT_NAMES[i]);
138: }
139: }
140: // get iterator over map entries
141: Iterator iter = map.entrySet().iterator();
142: System.out.println("Reading data");
143: while (iter.hasNext()) {
144: Map.Entry entry = (Map.Entry) iter.next();
145: System.out.println(entry.getKey().toString() + ' '
146: + entry.getValue());
147: }
148: }
149: }
|