###

Berkeley DB Reference Guide:
  Java API Tutorial - Index

### Creating indexed collections

In the prior Basic example, bindings and Java collections were created for accessing stores via their primary keys. In this example, bindings and collections are added for accessing the same stores via their index keys. As in the prior example, serial bindings and the Java Map class are used.

When a map is created from a DataIndex or a ForeignKeyIndex , the keys of the map will be the index keys. However, the values of the map will be the values of the store associated with the index. This is how index keys can be used to access the values in a store.

For example, the Supplier's City field is an index key that can be used to access the Supplier store. When a map is created using the supplierByCityIndex, the key to the map will be the City field, a String object. When Map.get is called passing the City as the key parameter, a SupplierValue object will be returned.

The SampleViews class is extended to create an index key binding for the Supplier's City field and three Java maps based on the three indices created in the prior section.

>     import com.sleepycat.bdb.bind.DataBinding;
>     import com.sleepycat.bdb.bind.serial.SerialBinding;
>     import com.sleepycat.bdb.collection.StoredEntrySet;
>     import com.sleepycat.bdb.collection.StoredMap;

>     public class SampleViews
>     {
>         ...
>         private StoredMap supplierByCityMap;
>         private StoredMap shipmentByPartMap;
>         private StoredMap shipmentBySupplierMap;

>         public SampleViews(SampleDatabase db)
>         {
>             ...
>             DataBinding cityKeyBinding =
>                 new SerialBinding(db.getCityKeyFormat());
>             ...
>             supplierByCityMap =
>                 new StoredMap(db.getSupplierByCityIndex(),
>                               cityKeyBinding, supplierValueBinding, true);
>             shipmentByPartMap =
>                 new StoredMap(db.getShipmentByPartIndex(),
>                               partKeyBinding, shipmentValueBinding, true);
>             shipmentBySupplierMap =
>                 new StoredMap(db.getShipmentBySupplierIndex(),
>                               supplierKeyBinding, shipmentValueBinding, true);

>         }
>     }

In general, the indexed maps are created here in the same way as the unindexed maps were created in the Basic example. The differences are:

- The first parameter of the StoredMap constructor is a DataIndex rather than a DataStore .
- The second parameter is the index key binding rather than the primary key binding.

For the supplierByCityMap, the cityKeyBinding must first be created. This binding was not created in the Basic example because the City field is not a primary key.

For the shipmentByPartMap and shipmentBySupplierMap, the partKeyBinding and supplierKeyBinding are used. These were created in the Basic example and used as the primary key bindings for the partMap and supplierMap.

The value bindings--supplierValueBinding and shipmentValueBinding--were also created in the Basic example.

This illustrates that bindings and formats may and should be reused where appropriate for creating maps and other collections.

The following getter methods return the stored maps for use by other classes in the example program. Convenience methods for returning entry sets are also included.

>     public class SampleViews
>     {
>         ...
>         public final StoredMap getShipmentByPartMap()
>         {
>             return shipmentByPartMap;
>         }

>         public final StoredMap getShipmentBySupplierMap()
>         {
>             return shipmentBySupplierMap;
>         }

>         public final StoredMap getSupplierByCityMap()
>         {
>             return supplierByCityMap;
>         }

>         public final StoredEntrySet getShipmentByPartEntrySet()
>         {
>             return (StoredEntrySet) shipmentByPartMap.entrySet();
>         }

>         public final StoredEntrySet getShipmentBySupplierEntrySet()
>         {
>             return (StoredEntrySet) shipmentBySupplierMap.entrySet();
>         }

>         public final StoredEntrySet getSupplierByCityEntrySet()
>         {
>             return (StoredEntrySet) supplierByCityMap.entrySet();
>         }
>     }

Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.