KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > collections > hello > HelloDatabaseWorld


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: HelloDatabaseWorld.java,v 1.23 2006/10/31 19:56:10 mark Exp $
7  */

8
9 package collections.hello;
10
11 import java.io.File JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.SortedMap JavaDoc;
15
16 import com.sleepycat.bind.serial.ClassCatalog;
17 import com.sleepycat.bind.serial.SerialBinding;
18 import com.sleepycat.bind.serial.StoredClassCatalog;
19 import com.sleepycat.bind.tuple.TupleBinding;
20 import com.sleepycat.collections.StoredSortedMap;
21 import com.sleepycat.collections.TransactionRunner;
22 import com.sleepycat.collections.TransactionWorker;
23 import com.sleepycat.je.Database;
24 import com.sleepycat.je.DatabaseConfig;
25 import com.sleepycat.je.Environment;
26 import com.sleepycat.je.EnvironmentConfig;
27
28 /**
29  * @author Mark Hayes
30  */

31 public class HelloDatabaseWorld implements TransactionWorker {
32
33     private static final String JavaDoc[] INT_NAMES = {
34         "Hello", "Database", "World",
35     };
36     private static boolean create = true;
37
38     private Environment env;
39     private ClassCatalog catalog;
40     private Database db;
41     private SortedMap JavaDoc map;
42
43     /** Creates the environment and runs a transaction */
44     public static void main(String JavaDoc[] argv)
45         throws Exception JavaDoc {
46
47         String JavaDoc dir = "./tmp";
48
49         // environment is transactional
50
EnvironmentConfig envConfig = new EnvironmentConfig();
51         envConfig.setTransactional(true);
52         if (create) {
53             envConfig.setAllowCreate(true);
54         }
55         Environment env = new Environment(new File JavaDoc(dir), envConfig);
56
57         // create the application and run a transaction
58
HelloDatabaseWorld worker = new HelloDatabaseWorld(env);
59         TransactionRunner runner = new TransactionRunner(env);
60         try {
61             // open and access the database within a transaction
62
runner.run(worker);
63         } finally {
64             // close the database outside the transaction
65
worker.close();
66         }
67     }
68
69     /** Creates the database for this application */
70     private HelloDatabaseWorld(Environment env)
71         throws Exception JavaDoc {
72
73         this.env = env;
74         open();
75     }
76
77     /** Performs work within a transaction. */
78     public void doWork()
79         throws Exception JavaDoc {
80
81         writeAndRead();
82     }
83
84     /** Opens the database and creates the Map. */
85     private void open()
86         throws Exception JavaDoc {
87
88         // use a generic database configuration
89
DatabaseConfig dbConfig = new DatabaseConfig();
90         dbConfig.setTransactional(true);
91         if (create) {
92             dbConfig.setAllowCreate(true);
93         }
94
95         // catalog is needed for serial bindings (java serialization)
96
Database catalogDb = env.openDatabase(null, "catalog", dbConfig);
97         catalog = new StoredClassCatalog(catalogDb);
98
99         // use Integer tuple binding for key entries
100
TupleBinding keyBinding =
101             TupleBinding.getPrimitiveBinding(Integer JavaDoc.class);
102
103         // use String serial binding for data entries
104
SerialBinding dataBinding = new SerialBinding(catalog, String JavaDoc.class);
105
106         this.db = env.openDatabase(null, "helloworld", dbConfig);
107
108         // create a map view of the database
109
this.map = new StoredSortedMap(db, keyBinding, dataBinding, true);
110     }
111
112     /** Closes the database. */
113     private void close()
114         throws Exception JavaDoc {
115
116         if (catalog != null) {
117             catalog.close();
118             catalog = null;
119         }
120         if (db != null) {
121             db.close();
122             db = null;
123         }
124         if (env != null) {
125             env.close();
126             env = null;
127         }
128     }
129
130     /** Writes and reads the database via the Map. */
131     private void writeAndRead() {
132
133         // check for existing data
134
Integer JavaDoc key = new Integer JavaDoc(0);
135         String JavaDoc val = (String JavaDoc) map.get(key);
136         if (val == null) {
137             System.out.println("Writing data");
138             // write in reverse order to show that keys are sorted
139
for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) {
140                 map.put(new Integer JavaDoc(i), INT_NAMES[i]);
141             }
142         }
143         // get iterator over map entries
144
Iterator JavaDoc iter = map.entrySet().iterator();
145         System.out.println("Reading data");
146         while (iter.hasNext()) {
147             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
148             System.out.println(entry.getKey().toString() + ' ' +
149                                entry.getValue());
150         }
151     }
152 }
153
Popular Tags