部分範囲の指定
ストアで複数キー操作を行う場合、操作対象のレコードの範囲を指定できます。これは、KeyRange
クラスを使用して行います。このクラスで、複数取得操作で使用されるキーの直下のキー・コンポーネントに対してString
値の範囲を定義します。
たとえば、次のキーを使用するとします。
/Smith/Bob/-/birthdate
/Smith/Bob/-/phonenumber
/Smith/Bob/-/image
/Smith/Bob/-/userID
/Smith/Patricia/-/birthdate
/Smith/Patricia/-/phonenumber
/Smith/Patricia/-/image
/Smith/Patricia/-/userID
/Smith/Richard/-/birthdate
/Smith/Richard/-/phonenumber
/Smith/Richard/-/image
/Smith/Richard/-/userID
/Wong/Bill/-/birthdate
/Wong/Bill/-/phonenumber
/Wong/Bill/-/image
/Wong/Bill/-/userID
この仮定で、KeyRange
を作成して、ユーザーBob SmithとPatricia Smithに関連するすべてのレコードに対する操作を行うとします。これを行う場合、範囲の上限と下限を定義するキー・コンポーネントを指定する必要があります。指定したキー・コンポーネントを含めるか、除外するかも指定する必要があります。
このケースでは、文字列"Bob"を使用してキー範囲の始まりを定義し、キー範囲の終わりを"Patricia"と定義します。キー範囲の両端は含まれます。
package kvstore.basicExample;
import oracle.kv.KeyRange;
...
KeyRange kr = new KeyRange("Bob", true, "Patricia", true);
複数キー操作を行う際、KeyRange
インスタンスを使用します。たとえば、KVStore.storeIterator()
を使用してストアからレコードを取得するとします。
package kvstore.basicExample;
...
import oracle.kv.Direction;
import oracle.kv.Key;
import oracle.kv.Value;
import oracle.kv.KeyRange;
import oracle.kv.KeyValueVersion;
import oracle.kv.RequestTimeoutException;
import java.util.ArrayList;
import java.util.Iterator;
...
ArrayList<String> majorComponents = new ArrayList<String>();
...
// Define the major and minor path components for the key
majorComponents.add("Smith");
// Create the retrieval key
Key myKey = Key.createKey(majorComponents);
KeyRange kr = new KeyRange("Bob", true, "Patricia", true);
// Now retrieve the records. Note that we do not show the creation of
// the kvstore handle here.
try {
Iterator<KeyValueVersion> i =
kvstore.storeIterator(Direction.FORWARD, 0,
myKey, kr, null);
while (i.hasNext()) {
Value v = i.next().getValue();
// Do some work with the Value here
}
} catch (RequestTimeoutException re) {
// The operation was not completed within the
// timeout value
}