Before we write the main parts of our example programs, there is one last common piece of code that we need. Both our examples are required to work with primary and secondary indices contained within our EntityStore. We could just refer to these directly every time we need to access a primary or secondary index, but instead we choose to encapsulate this information in a class so as to share that code between the applications.
You retrieve a primary index using the EntityStore.getPrimaryIndex() method. To do this, you indicate the index key type (that is, whether it is a String, Integer, and so forth) and the class of the entities stored in the index.
For example, the following retrieves the primary index for the Inventory class. These index keys are of type String.
PrimaryIndex<String,Inventory> inventoryBySku = store.getPrimaryIndex(String.class, Inventory.class);
You retrieve a secondary index using the EntityStore.getSecondaryIndex() method. Because secondary indices actually refer to a primary index somewhere in your data store, to access a secondary index you:
Provide the primary index as returned by EntityStore.getPrimaryIndex().
Identify the key data type used by the secondary index (String, Long, and so forth).
Identify the name of the secondary key field. When you declare the SecondaryIndex object, you identify the entity class to which the secondary index must refer.
For example, the following first retrieves the primary index, and then uses that to retrieve a secondary index. The secondary key is held by the itemName field of the Inventory class.
PrimaryIndex<String,Inventory> inventoryBySku = store.getPrimaryIndex(String.class, Inventory.class); SecondaryIndex<String,String,Inventory> inventoryByName = store.getSecondaryIndex(inventoryBySku, String.class, "itemName");
Now that you understand how to retrieve primary and secondary indices, we can implement our DataAccessor class. Again, this class is shared by both our example programs and it is used to access the primary and secondary indices that our programs use.
If you compare this class against our Vendor and Inventory class implementations, you will see that the primary and secondary indices declared there are referenced by this class.
See Vendor.class and Inventory.class for those implementations.
package persist.gettingStarted; import java.io.File; import com.sleepycat.je.DatabaseException; import com.sleepycat.persist.EntityStore; import com.sleepycat.persist.PrimaryIndex; import com.sleepycat.persist.SecondaryIndex; public class DataAccessor { // Open the indices public DataAccessor(EntityStore store) throws DatabaseException { // Primary key for Inventory classes inventoryBySku = store.getPrimaryIndex( String.class, Inventory.class); // Secondary key for Inventory classes // Last field in the getSecondaryIndex() method must be // the name of a class member; in this case, an Inventory.class // data member. inventoryByName = store.getSecondaryIndex( inventoryBySku, String.class, "itemName"); // Primary key for Vendor class vendorByName = store.getPrimaryIndex( String.class, Vendor.class); } // Inventory Accessors PrimaryIndex<String,Inventory> inventoryBySku; SecondaryIndex<String,String,Inventory> inventoryByName; // Vendor Accessors PrimaryIndex<String,Vendor> vendorByName; }