summaryrefslogtreecommitdiff
path: root/examples_java/src/persist/PersonExample.java
blob: 484f1e768da362763edcd9bb8b1b8d125e577bde (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

package persist;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Set;

import com.sleepycat.db.DatabaseException;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityIndex;
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.Persistent;
import com.sleepycat.persist.model.PrimaryKey;
import com.sleepycat.persist.model.SecondaryKey;
import static com.sleepycat.persist.model.DeleteAction.NULLIFY;
import static com.sleepycat.persist.model.Relationship.ONE_TO_ONE;
import static com.sleepycat.persist.model.Relationship.ONE_TO_MANY;
import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;
import static com.sleepycat.persist.model.Relationship.MANY_TO_MANY;

public class PersonExample {

    /* An entity class. */
    @Entity
    static class Person {

        @PrimaryKey
        String ssn;

        String name;
        Address address;

        @SecondaryKey(relate=MANY_TO_ONE, relatedEntity=Person.class)
        String parentSsn;

        @SecondaryKey(relate=ONE_TO_MANY)
        Set<String> emailAddresses = new HashSet<String>();

        @SecondaryKey(relate=MANY_TO_MANY,
                      relatedEntity=Employer.class,
                      onRelatedEntityDelete=NULLIFY)
        Set<Long> employerIds = new HashSet<Long>();

        Person(String name, String ssn, String parentSsn) {
            this.name = name;
            this.ssn = ssn;
            this.parentSsn = parentSsn;
        }

        private Person() {} // For deserialization
    }

    /* Another entity class. */
    @Entity
    static class Employer {

        @PrimaryKey(sequence="ID")
        long id;

        @SecondaryKey(relate=ONE_TO_ONE)
        String name;

        Address address;

        Employer(String name) {
            this.name = name;
        }

        private Employer() {} // For deserialization
    }

    /* A persistent class used in other classes. */
    @Persistent
    static class Address {
        String street;
        String city;
        String state;
        int zipCode;
        private Address() {} // For deserialization
    }

    /* The data accessor class for the entity model. */
    static class PersonAccessor {

        /* Person accessors */
        PrimaryIndex<String,Person> personBySsn;
        SecondaryIndex<String,String,Person> personByParentSsn;
        SecondaryIndex<String,String,Person> personByEmailAddresses;
        SecondaryIndex<Long,String,Person> personByEmployerIds;

        /* Employer accessors */
        PrimaryIndex<Long,Employer> employerById;
        SecondaryIndex<String,Long,Employer> employerByName;

        /* Opens all primary and secondary indices. */
        public PersonAccessor(EntityStore store)
            throws DatabaseException {

            personBySsn = store.getPrimaryIndex(
                String.class, Person.class);

            personByParentSsn = store.getSecondaryIndex(
                personBySsn, String.class, "parentSsn");

            personByEmailAddresses = store.getSecondaryIndex(
                personBySsn, String.class, "emailAddresses");

            personByEmployerIds = store.getSecondaryIndex(
                personBySsn, Long.class, "employerIds");

            employerById = store.getPrimaryIndex(
                Long.class, Employer.class);

            employerByName = store.getSecondaryIndex(
                employerById, String.class, "name");
        }
    }

    public static void main(String[] args)
        throws DatabaseException, FileNotFoundException {

        if (args.length != 2 || !"-h".equals(args[0])) {
            System.err.println
                ("Usage: java " + PersonExample.class.getName() +
                 " -h <envHome>");
            System.exit(2);
        }
        PersonExample example = new PersonExample(new File(args[1]));
        example.run();
        example.close();
    }

    private Environment env;
    private EntityStore store;
    private PersonAccessor dao;

    private PersonExample(File envHome)
        throws DatabaseException, FileNotFoundException {

        /* Open a transactional Berkeley DB engine environment. */
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setTransactional(true);
        envConfig.setInitializeCache(true);
        envConfig.setInitializeLocking(true);
        env = new Environment(envHome, envConfig);

        /* Open a transactional entity store. */
        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(true);
        storeConfig.setTransactional(true);
        store = new EntityStore(env, "PersonStore", storeConfig);

        /* Initialize the data access object. */
        dao = new PersonAccessor(store);
    }

    private void run()
        throws DatabaseException {

        /*
         * Add a parent and two children using the Person primary index.
         * Specifying a non-null parentSsn adds the child Person to the
         * sub-index of children for that parent key.
         */
        dao.personBySsn.put
            (new Person("Bob Smith", "111-11-1111", null));
        dao.personBySsn.put
            (new Person("Mary Smith", "333-33-3333", "111-11-1111"));
        dao.personBySsn.put
            (new Person("Jack Smith", "222-22-2222", "111-11-1111"));

        /* Print the children of a parent using a sub-index and a cursor. */
        EntityCursor<Person> children =
            dao.personByParentSsn.subIndex("111-11-1111").entities();
        try {
            for (Person child : children) {
                System.out.println(child.ssn + ' ' + child.name);
            }
        } finally {
            children.close();
        }

        /* Get Bob by primary key using the primary index. */
        Person bob = dao.personBySsn.get("111-11-1111");
        assert bob != null;

        /*
         * Create two employers if they do not already exist.  Their primary
         * keys are assigned from a sequence.
         */
        Employer gizmoInc = dao.employerByName.get("Gizmo Inc");
        if (gizmoInc == null) {
            gizmoInc = new Employer("Gizmo Inc");
            dao.employerById.put(gizmoInc);
        }
        Employer gadgetInc = dao.employerByName.get("Gadget Inc");
        if (gadgetInc == null) {
            gadgetInc = new Employer("Gadget Inc");
            dao.employerById.put(gadgetInc);
        }

        /* Bob has two jobs and two email addresses. */
        bob.employerIds.add(gizmoInc.id);
        bob.employerIds.add(gadgetInc.id);
        bob.emailAddresses.add("bob@bob.com");
        bob.emailAddresses.add("bob@gmail.com");

        /* Update Bob's record. */
        dao.personBySsn.put(bob);

        /* Bob can now be found by both email addresses. */
        bob = dao.personByEmailAddresses.get("bob@bob.com");
        assert bob != null;
        bob = dao.personByEmailAddresses.get("bob@gmail.com");
        assert bob != null;

        /* Bob can also be found as an employee of both employers. */
        EntityIndex<String,Person> employees;
        employees = dao.personByEmployerIds.subIndex(gizmoInc.id);
        assert employees.contains("111-11-1111");
        employees = dao.personByEmployerIds.subIndex(gadgetInc.id);
        assert employees.contains("111-11-1111");

        /*
         * When an employer is deleted, the onRelatedEntityDelete=NULLIFY for
         * the employerIds key causes the deleted ID to be removed from Bob's
         * employerIds.
         */
        dao.employerById.delete(gizmoInc.id);
        bob = dao.personBySsn.get("111-11-1111");
        assert bob != null;
        assert !bob.employerIds.contains(gizmoInc.id);
    }

    private void close()
        throws DatabaseException {

        store.close();
        env.close();
    }
}