1 8 9 package je; 10 11 import java.io.File ; 12 import java.io.Serializable ; 13 14 import com.sleepycat.bind.EntryBinding; 15 import com.sleepycat.bind.serial.SerialBinding; 16 import com.sleepycat.bind.serial.StoredClassCatalog; 17 import com.sleepycat.bind.tuple.IntegerBinding; 18 import com.sleepycat.bind.tuple.TupleBinding; 19 import com.sleepycat.je.Cursor; 20 import com.sleepycat.je.Database; 21 import com.sleepycat.je.DatabaseConfig; 22 import com.sleepycat.je.DatabaseEntry; 23 import com.sleepycat.je.DatabaseException; 24 import com.sleepycat.je.Environment; 25 import com.sleepycat.je.EnvironmentConfig; 26 import com.sleepycat.je.LockMode; 27 import com.sleepycat.je.OperationStatus; 28 import com.sleepycat.je.SecondaryConfig; 29 import com.sleepycat.je.SecondaryDatabase; 30 import com.sleepycat.je.SecondaryKeyCreator; 31 import com.sleepycat.je.Transaction; 32 33 37 class SecondaryExample { 38 private static final int EXIT_SUCCESS = 0; 39 private static final int EXIT_FAILURE = 1; 40 41 private int numRecords; private int offset; private boolean doInsert; private File envDir; 45 46 public SecondaryExample(int numRecords, 47 boolean doInsert, 48 File envDir, 49 int offset) { 50 this.numRecords = numRecords; 51 this.doInsert = doInsert; 52 this.envDir = envDir; 53 this.offset = offset; 54 } 55 56 59 public static void usage() { 60 System.out.println("usage: java " + 61 "je.SecondaryExample " + 62 "<dbEnvHomeDirectory> " + 63 "<insert|retrieve> <numRecords> [offset]"); 64 System.exit(EXIT_FAILURE); 65 } 66 67 public static void main(String argv[]) { 68 69 if (argv.length < 2) { 70 usage(); 71 return; 72 } 73 File envHomeDirectory = new File (argv[0]); 74 75 boolean doInsertArg = false; 76 if (argv[1].equalsIgnoreCase("insert")) { 77 doInsertArg = true; 78 } else if (argv[1].equalsIgnoreCase("retrieve")) { 79 doInsertArg = false; 80 } else { 81 usage(); 82 } 83 84 int startOffset = 0; 85 int numRecordsVal = 0; 86 87 if (doInsertArg) { 88 89 if (argv.length > 2) { 90 numRecordsVal = Integer.parseInt(argv[2]); 91 } else { 92 usage(); 93 return; 94 } 95 96 if (argv.length > 3) { 97 startOffset = Integer.parseInt(argv[3]); 98 } 99 } 100 101 try { 102 SecondaryExample app = new SecondaryExample(numRecordsVal, 103 doInsertArg, 104 envHomeDirectory, 105 startOffset); 106 app.run(); 107 } catch (DatabaseException e) { 108 e.printStackTrace(); 109 System.exit(EXIT_FAILURE); 110 } 111 System.exit(EXIT_SUCCESS); 112 } 113 114 117 public void run() throws DatabaseException { 118 119 120 EnvironmentConfig envConfig = new EnvironmentConfig(); 121 envConfig.setTransactional(true); 122 envConfig.setAllowCreate(true); 123 Environment exampleEnv = new Environment(envDir, envConfig); 124 125 130 Transaction txn = exampleEnv.beginTransaction(null, null); 131 DatabaseConfig dbConfig = new DatabaseConfig(); 132 dbConfig.setTransactional(true); 133 dbConfig.setAllowCreate(true); 134 Database exampleDb = 135 exampleEnv.openDatabase(txn, "bindingsDb", dbConfig); 136 137 145 DatabaseConfig catalogConfig = new DatabaseConfig(); 146 catalogConfig.setTransactional(true); 147 catalogConfig.setAllowCreate(true); 148 Database catalogDb = 149 exampleEnv.openDatabase(txn, "catalogDb", catalogConfig); 150 StoredClassCatalog catalog = new StoredClassCatalog(catalogDb); 151 152 156 EntryBinding dataBinding = new SerialBinding(catalog, MyData.class); 157 158 163 164 168 EntryBinding secKeyBinding = 169 TupleBinding.getPrimitiveBinding(String .class); 170 171 175 SecondaryConfig secConfig = new SecondaryConfig(); 176 secConfig.setTransactional(true); 177 secConfig.setAllowCreate(true); 178 secConfig.setSortedDuplicates(true); 179 secConfig.setKeyCreator(new MyKeyCreator(secKeyBinding, dataBinding)); 180 SecondaryDatabase exampleSecDb = 181 exampleEnv.openSecondaryDatabase(txn, "bindingsSecDb", 182 exampleDb, secConfig); 183 txn.commit(); 184 185 186 DatabaseEntry keyEntry = new DatabaseEntry(); 187 DatabaseEntry dataEntry = new DatabaseEntry(); 188 189 if (doInsert) { 190 191 197 for (int i = offset; i < numRecords + offset; i++) { 198 txn = exampleEnv.beginTransaction(null, null); 199 StringBuffer stars = new StringBuffer (); 200 for (int j = 0; j < i; j++) { 201 stars.append('*'); 202 } 203 MyData data = new MyData(i, stars.toString()); 204 205 IntegerBinding.intToEntry(i, keyEntry); 206 dataBinding.objectToEntry(data, dataEntry); 207 208 OperationStatus status = 209 exampleDb.put(txn, keyEntry, dataEntry); 210 211 218 if (status != OperationStatus.SUCCESS) { 219 throw new DatabaseException 220 ("Data insertion got status " + status); 221 } 222 txn.commit(); 223 } 224 } else { 225 226 235 txn = exampleEnv.beginTransaction(null, null); 236 Cursor cursor = exampleSecDb.openCursor(txn, null); 237 238 while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) == 239 OperationStatus.SUCCESS) { 240 241 String key = (String ) secKeyBinding.entryToObject(keyEntry); 242 MyData data = (MyData) dataBinding.entryToObject(dataEntry); 243 244 System.out.println("key=" + key + " data=" + data); 245 } 246 cursor.close(); 247 txn.commit(); 248 } 249 250 254 catalogDb.close(); 255 exampleSecDb.close(); 256 exampleDb.close(); 257 exampleEnv.close(); 258 } 259 260 private static class MyData implements Serializable { 261 262 private int num; 263 private String msg; 264 265 MyData(int number, String message) { 266 this.num = number; 267 this.msg = message; 268 } 269 270 String getMessage() { 271 return msg; 272 } 273 274 public String toString() { 275 return String.valueOf(num) + ' ' + msg; 276 } 277 } 278 279 284 private static class MyKeyCreator implements SecondaryKeyCreator { 285 286 private EntryBinding secKeyBinding; 287 private EntryBinding dataBinding; 288 289 MyKeyCreator(EntryBinding secKeyBinding, EntryBinding dataBinding) { 290 this.secKeyBinding = secKeyBinding; 291 this.dataBinding = dataBinding; 292 } 293 294 public boolean createSecondaryKey(SecondaryDatabase secondaryDb, 295 DatabaseEntry keyEntry, 296 DatabaseEntry dataEntry, 297 DatabaseEntry resultEntry) 298 throws DatabaseException { 299 300 305 MyData data = (MyData) dataBinding.entryToObject(dataEntry); 306 String key = data.getMessage(); 307 if (key != null) { 308 secKeyBinding.objectToEntry(key, resultEntry); 309 return true; 310 } else { 311 312 318 return false; 319 } 320 } 321 } 322 } 323 | Popular Tags |