Library Version 12.1.6.0

Creating Indexed Collections
Prev | Chapter 3. Using Secondary Indices | Next

## Creating Indexed Collections

In the prior Basic example, bindings and Java collections were created for accessing databases via their primary keys. In this example, bindings and collections are added for accessing the same databases 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 SecondaryDatabase, the keys of the map will be the index keys. However, the values of the map will be the values of the primary database associated with the index. This is how index keys can be used to access the values in a primary database.

For example, the Supplier's City field is an index key that can be used to access the Supplier database. When a map is created using the `supplierByCityDb()` method, 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 `SupplierData` 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.bind.EntryBinding;
    import com.sleepycat.bind.serial.SerialBinding;
    import com.sleepycat.collections.StoredEntrySet;
    import com.sleepycat.collections.StoredSortedMap;
    ...

    public class SampleViews
    {
        ...
        private StoredSortedMap supplierByCityMap;
        private StoredSortedMap shipmentByPartMap;
        private StoredSortedMap shipmentBySupplierMap;
        ...

        public SampleViews(SampleDatabase db)
        {
            ClassCatalog catalog = db.getClassCatalog();
            ...
            EntryBinding cityKeyBinding =
                new SerialBinding(catalog, String.class);
            ...
            supplierByCityMap =
                new StoredSortedMap(db.getSupplierByCityDatabase(),
                              cityKeyBinding, supplierDataBinding, true);
            shipmentByPartMap =
                new StoredSortedMap(db.getShipmentByPartDatabase(),
                              partKeyBinding, supplierDataBinding, true);
            shipmentBySupplierMap =
                new StoredSortedMap(db.getShipmentBySupplierDatabase(),
                              supplierKeyBinding, supplierDataBinding, 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 StoredSortedMap constructor is a SecondaryDatabase rather than a Database.

- 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.

Like the bindings created earlier for keys and values, the `cityKeyBinding` is a SerialBinding. Unlike the bindings created earlier, it is an example of creating a binding for a built-in Java class, String, instead of an application-defined class. Any serializable class may be used.

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 — `supplierDataBinding` and `shipmentDataBinding` — 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 StoredSortedMap getShipmentByPartMap()
        {
            return shipmentByPartMap;
        }

        public final StoredSortedMap getShipmentBySupplierMap()
        {
            return shipmentBySupplierMap;
        }

        public final StoredSortedMap getSupplierByCityMap()
        {
            return supplierByCityMap;
        }

        public final StoredEntrySet getShipmentByPartMap()
        {
            return (StoredEntrySet) shipmentByPartMap();
        }

        public final StoredEntrySet getShipmentBySupplierMap()
        {
            return (StoredEntrySet) shipmentBySupplierMap();
        }

        public final StoredEntrySet getSupplierByCityMap()
        {
            return (StoredEntrySet) supplierByCityMap();
        }
        ...
    }

Prev | Up | Next
More Secondary Key Indices | Home | Retrieving Items by Index Key