我可以使用AllocateID作为“字符串”吗? ?数据存储 [英] Can I use AllocateIDs as "string" ? datastore
问题描述
我需要为数据存储区中的每个实体分配一个唯一的标识符。我发现 AllocateID ,但问题在于它会生成整数,而我使用字符串作为键。将整数转换为字符串还是存在冲突风险(即数据存储返回与当前使用的字符串键相同的整数)是安全的。 ?
让我们先清楚一些事情:
identifier 实体密钥的一部分可以be
- a 键名字符串
- 或整数数字ID
但不是两种。所以当你保存一个实体时,它的关键字有一个字符串
id,称为 name OR(异或)和一个 int64
id
2个可选标识符字段是不同的:如果您有一个实体 name =1234
它与
当您在未明确指定名称
或 intID
的情况下保存新实体时,数据存储将分配新的唯一 intID
标识符。数据存储区永远不会自行分配字符串
名称
。如果您指定字符串$ c>,则只能包含
名称
标识符的实体
数据存储知道自动生成的 intID code> s会自动生成,并且永远不会生成两次( well behavior )相同的
intID
。但是,如果您自己指定 intID
,则在保存新实体时,必须注意它是唯一的。这需要首先检查你想使用的 intID
是否还没有被使用(例如先查询它是否没有实体具有 intID
),但即使这样也不能100%保证当你最终实际保存一个实体时,它仍然没有被使用。 AllocateIDs()
函数可用于获取 intID
s的连续范围,数据存储不会用它来生成 intID
以后,所以你可以安全地自由使用 intID
s的分配范围。这也意味着,如果有并发请求也试图保存新实体(或者在同一实例中或者在其他实例中),它们也将永远不会使用这些 intID
s如果标识符生成留给数据存储。
回到你的问题
你真的需要手动标识符分配?在大多数情况下,只有在您已经具有实体的独特属性时才会使用/需要这些属性,而这些属性对于两个不同的实体(例如实体 Person
)不能相同有一个属性 IdentityCardId
,这对每个人来说都是独一无二的)。
如果你有这样一个独特的属性,可以使用本身确保唯一性的那种。如果你没有这样的属性,那么你不应该首先使用手动标识符赋值,你可以使用/依靠自动 intID
赋值数据存储。
请注意,您可以使用同一类型的不同实体的混合标识符(例如,您可以使用 Person
$ c> intID 和另一个 Person
,其中 name
)。
如上所述, intID
s和 name
s是不同的。因此, AllocateIDs()
不会将名称
标识符的实体考虑在内,因为名称
包含一个有效的数字。从数据存储中,您无法获得帮助,将名称
标识符(类似于 AllocateIDs()
)分配给分配 intID
s),所以它必须是应用程序逻辑,以确保分配的名称
是唯一的,否则您将以覆盖/替换现有的实体。
I need to assign a unique identifier to each entity in datastore. I've found AllocateIDs but the issue is that it generates integers and I use strings for the keys. Is it safe to convert the integer to a string or there is a risk of collision (i.e. datastore to return me the same integer that I'm currently using as a string key). ?
Let's clear some things first:
The identifier part of an entity's key can either be
- a key name string
- or an integer numeric ID
But not both. So when you save an entity, its key either has a string
id called name OR (exclusive OR) an int64
id called intID.
The 2 optional identifier fields are distinct: if you have an entity with name="1234"
it is different from that of with intID=1234
.
When you save a new entity without explicitly specifying a name
or intID
, the datastore will assign a new unique intID
identifier to it. The datastore will never assign a string
name
by itself. You can only have entities with string
name
identifiers if you specify a string
name
yourself.
The datastore knows about the automatically generated intID
s it generates itself, and will never generate the same intID
twice (well behavior). However when saving a new entity if you specify an intID
yourself, you have to take care about it being unique. This would require to first check if the intID
you whish to use is not yet in use (e.g. by querying it first to see if no entity has that intID
yet) but even this would not be 100% guarantee that by the time you end up actually saving an entity with this will still be unused. The AllocateIDs()
function can be used to obtain a continuous range of intID
s which the datastore will not use to generate intID
s by itself later on, so you are free to use the allocated range of intID
s safely. This also means that if there are concurrent requests also trying to save new entities (either in the same instance or in other instances), they will also never end up using these intID
s if identifier generation is left to the datastore.
Back to you question
Do you really need manual identifier assignment? In most of the cases this is only used/required if you already have a unique property of the entity which cannot be the same for 2 different entities (for example entity is Person
which has a property IdentityCardId
which is already unique to each person).
If you have such a unique property, you may use that which itself by nature ensures uniqueness. If you have no such property, then you should not use manual identifier assignment in the first place, you can just use/rely on automatic intID
assignment of the datastore.
Note that you can have mixed identifiers of different entities of the same kind (e.g. you can have a Person
with intID
and another Person
with name
).
As noted above, intID
s and name
s are distinct. So AllocateIDs()
does not take entities with name
identifiers into account just because the name
contains a valid number. From the datastore you don't get help to "allocate" a name
identifier (analog to the AllocateIDs()
to allocate intID
s), so it must be application logic to ensure the assigned name
is unique else you will end up "overwriting"/replacing an existing entity.
这篇关于我可以使用AllocateID作为“字符串”吗? ?数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!