You create a sequence by constructing an array of
Operation
objects. For each object, you specify the
necessary operation information using an
OperationType
object provided to the
object's ONDB_OPERATION
key.
Each element in the array represents exactly one operation in
the store.
For example, suppose you are using a table defined like this:
CREATE TABLE myTable ( itemType STRING, itemCategory STRING, itemClass STRING, itemColor STRING, itemSize STRING, price FLOAT, inventoryCount INTEGER, PRIMARY KEY (SHARD(itemType, itemCategory, itemClass), itemColor, itemSize) )
With tables containing data like this:
Row 1:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: small |
price: 12.07 |
inventoryCount: 127 |
Row 2:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: medium |
price: 13.07 |
inventoryCount: 201 |
Row 3:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: large |
price: 14.07 |
inventoryCount: 39 |
And further suppose that this table has rows that require an update (such as a price and inventory refresh), and you want the update to occur in such a fashion as to ensure it is performed consistently for all the rows.
Then you can create a sequence in the following way:
from nosqldb import Factory from nosqldb import IllegalArgumentException from nosqldb import Operation from nosqldb import OperationType from nosqldb import ProxyConfig from nosqldb import Row from nosqldb import StoreConfig import logging import os import sys ### Constants needed for operations from nosqldb import ONDB_OPERATION from nosqldb import ONDB_OPERATION_TYPE from nosqldb import ONDB_TABLE_NAME from nosqldb import ONDB_ROW from nosqldb import ONDB_ABORT_IF_UNSUCCESSFUL op_array = [] ... ## Skipped setup and logging functions for brevity ... def add_op(op_t, table_name, if_unsuccess, item_type, item_cat, item_class, item_color, item_size, price, inv_count): global op_array row_d = { 'itemType' : item_type, 'itemCategory' : item_cat, 'itemClass' : item_class, 'itemColor' : item_color, 'itemSize' : item_size, 'price' : price, 'inventoryCount' : inv_count } op_row = Row(row_d) op_type = OperationType({ONDB_OPERATION_TYPE : op_t}) op = Operation({ ONDB_OPERATION : op_type, ONDB_TABLE_NAME : table_name, ONDB_ROW : op_row, ONDB_ABORT_IF_UNSUCCESSFUL : True }) op_array.append(op) ... if __name__ == '__main__': ... add_op('PUT', 'myTable', True, "Hats", "baseball", "longbill", "red", "small", 13.07, 107) add_op('PUT', 'myTable', True, "Hats", "baseball", "longbill", "red", "medium", 14.07, 198) add_op('PUT', 'myTable', True, "Hats", "baseball", "longbill", "red", "large", 15.07, 140) ...
Note in the above example that we update only those rows that
share the same shard key. In this case, the shard key includes
the itemType
, itemCategory
,
and itemClass
fields. If the value for any
of those fields is different from the others, we could not
successfully execute the sequence.