Ejemplo n.º 1
0
def sql_cursor_get_page(conn: 'Connection', cursor: int) -> APIResult:
    """
    Retrieves the next SQL query cursor page by cursor ID from `sql`.

    :param conn: connection to GridGain server,
    :param cursor: cursor ID,
    :return: API result data object. Contains zero status and a value
     of type dict with results on success, non-zero status and an error
     description otherwise.

     Value dict is of following format:

     * `data`: dict, result rows as key-value pairs,
     * `more`: bool, True if more data is available for subsequent
       ‘sql_cursor_get_page’ calls.
    """

    query_struct = Query(OP_QUERY_SQL_CURSOR_GET_PAGE, [
        ('cursor', Long),
    ])
    result = query_struct.perform(
        conn,
        query_params={
            'cursor': cursor,
        },
        response_config=[
            ('data', Map),
            ('more', Bool),
        ],
    )
    if result.status == 0:
        result.value = dict(result.value)
    return result
Ejemplo n.º 2
0
def cache_destroy(
    connection: 'Connection',
    cache: Union[str, int],
    query_id=None,
) -> 'APIResult':
    """
    Destroys cache with a given name.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object.
    """

    query_struct = Query(
        OP_CACHE_DESTROY,
        [
            ('hash_code', Int),
        ],
        query_id=query_id,
    )
    return query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
        },
    )
Ejemplo n.º 3
0
def resource_close(
    conn: 'Connection', cursor: int, query_id: int = None
) -> APIResult:
    """
    Closes a resource, such as query cursor.

    :param conn: connection to GridGain server,
    :param cursor: cursor ID,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status on success,
     non-zero status and an error description otherwise.
    """

    query_struct = Query(
        OP_RESOURCE_CLOSE,
        [
            ('cursor', Long),
        ],
        query_id=query_id,
    )
    return query_struct.perform(
        conn,
        query_params={
            'cursor': cursor,
        },
    )
Ejemplo n.º 4
0
def cache_get_or_create(
    connection: 'Connection',
    name: str,
    query_id=None,
) -> 'APIResult':
    """
    Creates a cache with a given name. Does nothing if the cache exists.

    :param connection: connection to GridGain server,
    :param name: cache name,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status if a cache is
     created successfully, non-zero status and an error description otherwise.
    """

    query_struct = Query(
        OP_CACHE_GET_OR_CREATE_WITH_NAME,
        [
            ('cache_name', String),
        ],
        query_id=query_id,
    )
    return query_struct.perform(
        connection,
        query_params={
            'cache_name': name,
        },
    )
Ejemplo n.º 5
0
def cache_clear(
    connection: 'Connection',
    cache: Union[str, int],
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Clears the cache without notifying listeners or cache writers.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param binary: (optional) pass True to keep the value in binary form.
     False by default,
    :param query_id: (optional) a value generated by client and returned
     as-is in response.query_id. When the parameter is omitted, a random
     value is generated,
    :return: API result data object. Contains zero status on success,
     non-zero status and an error description otherwise.
    """

    query_struct = Query(
        OP_CACHE_CLEAR,
        [
            ('hash_code', Int),
            ('flag', Byte),
        ],
        query_id=query_id,
    )
    return query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
        },
    )
Ejemplo n.º 6
0
def __resource_close(conn, cursor):
    query_struct = Query(OP_RESOURCE_CLOSE, [
        ('cursor', Long),
    ])
    return query_perform(query_struct, conn, query_params={
        'cursor': cursor,
    })
Ejemplo n.º 7
0
def sql_fields_cursor_get_page(
    conn: 'Connection', cursor: int, field_count: int, query_id: int = None,
) -> APIResult:
    """
    Retrieves the next query result page by cursor ID from `sql_fields`.

    :param conn: connection to GridGain server,
    :param cursor: cursor ID,
    :param field_count: a number of fields in a row,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status and a value
     of type dict with results on success, non-zero status and an error
     description otherwise.

     Value dict is of following format:

     * `data`: list, result values,
     * `more`: bool, True if more data is available for subsequent
       ‘sql_fields_cursor_get_page’ calls.
    """

    query_struct = Query(
        OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE,
        [
            ('cursor', Long),
        ],
        query_id=query_id,
    )
    result = query_struct.perform(
        conn,
        query_params={
            'cursor': cursor,
        },
        response_config=[
            ('data', StructArray([
                ('field_{}'.format(i), AnyDataObject)
                for i in range(field_count)
            ])),
            ('more', Bool),
        ]
    )
    if result.status != 0:
        return result

    value = result.value
    result.value = {
        'data': [],
        'more': value['more']
    }
    for row_dict in value['data']:
        row = []
        for field_key in sorted(row_dict.keys()):
            row.append(row_dict[field_key])
        result.value['data'].append(row)
    return result
Ejemplo n.º 8
0
def cache_get_and_replace(
    connection: 'Connection',
    cache: Union[str, int],
    key: Any,
    value: Any,
    key_hint: 'GridGainDataType' = None,
    value_hint: 'GridGainDataType' = None,
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Puts a value with a given key to cache, returning previous value
    for that key, if and only if there is a value currently mapped
    for that key.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param key: key for the cache entry. Can be of any supported type,
    :param value: value for the key,
    :param key_hint: (optional) GridGain data type, for which the given key
     should be converted,
    :param value_hint: (optional) GridGain data type, for which the given value
     should be converted.
    :param binary: pass True to keep the value in binary form. False
     by default,
    :param query_id: a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status and an old value
     or None on success, non-zero status and an error description otherwise.
    """

    query_struct = Query(
        OP_CACHE_GET_AND_REPLACE,
        [
            ('hash_code', Int),
            ('flag', Byte),
            ('key', key_hint or AnyDataObject),
            ('value', value_hint or AnyDataObject),
        ],
        query_id=query_id,
    )
    result = query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
            'key': key,
            'value': value,
        },
        response_config=[
            ('value', AnyDataObject),
        ],
    )
    if result.status == 0:
        result.value = result.value['value']
    return result
Ejemplo n.º 9
0
def cache_remove_if_equals(
    connection: 'Connection',
    cache: Union[str, int],
    key: Any,
    sample: Any,
    key_hint: 'GridGainDataType' = None,
    sample_hint: 'GridGainDataType' = None,
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Removes an entry with a given key if provided value is equal to
    actual value, notifying listeners and cache writers.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param key:  key for the cache entry,
    :param sample: a sample to compare the stored value with,
    :param key_hint: (optional) GridGain data type, for which the given key
     should be converted,
    :param sample_hint: (optional) GridGain data type, for whic
     the given sample should be converted
    :param binary: (optional) pass True to keep the value in binary form.
     False by default,
    :param query_id: (optional) a value generated by client and returned
     as-is in response.query_id. When the parameter is omitted, a random
     value is generated,
    :return: API result data object. Contains zero status and a boolean
     success code, or non-zero status and an error description if something
     has gone wrong.
    """

    query_struct = Query(
        OP_CACHE_REMOVE_IF_EQUALS,
        [
            ('hash_code', Int),
            ('flag', Byte),
            ('key', key_hint or AnyDataObject),
            ('sample', sample_hint or AnyDataObject),
        ],
        query_id=query_id,
    )
    result = query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
            'key': key,
            'sample': sample,
        },
        response_config=[
            ('success', Bool),
        ],
    )
    if result.status == 0:
        result.value = result.value['success']
    return result
Ejemplo n.º 10
0
def __cluster_get_state(connection):
    if not connection.protocol_context.is_cluster_api_supported():
        raise NotSupportedByClusterError('Cluster API is not supported by the cluster')

    query_struct = Query(OP_CLUSTER_GET_STATE)
    return query_perform(
        query_struct, connection,
        response_config=[('state', Byte)],
        post_process_fun=__post_process_get_state
    )
Ejemplo n.º 11
0
def __cache_get_configuration(connection, cache_info):
    query_struct = Query(OP_CACHE_GET_CONFIGURATION,
                         [('cache_info', CacheInfo)])
    return query_perform(query_struct,
                         connection,
                         query_params={'cache_info': cache_info},
                         response_config=[('cache_config',
                                           get_cache_config_struct(
                                               connection.protocol_context))],
                         post_process_fun=__post_process_cache_config)
Ejemplo n.º 12
0
def __tx_end(conn, tx_id, committed):
    query_struct = Query(
        OP_TX_END,
        [('tx_id', Int), ('committed', Bool)],
    )
    return query_perform(query_struct,
                         conn,
                         query_params={
                             'tx_id': tx_id,
                             'committed': committed
                         })
Ejemplo n.º 13
0
def __cache_remove_all(connection, cache_info):
    query_struct = Query(OP_CACHE_REMOVE_ALL, [
        ('cache_info', CacheInfo),
    ])
    return query_perform(
        query_struct,
        connection,
        query_params={
            'cache_info': cache_info,
        },
    )
Ejemplo n.º 14
0
def __cache_clear(connection, cache_info):
    query_struct = Query(OP_CACHE_CLEAR, [
        ('cache_info', CacheInfo),
    ])
    return query_perform(
        query_struct,
        connection,
        query_params={
            'cache_info': cache_info,
        },
    )
Ejemplo n.º 15
0
def __get_binary_type(conn, binary_type):
    query_struct = Query(OP_GET_BINARY_TYPE, [
        ('type_id', Int),
    ],
                         response_type=BinaryTypeResponse)

    return query_perform(query_struct,
                         conn,
                         query_params={
                             'type_id': entity_id(binary_type),
                         })
Ejemplo n.º 16
0
def cache_get_size(
    connection: 'Connection',
    cache: Union[str, int],
    peek_modes: int = 0,
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Gets the number of entries in cache.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param peek_modes: (optional) limit count to near cache partition
     (PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache
     (PeekModes.BACKUP). Defaults to all cache partitions (PeekModes.ALL),
    :param binary: (optional) pass True to keep the value in binary form.
     False by default,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status and a number of
     cache entries on success, non-zero status and an error description
     otherwise.
    """
    if not isinstance(peek_modes, (list, tuple)):
        if peek_modes == 0:
            peek_modes = []
        else:
            peek_modes = [peek_modes]

    query_struct = Query(
        OP_CACHE_GET_SIZE,
        [
            ('hash_code', Int),
            ('flag', Byte),
            ('peek_modes', PeekModes),
        ],
        query_id=query_id,
    )
    result = query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
            'peek_modes': peek_modes,
        },
        response_config=[
            ('count', Long),
        ],
    )
    if result.status == 0:
        result.value = result.value['count']
    return result
Ejemplo n.º 17
0
def __cache_put_all(connection, cache_info, pairs):
    query_struct = Query(OP_CACHE_PUT_ALL, [
        ('cache_info', CacheInfo),
        ('data', Map),
    ])
    return query_perform(
        query_struct,
        connection,
        query_params={
            'cache_info': cache_info,
            'data': pairs,
        },
    )
Ejemplo n.º 18
0
def __tx_start(conn, concurrency, isolation, timeout, label):
    query_struct = Query(OP_TX_START, [('concurrency', Byte),
                                       ('isolation', Byte), ('timeout', Long),
                                       ('label', String)])
    return query_perform(query_struct,
                         conn,
                         query_params={
                             'concurrency': concurrency,
                             'isolation': isolation,
                             'timeout': timeout,
                             'label': label
                         },
                         response_config=[('tx_id', Int)])
Ejemplo n.º 19
0
def __cache_put(connection, cache_info, key, value, key_hint, value_hint):
    query_struct = Query(OP_CACHE_PUT, [
        ('cache_info', CacheInfo),
        ('key', key_hint or AnyDataObject),
        ('value', value_hint or AnyDataObject),
    ])
    return query_perform(query_struct,
                         connection,
                         query_params={
                             'cache_info': cache_info,
                             'key': key,
                             'value': value
                         })
Ejemplo n.º 20
0
def __cache_remove_keys(connection, cache_info, keys):
    query_struct = Query(OP_CACHE_REMOVE_KEYS, [
        ('cache_info', CacheInfo),
        ('keys', AnyDataArray()),
    ])
    return query_perform(
        query_struct,
        connection,
        query_params={
            'cache_info': cache_info,
            'keys': keys,
        },
    )
Ejemplo n.º 21
0
def __cache_clear_key(connection, cache_info, key, key_hint):
    query_struct = Query(OP_CACHE_CLEAR_KEY, [
        ('cache_info', CacheInfo),
        ('key', key_hint or AnyDataObject),
    ])
    return query_perform(
        query_struct,
        connection,
        query_params={
            'cache_info': cache_info,
            'key': key,
        },
    )
Ejemplo n.º 22
0
def __scan_cursor_get_page(conn, cursor):
    query_struct = Query(OP_QUERY_SCAN_CURSOR_GET_PAGE, [
        ('cursor', Long),
    ])
    return query_perform(query_struct,
                         conn,
                         query_params={
                             'cursor': cursor,
                         },
                         response_config=[
                             ('data', Map),
                             ('more', Bool),
                         ],
                         post_process_fun=__query_result_post_process)
Ejemplo n.º 23
0
def cache_get(
    connection: 'Connection',
    cache: Union[str, int],
    key: Any,
    key_hint: 'GridGainDataType' = None,
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Retrieves a value from cache by key.

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param key: key for the cache entry. Can be of any supported type,
    :param key_hint: (optional) GridGain data type, for which the given key
     should be converted,
    :param binary: (optional) pass True to keep the value in binary form.
     False by default,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status and a value
     retrieved on success, non-zero status and an error description on failure.
    """

    query_struct = Query(
        OP_CACHE_GET,
        [
            ('hash_code', Int),
            ('flag', Byte),
            ('key', key_hint or AnyDataObject),
        ],
        query_id=query_id,
    )
    result = query_struct.perform(
        connection,
        query_params={
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
            'key': key,
        },
        response_config=[
            ('value', AnyDataObject),
        ],
    )
    if result.status != 0:
        return result
    result.value = result.value['value']
    return result
Ejemplo n.º 24
0
def __sql_fields(conn, cache_info, query_str, page_size, query_args, schema,
                 statement_type, distributed_joins, local, replicated_only,
                 enforce_join_order, collocated, lazy, include_field_names,
                 max_rows, timeout):
    if query_args is None:
        query_args = []

    query_struct = Query(OP_QUERY_SQL_FIELDS, [
        ('cache_info', CacheInfo),
        ('schema', String),
        ('page_size', Int),
        ('max_rows', Int),
        ('query_str', String),
        ('query_args', AnyDataArray()),
        ('statement_type', StatementType),
        ('distributed_joins', Bool),
        ('local', Bool),
        ('replicated_only', Bool),
        ('enforce_join_order', Bool),
        ('collocated', Bool),
        ('lazy', Bool),
        ('timeout', Long),
        ('include_field_names', Bool),
    ],
                         response_type=SQLResponse)

    return query_perform(
        query_struct,
        conn,
        query_params={
            'cache_info': cache_info,
            'schema': schema,
            'page_size': page_size,
            'max_rows': max_rows,
            'query_str': query_str,
            'query_args': query_args,
            'statement_type': statement_type,
            'distributed_joins': distributed_joins,
            'local': local,
            'replicated_only': replicated_only,
            'enforce_join_order': enforce_join_order,
            'collocated': collocated,
            'lazy': lazy,
            'timeout': timeout,
            'include_field_names': include_field_names,
        },
        include_field_names=include_field_names,
        has_cursor=True,
    )
Ejemplo n.º 25
0
def __cache_contains_keys(connection, cache_info, keys):
    query_struct = Query(OP_CACHE_CONTAINS_KEYS, [
        ('cache_info', CacheInfo),
        ('keys', AnyDataArray()),
    ])
    return query_perform(query_struct,
                         connection,
                         query_params={
                             'cache_info': cache_info,
                             'keys': keys,
                         },
                         response_config=[
                             ('value', Bool),
                         ],
                         post_process_fun=__post_process_value_by_key('value'))
Ejemplo n.º 26
0
def __cache_contains_key(connection, cache_info, key, key_hint):
    query_struct = Query(OP_CACHE_CONTAINS_KEY, [
        ('cache_info', CacheInfo),
        ('key', key_hint or AnyDataObject),
    ])
    return query_perform(query_struct,
                         connection,
                         query_params={
                             'cache_info': cache_info,
                             'key': key,
                         },
                         response_config=[
                             ('value', Bool),
                         ],
                         post_process_fun=__post_process_value_by_key('value'))
Ejemplo n.º 27
0
def __cache_get_and_remove(connection, cache_info, key, key_hint):
    query_struct = Query(OP_CACHE_GET_AND_REMOVE, [
        ('cache_info', CacheInfo),
        ('key', key_hint or AnyDataObject),
    ])
    return query_perform(query_struct,
                         connection,
                         query_params={
                             'cache_info': cache_info,
                             'key': key,
                         },
                         response_config=[
                             ('value', AnyDataObject),
                         ],
                         post_process_fun=__post_process_value_by_key('value'))
Ejemplo n.º 28
0
def __cache_get_all(connection, cache_info, keys):
    query_struct = Query(OP_CACHE_GET_ALL, [
        ('cache_info', CacheInfo),
        ('keys', AnyDataArray()),
    ])
    return query_perform(query_struct,
                         connection,
                         query_params={
                             'cache_info': cache_info,
                             'keys': keys,
                         },
                         response_config=[
                             ('data', Map),
                         ],
                         post_process_fun=__post_process_value_by_key('data'))
Ejemplo n.º 29
0
def __sql_fields_cursor_get_page(conn, cursor, field_count):
    query_struct = Query(OP_QUERY_SQL_FIELDS_CURSOR_GET_PAGE, [
        ('cursor', Long),
    ])
    return query_perform(query_struct,
                         conn,
                         query_params={
                             'cursor': cursor,
                         },
                         response_config=[
                             ('data',
                              StructArray([(f'field_{i}', AnyDataObject)
                                           for i in range(field_count)])),
                             ('more', Bool),
                         ],
                         post_process_fun=__post_process_sql_fields_cursor)
Ejemplo n.º 30
0
def cache_put(
    connection: 'Connection',
    cache: Union[str, int],
    key: Any,
    value: Any,
    key_hint: 'GridGainDataType' = None,
    value_hint: 'GridGainDataType' = None,
    binary: bool = False,
    query_id: Optional[int] = None,
) -> 'APIResult':
    """
    Puts a value with a given key to cache (overwriting existing value if any).

    :param connection: connection to GridGain server,
    :param cache: name or ID of the cache,
    :param key: key for the cache entry. Can be of any supported type,
    :param value: value for the key,
    :param key_hint: (optional) GridGain data type, for which the given key
     should be converted,
    :param value_hint: (optional) GridGain data type, for which the given
     value should be converted.
    :param binary: (optional) pass True to keep the value in binary form.
     False by default,
    :param query_id: (optional) a value generated by client and returned as-is
     in response.query_id. When the parameter is omitted, a random value
     is generated,
    :return: API result data object. Contains zero status if a value
     is written, non-zero status and an error description otherwise.
    """

    query_struct = Query(
        OP_CACHE_PUT,
        [
            ('hash_code', Int),
            ('flag', Byte),
            ('key', key_hint or AnyDataObject),
            ('value', value_hint or AnyDataObject),
        ],
        query_id=query_id,
    )
    return query_struct.perform(
        connection, {
            'hash_code': cache_id(cache),
            'flag': 1 if binary else 0,
            'key': key,
            'value': value,
        })