You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2018/10/15 10:29:55 UTC
[4/6] ignite git commit: IGNITE-7782 Python thin client
http://git-wip-us.apache.org/repos/asf/ignite/blob/7e547b13/modules/platforms/python/pyignite/api/cache_config.py
----------------------------------------------------------------------
diff --git a/modules/platforms/python/pyignite/api/cache_config.py b/modules/platforms/python/pyignite/api/cache_config.py
new file mode 100644
index 0000000..cfea416
--- /dev/null
+++ b/modules/platforms/python/pyignite/api/cache_config.py
@@ -0,0 +1,279 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+Set of functions to manipulate caches.
+
+Ignite `cache` can be viewed as a named entity designed to store key-value
+pairs. Each cache is split transparently between different Ignite partitions.
+
+The choice of `cache` term is due to historical reasons. (Ignite initially had
+only non-persistent storage tier.)
+"""
+
+from typing import Union
+
+from pyignite.datatypes.cache_config import cache_config_struct
+from pyignite.datatypes.cache_properties import prop_map
+from pyignite.datatypes import (
+ Int, Byte, prop_codes, Short, String, StringArray,
+)
+from pyignite.queries import Query, ConfigQuery
+from pyignite.queries.op_codes import *
+from pyignite.utils import cache_id
+
+
+def compact_cache_config(cache_config: dict) -> dict:
+ """
+ This is to make cache config read/write-symmetrical.
+
+ :param cache_config: dict of cache config properties,
+ like {'is_onheapcache_enabled': 1},
+ :return: the same dict, but with property codes as keys,
+ like {PROP_IS_ONHEAPCACHE_ENABLED: 1}.
+ """
+ result = {}
+ for k, v in cache_config.items():
+ if k == 'length':
+ continue
+ prop_code = getattr(prop_codes, 'PROP_{}'.format(k.upper()))
+ result[prop_code] = v
+ return result
+
+
+def cache_get_configuration(
+ connection: 'Connection', cache: Union[str, int], flags: int=0, query_id=None,
+) -> 'APIResult':
+ """
+ Gets configuration for the given cache.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param flags: Ignite documentation is unclear on this subject,
+ :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. Result value is OrderedDict with
+ the cache configuration parameters.
+ """
+
+ query_struct = Query(
+ OP_CACHE_GET_CONFIGURATION,
+ [
+ ('hash_code', Int),
+ ('flags', Byte),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flags': flags,
+ },
+ response_config=[
+ ('cache_config', cache_config_struct),
+ ],
+ )
+ if result.status == 0:
+ result.value = compact_cache_config(result.value['cache_config'])
+ return result
+
+
+def cache_create(
+ connection: 'Connection', name: str, query_id=None,
+) -> 'APIResult':
+ """
+ Creates a cache with a given name. Returns error if a cache with specified
+ name already exists.
+
+ :param connection: connection to Ignite 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_CREATE_WITH_NAME,
+ [
+ ('cache_name', String),
+ ],
+ query_id=query_id,
+ )
+ return query_struct.perform(
+ connection,
+ query_params={
+ 'cache_name': name,
+ },
+ )
+
+
+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 Ignite 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,
+ },
+ )
+
+
+def cache_destroy(
+ connection: 'Connection', cache: Union[str, int], query_id=None,
+) -> 'APIResult':
+ """
+ Destroys cache with a given name.
+
+ :param connection: connection to Ignite 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),
+ },
+ )
+
+
+def cache_get_names(connection: 'Connection', query_id=None) -> 'APIResult':
+ """
+ Gets existing cache names.
+
+ :param connection: connection to Ignite server,
+ :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 list of cache
+ names, non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(OP_CACHE_GET_NAMES, query_id=query_id)
+ result = query_struct.perform(
+ connection,
+ response_config=[
+ ('cache_names', StringArray),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['cache_names']
+ return result
+
+
+def cache_create_with_config(
+ connection: 'Connection', cache_props: dict, query_id=None,
+) -> 'APIResult':
+ """
+ Creates cache with provided configuration. An error is returned
+ if the name is already in use.
+
+ :param connection: connection to Ignite server,
+ :param cache_props: cache configuration properties to create cache with
+ in form of dictionary {property code: python value}.
+ You must supply at least name (PROP_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 cache was created,
+ non-zero status and an error description otherwise.
+ """
+
+ prop_types = {}
+ prop_values = {}
+ for i, prop_item in enumerate(cache_props.items()):
+ prop_code, prop_value = prop_item
+ prop_name = 'property_{}'.format(i)
+ prop_types[prop_name] = prop_map(prop_code)
+ prop_values[prop_name] = prop_value
+ prop_values['param_count'] = len(cache_props)
+
+ query_struct = ConfigQuery(
+ OP_CACHE_CREATE_WITH_CONFIGURATION,
+ [
+ ('param_count', Short),
+ ] + list(prop_types.items()),
+ query_id=query_id,
+ )
+ return query_struct.perform(connection, query_params=prop_values)
+
+
+def cache_get_or_create_with_config(
+ connection: 'Connection', cache_props: dict, query_id=None,
+) -> 'APIResult':
+ """
+ Creates cache with provided configuration. Does nothing if the name
+ is already in use.
+
+ :param connection: connection to Ignite server,
+ :param cache_props: cache configuration properties to create cache with
+ in form of dictionary {property code: python value}.
+ You must supply at least name (PROP_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 cache was created,
+ non-zero status and an error description otherwise.
+ """
+
+ prop_types = {}
+ prop_values = {}
+ for i, prop_item in enumerate(cache_props.items()):
+ prop_code, prop_value = prop_item
+ prop_name = 'property_{}'.format(i)
+ prop_types[prop_name] = prop_map(prop_code)
+ prop_values[prop_name] = prop_value
+ prop_values['param_count'] = len(cache_props)
+
+ query_struct = ConfigQuery(
+ OP_CACHE_GET_OR_CREATE_WITH_CONFIGURATION,
+ [
+ ('param_count', Short),
+ ] + list(prop_types.items()),
+ query_id=query_id,
+ )
+ return query_struct.perform(connection, query_params=prop_values)
http://git-wip-us.apache.org/repos/asf/ignite/blob/7e547b13/modules/platforms/python/pyignite/api/key_value.py
----------------------------------------------------------------------
diff --git a/modules/platforms/python/pyignite/api/key_value.py b/modules/platforms/python/pyignite/api/key_value.py
new file mode 100644
index 0000000..56f5378
--- /dev/null
+++ b/modules/platforms/python/pyignite/api/key_value.py
@@ -0,0 +1,995 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from typing import Iterable, Union
+
+from pyignite.queries.op_codes import *
+from pyignite.datatypes import (
+ Map, Bool, Byte, Int, Long, AnyDataArray, AnyDataObject,
+)
+from pyignite.datatypes.key_value import PeekModes
+from pyignite.queries import Query, Response
+from pyignite.utils import cache_id
+
+
+def cache_put(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache (overwriting existing value if any).
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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,
+ })
+
+
+def cache_get(
+ connection: 'Connection', cache: Union[str, int], key,
+ key_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Retrieves a value from cache by key.
+
+ :param connection: connection to Ignite 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) Ignite 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
+
+
+def cache_get_all(
+ connection: 'Connection', cache: Union[str, int], keys: Iterable,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Retrieves multiple key-value pairs from cache.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param keys: list of keys or tuples of (key, key_hint),
+ :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 dict, made of
+ retrieved key-value pairs, non-zero status and an error description
+ on failure.
+ """
+
+ query_struct = Query(
+ OP_CACHE_GET_ALL,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('keys', AnyDataArray()),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'keys': keys,
+ },
+ response_config=[
+ ('data', Map),
+ ],
+ )
+ if result.status == 0:
+ result.value = dict(result.value)['data']
+ return result
+
+
+def cache_put_all(
+ connection: 'Connection', cache: Union[str, int], pairs: dict,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts multiple key-value pairs to cache (overwriting existing associations
+ if any).
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param pairs: dictionary type parameters, contains key-value pairs to save.
+ Each key or value can be an item of representable Python type or a tuple
+ of (item, hint),
+ :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 key-value pairs
+ are written, non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(
+ OP_CACHE_PUT_ALL,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('data', Map),
+ ],
+ query_id=query_id,
+ )
+ return query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'data': pairs,
+ },
+ )
+
+
+def cache_contains_key(
+ connection: 'Connection', cache: Union[str, int], key,
+ key_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Returns a value indicating whether given key is present in cache.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ 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 a bool value
+ retrieved on success: `True` when key is present, `False` otherwise,
+ non-zero status and an error description on failure.
+ """
+
+ query_struct = Query(
+ OP_CACHE_CONTAINS_KEY,
+ [
+ ('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', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['value']
+ return result
+
+
+def cache_contains_keys(
+ connection: 'Connection', cache: Union[str, int], keys: Iterable,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Returns a value indicating whether all given keys are present in cache.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param keys: a list of keys or (key, type hint) tuples,
+ :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 a bool value
+ retrieved on success: `True` when all keys are present, `False` otherwise,
+ non-zero status and an error description on failure.
+ """
+
+ query_struct = Query(
+ OP_CACHE_CONTAINS_KEYS,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('keys', AnyDataArray()),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'keys': keys,
+ },
+ response_config=[
+ ('value', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['value']
+ return result
+
+
+def cache_get_and_put(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache, and returns the previous value
+ for that key, or null value if there was not such key.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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 if a value is written, non-zero status and an error description
+ in case of error.
+ """
+
+ query_struct = Query(
+ OP_CACHE_GET_AND_PUT,
+ [
+ ('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
+
+
+def cache_get_and_replace(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=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 Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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
+
+
+def cache_get_and_remove(
+ connection: 'Connection', cache: Union[str, int], key,
+ key_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Removes the cache entry with specified key, returning the value.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ 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, non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(
+ OP_CACHE_GET_AND_REMOVE, [
+ ('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:
+ result.value = result.value['value']
+ return result
+
+
+def cache_put_if_absent(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache only if the key
+ does not already exist.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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 on success,
+ non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(
+ OP_CACHE_PUT_IF_ABSENT,
+ [
+ ('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=[
+ ('success', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['success']
+ return result
+
+
+def cache_get_and_put_if_absent(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache only if the key does not
+ already exist.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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 and an old value
+ or None on success, non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(
+ OP_CACHE_GET_AND_PUT_IF_ABSENT,
+ [
+ ('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
+
+
+def cache_replace(
+ connection: 'Connection', cache: Union[str, int], key, value,
+ key_hint=None, value_hint=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache only if the key already exist.
+
+ :param connection: connection to Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param value_hint: (optional) Ignite 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 a boolean
+ success code, or non-zero status and an error description if something
+ has gone wrong.
+ """
+
+ query_struct = Query(
+ OP_CACHE_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=[
+ ('success', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['success']
+ return result
+
+
+def cache_replace_if_equals(
+ connection: 'Connection', cache: Union[str, int], key, sample, value,
+ key_hint=None, sample_hint=None, value_hint=None,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Puts a value with a given key to cache only if the key already exists
+ and value equals provided sample.
+
+ :param connection: connection to Ignite 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 value: new value for the given key,
+ :param key_hint: (optional) Ignite data type, for which the given key
+ should be converted,
+ :param sample_hint: (optional) Ignite data type, for whic
+ the given sample should be converted
+ :param value_hint: (optional) Ignite 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 and a boolean
+ success code, or non-zero status and an error description if something
+ has gone wrong.
+ """
+
+ query_struct = Query(
+ OP_CACHE_REPLACE_IF_EQUALS,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('key', key_hint or AnyDataObject),
+ ('sample', sample_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,
+ 'sample': sample,
+ 'value': value,
+ },
+ response_config=[
+ ('success', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['success']
+ return result
+
+
+def cache_clear(
+ connection: 'Connection', cache: Union[str, int], binary=False,
+ query_id=None,
+) -> 'APIResult':
+ """
+ Clears the cache without notifying listeners or cache writers.
+
+ :param connection: connection to Ignite 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,
+ },
+ )
+
+
+def cache_clear_key(
+ connection: 'Connection', cache: Union[str, int], key,
+ key_hint: object=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Clears the cache key without notifying listeners or cache writers.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param key: key for the cache entry,
+ :param key_hint: (optional) Ignite 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 on success,
+ non-zero status and an error description otherwise.
+ """
+
+ query_struct = Query(
+ OP_CACHE_CLEAR_KEY,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('key', key_hint or AnyDataObject),
+ ],
+ query_id=query_id,
+ )
+ return query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'key': key,
+ },
+ )
+
+
+def cache_clear_keys(
+ connection: 'Connection', cache: Union[str, int], keys: list,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Clears the cache keys without notifying listeners or cache writers.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param keys: list of keys or tuples of (key, key_hint),
+ :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_KEYS,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('keys', AnyDataArray()),
+ ],
+ query_id=query_id,
+ )
+ return query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'keys': keys,
+ },
+ )
+
+
+def cache_remove_key(
+ connection: 'Connection', cache: Union[str, int], key,
+ key_hint: object=None, binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Clears the cache key without notifying listeners or cache writers.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param key: key for the cache entry,
+ :param key_hint: (optional) Ignite 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 boolean
+ success code, or non-zero status and an error description if something
+ has gone wrong.
+ """
+
+ query_struct = Query(
+ OP_CACHE_REMOVE_KEY,
+ [
+ ('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=[
+ ('success', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = result.value['success']
+ return result
+
+
+def cache_remove_if_equals(
+ connection: 'Connection', cache: Union[str, int], key, sample,
+ key_hint=None, sample_hint=None,
+ binary=False, query_id=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 Ignite 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) Ignite data type, for which the given key
+ should be converted,
+ :param sample_hint: (optional) Ignite 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
+
+
+def cache_remove_keys(
+ connection: 'Connection', cache: Union[str, int], keys: Iterable,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Removes entries with given keys, notifying listeners and cache writers.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param keys: list of keys or tuples of (key, key_hint),
+ :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_REMOVE_KEYS,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('keys', AnyDataArray()),
+ ],
+ query_id=query_id,
+ )
+ return query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'keys': keys,
+ },
+ )
+
+
+def cache_remove_all(
+ connection: 'Connection', cache: Union[str, int], binary=False,
+ query_id=None,
+) -> 'APIResult':
+ """
+ Removes all entries from cache, notifying listeners and cache writers.
+
+ :param connection: connection to Ignite 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_REMOVE_ALL,
+ [
+ ('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,
+ },
+ )
+
+
+def cache_get_size(
+ connection: 'Connection', cache: Union[str, int], peek_modes=0,
+ binary=False, query_id=None,
+) -> 'APIResult':
+ """
+ Gets the number of entries in cache.
+
+ :param connection: connection to Ignite 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
http://git-wip-us.apache.org/repos/asf/ignite/blob/7e547b13/modules/platforms/python/pyignite/api/result.py
----------------------------------------------------------------------
diff --git a/modules/platforms/python/pyignite/api/result.py b/modules/platforms/python/pyignite/api/result.py
new file mode 100644
index 0000000..864ef61
--- /dev/null
+++ b/modules/platforms/python/pyignite/api/result.py
@@ -0,0 +1,38 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from pyignite.datatypes import String
+
+
+class APIResult:
+ """
+ Dataclass which represents the result of API request.
+
+ Fields are:
+
+ * status: request status code. 0 if successful,
+ * message: 'Success' if status == 0, verbatim error description
+ otherwise,
+ * value: return value or None.
+ """
+
+ message = 'Success'
+ value = None
+
+ def __init__(self, response: 'Response'):
+ self.status = response.status_code
+ self.query_id = response.query_id
+ if hasattr(response, 'error_message'):
+ self.message = String.to_python(response.error_message)
http://git-wip-us.apache.org/repos/asf/ignite/blob/7e547b13/modules/platforms/python/pyignite/api/sql.py
----------------------------------------------------------------------
diff --git a/modules/platforms/python/pyignite/api/sql.py b/modules/platforms/python/pyignite/api/sql.py
new file mode 100644
index 0000000..f2f96fc
--- /dev/null
+++ b/modules/platforms/python/pyignite/api/sql.py
@@ -0,0 +1,478 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+Only key-value queries (scan queries) are implemented. SQL part is still
+in progress.
+"""
+
+from typing import Union
+
+from pyignite.datatypes import (
+ AnyDataArray, AnyDataObject, Bool, Byte, Int, Long, Map, Null, String,
+ StructArray,
+)
+from pyignite.datatypes.sql import StatementType
+from pyignite.queries import Query, Response, SQLResponse
+from pyignite.queries.op_codes import *
+from pyignite.utils import cache_id
+from .result import APIResult
+
+
+def scan(
+ connection: 'Connection', cache: Union[str, int], page_size: int,
+ partitions: int=-1, local: bool=False, binary: bool=False, query_id=None,
+) -> APIResult:
+ """
+ Performs scan query.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param page_size: cursor page size,
+ :param partitions: (optional) number of partitions to query
+ (negative to query entire cache),
+ :param local: (optional) pass True if this query should be executed
+ on local node only. Defaults to False,
+ :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
+ of type dict with results on success, non-zero status and an error
+ description otherwise.
+
+ Value dict is of following format:
+
+ * `cursor`: int, cursor ID,
+ * `data`: dict, result rows as key-value pairs,
+ * `more`: bool, True if more data is available for subsequent
+ ‘scan_cursor_get_page’ calls.
+ """
+
+ query_struct = Query(
+ OP_QUERY_SCAN,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('filter', Null),
+ ('page_size', Int),
+ ('partitions', Int),
+ ('local', Bool),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'filter': None,
+ 'page_size': page_size,
+ 'partitions': partitions,
+ 'local': 1 if local else 0,
+ },
+ response_config=[
+ ('cursor', Long),
+ ('data', Map),
+ ('more', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = dict(result.value)
+ return result
+
+
+def scan_cursor_get_page(
+ connection: 'Connection', cursor: int, query_id=None,
+) -> APIResult:
+ """
+ Fetches the next scan query cursor page by cursor ID that is obtained
+ from `scan` function.
+
+ :param connection: connection to Ignite 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 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
+ ‘scan_cursor_get_page’ calls.
+ """
+
+ query_struct = Query(
+ OP_QUERY_SCAN_CURSOR_GET_PAGE,
+ [
+ ('cursor', Long),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'cursor': cursor,
+ },
+ response_config=[
+ ('data', Map),
+ ('more', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = dict(result.value)
+ return result
+
+
+def sql(
+ connection: 'Connection', cache: Union[str, int],
+ table_name: str, query_str: str, page_size: int, query_args=None,
+ distributed_joins: bool=False, replicated_only: bool=False,
+ local: bool=False, timeout: int=0, binary: bool=False, query_id=None
+) -> APIResult:
+ """
+ Executes an SQL query over data stored in the cluster. The query returns
+ the whole record (key and value).
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param table_name: name of a type or SQL table,
+ :param query_str: SQL query string,
+ :param page_size: cursor page size,
+ :param query_args: (optional) query arguments,
+ :param distributed_joins: (optional) distributed joins. Defaults to False,
+ :param replicated_only: (optional) whether query contains only replicated
+ tables or not. Defaults to False,
+ :param local: (optional) pass True if this query should be executed
+ on local node only. Defaults to False,
+ :param timeout: (optional) non-negative timeout value in ms. Zero disables
+ timeout (default),
+ :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
+ of type dict with results on success, non-zero status and an error
+ description otherwise.
+
+ Value dict is of following format:
+
+ * `cursor`: int, cursor ID,
+ * `data`: dict, result rows as key-value pairs,
+ * `more`: bool, True if more data is available for subsequent
+ ‘sql_get_page’ calls.
+ """
+
+ if query_args is None:
+ query_args = []
+
+ query_struct = Query(
+ OP_QUERY_SQL,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('table_name', String),
+ ('query_str', String),
+ ('query_args', AnyDataArray()),
+ ('distributed_joins', Bool),
+ ('local', Bool),
+ ('replicated_only', Bool),
+ ('page_size', Int),
+ ('timeout', Long),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ 'table_name': table_name,
+ 'query_str': query_str,
+ 'query_args': query_args,
+ 'distributed_joins': 1 if distributed_joins else 0,
+ 'local': 1 if local else 0,
+ 'replicated_only': 1 if replicated_only else 0,
+ 'page_size': page_size,
+ 'timeout': timeout,
+ },
+ response_config=[
+ ('cursor', Long),
+ ('data', Map),
+ ('more', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = dict(result.value)
+ return result
+
+
+def sql_cursor_get_page(
+ connection: 'Connection', cursor: int, query_id=None,
+) -> APIResult:
+ """
+ Retrieves the next SQL query cursor page by cursor ID from `sql`.
+
+ :param connection: connection to Ignite 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 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),
+ ],
+ query_id=query_id,
+ )
+ result = query_struct.perform(
+ connection,
+ query_params={
+ 'cursor': cursor,
+ },
+ response_config=[
+ ('data', Map),
+ ('more', Bool),
+ ],
+ )
+ if result.status == 0:
+ result.value = dict(result.value)
+ return result
+
+
+def sql_fields(
+ connection: 'Connection', cache: Union[str, int],
+ query_str: str, page_size: int, query_args=None, schema: str=None,
+ statement_type: int=StatementType.ANY, distributed_joins: bool=False,
+ local: bool=False, replicated_only: bool=False,
+ enforce_join_order: bool=False, collocated: bool=False, lazy: bool=False,
+ include_field_names: bool=False, max_rows: int=-1, timeout: int=0,
+ binary: bool=False, query_id=None
+) -> APIResult:
+ """
+ Performs SQL fields query.
+
+ :param connection: connection to Ignite server,
+ :param cache: name or ID of the cache,
+ :param query_str: SQL query string,
+ :param page_size: cursor page size,
+ :param query_args: (optional) query arguments. List of values or
+ (value, type hint) tuples,
+ :param schema: (optional) schema for the query. Defaults to `PUBLIC`,
+ :param statement_type: (optional) statement type. Can be:
+
+ * StatementType.ALL − any type (default),
+ * StatementType.SELECT − select,
+ * StatementType.UPDATE − update.
+
+ :param distributed_joins: (optional) distributed joins. Defaults to False,
+ :param local: (optional) pass True if this query should be executed
+ on local node only. Defaults to False,
+ :param replicated_only: (optional) whether query contains only
+ replicated tables or not. Defaults to False,
+ :param enforce_join_order: (optional) enforce join order. Defaults
+ to False,
+ :param collocated: (optional) whether your data is co-located or not.
+ Defaults to False,
+ :param lazy: (optional) lazy query execution. Defaults to False,
+ :param include_field_names: (optional) include field names in result.
+ Defaults to False,
+ :param max_rows: (optional) query-wide maximum of rows. Defaults to -1
+ (all rows),
+ :param timeout: (optional) non-negative timeout value in ms. Zero disables
+ timeout (default),
+ :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
+ of type dict with results on success, non-zero status and an error
+ description otherwise.
+
+ Value dict is of following format:
+
+ * `cursor`: int, cursor ID,
+ * `data`: list, result values,
+ * `more`: bool, True if more data is available for subsequent
+ ‘sql_fields_cursor_get_page’ calls.
+ """
+ if query_args is None:
+ query_args = []
+
+ query_struct = Query(
+ OP_QUERY_SQL_FIELDS,
+ [
+ ('hash_code', Int),
+ ('flag', Byte),
+ ('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),
+ ],
+ query_id=query_id,
+ )
+
+ _, send_buffer = query_struct.from_python({
+ 'hash_code': cache_id(cache),
+ 'flag': 1 if binary else 0,
+ '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,
+ })
+
+ connection.send(send_buffer)
+
+ response_struct = SQLResponse(
+ include_field_names=include_field_names,
+ has_cursor=True,
+ )
+ response_class, recv_buffer = response_struct.parse(connection)
+ response = response_class.from_buffer_copy(recv_buffer)
+
+ result = APIResult(response)
+ if result.status != 0:
+ return result
+ result.value = response_struct.to_python(response)
+ return result
+
+
+def sql_fields_cursor_get_page(
+ connection: 'Connection', cursor: int, field_count: int, query_id=None,
+) -> APIResult:
+ """
+ Retrieves the next query result page by cursor ID from `sql_fields`.
+
+ :param connection: connection to Ignite 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,
+ )
+
+ _, send_buffer = query_struct.from_python({
+ 'cursor': cursor,
+ })
+
+ connection.send(send_buffer)
+
+ response_struct = Response([
+ ('data', StructArray([
+ ('field_{}'.format(i), AnyDataObject) for i in range(field_count)
+ ])),
+ ('more', Bool),
+ ])
+ response_class, recv_buffer = response_struct.parse(connection)
+ response = response_class.from_buffer_copy(recv_buffer)
+
+ result = APIResult(response)
+ if result.status != 0:
+ return result
+ value = response_struct.to_python(response)
+ 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
+
+
+def resource_close(
+ connection: 'Connection', cursor: int, query_id=None
+) -> APIResult:
+ """
+ Closes a resource, such as query cursor.
+
+ :param connection: connection to Ignite 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(
+ connection,
+ query_params={
+ 'cursor': cursor,
+ },
+ )
http://git-wip-us.apache.org/repos/asf/ignite/blob/7e547b13/modules/platforms/python/pyignite/binary.py
----------------------------------------------------------------------
diff --git a/modules/platforms/python/pyignite/binary.py b/modules/platforms/python/pyignite/binary.py
new file mode 100644
index 0000000..e726730
--- /dev/null
+++ b/modules/platforms/python/pyignite/binary.py
@@ -0,0 +1,136 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""
+:class:`~pyignite.binary.GenericObjectMeta` is a metaclass used to create
+classes, which objects serve as a native Python values for Ignite Complex
+object data type. You can use this metaclass with your existing classes
+to save and restore their selected attributes and properties to/from
+Ignite caches. It is also used internally by `pyignite` to create simple
+data classes “on the fly” when retrieving arbitrary Complex objects.
+
+You can get the examples of using Complex objects in the
+:ref:`complex_object_usage` section of `pyignite` documentation.
+"""
+
+from collections import OrderedDict
+from typing import Any
+
+import attr
+
+from .datatypes import *
+from .exceptions import ParseError
+from .utils import entity_id, schema_id
+
+
+ALLOWED_FIELD_TYPES = [
+ Null, ByteObject, ShortObject, IntObject, LongObject, FloatObject,
+ DoubleObject, CharObject, BoolObject, UUIDObject, DateObject,
+ TimestampObject, TimeObject, EnumObject, BinaryEnumObject,
+ ByteArrayObject, ShortArrayObject, IntArrayObject, LongArrayObject,
+ FloatArrayObject, DoubleArrayObject, CharArrayObject, BoolArrayObject,
+ UUIDArrayObject, DateArrayObject, TimestampArrayObject,
+ TimeArrayObject, EnumArrayObject, String, StringArrayObject,
+ DecimalObject, DecimalArrayObject, ObjectArrayObject, CollectionObject,
+ MapObject, BinaryObject, WrappedDataObject,
+]
+
+
+class GenericObjectPropsMixin:
+ """
+ This class is mixed both to metaclass and to resulting class to make class
+ properties universally available. You should not subclass it directly.
+ """
+ @property
+ def type_name(self) -> str:
+ """ Binary object type name. """
+ return self._type_name
+
+ @property
+ def type_id(self) -> int:
+ """ Binary object type ID. """
+ return entity_id(self._type_name)
+
+ @property
+ def schema(self) -> OrderedDict:
+ """ Binary object schema. """
+ return self._schema.copy()
+
+ @property
+ def schema_id(self) -> int:
+ """ Binary object schema ID. """
+ return schema_id(self._schema)
+
+ def __new__(cls, *args, **kwargs) -> Any:
+ # allow all items in Binary Object schema to be populated as optional
+ # arguments to `__init__()` with sensible defaults.
+ if cls is not GenericObjectMeta:
+ attributes = {
+ k: attr.ib(
+ type=getattr(v, 'pythonic', type(None)),
+ default=getattr(v, 'default', None),
+ ) for k, v in cls.schema.items()
+ }
+ attributes.update({'version': attr.ib(type=int, default=1)})
+ cls = attr.s(cls, these=attributes)
+ # skip parameters
+ return super().__new__(cls)
+
+
+class GenericObjectMeta(type, GenericObjectPropsMixin):
+ """
+ Complex (or Binary) Object metaclass. It is aimed to help user create
+ classes, which objects could serve as a pythonic representation of the
+ :class:`~pyignite.datatypes.complex.BinaryObject` Ignite data type.
+ """
+ _schema = None
+ _type_name = None
+ version = None
+
+ def __new__(
+ mcs: Any, name: str, base_classes: tuple, namespace: dict, **kwargs
+ ) -> Any:
+ """ Sort out class creation arguments. """
+ return super().__new__(
+ mcs, name, (GenericObjectPropsMixin, )+base_classes, namespace
+ )
+
+ @staticmethod
+ def _validate_schema(schema: dict):
+ for field_type in schema.values():
+ if field_type not in ALLOWED_FIELD_TYPES:
+ raise ParseError(
+ 'Wrong binary field type: {}'.format(field_type)
+ )
+
+ def __init__(
+ cls, name: str, base_classes: tuple, namespace: dict,
+ type_name: str=None, schema: OrderedDict=None, **kwargs
+ ):
+ """
+ Initializes binary object class.
+
+ :param type_name: (optional) binary object name. Defaults to class
+ name,
+ :param schema: (optional) a dict of field names: field types,
+ :raise: ParseError if one or more binary field types
+ did not recognized.
+ """
+ cls._type_name = type_name or cls.__name__
+ cls._type_id = entity_id(cls._type_name)
+ schema = schema or OrderedDict()
+ cls._validate_schema(schema)
+ cls._schema = schema
+ super().__init__(name, base_classes, namespace)