001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: CustomKeyOrderExample.java,v 1.4.2.2 2008/01/07 15:14:04 cwl Exp $
007: */
008:
009: package persist;
010:
011: import java.io.File;
012:
013: import com.sleepycat.je.DatabaseException;
014: import com.sleepycat.je.Environment;
015: import com.sleepycat.je.EnvironmentConfig;
016: import com.sleepycat.persist.EntityCursor;
017: import com.sleepycat.persist.EntityStore;
018: import com.sleepycat.persist.PrimaryIndex;
019: import com.sleepycat.persist.StoreConfig;
020: import com.sleepycat.persist.model.Entity;
021: import com.sleepycat.persist.model.KeyField;
022: import com.sleepycat.persist.model.Persistent;
023: import com.sleepycat.persist.model.PrimaryKey;
024:
025: public class CustomKeyOrderExample {
026:
027: @Entity
028: static class Person {
029:
030: @PrimaryKey
031: ReverseOrder name;
032:
033: Person(String name) {
034: this .name = new ReverseOrder(name);
035: }
036:
037: private Person() {
038: } // For deserialization
039:
040: @Override
041: public String toString() {
042: return name.value;
043: }
044: }
045:
046: @Persistent
047: static class ReverseOrder implements Comparable<ReverseOrder> {
048:
049: @KeyField(1)
050: String value;
051:
052: ReverseOrder(String value) {
053: this .value = value;
054: }
055:
056: private ReverseOrder() {
057: } // For deserialization
058:
059: public int compareTo(ReverseOrder o) {
060: return o.value.compareTo(value);
061: }
062: }
063:
064: public static void main(String[] args) throws DatabaseException {
065:
066: if (args.length != 2 || !"-h".equals(args[0])) {
067: System.err.println("Usage: java "
068: + CustomKeyOrderExample.class.getName()
069: + " -h <envHome>");
070: System.exit(2);
071: }
072: CustomKeyOrderExample example = new CustomKeyOrderExample(
073: new File(args[1]));
074: example.run();
075: example.close();
076: }
077:
078: private Environment env;
079: private EntityStore store;
080:
081: private CustomKeyOrderExample(File envHome)
082: throws DatabaseException {
083:
084: /* Open a transactional Berkeley DB engine environment. */
085: EnvironmentConfig envConfig = new EnvironmentConfig();
086: envConfig.setAllowCreate(true);
087: envConfig.setTransactional(true);
088: env = new Environment(envHome, envConfig);
089:
090: /* Open a transactional entity store. */
091: StoreConfig storeConfig = new StoreConfig();
092: storeConfig.setAllowCreate(true);
093: storeConfig.setTransactional(true);
094: store = new EntityStore(env, "TestStore", storeConfig);
095: }
096:
097: private void run() throws DatabaseException {
098:
099: PrimaryIndex<ReverseOrder, Person> index = store
100: .getPrimaryIndex(ReverseOrder.class, Person.class);
101:
102: index.put(new Person("Andy"));
103: index.put(new Person("Lisa"));
104: index.put(new Person("Zola"));
105:
106: /* Print the entities in key order. */
107: EntityCursor<Person> people = index.entities();
108: try {
109: for (Person person : people) {
110: System.out.println(person);
111: }
112: } finally {
113: people.close();
114: }
115: }
116:
117: private void close() throws DatabaseException {
118:
119: store.close();
120: env.close();
121: }
122: }
|