OperationFactory
クラスを使用して、ストアでの各操作を表すOperation
クラス・インスタンスを作成してシーケンスを作成します。OperationFactory
のインスタンスは、KVStore.getOperationFactory()
を使用して取得します。
たとえば、次のキーを使用するとします。
/Products/Hats/-/baseball /Products/Hats/-/baseball/longbill /Products/Hats/-/baseball/longbill/blue /Products/Hats/-/baseball/longbill/red /Products/Hats/-/baseball/shortbill /Products/Hats/-/baseball/shortbill/blue /Products/Hats/-/baseball/shortbill/red /Products/Hats/-/western /Products/Hats/-/western/felt /Products/Hats/-/western/felt/black /Products/Hats/-/western/felt/gray /Products/Hats/-/western/leather /Products/Hats/-/western/leather/black /Products/Hats/-/western/leather/gray
さらに、次の各レコードに、情報がすべてのレコードで必ず整合性がとれるような方法で読取りや更新を行う情報(価格の更新日など)が含まれているとします。
/Products/Hats/-/western /Products/Hats/-/western/felt /Products/Hats/-/western/leather
次のようにシーケンスを作成します。
package kvstore.basicExample; ... import oracle.kv.Key; import oracle.kv.Value; import oracle.kv.Operation; import oracle.kv.OperationFactory; import java.util.ArrayList; import org.apache.avro.Schema; import oracle.kv.avro.GenericAvroBinding; import oracle.kv.avro.GenericRecord; ... // Get the operation factory. Note that we do not show the // creation of the kvstore handle here. OperationFactory of = kvstore.getOperationFactory(); // We need a List to hold the operations in the // sequence. ArrayList<Operation> opList = new ArrayList<Operation>(); ... ArrayList<String> majorComponents = new ArrayList<String>(); ArrayList<String> minorComponents1 = new ArrayList<String>(); ArrayList<String> minorComponents2 = new ArrayList<String>(); ArrayList<String> minorComponents3 = new ArrayList<String>(); ... // Define the major and minor path components for our keys majorComponents.add("Products"); majorComponents.add("Hats"); minorComponents1.add("western"); minorComponents2.add("western"); minorComponents2.add("felt"); minorComponents3.add("western"); minorComponents3.add("leather"); // Create the three keys that we will need Key key1 = Key.createKey(majorComponents, minorComponents1); Key key2 = Key.createKey(majorComponents, minorComponents2); Key key3 = Key.createKey(majorComponents, minorComponents3); ... // Binding and schema creation omitted ... final GenericRecord hat1 = new GenericData.Record(hatSchema); hat1.put("randomHatData", "someRandomData"); final Value value1 = binding.toValue(hat1); final GenericRecord hat2 = new GenericData.Record(hatSchema); hat2.put("randomHatData", "someMoreRandomData"); final Value value2 = binding.toValue(hat2); final GenericRecord hat3 = new GenericData.Record(hatSchema); hat3.put("randomHatData", "someMoreRandomData"); final Value value3 = binding.toValue(hat3); ... // Here we would perform whatever actions we need to create // our record values. We won't show how the values get created, // but assume it results in three Value objects: value1, value2, // and value3. ... // Now create our list of operations for the key pairs // key1/value1, key2/value2, and key3/value3. In this // trivial example we will put store all three records // in a single atomic operation. opList.add(of.createPut(key1, value1)); opList.add(of.createPut(key2, value2)); opList.add(of.createPut(key3, value3));
前述の例で、マイナー・パス・コンポーネントのみが異なる3つの一意キーを作成することに注意してください。3つのキーの1つでもメジャー・パス・コンポーネントが異なると、シーケンスは正常に実行されません。