1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2004,2008 Oracle. All rights reserved.
*
* $Id: EventExampleDPL.java,v 1.5 2008/02/05 23:28:17 mark Exp $
*/
package persist;
import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.SecondaryKey;
/**
* EventExampleDPL is a trivial example which stores Java objects that
* represent an event. Events are primarily indexed by a timestamp, but have
* other attributes, such as price, account reps, customer name and
* quantity. Some of those other attributes are indexed.
* <p>
* The example simply shows the creation of a BDB environment and database,
* inserting some events, and retrieving the events using the Direct
* Persistence layer.
* <p>
* This example is meant to be paired with its twin, EventExample.java.
* EventExample.java and EventExampleDPL.java perform the same functionality,
* but use the Base API and the Direct Persistence Layer API, respectively.
* This may be a useful way to compare the two APIs.
* <p>
* To run the example:
* <pre>
* javac EventExampleDPL.java
* java EventExampleDPL -h <environmentDirectory>
* </pre>
*/
public class EventExampleDPL {
/*
* The Event class embodies our example event and is the application
* data. The @Entity annotation indicates that this class defines the
* objects stored in a BDB database.
*/
@Entity
static class Event {
@PrimaryKey
private Date time;
@SecondaryKey(relate=MANY_TO_ONE)
private int price;
private Set<String> accountReps;
private String customerName;
private int quantity;
Event(Date time,
int price,
String customerName) {
this.time = time;
this.price = price;
this.customerName = customerName;
this.accountReps = new HashSet<String>();
}
private Event() {} // For deserialization
void addRep(String rep) {
accountReps.add(rep);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("time=").append(time);
sb.append(" price=").append(price);
sb.append(" customerName=").append(customerName);
sb.append(" reps=");
if (accountReps.size() == 0) {
sb.append("none");
} else {
for (String rep: accountReps) {
sb.append(rep).append(" ");
}
}
return sb.toString();
}
}
/* A BDB environment is roughly equivalent to a relational database. */
private Environment env;
private EntityStore store;
/*
* Event accessors let us access events by the primary index (time)
* as well as by the rep and price fields
*/
PrimaryIndex<Date,Event> eventByTime;
SecondaryIndex<Integer,Date,Event> eventByPrice;
/* Used for generating example data. */
private Calendar cal;
/*
* First manually make a directory to house the BDB environment.
* Usage: java EventExampleDPL -h <envHome>
* All BDB on-disk storage is held within envHome.
*/
public static void main(String[] args)
throws DatabaseException {
if (args.length != 2 || !"-h".equals(args[0])) {
System.err.println
("Usage: java " + EventExampleDPL.class.getName() +
" -h <envHome>");
System.exit(2);
}
EventExampleDPL example = new EventExampleDPL(new File(args[1]));
example.run();
example.close();
}
private EventExampleDPL(File envHome)
throws DatabaseException {
/* Open a transactional Berkeley DB engine environment. */
System.out.println("-> Creating a BDB environment");
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
envConfig.setTransactional(true);
env = new Environment(envHome, envConfig);
/* Initialize the data access object. */
init();
cal = Calendar.getInstance();
}
/**
* Create all primary and secondary indices.
*/
private void init()
throws DatabaseException {
/* Open a transactional entity store. */
System.out.println("-> Creating a BDB database");
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true);
storeConfig.setTransactional(true);
store = new EntityStore(env, "ExampleStore", storeConfig);
eventByTime = store.getPrimaryIndex(Date.class, Event.class);
eventByPrice = store.getSecondaryIndex(eventByTime,
Integer.class,
"price");
}
private void run()
throws DatabaseException {
Random rand = new Random();
/*
* Create a set of events. Each insertion is a separate, auto-commit
* transaction.
*/
System.out.println("-> Inserting 4 events");
eventByTime.put(new Event(makeDate(1), 100, "Company_A"));
eventByTime.put(new Event(makeDate(2), 2, "Company_B"));
eventByTime.put(new Event(makeDate(3), 20, "Company_C"));
eventByTime.put(new Event(makeDate(4), 40, "CompanyD"));
/* Load a whole set of events transactionally. */
Transaction txn = env.beginTransaction(null, null);
int maxPrice = 50;
System.out.println("-> Inserting some randomly generated events");
for (int i = 0; i < 25; i++) {
Event e = new Event(makeDate(rand.nextInt(365)),
rand.nextInt(maxPrice),
"Company_X");
if ((i%2) ==0) {
e.addRep("Bob");
e.addRep("Nikunj");
} else {
e.addRep("Yongmin");
}
eventByTime.put(e);
}
txn.commitWriteNoSync();
/*
* Windows of events - display the events between June 1 and Aug 31
*/
System.out.println("\n-> Display the events between June 1 and Aug 31");
Date startDate = makeDate(Calendar.JUNE, 1);
Date endDate = makeDate(Calendar.AUGUST, 31);
EntityCursor<Event> eventWindow =
eventByTime.entities(startDate, true, endDate, true);
printEvents(eventWindow);
/*
* Display all events, ordered by a secondary index on price.
*/
System.out.println("\n-> Display all events, ordered by price");
EntityCursor<Event> byPriceEvents = eventByPrice.entities();
printEvents(byPriceEvents);
}
private void close()
throws DatabaseException {
store.close();
env.close();
}
/**
* Print all events covered by this cursor.
*/
private void printEvents(EntityCursor<Event> eCursor)
throws DatabaseException {
try {
for (Event e: eCursor) {
System.out.println(e);
}
} finally {
/* Be sure to close the cursor. */
eCursor.close();
}
}
/**
* Little utility for making up java.util.Dates for different days, just
* to generate test data.
*/
private Date makeDate(int day) {
cal.set((Calendar.DAY_OF_YEAR), day);
return cal.getTime();
}
/**
* Little utility for making up java.util.Dates for different days, just
* to make the test data easier to read.
*/
private Date makeDate(int month, int day) {
cal.set((Calendar.MONTH), month);
cal.set((Calendar.DAY_OF_MONTH), day);
return cal.getTime();
}
}
|