索引を使用した読取り

store.SearchByIndex()を使用して、指定した索引に基づいて表の行を読み取ります。この関数を使用するには、まずCREATE INDEX文を使用して索引を作成する必要があります。

結果セットのベースにする索引値を識別する方法は2つあります。最初の方法は、取得した索引付きフィールドと値を表すstore.CreateRow()を使用して行を作成することです。2つ目の方法は、FetchOptionsオブジェクト構造と、返される索引の開始値と終了値を指定するFieldRange.StartValueFieldRange.EndValueの設定を作成することです。store.CreateRow()メソッドとFetchOptions構造を一緒に使用して、戻りセット値を制限できます。

store.CreateRow()FetchOptionsの両方の値がNULLの場合、表のすべての行が戻りセットに含まれます。

たとえば、次のように定義された表があるとします。

CREATE TABLE student_data (
	id STRING,
	firstName STRING,
	lastName STRING,
	email STRING,
	dateOfBirth STRING,
	PRIMARY KEY(SHARD(firstName, lastName), id)
)

索引は次のとおりです。

// Index is created with name "dob"
CREATE INDEX dob ON student_data (dateOfBirth)

また、次のようなデータを表に移入します。

using oracle.kv.client;
using oracle.kv.client.config;
...
static void Main(string[] args) {
	... //connecting to the store and creating the table is skipped for brevity
	putRow(store, "14SC123", "John", "Doe", "john.doe@example.com", "1996-01-19");
	putRow(store, "14SC124", "Mike", "Ruben", "mike.ruben@example.com", "1997-02-27");
	putRow(store, "14SC125", "Ram", "Paul", "ram.paul@example.com", "1997-12-31");
	readUsingIndexes(store);
}
public static void putRow(IKVStore store, String id, String firstName, String lastName, String email, String dateOfBirth)
{
	String tableName = "student_data";
	var row = store.CreateRow(tableName);
	IRow insertedRow = null;
	row["id"] = id;
	row["firstName"] = firstName;
	row["lastName"] = lastName;
	row["email"] = email;
	row["dateOfBirth"] = dateOfBirth;
	insertedRow = store.Put(row);
	Console.WriteLine(insertedRow); //prints the inserted row in JSON format
}

さらに、次の関数を使用し、dob索引を使用してデータを読み取ります。次の例では、BLOCK 1 (コード内のコメントを参照)は、BLOCK 2とともに使用すると例外がスローされるためコメント・アウトされています。表全体を出力するにはBLOCK 1BLOCK 2の両方をコメントにします。

public static void readUsingIndexes(IKVStore store)
{
	String tableName = "student_data";
	var row = store.CreateRow(tableName);
	FetchOptions fetchoptions = new FetchOptions();

	// BLOCK 1:
	// Uncomment this block to look up only table rows with a 
	// dateOfBirth field set to "1997-02-27". If this
	// block and BLOCK 2 are both used, then the result set 
	// will be empty.
	//row["dateOfBirth"] = "1997-02-27";

	// BLOCK 2:
	// This field range restricts the results set to only
	// those rows with a dateOfBirth field value between
	// "1996-01-01" and "1997-12-31", inclusive. 
	fetchoptions.FieldRange.FieldName = "dateOfBirth";
	fetchoptions.FieldRange.StartValue = "1996-01-01";
	fetchoptions.FieldRange.StartIsInclusive = true;
	fetchoptions.FieldRange.EndValue = "1997-12-31";
	fetchoptions.FieldRange.EndIsInclusive = true;

	// "dob" is the name of the index 
	var fetchedRows = store.SearchByIndex(row, "dob", fetchoptions);                
	foreach (IRow fetchedRow in fetchedRows)
		Console.WriteLine(fetchedRow);
}

store.SearchByIndexAsync()は、行の非同期検索に使用できるstore.SearchByIndex()のバージョンです。