• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/examples_java/src/persist/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: CustomKeyOrderExample.java,v 1.1 2008/02/07 17:12:24 mark Exp $
7 */
8
9package persist;
10
11import java.io.File;
12import java.io.FileNotFoundException;
13
14import com.sleepycat.db.DatabaseException;
15import com.sleepycat.db.Environment;
16import com.sleepycat.db.EnvironmentConfig;
17import com.sleepycat.persist.EntityCursor;
18import com.sleepycat.persist.EntityStore;
19import com.sleepycat.persist.PrimaryIndex;
20import com.sleepycat.persist.StoreConfig;
21import com.sleepycat.persist.model.Entity;
22import com.sleepycat.persist.model.KeyField;
23import com.sleepycat.persist.model.Persistent;
24import com.sleepycat.persist.model.PrimaryKey;
25
26public class CustomKeyOrderExample {
27
28    @Entity
29    static class Person {
30
31        @PrimaryKey
32        ReverseOrder name;
33
34        Person(String name) {
35            this.name = new ReverseOrder(name);
36        }
37
38        private Person() {} // For deserialization
39
40        @Override
41        public String toString() {
42            return name.value;
43        }
44    }
45
46    @Persistent
47    static class ReverseOrder implements Comparable<ReverseOrder> {
48
49        @KeyField(1)
50        String value;
51
52        ReverseOrder(String value) {
53            this.value = value;
54        }
55
56        private ReverseOrder() {} // For deserialization
57
58        public int compareTo(ReverseOrder o) {
59            return o.value.compareTo(value);
60        }
61    }
62
63    public static void main(String[] args)
64        throws DatabaseException, FileNotFoundException {
65
66        if (args.length != 2 || !"-h".equals(args[0])) {
67            System.err.println
68                ("Usage: java " + CustomKeyOrderExample.class.getName() +
69                 " -h <envHome>");
70            System.exit(2);
71        }
72        CustomKeyOrderExample example =
73            new CustomKeyOrderExample(new File(args[1]));
74        example.run();
75        example.close();
76    }
77
78    private Environment env;
79    private EntityStore store;
80
81    private CustomKeyOrderExample(File envHome)
82        throws DatabaseException, FileNotFoundException {
83
84        /* Open a transactional Berkeley DB engine environment. */
85        EnvironmentConfig envConfig = new EnvironmentConfig();
86        envConfig.setAllowCreate(true);
87        envConfig.setTransactional(true);
88        envConfig.setInitializeCache(true);
89        envConfig.setInitializeLocking(true);
90        env = new Environment(envHome, envConfig);
91
92        /* Open a transactional entity store. */
93        StoreConfig storeConfig = new StoreConfig();
94        storeConfig.setAllowCreate(true);
95        storeConfig.setTransactional(true);
96        store = new EntityStore(env, "TestStore", storeConfig);
97    }
98
99    private void run()
100        throws DatabaseException {
101
102        PrimaryIndex<ReverseOrder,Person> index =
103            store.getPrimaryIndex(ReverseOrder.class, Person.class);
104
105        index.put(new Person("Andy"));
106        index.put(new Person("Lisa"));
107        index.put(new Person("Zola"));
108
109        /* Print the entities in key order. */
110        EntityCursor<Person> people = index.entities();
111        try {
112            for (Person person : people) {
113                System.out.println(person);
114            }
115        } finally {
116            people.close();
117        }
118    }
119
120    private void close()
121        throws DatabaseException {
122
123        store.close();
124        env.close();
125    }
126}
127