如何正确分配_ids?

发布于 2025-01-05 02:36:34 字数 954 浏览 3 评论 0原文

我希望我的 id 是 < 999999999 但现在尝试编程它似乎具有相反的效果,我只是让 ids 变大,当我尝试分配 ids start > 时结尾 ?

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)

我的日志输出显示分配的 ID 是错误的:

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

我该如何解决这个问题?

更新

我最终使用的肮脏的解决方法是制作一个自己的 ID 系统,我不应该这样做,但这是这种情况下唯一的解决方案,我不认为这会产生冲突或重复,如果实体启动,它可能会很慢填充,但就目前而言,这似乎是一个对用户来说可以接受的解决方案,尽管从代码来看可能看起来不太好:

    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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2025-01-12 02:36:34

正如 NDB 文档中所述: allocateIds(max=) 将返回第一个可用的 id,以防您尝试保留已分配的 ID。

在您的情况下,之前已经分配了 999999999 之前的所有 id(可能是通过对 allocate_ids 的其他调用),98765439124 是第一个可用的 id,98765439123 是最后一个已分配的。

请参阅以下示例:

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

分配 26740080011040 以内的所有 id

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

26740080011040 以内的所有 id 均已分配,第一个可用的 id 为 26740080011041,最后分配的为 26740080011040

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

分配全部id 最多26740080011050

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文