より効率的なデータ・アクセスのために索引付けを使用します。
HashIndex クラスは、組み込みアプリケーション索引インターフェースである MapIndex と MapRangeIndex の両方をサポートすることのできる組み込み索引プラグイン実装です。 ユーザー独自の索引を作成することもできます。HashIndex を静的索引または動的索引としてバックアップ・マップに追加し、MapIndex または MapRangeIndex の索引プロキシー・オブジェクトを取得し、その索引プロキシー・オブジェクトを使用してキャッシュ・オブジェクトを検索することができます。
ローカル・マップ内のキーを反復処理する場合は、デフォルトの索引を 使用できます。この索引はまったく構成を必要としませんが、エージェントを使用するか ShardEvents.shardActivated(ObjectGrid shard) メソッドから 取得した ObjectGrid インスタンスを使用して、断片に対して使用しなければなりません。
<backingMapPluginCollection id="person">
<bean id="MapIndexplugin"
className="com.ibm.websphere.objectgrid.plugins.index.HashIndex">
<property name="Name" type="java.lang.String" value="CODE"
description="index name" />
<property name="RangeIndex" type="boolean" value="true"
description="true for MapRangeIndex" />
<property name="AttributeName" type="java.lang.String" value="employeeCode"
description="attribute name" />
</bean>
</backingMapPluginCollection>
この XML 構成例では、 組み込み HashIndex クラスが索引プラグインとして使用されています。HashIndex クラスは、 ユーザーが構成できるプロパティーをサポートしています。上の例にある Name、RangeIndex、 AttributeName などです。
BackingMap インターフェースには、静的索引プラグインを追加するために使用できるメソッドとして、addMapIndexplugin と setMapIndexplugins の 2 つがあります。詳しくは、BackingMap APIを参照してください。次の例は、XML 構成の例と同じ構成を作成します。
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
import com.ibm.websphere.objectgrid.ObjectGridManager;
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.BackingMap;
ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid ivObjectGrid = ogManager.createObjectGrid( "grid" );
BackingMap personBackingMap = ivObjectGrid.getMap("person");
// use the builtin HashIndex class as the index plugin class.
HashIndex mapIndexplugin = new HashIndex();
mapIndexplugin.setName("CODE");
mapIndexplugin.setAttributeName("EmployeeCode");
mapIndexplugin.setRangeIndex(true);
personBackingMap.addMapIndexplugin(mapIndexplugin);
MapIndex keyIndex = (MapIndex)
objMap.getIndex(MapIndexPlugin.SYSTEM_KEY_INDEX_NAME);
Iterator keyIterator = keyIndex.findAll();
静的索引プラグイン が BackingMap 構成に追加され、含んでいる ObjectGrid インスタンスが初期化された後であれば、 アプリケーションは BackingMap の ObjectMap インスタンスから名前によって索引オブジェクトを取得できます。 索引オブジェクトは、アプリケーション索引インターフェースにキャストします。これで、アプリケーション索引インターフェース がサポートしている操作を実行できるようになります。
Session session = ivObjectGrid.getSession();
ObjectMap map = session.getMap("person ");
MapRangeIndex codeIndex = (MapRangeIndex) m.getIndex("CODE");
Iterator iter = codeIndex.findLessEqual(new Integer(15));
while (iter.hasNext()) {
Object key = iter.next();
Object value = map.get(key);
}
// Close the session (optional in Version 7.1.1 and later) for improved performance
session.close();
BackingMap インスタンスから動的索引を、いつでもプログラマチックに作成および除去することができます。 動的索引と静的索引の違いは、動的索引は、索引を含む ObjectGrid インスタンスが初期化されたあとでも作成できる、という点です。動的索引付けは、静的索引付けとは違って 非同期プロセスであり、使用される前に作動可能状態に なっている必要があります。このメソッドは、動的索引の取得および使用に、静的索引と同じアプローチを使用します。 動的索引は、不要になると除去できます。BackingMap インターフェースには、動的索引を作成および除去するためのメソッドがあります。
createDynamicIndex メソッドおよび removeDynamicIndex メソッドの詳細については、BackingMap API を参照してください。
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
import com.ibm.websphere.objectgrid.ObjectGridManager;
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.BackingMap;
ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager();
ObjectGrid og = ogManager.createObjectGrid("grid");
BackingMap bm = og.getMap("person");
og.initialize();
// create index after ObjectGrid initialization without DynamicIndexCallback.
bm.createDynamicIndex("CODE", true, "employeeCode", 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 = og.getSession();
ObjectMap m = session.getMap("person");
MapRangeIndex codeIndex = null;
int counter = 0;
int maxCounter = 10;
boolean ready = false;
while (!ready && counter < maxCounter) {
try {
counter++;
codeIndex = (MapRangeIndex) m.getIndex("CODE");
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.");
}
// Use the index to peform queries
// Refer to the MapIndex or MapRangeIndex interface for supported operations.
// The object attribute on which the index is created is the EmployeeCode.
// Assume that the EmployeeCode attribute is Integer type: the
// parameter that is passed into index operations has this data type.
Iterator iter = codeIndex.findLessEqual(new Integer(15));
// remove the dynamic index when no longer needed
bm.removeDynamicIndex("CODE");
// Close the session (optional in Version 7.1.1 and later) for improved performance
session.close();