Configuring the HashIndex plug-in

You can configure the built-in HashIndex, the com.ibm.websphere.objectgrid.plugins.index.HashIndex class, programmatically with a dynamic index.

About this task

Configuring a composite index is the same as configuring a regular index with XML, except for the attributeName property value. In a composite index, the value of attributeName property is a comma-delimited list of attributes. For example, the value class Address has three attributes: city, state, and zipcode. A composite index can be defined with the attributeName property value as "city,state,zipcode" indicating that city, state, and zipcode are included in the composite index.

The composite HashIndexes do not support range lookups and therefore cannot have the RangeIndex property set to true.

Procedure

Configure a composite index programmatically. Only applicable for a dynamic index.

The following example code creates the same composite index:

	  HashIndex mapIndex = new HashIndex();
    mapIndex.setName("Address.CityStateZip");
    mapIndex.setAttributeName(("city,state,zipcode"));
    mapIndex.setRangeIndex(false);

    

BackingMap bm = objectGrid.getMap(“mymap”);
        bm.createDynamicIndex(mapIndex, null);

        try {
            // If not using DynamicIndexCallback, need to wait for the Index to be ready.
            // The waiting time depends on the current size of the map
            Thread.sleep(3000);
        } catch (Throwable t) {
            // ...
        }

        // When the index is ready, applications can try to get application index
        // interface instance.
        // Applications have to find a way to ensure that the index is ready to use,
        // if not using DynamicIndexCallback interface.
        // The following example demonstrates the way to wait for the index to be ready
        // Consider the size of the map in the total waiting time.

        Session session = obectGrid.getSession();
        ObjectMap m = session.getMap("mymap");
        MapRangeIndex codeIndex = null;

        int counter = 0;
        int maxCounter = 10;
        boolean ready = false;
        while (!ready && counter < maxCounter) {
            try {
                counter++;
                codeIndex = (MapRangeIndex) m.getIndex("Address.CityStateZip");
                ready = true;
            } catch (IndexNotReadyException e) {
                // implies index is not ready, ...
                System.out.println("Index is not ready. continue to wait.");
                try {
                    Thread.sleep(3000);
                } catch (Throwable tt) {
                    // ...
                }
            } catch (Throwable t) {
                // unexpected exception
                t.printStackTrace();
            }
        }

        if (!ready) {
            System.out.println("Index is not ready.  Need to handle this situation.");
        }