How to distribute id?

I want my identifiers to be <999999999, but now, trying to program that it looks like the opposite effect, I only increase the identifiers, and when I try to highlight the start> end identifiers?

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) 

The output of my log shows that the identifier that gets allocated is incorrect:

 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 that I end up using is creating my own identifier system that I shouldn't do, but this is the only solution in this case, and I don't think it will create conflicts or duplicates, it can be slow if the objects begin to fill up, but at the moment it seems like a solution that works acceptable for the user, although it may not seem very good if you look 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() 
+4
source share
1 answer

As explained in the NDB documentation : allocateIds(max=) will return the first available id if you try to reserve identifiers already allocated.

In your case, all identifiers up to 999999999 have already been allocated earlier (perhaps by other calls to allocate_ids ), 98765439124 is the first available identifier, 98765439123 is the last one that was allocated.

See the following example:

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

Select all identifiers up to 26740080011040

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

All identifiers up to 26740080011040 have already been allocated, the first identifiers available are 26740080011041, the last one allocated is 26740080011040

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

Select all identifiers up to 26740080011050

+5
source

Source: https://habr.com/ru/post/1396126/


All Articles