1 8 9 package persist; 10 11 import java.io.File ; 12 import java.io.Serializable ; 13 import java.util.Calendar ; 14 import java.util.Date ; 15 import java.util.HashSet ; 16 import java.util.Random ; 17 import java.util.Set ; 18 19 import com.sleepycat.bind.EntryBinding; 20 import com.sleepycat.bind.serial.SerialBinding; 21 import com.sleepycat.bind.serial.StoredClassCatalog; 22 import com.sleepycat.bind.tuple.IntegerBinding; 23 import com.sleepycat.bind.tuple.LongBinding; 24 import com.sleepycat.je.Cursor; 25 import com.sleepycat.je.Database; 26 import com.sleepycat.je.DatabaseConfig; 27 import com.sleepycat.je.DatabaseEntry; 28 import com.sleepycat.je.DatabaseException; 29 import com.sleepycat.je.Environment; 30 import com.sleepycat.je.EnvironmentConfig; 31 import com.sleepycat.je.OperationStatus; 32 import com.sleepycat.je.SecondaryConfig; 33 import com.sleepycat.je.SecondaryCursor; 34 import com.sleepycat.je.SecondaryDatabase; 35 import com.sleepycat.je.SecondaryKeyCreator; 36 import com.sleepycat.je.Transaction; 37 38 59 public class EventExample { 60 61 67 static class Event implements Serializable { 68 69 70 private int price; 71 private Set <String > accountReps; 72 73 private String customerName; 74 private int quantity; 75 76 Event(int price, 77 String customerName) { 78 79 this.price = price; 80 this.customerName = customerName; 81 this.accountReps = new HashSet <String >(); 82 } 83 84 void addRep(String rep) { 85 accountReps.add(rep); 86 } 87 88 @Override  89 public String toString() { 90 StringBuilder sb = new StringBuilder (); 91 sb.append(" price=").append(price); 92 sb.append(" customerName=").append(customerName); 93 sb.append(" reps="); 94 if (accountReps.size() == 0) { 95 sb.append("none"); 96 } else { 97 for (String rep: accountReps) { 98 sb.append(rep).append(" "); 99 } 100 } 101 return sb.toString(); 102 } 103 104 int getPrice() { 105 return price; 106 } 107 } 108 109 110 private Environment env; 111 112 116 private Database eventDb; 117 118 119 private SecondaryDatabase eventByPriceDb; 120 121 126 private Database catalogDb; 127 private EntryBinding eventBinding; 128 129 130 private Calendar cal; 131 132 133 138 public static void main(String [] args) 139 throws DatabaseException { 140 141 if (args.length != 2 || !"-h".equals(args[0])) { 142 System.err.println 143 ("Usage: java " + EventExample.class.getName() + 144 " -h <envHome>"); 145 System.exit(2); 146 } 147 EventExample example = new EventExample(new File (args[1])); 148 example.run(); 149 example.close(); 150 } 151 152 private EventExample(File envHome) 153 throws DatabaseException { 154 155 156 System.out.println("-> Creating a JE environment"); 157 EnvironmentConfig envConfig = new EnvironmentConfig(); 158 envConfig.setAllowCreate(true); 159 envConfig.setTransactional(true); 160 env = new Environment(envHome, envConfig); 161 162 init(); 163 cal = Calendar.getInstance(); 164 } 165 166 169 private void init() 170 throws DatabaseException { 171 172 System.out.println("-> Creating a JE database"); 173 DatabaseConfig dbConfig = new DatabaseConfig(); 174 dbConfig.setTransactional(true); 175 dbConfig.setAllowCreate(true); 176 eventDb = env.openDatabase(null, "eventDb", dbConfig); 179 180 181 195 DatabaseConfig catalogConfig = new DatabaseConfig(); 196 catalogConfig.setTransactional(true); 197 catalogConfig.setAllowCreate(true); 198 catalogDb = env.openDatabase(null, "catalogDb", catalogConfig); 199 StoredClassCatalog catalog = new StoredClassCatalog(catalogDb); 200 201 207 eventBinding = new SerialBinding(catalog, Event.class); 208 209 214 SecondaryConfig secConfig = new SecondaryConfig(); 215 secConfig.setTransactional(true); 216 secConfig.setAllowCreate(true); 217 secConfig.setSortedDuplicates(true); 218 secConfig.setKeyCreator(new PriceKeyCreator(eventBinding)); 219 eventByPriceDb = env.openSecondaryDatabase(null, 220 "priceDb", 221 eventDb, 222 secConfig); 223 224 } 225 226 private void run() 227 throws DatabaseException { 228 229 Random rand = new Random (); 230 231 232 DatabaseEntry key = new DatabaseEntry(); 233 DatabaseEntry data = new DatabaseEntry(); 234 235 239 System.out.println("-> Inserting 4 events"); 240 LongBinding.longToEntry(makeDate(1), key); 241 eventBinding.objectToEntry(new Event(100, "Company_A"), 242 data); 243 eventDb.put(null, key, data); 244 245 LongBinding.longToEntry(makeDate(2), key); 246 eventBinding.objectToEntry(new Event(2, "Company_B"), 247 data); 248 eventDb.put(null, key, data); 249 250 LongBinding.longToEntry(makeDate(3), key); 251 eventBinding.objectToEntry(new Event(20, "Company_C"), 252 data); 253 eventDb.put(null, key, data); 254 255 LongBinding.longToEntry(makeDate(4), key); 256 eventBinding.objectToEntry(new Event(40, "CompanyD"), 257 data); 258 eventDb.put(null, key, data); 259 260 261 Transaction txn = env.beginTransaction(null, null); 262 int maxPrice = 50; 263 System.out.println("-> Inserting some randomly generated events"); 264 for (int i = 0; i < 25; i++) { 265 long time = makeDate(rand.nextInt(365)); 266 Event e = new Event(rand.nextInt(maxPrice),"Company_X"); 267 if ((i%2) ==0) { 268 e.addRep("Jane"); 269 e.addRep("Nikunj"); 270 } else { 271 e.addRep("Yongmin"); 272 } 273 LongBinding.longToEntry(time, key); 274 eventBinding.objectToEntry(e, data); 275 eventDb.put(txn, key, data); 276 } 277 txn.commitWriteNoSync(); 278 279 282 System.out.println("\n-> Display the events between June 1 and Aug 31"); 283 long startDate = makeDate(Calendar.JUNE, 31); 284 long endDate = makeDate(Calendar.AUGUST, 31); 285 286 287 Cursor eventWindow = eventDb.openCursor(null, null); 288 LongBinding.longToEntry(makeDate(Calendar.JUNE, 1), key); 289 290 if ((eventWindow.getSearchKeyRange(key, data, null)) != 291 OperationStatus.SUCCESS) { 292 System.out.println("No events found!"); 293 eventWindow.close(); 294 return; 295 } 296 try { 297 printEvents(key, data, eventWindow, endDate); 298 } finally { 299 eventWindow.close(); 300 } 301 302 305 System.out.println("\n-> Display all events, ordered by price"); 306 SecondaryCursor priceCursor = 307 eventByPriceDb.openSecondaryCursor(null, null); 308 try { 309 printEvents(priceCursor); 310 } finally { 311 priceCursor.close(); 312 } 313 } 314 315 private void close() 316 throws DatabaseException { 317 318 eventByPriceDb.close(); 319 eventDb.close(); 320 catalogDb.close(); 321 env.close(); 322 } 323 324 329 private void printEvents(DatabaseEntry firstKey, 330 DatabaseEntry firstData, 331 Cursor cursor, 332 long endDate) 333 throws DatabaseException { 334 335 System.out.println("time=" + 336 new Date (LongBinding.entryToLong(firstKey)) + 337 eventBinding.entryToObject(firstData)); 338 DatabaseEntry key = new DatabaseEntry(); 339 DatabaseEntry data = new DatabaseEntry(); 340 341 while (cursor.getNext(key, data, null) == 342 OperationStatus.SUCCESS) { 343 if (LongBinding.entryToLong(key) > endDate) { 344 break; 345 } 346 System.out.println("time=" + 347 new Date (LongBinding.entryToLong(key)) + 348 eventBinding.entryToObject(data)); 349 } 350 } 351 352 private void printEvents(SecondaryCursor cursor) 353 throws DatabaseException { 354 DatabaseEntry timeKey = new DatabaseEntry(); 355 DatabaseEntry priceKey = new DatabaseEntry(); 356 DatabaseEntry eventData = new DatabaseEntry(); 357 358 while (cursor.getNext(priceKey, timeKey, eventData, null) == 359 OperationStatus.SUCCESS) { 360 System.out.println("time=" + 361 new Date (LongBinding.entryToLong(timeKey)) + 362 eventBinding.entryToObject(eventData)); 363 } 364 } 365 366 370 private long makeDate(int day) { 371 372 cal.set((Calendar.DAY_OF_YEAR), day); 373 return cal.getTime().getTime(); 374 } 375 379 private long makeDate(int month, int day) { 380 381 cal.set((Calendar.MONTH), month); 382 cal.set((Calendar.DAY_OF_MONTH), day); 383 return cal.getTime().getTime(); 384 } 385 386 391 private static class PriceKeyCreator implements SecondaryKeyCreator { 392 393 private EntryBinding dataBinding; 394 395 PriceKeyCreator(EntryBinding eventBinding) { 396 this.dataBinding = eventBinding; 397 } 398 399 public boolean createSecondaryKey(SecondaryDatabase secondaryDb, 400 DatabaseEntry keyEntry, 401 DatabaseEntry dataEntry, 402 DatabaseEntry resultEntry) 403 throws DatabaseException { 404 405 410 Event e = (Event) dataBinding.entryToObject(dataEntry); 411 int price = e.getPrice(); 412 IntegerBinding.intToEntry(price, resultEntry); 413 return true; 414 } 415 } 416 } 417
| Popular Tags
|