'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.'

@staticmethod
def guess_end_key(kind, key_start, probe_count=30, split_rate=5):

    if (ndb is not None):
        if isinstance(key_start, ndb.Key):
            key_start = key_start.to_old_key()
    app = key_start.app()
    namespace = key_start.namespace()
    full_path = key_start.to_path()
    for (index, piece) in enumerate(full_path):
        if ((index % 2) == 0):
            continue
        elif isinstance(piece, basestring):
            full_path[index] = u'\xffff'
        else:
            full_path[index] = ((2 ** 63) - 1)
    key_end = db.Key.from_path(*full_path, **{'_app': app, 'namespace': namespace})
    split_key = key_end
    for i in xrange(probe_count):
        for j in xrange(split_rate):
            split_key = KeyRange.split_keys(key_start, split_key, 1)
        results = datastore.Query(kind, {'__key__    >': split_key}, namespace=namespace, _app=app, keys_only=True).Get(1)
        if results:
            if (results[0].name() and (not key_start.name())):
                return KeyRange.guess_end_key(kind, results[0], (probe_count - 1), split_rate)
            else:
                split_rate = 1
                key_start = results[0]
                split_key = key_end
        else:
            key_end = split_key
    return key_end
