def count(cls, hash_key=None, range_key_condition=None, filter_condition=None, consistent_read=False, index_name=None, limit=None, rate_limit=None): """ Provides a filtered count :param hash_key: The hash key to query. Can be None. :param range_key_condition: Condition for range key :param filter_condition: Condition used to restrict the query results :param consistent_read: If True, a consistent read is performed :param index_name: If set, then this index is used :param rate_limit: If set then consumed capacity will be limited to this amount per second """ if hash_key is None: if filter_condition is not None: raise ValueError('A hash_key must be given to use filters') return cls.describe_table().get(ITEM_COUNT) cls._get_indexes() if index_name: hash_key = cls._index_classes[index_name]._hash_key_attribute( ).serialize(hash_key) else: hash_key = cls._serialize_keys(hash_key)[0] query_args = (hash_key, ) query_kwargs = dict(range_key_condition=range_key_condition, filter_condition=filter_condition, index_name=index_name, consistent_read=consistent_read, limit=limit, select=COUNT) result_iterator = ResultIterator(cls._get_connection().query, query_args, query_kwargs, limit=limit, rate_limit=rate_limit) # iterate through results list(result_iterator) return result_iterator.total_count
def scan(cls, filter_condition=None, segment=None, total_segments=None, limit=None, last_evaluated_key=None, page_size=None, consistent_read=None, index_name=None, rate_limit=None, attributes_to_get=None): """ Iterates through all items in the table :param filter_condition: Condition used to restrict the scan results :param segment: If set, then scans the segment :param total_segments: If set, then specifies total segments :param limit: Used to limit the number of results returned :param last_evaluated_key: If set, provides the starting point for scan. :param page_size: Page size of the scan to DynamoDB :param consistent_read: If True, a consistent read is performed :param index_name: If set, then this index is used :param rate_limit: If set then consumed capacity will be limited to this amount per second :param attributes_to_get: If set, specifies the properties to include in the projection expression """ if page_size is None: page_size = limit scan_args = () scan_kwargs = dict( filter_condition=filter_condition, exclusive_start_key=last_evaluated_key, segment=segment, limit=page_size, total_segments=total_segments, consistent_read=consistent_read, index_name=index_name, attributes_to_get=attributes_to_get ) return ResultIterator( cls._get_connection().scan, scan_args, scan_kwargs, map_fn=cls.from_raw_data, limit=limit, rate_limit=rate_limit, )
def query( cls: Type[_T], hash_key: _KeyType, range_key_condition: Optional[Condition] = None, filter_condition: Optional[Condition] = None, consistent_read: bool = False, index_name: Optional[str] = None, scan_index_forward: Optional[bool] = None, limit: Optional[int] = None, last_evaluated_key: Optional[Dict[str, Dict[str, Any]]] = None, attributes_to_get: Optional[Iterable[str]] = None, page_size: Optional[int] = None, rate_limit: Optional[float] = None, ) -> ResultIterator[_T]: """ Provides a high level query API :param hash_key: The hash key to query :param range_key_condition: Condition for range key :param filter_condition: Condition used to restrict the query results :param consistent_read: If True, a consistent read is performed :param index_name: If set, then this index is used :param limit: Used to limit the number of results returned :param scan_index_forward: If set, then used to specify the same parameter to the DynamoDB API. Controls descending or ascending results :param last_evaluated_key: If set, provides the starting point for query. :param attributes_to_get: If set, only returns these elements :param page_size: Page size of the query to DynamoDB :param rate_limit: If set then consumed capacity will be limited to this amount per second """ cls._get_indexes() if index_name and cls._index_classes: hash_key = cls._index_classes[index_name]._hash_key_attribute( ).serialize(hash_key) else: hash_key = cls._serialize_keys(hash_key)[0] # If this class has a discriminator attribute, filter the query to only return instances of this class. discriminator_attr = cls._get_discriminator_attribute() if discriminator_attr: filter_condition &= discriminator_attr.is_in( *discriminator_attr.get_registered_subclasses(cls)) if page_size is None: page_size = limit query_args = (hash_key, ) query_kwargs = dict( range_key_condition=range_key_condition, filter_condition=filter_condition, index_name=index_name, exclusive_start_key=last_evaluated_key, consistent_read=consistent_read, scan_index_forward=scan_index_forward, limit=page_size, attributes_to_get=attributes_to_get, ) return ResultIterator(cls._get_connection().query, query_args, query_kwargs, map_fn=cls.from_raw_data, limit=limit, rate_limit=rate_limit)
def query(cls, hash_key, range_key_condition=None, filter_condition=None, consistent_read=False, index_name=None, scan_index_forward=None, limit=None, last_evaluated_key=None, attributes_to_get=None, page_size=None, rate_limit=None): """ Provides a high level query API :param hash_key: The hash key to query :param range_key_condition: Condition for range key :param filter_condition: Condition used to restrict the query results :param consistent_read: If True, a consistent read is performed :param index_name: If set, then this index is used :param limit: Used to limit the number of results returned :param scan_index_forward: If set, then used to specify the same parameter to the DynamoDB API. Controls descending or ascending results :param last_evaluated_key: If set, provides the starting point for query. :param attributes_to_get: If set, only returns these elements :param page_size: Page size of the query to DynamoDB :param rate_limit: If set then consumed capacity will be limited to this amount per second """ cls._get_indexes() if index_name: hash_key = cls._index_classes[index_name]._hash_key_attribute( ).serialize(hash_key) key_attribute_classes = cls._index_classes[ index_name]._get_attributes() non_key_attribute_classes = cls.get_attributes() else: hash_key = cls._serialize_keys(hash_key)[0] non_key_attribute_classes = {} key_attribute_classes = {} for name, attr in cls.get_attributes().items(): if attr.is_range_key or attr.is_hash_key: key_attribute_classes[name] = attr else: non_key_attribute_classes[name] = attr if page_size is None: page_size = limit query_args = (hash_key, ) query_kwargs = dict( range_key_condition=range_key_condition, filter_condition=filter_condition, index_name=index_name, exclusive_start_key=last_evaluated_key, consistent_read=consistent_read, scan_index_forward=scan_index_forward, limit=page_size, attributes_to_get=attributes_to_get, ) return ResultIterator(cls._get_connection().query, query_args, query_kwargs, map_fn=cls.from_raw_data, limit=limit, rate_limit=rate_limit)