def exec_query(self, limit=100): query = defaultdict(lambda: {'Keys': []}) w = 0 for task in self._task: query[HatsudenkiClient.resolve_table_name(task.table_name)]['Keys'].append(task.keys) w += 1 if w >= limit: r = dict(query) w = 0 query.clear() yield HatsudenkiClient.batch_get_item(r) if w is not 0: yield HatsudenkiClient.batch_get_item(dict(query))
def append(self, table: SoloHatsudenkiTable): self._check() query = { TransactionGetKind.Get.value: { 'TableName': HatsudenkiClient.resolve_table_name(table.get_collection_name()), 'Key': table.serialized_key } } self._task.append(query)
def exec_query(self, limit=25): task_len = len(self._task) chunk_heads = (r * limit for r in range(task_len // limit + (0 < (task_len % limit)))) def group(head): return itertools.groupby(self._task[head:(head + limit)], lambda x: x.real_table_name) def query(head): return {k: [val.query for val in v] for k, v in group(head)} return (HatsudenkiClient.batch_write_item(query(h)) for h in chunk_heads)
def append_put(self, table: SoloHatsudenkiTable, overwrite=False): self._check() cond = table.not_exist_condition() if not overwrite else None query = { TransactionWriteKind.Put.value: { 'TableName': HatsudenkiClient.resolve_table_name(table.get_collection_name()), 'Item': table.serialize(), **(cond.to_parameter() if cond is not None else {}) } } self._task.append(query)
async def scan_generator( cls: Type[T], filter_dict: dict = None, tick=50, prj: List[str] = None) -> AsyncGenerator[List[T], None]: fc = None if filter_dict: fc = cls.filter_parse(filter_dict) async for l in HatsudenkiClient.scan_generator( table_name=cls.get_collection_name(), filter_cond=fc, tick=tick, prj=prj): yield cls.from_raw_dict_list(l)
def get(self, target_table_type: Type[T]) -> List[T]: col_items = self.result[HatsudenkiClient.resolve_table_name(target_table_type.get_collection_name())] tt = target_table_type.get_table_type() if tt in {TableType.RootTable, TableType.SingleSoloTable, TableType.SingleMultiTable}: yield from (target_table_type.deserialize(c) for c in col_items) return for item in col_items: if tt is TableType.ChildTable: if item[target_table_type.get_range_key_name()]['S'].startswith(target_table_type.get_tag_name()): yield target_table_type.deserialize(item) elif tt is TableType.ChildSoloTable: if item[target_table_type.get_range_key_name()]['S'] == target_table_type.get_tag_name(): yield target_table_type.deserialize(item)
def append_update(self, table: SoloHatsudenkiTable): self._check() upd = UpdateExpression() table.build_update_expression(upd) cond = table.exist_condition() upd.add('_v', 1) cond.op_and() with cond: cond.equal('_v', table._v) cond.op_or() cond.attribute_exists('_v') key = table.serialized_key query = { TransactionWriteKind.Update.value: { 'TableName': HatsudenkiClient.resolve_table_name(table.get_collection_name()), 'Key': key, **(upd.to_parameter(cond)) } } self._task.append(query)
def real_table_name(self): return HatsudenkiClient.resolve_table_name(self.table_name)