保证新实体的数字自动递增ID总是大于现有ID? [英] Is it guaranteed that numeric auto-incremented ID of new entity always bigger than existing IDs?

查看:103
本文介绍了保证新实体的数字自动递增ID总是大于现有ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否保证新实体的自动递增ID总是大于现有ID?



基本上我想定期将后台任务中的实体(例如Comment)转储到所以如果现在有100个实体,我会将它们存储在blob中,并为这个blob创建助手实体,比如

  class BlobRange 
{
long fromId; // Comment.id
long toId; // Comment.id
字符串blobKey;
}

下一次后台任务会发现最大的 BlobRange.toId 并且将获取 > 大于的评论 > BlobRange.toId ,在本例中它将大于100。

。 ID似乎以块分配(请参阅 here )。我亲眼看到ID分配如下:1001,2001,1002,3001,2002等。似乎它们在一个块内连续递增,但有几个块并行使用。



所以你不能依靠这个去检查新的实体。



改为使用查询游标。创建Comment时记录的时间戳记属性(可以是类型 long 的unix时间戳记)。然后使用查询和游标来检测新实体。


Is it guaranteed that auto-incremented ID of new entity always bigger than existing IDs ?

Basically I want to periodically dump entity (e.g. Comment) in background task into big blobs as they get created by customers.

So if there are 100 entities right now I'll store them in blob and create helper entity for this blob like

class BlobRange
{
    long fromId;    // Comment.id
    long toId;      // Comment.id
    String blobKey;
}

Next time background task would find biggest BlobRange.toId and would fetch new chunk of Comment whose id is greater than BlobRange.toId, which in this example would be greater than 100.

解决方案

Afaik, no. IDs seem to be allocated in blocks (see here). I've personally seen IDs allocated something like this: 1001, 2001, 1002, 3001, 2002, etc.. It seems that they are incremented contiguously within a block, but there are several blocks used in parallel.

So you can not rely on this to check for new entities.

Instead use Query Cursors for this. Create a timestamp property (can be a unix timestamp of type long) on Comment that records when Comment was created. Then use query and cursors to detect new entities.

这篇关于保证新实体的数字自动递增ID总是大于现有ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆