Search code examples
pythongoogle-app-enginegoogle-cloud-datastorepython-2.7webapp2

How to allocate_ids correctly?


I want my ids to be < 999999999 but now trying to program that it seems to have the opposite effect, I'm only getting ids larger and when I try to allocate ids start > end ?

start, end = User.allocate_ids(max=999999999)
logging.info('start %d' % start)
logging.info('end %d' % end)
lower = start if start < end else end
key = User(id=lower).put()
logging.info('key: '+str(key))
user = key.get()
user.add_auth_id(email)

My log output show that the ID that gets allocated is wrong:

2012-02-13 03:19:07.396 start 98765439124
I 2012-02-13 03:19:07.396 end 98765439123

How can I fix this?

Update

The dirty workaround I end up using is making sort of an own ID system which I should not do but it was the only solution in this case and I do not think this will create conflicts or duplicates, it just might be slow if entities start filling up, but for now this seems like a solution that works acceptably towards the user though it might not look that good looking at the code:

    new_id = random.randint(1,999999999)
    logging.info('testing new id: %d' % new_id)
    while User.get_by_id(new_id) != None:
        new_id = random.randint(1,999999999)
    logging.info('creating new id: %d' % new_id)
    key = User(id=new_id).put()

Solution

  • As explained in NDB documentation: allocateIds(max=) will returns the first id that is available, in case you try to reserve IDs that have already been allocated.

    In your case all ids up to 999999999 already have been allocated before (maybe by other calls to allocate_ids), 98765439124 is the first id that is available, 98765439123 is the last one that have been allocated.

    See the following example:

    >>> Foo.allocate_ids(max=26740080011040)
    (26740080011031L, 26740080011040L)
    

    Allocate all ids up to 26740080011040

    >>> Foo.allocate_ids(max=26740080011040)
    (26740080011041L, 26740080011040L)
    

    All ids up to 26740080011040 already have been allocated, first available ids is 26740080011041, last allocated is 26740080011040

    >>> Foo.allocate_ids(max=26740080011050)
    (26740080011041L, 26740080011050L)
    

    Allocate all ids up to 26740080011050