1 // file ItemNameKeyCreator.java
2 
3 package je.gettingStarted;
4 
5 import java.io.IOException;
6 
7 import com.sleepycat.bind.tuple.TupleBinding;
8 import com.sleepycat.je.DatabaseEntry;
9 import com.sleepycat.je.SecondaryDatabase;
10 import com.sleepycat.je.SecondaryKeyCreator;
11 
12 public class ItemNameKeyCreator implements SecondaryKeyCreator {
13 
14     private final TupleBinding theBinding;
15 
16     // Use the constructor to set the tuple binding
ItemNameKeyCreator(TupleBinding binding)17     ItemNameKeyCreator(TupleBinding binding) {
18         theBinding = binding;
19     }
20 
21     // Abstract method that we must implement
createSecondaryKey(SecondaryDatabase secDb, DatabaseEntry keyEntry, DatabaseEntry dataEntry, DatabaseEntry resultEntry)22     public boolean createSecondaryKey(SecondaryDatabase secDb,
23              DatabaseEntry keyEntry,      // From the primary
24              DatabaseEntry dataEntry,     // From the primary
25              DatabaseEntry resultEntry) { // set the key data on this.
26         if (dataEntry != null) {
27             // Convert dataEntry to an Inventory object
28             Inventory inventoryItem =
29                   (Inventory)theBinding.entryToObject(dataEntry);
30             // Get the item name and use that as the key
31             String theItem = inventoryItem.getItemName();
32             try {
33                 resultEntry.setData(theItem.getBytes("UTF-8"));
34             } catch (IOException willNeverOccur) {}
35         }
36         return true;
37     }
38 }
39