1 8 9 package persist; 10 11 import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE; 12 13 import java.io.File ; 14 import java.util.Calendar ; 15 import java.util.Date ; 16 import java.util.HashSet ; 17 import java.util.Random ; 18 import java.util.Set ; 19 20 import com.sleepycat.je.DatabaseException; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.Transaction; 24 import com.sleepycat.persist.EntityCursor; 25 import com.sleepycat.persist.EntityStore; 26 import com.sleepycat.persist.PrimaryIndex; 27 import com.sleepycat.persist.SecondaryIndex; 28 import com.sleepycat.persist.StoreConfig; 29 import com.sleepycat.persist.model.Entity; 30 import com.sleepycat.persist.model.PrimaryKey; 31 import com.sleepycat.persist.model.SecondaryKey; 32 33 54 public class EventExampleDPL { 55 56 61 @Entity 62 static class Event { 63 64 @PrimaryKey 65 private Date time; 66 67 @SecondaryKey(relate=MANY_TO_ONE) 68 private int price; 69 70 private Set <String > accountReps; 71 72 private String customerName; 73 private int quantity; 74 75 Event(Date time, 76 int price, 77 String customerName) { 78 79 this.time = time; 80 this.price = price; 81 this.customerName = customerName; 82 this.accountReps = new HashSet <String >(); 83 } 84 85 private Event() {} 87 void addRep(String rep) { 88 accountReps.add(rep); 89 } 90 91 @Override  92 public String toString() { 93 StringBuilder sb = new StringBuilder (); 94 sb.append("time=").append(time); 95 sb.append(" price=").append(price); 96 sb.append(" customerName=").append(customerName); 97 sb.append(" reps="); 98 if (accountReps.size() == 0) { 99 sb.append("none"); 100 } else { 101 for (String rep: accountReps) { 102 sb.append(rep).append(" "); 103 } 104 } 105 return sb.toString(); 106 } 107 } 108 109 110 private Environment env; 111 private EntityStore store; 112 113 117 PrimaryIndex<Date ,Event> eventByTime; 118 SecondaryIndex<Integer ,Date ,Event> eventByPrice; 119 120 121 private Calendar cal; 122 123 128 public static void main(String [] args) 129 throws DatabaseException { 130 131 if (args.length != 2 || !"-h".equals(args[0])) { 132 System.err.println 133 ("Usage: java " + EventExampleDPL.class.getName() + 134 " -h <envHome>"); 135 System.exit(2); 136 } 137 EventExampleDPL example = new EventExampleDPL(new File (args[1])); 138 example.run(); 139 example.close(); 140 } 141 142 private EventExampleDPL(File envHome) 143 throws DatabaseException { 144 145 146 System.out.println("-> Creating a JE environment"); 147 EnvironmentConfig envConfig = new EnvironmentConfig(); 148 envConfig.setAllowCreate(true); 149 envConfig.setTransactional(true); 150 env = new Environment(envHome, envConfig); 151 152 153 init(); 154 cal = Calendar.getInstance(); 155 } 156 157 160 private void init() 161 throws DatabaseException { 162 163 164 System.out.println("-> Creating a JE database"); 165 StoreConfig storeConfig = new StoreConfig(); 166 storeConfig.setAllowCreate(true); 167 storeConfig.setTransactional(true); 168 store = new EntityStore(env, "ExampleStore", storeConfig); 169 170 eventByTime = store.getPrimaryIndex(Date .class, Event.class); 171 eventByPrice = store.getSecondaryIndex(eventByTime, 172 Integer .class, 173 "price"); 174 } 175 176 private void run() 177 throws DatabaseException { 178 179 Random rand = new Random (); 180 181 185 System.out.println("-> Inserting 4 events"); 186 eventByTime.put(new Event(makeDate(1), 100, "Company_A")); 187 eventByTime.put(new Event(makeDate(2), 2, "Company_B")); 188 eventByTime.put(new Event(makeDate(3), 20, "Company_C")); 189 eventByTime.put(new Event(makeDate(4), 40, "CompanyD")); 190 191 192 Transaction txn = env.beginTransaction(null, null); 193 int maxPrice = 50; 194 System.out.println("-> Inserting some randomly generated events"); 195 for (int i = 0; i < 25; i++) { 196 Event e = new Event(makeDate(rand.nextInt(365)), 197 rand.nextInt(maxPrice), 198 "Company_X"); 199 if ((i%2) ==0) { 200 e.addRep("Bob"); 201 e.addRep("Nikunj"); 202 } else { 203 e.addRep("Yongmin"); 204 } 205 eventByTime.put(e); 206 } 207 txn.commitWriteNoSync(); 208 209 212 System.out.println("\n-> Display the events between June 1 and Aug 31"); 213 Date startDate = makeDate(Calendar.JUNE, 1); 214 Date endDate = makeDate(Calendar.AUGUST, 31); 215 216 EntityCursor<Event> eventWindow = 217 eventByTime.entities(startDate, true, endDate, true); 218 printEvents(eventWindow); 219 220 223 System.out.println("\n-> Display all events, ordered by price"); 224 EntityCursor<Event> byPriceEvents = eventByPrice.entities(); 225 printEvents(byPriceEvents); 226 } 227 228 private void close() 229 throws DatabaseException { 230 231 store.close(); 232 env.close(); 233 } 234 235 238 private void printEvents(EntityCursor<Event> eCursor) 239 throws DatabaseException { 240 try { 241 for (Event e: eCursor) { 242 System.out.println(e); 243 } 244 } finally { 245 246 eCursor.close(); 247 } 248 } 249 250 254 private Date makeDate(int day) { 255 256 cal.set((Calendar.DAY_OF_YEAR), day); 257 return cal.getTime(); 258 } 259 260 264 private Date makeDate(int month, int day) { 265 266 cal.set((Calendar.MONTH), month); 267 cal.set((Calendar.DAY_OF_MONTH), day); 268 return cal.getTime(); 269 } 270 } 271
| Popular Tags
|