示例#1
0
    async def batch_write_item(cls, request_items: dict):
        _start = cls._take()
        p = {
            'RequestItems': request_items
        }
        res = await cls._client.batch_write_item(**p)
        cls._cheese(_start, 'batch_write_item', p)

        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'
        res = await cls._client.batch_write_item(**p)
        cls._cheese(_start, 'batch_write_item', p)
        if cls._use_profiler:
            QueryCounter.count('batch_write')
            consumed_cu_list = res.get('ConsumedCapacity', False)
            for consumed_cu in consumed_cu_list:
                QueryCounter.count_write_ccu(consumed_cu)
        return res['UnprocessedItems']
示例#2
0
    async def transaction_write(cls, items: list):
        _start = cls._take()
        p = {
            'TransactItems': items
        }

        res = await cls._client.transact_write_items(**p)
        cls._cheese(_start, 'transact_write', p)
        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'
        res = await cls._client.transact_write_items(**p)
        cls._cheese(_start, 'transact_write', p)

        if cls._use_profiler:
            QueryCounter.count('transact_write')
            consumed_cu_list = res.get('ConsumedCapacity', False)
            for consumed_cu in consumed_cu_list:
                QueryCounter.count_write_ccu(consumed_cu)
        return res
示例#3
0
    async def update_item(cls, table_name: str, key: dict, update: UpdateExpression,
                          condition: ConditionExpression = None, raw_table_name=False):
        """
        アイテムを更新

        :param table_name: 対象テーブル名
        :param key: ハッシュキー及びレンジキーの指定式
        :param update: 更新値式インスタンス
        :param condition: 更新条件式インスタンス
        :return: AWSレスポンス
        """
        _start = cls._take()

        p = {
            'TableName': cls.resolve_table_name(table_name, raw_table_name),
            'Key': key,
            **(update.to_parameter(condition))
        }

        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'
        res = await cls._client.update_item(**p)

        cls._cheese(_start, f'update_item {table_name}', p)
        if cls._use_profiler:
            if key.get('kind', None):
                QueryCounter.count('update', f"{table_name} - {key['kind']['S'].split('//')[0]}")
            else:
                QueryCounter.count('update', f"{table_name}")
            consumed_cu = res.get('ConsumedCapacity', False)
            if consumed_cu:
                QueryCounter.count_write_ccu(consumed_cu)
        return res
示例#4
0
    async def query(cls, table_name: str, key_cond: KeyConditionExpression, filter_cond: BaseExpression = None,
                    use_index_name: str = None, limit=0, prj: List[str] = None, raw_table_name=False):
        """
        条件式を複数指定する検索。複数件のアイテムを返却する

        :param table_name: 対象テーブル名
        :param key_cond: キー絞り込み
        :param filter_cond: フィルター
        :param use_index_name: 使用するインデックス名
        :param limit: リミット
        :param prj: プロジェクション情報
        :param raw_table_name: テーブル名にプリフィックスを付与しない
        :return: AWSレスポンス
        """

        _start = cls._take()

        p = {
            'TableName': cls.resolve_table_name(table_name, raw_table_name),
            **({'IndexName': use_index_name} if use_index_name is not None else {}),
            **({'Limit': limit} if limit > 0 is not None else {}),
            **(BaseExpression.merge(key_cond, filter_cond))
        }
        if prj is not None:
            p['ProjectionExpression'] = ','.join(prj)

        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'

        res = await cls._client.query(**p)

        cls._cheese(_start, f'query {table_name}', p)

        if cls._use_profiler:
            if p['ExpressionAttributeValues'].get(':key_value__1', None):
                QueryCounter.count('query',
                                   f"{table_name} - {p.get('IndexName')} - {p['ExpressionAttributeValues'][':key_value__1']}")
            else:
                QueryCounter.count('query', f"{table_name}")
            consumed_cu = res.get('ConsumedCapacity', False)
            if consumed_cu:
                QueryCounter.count_read_ccu(consumed_cu)
        return res['Items']
示例#5
0
    async def get_item(cls, table_name: str, key: dict, prj: List[str] = None):
        """
        キー指定式を使用して一件取得

        :param table_name: テーブル名
        :param key: キー指定式辞書配列
        :return: アイテム情報を格納した辞書配列。AWSレスポンス参照。アイテムが存在しない場合はNone
        """

        _start = cls._take()
        p = {
            'TableName': cls.resolve_table_name(table_name),
            'Key': key
        }

        if prj is not None:
            p['ProjectionExpression'] = ','.join(prj)

        res = await cls._client.get_item(**p)

        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'

        r = res.get('Item', None)

        cls._cheese(_start, f'get_item, {table_name}', p)
        if cls._use_profiler:
            if key.get('kind', None):
                QueryCounter.count('get', f"{table_name} - {key['kind']['S'].split('//')[0]}")
            else:
                QueryCounter.count('get', f"{table_name}")

            consumed_cu = res.get('ConsumedCapacity', False)
            if consumed_cu:
                QueryCounter.count_read_ccu(consumed_cu)

        return r
示例#6
0
    async def put_item(cls, table_name: str, item: dict, condition: ConditionExpression = None, raw_table_name=False):
        """
        | アイテムを作成
        | 更新条件式が成立しなかった場合は例外が発生する

        :param table_name: 対象テーブル名
        :param item: シリアライズされたアイテム情報を格納した辞書配列
        :param condition: 更新条件式インスタンス
        :param raw_table_name: テーブル名にプリフィックスを付与しない
        :return: AWSレスポンス
        """
        _start = cls._take()

        p = {
            'TableName': cls.resolve_table_name(table_name, raw_table_name),
            'Item': item,

            **(condition.to_parameter() if condition is not None else {})
        }
        if cls._use_profiler:
            p['ReturnConsumedCapacity'] = 'INDEXES'

        res = await cls._client.put_item(**p)

        cls._cheese(_start, f'put_item {table_name}', p)
        if cls._use_profiler:
            if item.get('kind', None):
                QueryCounter.count('put', f"{table_name} - {item['kind']['S'].split('//')[0]}")
            else:
                QueryCounter.count('put', f"{table_name}")

            consumed_cu = res.get('ConsumedCapacity', False)
            if consumed_cu:
                QueryCounter.count_write_ccu(consumed_cu)

        return res
示例#7
0
 def dump_count(cls):
     if cls._use_profiler:
         QueryCounter.dump_counter()