'Take a list of one time keys out of the database'

def claim_e2e_one_time_keys(self, query_list):

    def _claim_e2e_one_time_keys(txn):
        sql = 'SELECT    key_id,    key_json    FROM    e2e_one_time_keys_json    WHERE    user_id    =    ?    AND    device_id    =    ?    AND    algorithm    =    ?    LIMIT    1'
        result = {}
        delete = []
        for (user_id, device_id, algorithm) in query_list:
            user_result = result.setdefault(user_id, {})
            device_result = user_result.setdefault(device_id, {})
            txn.execute(sql, (user_id, device_id, algorithm))
            for (key_id, key_json) in txn:
                device_result[((algorithm + ':') + key_id)] = key_json
                delete.append((user_id, device_id, algorithm, key_id))
        sql = 'DELETE    FROM    e2e_one_time_keys_json    WHERE    user_id    =    ?    AND    device_id    =    ?    AND    algorithm    =    ?    AND    key_id    =    ?'
        for (user_id, device_id, algorithm, key_id) in delete:
            txn.execute(sql, (user_id, device_id, algorithm, key_id))
            self._invalidate_cache_and_stream(txn, self.count_e2e_one_time_keys, (user_id, device_id))
        return result
    return self.runInteraction('claim_e2e_one_time_keys', _claim_e2e_one_time_keys)
