google.appengine.ext.key_range.KeyRange

Represents a range of keys in the datastore.

Inherits From: expected_type

A KeyRange object represents a key range (key_start, include_start, key_end, include_end) and a scan direction (KeyRange.DESC or KeyRange.ASC).

key_start The starting key for this range (db.Key or ndb.Key).
key_end The ending key for this range (db.Key or ndb.Key).
direction The direction of the query for this range.
include_start Whether the start key should be included in the range.
include_end Whether the end key should be included in the range.
namespace The namespace for this range. If None then the current namespace is used.

Methods

advance

View source

Updates the start of the range immediately past the specified key.

Args
key A db.Key or ndb.Key.

bisect_string_range

View source

Returns a string that is approximately in the middle of the range.

(start, end) is treated as a string range, and it is assumed start <= end in the usual lexicographic string ordering. The output key mid is guaranteed to satisfy start <= mid <= end.

The method proceeds by comparing initial characters of start and end. When the characters are equal, they are appended to the mid string. In the first place that the characters differ, the difference characters are averaged and this average is appended to the mid string. If averaging resulted in rounding down, and additional character is added to the mid string to make up for the rounding down. This extra step is necessary for correctness in the case that the average of the two characters is equal to the character in the start string.

This method makes the assumption that most keys are ascii and it attempts to perform splitting within the ascii range when that results in a valid split.

Args
start A string.
end A string such that start <= end.

Returns
A string mid such that start <= mid <= end.

compute_split_points

View source

Computes a set of KeyRanges that are split points for a kind.

Args
kind String with the entity kind to split.
count Number of non-overlapping KeyRanges to generate.

Returns
A list of KeyRange objects that are non-overlapping. At most "count" + 1 KeyRange objects will be returned. At least one will be returned.

filter_datastore_query

View source

Add query filter to restrict to this key range.

Args
query A datastore.Query instance.
filters optional list of filters to apply to the query. Each filter is a tuple: (, , ). User filters are applied first.

Returns
The input query restricted to this key range.

filter_ndb_query

View source

Add query filter to restrict to this key range.

Args
query An ndb.Query instance.
filters optional list of filters to apply to the query. Each filter is a tuple: (, , ). User filters are applied first.

Returns
The input query restricted to this key range.

filter_query

View source

Add query filter to restrict to this key range.

Args
query A db.Query or ndb.Query instance.
filters optional list of filters to apply to the query. Each filter is a tuple: (, , ). User filters are applied first.

Returns
The input query restricted to this key range.

from_json

View source

Deserialize KeyRange from its json representation.

Args
json_str string with json representation created by key_range_to_json.

Returns
deserialized KeyRange instance.

guess_end_key

View source

Guess the end of a key range with a binary search of probe queries.

When the 'key_start' parameter has a key hierarchy, this function will only determine the key range for keys in a similar hierarchy. That means if the keys are in the form:

kind=Foo, name=bar/kind=Stuff, name=meep

only this range will be probed:

kind=Foo, name=/kind=Stuff, name=

That means other entities of kind 'Stuff' that are children of another parent entity kind will be skipped:

kind=Other, name=cookie/kind=Stuff, name=meep

Args
key_start The starting key of the search range. In most cases this should be id = 0 or name = ''. May be db.Key or ndb.Key.
kind String name of the entity kind.
probe_count Optional, how many probe queries to run.
split_rate Exponential rate to use for splitting the range on the way down from the full key space. For smaller ranges this should be higher so more of the keyspace is skipped on initial descent.

Returns
db.Key that is guaranteed to be as high or higher than the highest key existing for this Kind. Doing a query between 'key_start' and this returned Key (inclusive) will contain all entities of this Kind.

NOTE: Even though an ndb.Key instance is accepted as argument, the return value is always a db.Key instance.

make_ascending_datastore_query

View source

Construct a query for this key range without setting the scan direction.

Args
kind A string.
keys_only bool, default False, use keys_only on Query?
filters optional list of filters to apply to the query. Each filter is a tuple: (, , ). User filters are applied first.

Returns
A datastore.Query instance.

make_ascending_ndb_query

View source

Construct an NDB query for this key range, without the scan direction.

Args
kind_class An ndb.Model subclass.
keys_only bool, default False, query only for keys.

Returns
An ndb.Query instance.

make_ascending_query

View source

Construct a query for this key range without setting the scan direction.

Args
kind_class A kind implementation class (a subclass of either db.Model or ndb.Model).
keys_only bool, default False, query only for keys.
filters optional list of filters to apply to the query. Each filter is a tuple: (, , ). User filters are applied first.

Returns
A db.Query or ndb.Query instance (corresponding to kind_class).

make_directed_datastore_query

View source

Construct a query for this key range, including the scan direction.

Args
kind A string.
keys_only bool, default False, use keys_only on Query?

Returns
A datastore.Query instance.

Raises
KeyRangeError if self.direction is not in (KeyRange.ASC, KeyRange.DESC).

make_directed_ndb_query

View source

Construct an NDB query for this key range, including the scan direction.

Args
kind_class An ndb.Model subclass.
keys_only bool, default False, use keys_only on Query?

Returns
An ndb.Query instance.

Raises
KeyRangeError if self.direction is not in (KeyRange.ASC, KeyRange.DESC).

make_directed_query

View source

Construct a query for this key range, including the scan direction.

Args
kind_class A kind implementation class (a subclass of either db.Model or ndb.Model).
keys_only bool, default False, use keys_only on Query?

Returns
A db.Query or ndb.Query instance (corresponding to kind_class).

Raises
KeyRangeError if self.direction is not in (KeyRange.ASC, KeyRange.DESC).

split_keys

View source

Return a key that is between key_start and key_end inclusive.

This method compares components of the ancestor paths of key_start and key_end. The first place in the path that differs is approximately split in half. If the kind components differ, a new non-existent kind halfway between the two is used to split the space. If the id_or_name components differ, then a new id_or_name that is halfway between the two is selected. If the lower id_or_name is numeric and the upper id_or_name is a string, then the minumum string key u'' is used as the split id_or_name. The key that is returned is the shared portion of the ancestor path followed by the generated split component.

Args
key_start A db.Key or ndb.Key instance for the lower end of a range.
key_end A db.Key or ndb.Key instance for the upper end of a range.
batch_size The maximum size of a range that should not be split.

Returns
A db.Key instance, k, such that key_start <= k <= key_end.

NOTE: Even though ndb.Key instances are accepted as arguments, the return value is always a db.Key instance.

split_range

View source

Split this key range into a list of at most two ranges.

This method attempts to split the key range approximately in half. Numeric ranges are split in the middle into two equal ranges and string ranges are split lexicographically in the middle. If the key range is smaller than batch_size it is left unsplit.

Note that splitting is done without knowledge of the distribution of actual entities in the key range, so there is no guarantee (nor any particular reason to believe) that the entities of the range are evenly split.

Args
batch_size The maximum size of a key range that should not be split.

Returns
A list of one or two key ranges covering the same space as this range.

to_json

View source

Serialize KeyRange to json.

Returns
string with KeyRange json representation.

__eq__

View source

Return self==value.

__ge__

View source

Return self>=value.

__gt__

View source

Return self>value.

__le__

View source

Return self<=value.

__lt__

View source

Return self<value.

__ne__

View source

Return self!=value.

ASC 'ASC'
DESC 'DESC'