Implementing Key Creators
Prev | Chapter 6. Secondary Databases | Next

## Implementing Key Creators

You must provide every secondary database with a class that creates keys from primary records. You identify this class using the `SecondaryConfig.setKeyCreator()` method.

You can create keys using whatever data you want. Typically you will base your key on some information found in a record's data, but you can also use information found in the primary record's key. How you build your keys is entirely dependent upon the nature of the index that you want to maintain.

You implement a key creator by writing a class that implements the `SecondaryKeyCreator` interface. This interface requires you to implement the `SecondaryKeyCreator.createSecondaryKey()` method.

One thing to remember when implementing this method is that you will need a way to extract the necessary information from the data's `DatabaseEntry` and/or the key's `DatabaseEntry` that are provided on calls to this method. If you are using complex objects, then you are probably using the Bind APIs to perform this conversion. The easiest thing to do is to instantiate the `EntryBinding` or `TupleBinding` that you need to perform the conversion, and then provide this to your key creator's constructor. The Bind APIs are introduced in Using the BIND APIs.

`SecondaryKeyCreator.createSecondaryKey()` returns a boolean. A return value of `false` indicates that no secondary key exists, and therefore no record should be added to the secondary database for that primary record. If a record already exists in the secondary database, it is deleted.

For example, suppose your primary database uses the following class for its record data:

    package je.gettingStarted;

    public class PersonData {
        private String userID;
        private String surname;
        private String familiarName;

        public PersonData(String userID, String surname, String familiarName) {
            this.userID = userID;
            this.surname = surname;
            this.familiarName = familiarName;
        }

        public String getUserID() {
            return userID;
        }

        public String getSurname() {
            return surname;
        }

        public String getFamiliarName() {
            return familiarName;
        }
    }

Also, suppose that you have created a custom tuple binding, `PersonDataBinding`, that you use to convert `PersonData` objects to and from `DatabaseEntry` objects. (Custom tuple bindings are described in Custom Tuple Bindings.)

Finally, suppose you want a secondary database that is keyed based on the person's full name.

Then in this case you might create a key creator as follows:

    package je.gettingStarted;

    import com.sleepycat.bind.tuple.TupleBinding;

    import com.sleepycat.je.SecondaryKeyCreator;
    import com.sleepycat.je.DatabaseEntry;
    import com.sleepycat.je.DatabaseException;
    import com.sleepycat.je.SecondaryDatabase;

    import java.io.IOException;

    public class FullNameKeyCreator implements SecondaryKeyCreator {

        private TupleBinding theBinding;

        public FullNameKeyCreator(TupleBinding theBinding1) {
                theBinding = theBinding1;
        }

        public boolean createSecondaryKey(SecondaryDatabase secDb,
                                          DatabaseEntry keyEntry,
                                          DatabaseEntry dataEntry,
                                          DatabaseEntry resultEntry) {

            try {
                PersonData pd =
                    (PersonData) theBinding.entryToObject(dataEntry);
                    String fullName = pd.getFamiliarName() + " " +
                        pd.getSurname();
                    resultEntry.setData(fullName.getBytes("UTF-8"));
            } catch (IOException willNeverOccur) {}
            return true;
        }
    }

Finally, you use this key creator as follows:

    package je.gettingStarted;

    import com.sleepycat.bind.tuple.TupleBinding;

    import com.sleepycat.je.Database;
    import com.sleepycat.je.DatabaseException;
    import com.sleepycat.je.Environment;
    import com.sleepycat.je.SecondaryDatabase;
    import com.sleepycat.je.SecondaryConfig;

    ...
    Environment myEnv = null;
    Database myDb = null;
    SecondaryDatabase mySecDb = null;
    try {
        // Environment and primary database open omitted for brevity
    ...

        TupleBinding myDataBinding = new MyTupleBinding();
        FullNameKeyCreator fnkc = new FullNameKeyCreator(myDataBinding);

        SecondaryConfig mySecConfig = new SecondaryConfig();
        mySecConfig.setKeyCreator(fnkc);

        //Perform the actual open
        String secDbName = "mySecondaryDatabase";
        mySecDb = myEnv.openSecondaryDatabase(null, secDbName, myDb,
                                              mySecConfig);
    } catch (DatabaseException de) {
        // Exception handling goes here
    } finally {
        try {
            if (mySecDb != null) {
                mySecDb.close();
            }

            if (myDb != null) {
                myDb.close();
            }

            if (myEnv != null) {
                myEnv.close();
            }
        } catch (DatabaseException dbe) {
            // Exception handling goes here
        }
    }

Prev | Up | Next
Chapter 6. Secondary Databases | Home | Secondary Database Properties