1 8 9 package collections.hello; 10 11 import java.io.File ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import java.util.SortedMap ; 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 31 public class HelloDatabaseWorld implements TransactionWorker { 32 33 private static final String [] 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 map; 42 43 44 public static void main(String [] argv) 45 throws Exception { 46 47 String dir = "./tmp"; 48 49 EnvironmentConfig envConfig = new EnvironmentConfig(); 51 envConfig.setTransactional(true); 52 if (create) { 53 envConfig.setAllowCreate(true); 54 } 55 Environment env = new Environment(new File (dir), envConfig); 56 57 HelloDatabaseWorld worker = new HelloDatabaseWorld(env); 59 TransactionRunner runner = new TransactionRunner(env); 60 try { 61 runner.run(worker); 63 } finally { 64 worker.close(); 66 } 67 } 68 69 70 private HelloDatabaseWorld(Environment env) 71 throws Exception { 72 73 this.env = env; 74 open(); 75 } 76 77 78 public void doWork() 79 throws Exception { 80 81 writeAndRead(); 82 } 83 84 85 private void open() 86 throws Exception { 87 88 DatabaseConfig dbConfig = new DatabaseConfig(); 90 dbConfig.setTransactional(true); 91 if (create) { 92 dbConfig.setAllowCreate(true); 93 } 94 95 Database catalogDb = env.openDatabase(null, "catalog", dbConfig); 97 catalog = new StoredClassCatalog(catalogDb); 98 99 TupleBinding keyBinding = 101 TupleBinding.getPrimitiveBinding(Integer .class); 102 103 SerialBinding dataBinding = new SerialBinding(catalog, String .class); 105 106 this.db = env.openDatabase(null, "helloworld", dbConfig); 107 108 this.map = new StoredSortedMap(db, keyBinding, dataBinding, true); 110 } 111 112 113 private void close() 114 throws Exception { 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 131 private void writeAndRead() { 132 133 Integer key = new Integer (0); 135 String val = (String ) map.get(key); 136 if (val == null) { 137 System.out.println("Writing data"); 138 for (int i = INT_NAMES.length - 1; i >= 0; i -= 1) { 140 map.put(new Integer (i), INT_NAMES[i]); 141 } 142 } 143 Iterator iter = map.entrySet().iterator(); 145 System.out.println("Reading data"); 146 while (iter.hasNext()) { 147 Map.Entry entry = (Map.Entry ) iter.next(); 148 System.out.println(entry.getKey().toString() + ' ' + 149 entry.getValue()); 150 } 151 } 152 } 153 | Popular Tags |