Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

HelloDatabaseWorld.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: HelloDatabaseWorld.java,v 12.2 2005/06/16 20:22:17 bostic Exp $
00008  */
00009 
00010 package collections.hello;
00011 
00012 import java.io.File;
00013 import java.util.Iterator;
00014 import java.util.Map;
00015 import java.util.SortedMap;
00016 
00017 import com.sleepycat.bind.serial.ClassCatalog;
00018 import com.sleepycat.bind.serial.SerialBinding;
00019 import com.sleepycat.bind.serial.StoredClassCatalog;
00020 import com.sleepycat.bind.tuple.TupleBinding;
00021 import com.sleepycat.collections.StoredIterator;
00022 import com.sleepycat.collections.StoredSortedMap;
00023 import com.sleepycat.collections.TransactionRunner;
00024 import com.sleepycat.collections.TransactionWorker;
00025 import com.sleepycat.db.Database;
00026 import com.sleepycat.db.DatabaseConfig;
00027 import com.sleepycat.db.DatabaseType;
00028 import com.sleepycat.db.Environment;
00029 import com.sleepycat.db.EnvironmentConfig;
00030 
00034 public class HelloDatabaseWorld implements TransactionWorker {
00035 
00036     private static final String[] INT_NAMES = {
00037         "Hello", "Database", "World",
00038     };
00039     private static boolean create = true;
00040 
00041     private Environment env;
00042     private ClassCatalog catalog;
00043     private Database db;
00044     private SortedMap map;
00045 
00047     public static void main(String[] argv)
00048         throws Exception {
00049 
00050         String dir = "./tmp";
00051 
00052         // environment is transactional
00053         EnvironmentConfig envConfig = new EnvironmentConfig();
00054         envConfig.setTransactional(true);
00055         envConfig.setInitializeCache(true);
00056         envConfig.setInitializeLocking(true);
00057         if (create) {
00058             envConfig.setAllowCreate(true);
00059         }
00060         Environment env = new Environment(new File(dir), envConfig);
00061 
00062         // create the application and run a transaction
00063         HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
00064         TransactionRunner runner = new TransactionRunner(env);
00065         try {
00066             // open and access the database within a transaction
00067             runner.run(worker);
00068         } finally {
00069             // close the database outside the transaction
00070             worker.close();
00071         }
00072     }
00073 
00075     private HelloDatabaseWorld(Environment env)
00076         throws Exception {
00077 
00078         this.env = env;
00079         open();
00080     }
00081 
00083     public void doWork()
00084         throws Exception {
00085 
00086         writeAndRead();
00087     }
00088 
00090     private void open()
00091         throws Exception {
00092 
00093         // use a generic database configuration
00094         DatabaseConfig dbConfig = new DatabaseConfig();
00095         dbConfig.setTransactional(true);
00096         if (create) {
00097             dbConfig.setAllowCreate(true);
00098             dbConfig.setType(DatabaseType.BTREE);
00099         }
00100 
00101         // catalog is needed for serial bindings (java serialization)
00102         Database catalogDb = env.openDatabase(null, "catalog", null, dbConfig);
00103         catalog = new StoredClassCatalog(catalogDb);
00104 
00105         // use Integer tuple binding for key entries
00106         TupleBinding keyBinding =
00107             TupleBinding.getPrimitiveBinding(Integer.class);
00108 
00109         // use String serial binding for data entries
00110         SerialBinding dataBinding = new SerialBinding(catalog, String.class);
00111 
00112         this.db = env.openDatabase(null, "helloworld", null, dbConfig);
00113 
00114         // create a map view of the database
00115         this.map = new StoredSortedMap(db, keyBinding, dataBinding, true);
00116     }
00117 
00119     private void close()
00120         throws Exception {
00121 
00122         if (catalog != null) {
00123             catalog.close();
00124             catalog = null;
00125         }
00126         if (db != null) {
00127             db.close();
00128             db = null;
00129         }
00130         if (env != null) {
00131             env.close();
00132             env = null;
00133         }
00134     }
00135 
00137     private void writeAndRead() {
00138 
00139         // check for existing data
00140         Integer key = new Integer(0);
00141         String val = (String) map.get(key);
00142         if (val == null) {
00143             System.out.println("Writing data");
00144             // write in reverse order to show that keys are sorted
00145             for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) {
00146                 map.put(new Integer(i), INT_NAMES[i]);
00147             }
00148         }
00149         // get iterator over map entries
00150         Iterator iter = map.entrySet().iterator();
00151         try {
00152             System.out.println("Reading data");
00153             while (iter.hasNext()) {
00154                 Map.Entry entry = (Map.Entry) iter.next();
00155                 System.out.println(entry.getKey().toString() + ' ' +
00156                                    entry.getValue());
00157             }
00158         } finally {
00159             // all database iterators must be closed!!
00160             StoredIterator.close(iter);
00161         }
00162     }
00163 }

Generated on Sun Dec 25 12:14:26 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2