キャッシュ・オブジェクトのカスタム索引作成のためのプラグイン

MapIndexPlugin プラグイン (つまり索引) を使用すると、eXtreme Scale が提供する組み込み索引以上の、カスタムの索引付けストラテジーを書き込めます。

MapIndexPlugin 実装は、MapIndexPlugin インターフェースを使用し、 eXtreme Scale プラグインの共通規則に従う必要があります。

以下のセクションに、この索引インターフェースの重要なメソッドをいくつか示します。

setProperties メソッド

setProperties メソッドを使用して、索引プラグインをプログラマチックに初期化することができます。 このメソッドに渡される Properties オブジェクト・パラメーターには、索引プラグインの適切な初期化に必要な構成情報を含める必要があります。 分散環境では、索引プラグインの構成がクライアントとサーバーのプロセス間で移動するため、getProperties メソッドの実装と一緒に setProperties メソッドの実装が必要です。 以下に、このメソッドの実装例を示します。

setProperties(Properties properties)

// setProperties method sample code
    public void setProperties(Properties properties) {
        ivIndexProperties = properties;

        String ivRangeIndexString = properties.getProperty("rangeIndex");
        if (ivRangeIndexString != null && ivRangeIndexString.equals("true")) {
            setRangeIndex(true);
        }
        setName(properties.getProperty("indexName"));
        setAttributeName(properties.getProperty("attributeName"));

        String ivFieldAccessAttributeString = properties.getProperty("fieldAccessAttribute");
        if (ivFieldAccessAttributeString != null && ivFieldAccessAttributeString.equals("true")) {
            setFieldAccessAttribute(true);
        }

        String ivPOJOKeyIndexString = properties.getProperty("POJOKeyIndex");
        if (ivPOJOKeyIndexString != null && ivPOJOKeyIndexString.equals("true")) {
            setPOJOKeyIndex(true);
        }
    }

getProperties メソッド

getProperties メソッドは、MapIndexPlugin インスタンスから索引プラグインの構成を抽出します。 抽出したプロパティーを使用して、別の MapIndexPlugin インスタンスを初期化し内部状態が同一になるようにすることができます。 分散環境では、getProperties メソッドと setProperties メソッドの実装が必要です。 以下に、getProperties メソッドの実装例を示します。

getProperties()

// getProperties method sample code
    public Properties getProperties() {
        Properties p = new Properties();
        p.put("indexName", indexName);
        p.put("attributeName", attributeName);
        p.put("rangeIndex", ivRangeIndex ? "true" : "false");
        p.put("fieldAccessAttribute", ivFieldAccessAttribute ? "true" : "false");
        p.put("POJOKeyIndex", ivPOJOKeyIndex ? "true" : "false");
        return p;
    }

setEntityMetadata メソッド

setEntityMetadata メソッドは、初期化時に WebSphere® eXtreme Scale ランタイムにより呼び出され、関連する BackingMap の EntityMetadata を MapIndexPlugin インスタンスに設定します。 EntityMetadata は、タプル・オブジェクトの索引のサポートに必要です。 タプルとは、エンティティー・オブジェクトまたはそのキーを表すデータ・セットです。 BackingMap がエンティティー用である場合は、このメソッドを実装する必要があります。

以下のコード例は、setEntityMetadata メソッドを実装します。

setEntityMetadata(EntityMetadata entityMetadata)

// setEntityMetadata method sample code
    public void setEntityMetadata(EntityMetadata entityMetadata) {
        ivEntityMetadata = entityMetadata;
        if (ivEntityMetadata != null) {
            // this is a tuple map
            TupleMetadata valueMetadata = ivEntityMetadata.getValueMetadata();
            int numAttributes = valueMetadata.getNumAttributes();
            for (int i = 0; i < numAttributes; i++) {
                String tupleAttributeName = valueMetadata.getAttribute(i).getName();
                if (attributeName.equals(tupleAttributeName)) {
                    ivTupleValueIndex = i;
                    break;
                }
            }

            if (ivTupleValueIndex == -1) {
                // did not find the attribute in value tuple, try to find it on key tuple.
                // if found on key tuple, implies key indexing on one of tuple key attributes.
                TupleMetadata keyMetadata = ivEntityMetadata.getKeyMetadata();
                numAttributes = keyMetadata.getNumAttributes();
                for (int i = 0; i < numAttributes; i++) {
                    String tupleAttributeName = keyMetadata.getAttribute(i).getName();
                    if (attributeName.equals(tupleAttributeName)) {
                        ivTupleValueIndex = i;
                        ivKeyTupleAttributeIndex = true;
                        break;
                    }
                }
            }

            if (ivTupleValueIndex == -1) {
                // if entityMetadata is not null and we could not find the 
							// attributeName in entityMetadata, this is an
                // error
                throw new ObjectGridRuntimeException("Invalid attributeName.  Entity: " +
								 ivEntityMetadata.getName());
            }
        }
    }

属性名メソッド

setAttributeName メソッドは、索引付けされる属性の名前を設定します。 キャッシュ・オブジェクト・クラスは、索引付き属性に対し get メソッドを提供する必要があります。 例えば、オブジェクトに属性 employeeName または EmployeeName がある場合、索引ではそのオブジェクトで getEmployeeName メソッドを呼び出し、属性値を抽出します。 属性名はその get メソッド内の名前と同一にし、その属性では Comparable インターフェースを実装している必要があります。 属性がブール・タイプである場合は、isAttributeName メソッドのパターンを使用することもできます。

getAttributeName メソッドは、索引付き属性の名前を戻します。

getAttribute メソッド

getAttribute メソッドは、指定したオブジェクトからの索引付き属性値を戻します。 例えば、Employee オブジェクトに索引が付けられた employeeName という属性がある場合は、getAttribute メソッドを使用して、指定された Employee オブジェクトから employeeName の属性値を抽出できます。 このメソッドは、分散 WebSphere eXtreme Scale 環境の場合には必須です。

getAttribute(Object value)

// getAttribute method sample code
    public Object getAttribute(Object value) throws ObjectGridRuntimeException {
        if (ivPOJOKeyIndex) {
            // In the POJO key indexing case, no need to get attribute from value object.
            // The key itself is the attribute value used to build the index.
            return null;
        }

        try {
            Object attribute = null;
            if (value != null) {
                // handle Tuple value if ivTupleValueIndex != -1
                if (ivTupleValueIndex == -1) {
                    // regular value
                    if (ivFieldAccessAttribute) {
                        attribute = this.getAttributeField(value).get(value);
                    } else {
                        attribute = getAttributeMethod(value).invoke(value, emptyArray);
                    }
                } else {
                    // Tuple value
                    attribute = extractValueFromTuple(value);
                }
            }
            return attribute;
        } catch (InvocationTargetException e) {
             throw new ObjectGridRuntimeException(
                    "Caught unexpected Throwable during index update processing, 
											index name = " + indexName + ": " + t,
                    t);
        } catch (Throwable t) {
             throw new ObjectGridRuntimeException(
                    "Caught unexpected Throwable during index update processing, 
										index name = " + indexName + ": " + t,
                    t);
        }
    }