2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 2004, 2012 Oracle and/or its affiliates. All rights reserved.
11 import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
14 import java.io.FileNotFoundException;
15 import java.util.Calendar;
16 import java.util.Date;
17 import java.util.HashSet;
18 import java.util.Random;
21 import com.sleepycat.db.DatabaseException;
22 import com.sleepycat.db.Environment;
23 import com.sleepycat.db.EnvironmentConfig;
24 import com.sleepycat.db.Transaction;
25 import com.sleepycat.persist.EntityCursor;
26 import com.sleepycat.persist.EntityStore;
27 import com.sleepycat.persist.PrimaryIndex;
28 import com.sleepycat.persist.SecondaryIndex;
29 import com.sleepycat.persist.StoreConfig;
30 import com.sleepycat.persist.model.Entity;
31 import com.sleepycat.persist.model.PrimaryKey;
32 import com.sleepycat.persist.model.SecondaryKey;
35 * EventExampleDPL is a trivial example which stores Java objects that
36 * represent an event. Events are primarily indexed by a timestamp, but have
37 * other attributes, such as price, account reps, customer name and
38 * quantity. Some of those other attributes are indexed.
40 * The example simply shows the creation of a BDB environment and database,
41 * inserting some events, and retrieving the events using the Direct
44 * This example is meant to be paired with its twin, EventExample.java.
45 * EventExample.java and EventExampleDPL.java perform the same functionality,
46 * but use the Base API and the Direct Persistence Layer API, respectively.
47 * This may be a useful way to compare the two APIs.
51 * javac EventExampleDPL.java
52 * java EventExampleDPL -h <environmentDirectory>
55 public class EventExampleDPL {
58 * The Event class embodies our example event and is the application
59 * data. The @Entity annotation indicates that this class defines the
60 * objects stored in a BDB database.
68 @SecondaryKey(relate=MANY_TO_ONE)
71 private Set<String> accountReps;
73 private String customerName;
78 String customerName) {
82 this.customerName = customerName;
83 this.accountReps = new HashSet<String>();
86 private Event() {} // For deserialization
88 void addRep(String rep) {
93 public String toString() {
94 StringBuilder sb = new StringBuilder();
95 sb.append("time=").append(time);
96 sb.append(" price=").append(price);
97 sb.append(" customerName=").append(customerName);
99 if (accountReps.size() == 0) {
102 for (String rep: accountReps) {
103 sb.append(rep).append(" ");
106 return sb.toString();
110 /* A BDB environment is roughly equivalent to a relational database. */
111 private Environment env;
112 private EntityStore store;
115 * Event accessors let us access events by the primary index (time)
116 * as well as by the rep and price fields
118 PrimaryIndex<Date,Event> eventByTime;
119 SecondaryIndex<Integer,Date,Event> eventByPrice;
121 /* Used for generating example data. */
122 private Calendar cal;
125 * First manually make a directory to house the BDB environment.
126 * Usage: java EventExampleDPL -h <envHome>
127 * All BDB on-disk storage is held within envHome.
129 public static void main(String[] args)
130 throws DatabaseException, FileNotFoundException {
132 if (args.length != 2 || !"-h".equals(args[0])) {
134 ("Usage: java " + EventExampleDPL.class.getName() +
138 EventExampleDPL example = new EventExampleDPL(new File(args[1]));
143 private EventExampleDPL(File envHome)
144 throws DatabaseException, FileNotFoundException {
146 /* Open a transactional Berkeley DB engine environment. */
147 System.out.println("-> Creating a BDB environment");
148 EnvironmentConfig envConfig = new EnvironmentConfig();
149 envConfig.setAllowCreate(true);
150 envConfig.setTransactional(true);
151 envConfig.setInitializeCache(true);
152 envConfig.setInitializeLocking(true);
153 env = new Environment(envHome, envConfig);
155 /* Initialize the data access object. */
157 cal = Calendar.getInstance();
161 * Create all primary and secondary indices.
164 throws DatabaseException {
166 /* Open a transactional entity store. */
167 System.out.println("-> Creating a BDB database");
168 StoreConfig storeConfig = new StoreConfig();
169 storeConfig.setAllowCreate(true);
170 storeConfig.setTransactional(true);
171 store = new EntityStore(env, "ExampleStore", storeConfig);
173 eventByTime = store.getPrimaryIndex(Date.class, Event.class);
174 eventByPrice = store.getSecondaryIndex(eventByTime,
180 throws DatabaseException {
182 Random rand = new Random();
185 * Create a set of events. Each insertion is a separate, auto-commit
188 System.out.println("-> Inserting 4 events");
189 eventByTime.put(new Event(makeDate(1), 100, "Company_A"));
190 eventByTime.put(new Event(makeDate(2), 2, "Company_B"));
191 eventByTime.put(new Event(makeDate(3), 20, "Company_C"));
192 eventByTime.put(new Event(makeDate(4), 40, "CompanyD"));
194 /* Load a whole set of events transactionally. */
195 Transaction txn = env.beginTransaction(null, null);
197 System.out.println("-> Inserting some randomly generated events");
198 for (int i = 0; i < 25; i++) {
199 Event e = new Event(makeDate(rand.nextInt(365)),
200 rand.nextInt(maxPrice),
210 txn.commitWriteNoSync();
213 * Windows of events - display the events between June 1 and Aug 31
215 System.out.println("\n-> Display the events between June 1 and Aug 31");
216 Date startDate = makeDate(Calendar.JUNE, 1);
217 Date endDate = makeDate(Calendar.AUGUST, 31);
219 EntityCursor<Event> eventWindow =
220 eventByTime.entities(startDate, true, endDate, true);
221 printEvents(eventWindow);
224 * Display all events, ordered by a secondary index on price.
226 System.out.println("\n-> Display all events, ordered by price");
227 EntityCursor<Event> byPriceEvents = eventByPrice.entities();
228 printEvents(byPriceEvents);
232 throws DatabaseException {
239 * Print all events covered by this cursor.
241 private void printEvents(EntityCursor<Event> eCursor)
242 throws DatabaseException {
244 for (Event e: eCursor) {
245 System.out.println(e);
248 /* Be sure to close the cursor. */
254 * Little utility for making up java.util.Dates for different days, just
255 * to generate test data.
257 private Date makeDate(int day) {
259 cal.set((Calendar.DAY_OF_YEAR), day);
260 return cal.getTime();
264 * Little utility for making up java.util.Dates for different days, just
265 * to make the test data easier to read.
267 private Date makeDate(int month, int day) {
269 cal.set((Calendar.MONTH), month);
270 cal.set((Calendar.DAY_OF_MONTH), day);
271 return cal.getTime();