埋込みレコードの索引付け

レコード・フィールドにスカラー・データが含まれている場合には、埋込みレコードに索引を作成できます。索引を作成するには、通常のレコードとして定義します。フィールドを索引付けするには、埋込みレコードの名前を指定し、さらに、ドット表記法を使用してフィールドの名前を指定します。

索引を作成するには、まず表を作成します。

CREATE Table myRecordTable (
    uid INTEGER,
    myRecord RECORD (firstField STRING, secondField INTEGER),
    PRIMARY KEY (uid)
) 

表がストアに追加された後、索引を作成します。

CREATE INDEX recordFieldIndex on myRecordTable (myRecord.secondField) 

表の行に指定した値が付いた指定したレコード・フィールドが含まれている場合、データが取得されます。つまり、たとえば、次のような一連の表の行を作成する場合、

def writeStore(store, row_d):

    try:
        store.put("myRecordTable", row_d)
        logging.debug("Store write succeeded.")
    except IllegalArgumentException, iae:
        logging.error("Could not write table.")
        logging.error(iae.message)
        sys.exit(-1)

def populateTable(store):
    row_d = {'uid' : 12345,
             'myRecord' : {'firstField' : 'String field for 12345', 
                           'secondField' : 3388}
            }
    writeStore(store, row_d)

    row_d = {'uid' : 345,
             'myRecord' : {'firstField' : 'String field for 345',  
                           'secondField' : 3388}
            }
    writeStore(store, row_d)

    row_d = {'uid' : 111,
             'myRecord' : {'firstField' : 'String field for 111',
                           'secondField' : 12}
            }
    writeStore(store, row_d) 

指定した値に「secondField」が設定されている埋込みレコードを含む任意の表の行を取得できます。(前述の指定した埋込みレコード索引、索引付けされたmyRecord.secondField。)

一致する表の行を取得し、別の索引タイプと同じ方法で繰り返します。次に例を示します。

def readStore(store):
    try:
        key_d = {"myRecord" : {'secondField' : 3388}}
        row_list = store.index_iterator("myRecordTable",
                                        "recordFieldIndex",
                                        key_d,
                                        False)
        if not row_list:
            logging.debug("Table retrieval failed")
        else:
            logging.debug("Table retrieval succeeded.")
            for r in row_list:
                print r
    except IllegalArgumentException, iae:
        logging.error("Table retrieval failed.")
        logging.error(iae.message)