我可以使用 AllocateIDs 作为“字符串"吗??数据存储 [英] Can I use AllocateIDs as "string" ? datastore

查看:37
本文介绍了我可以使用 AllocateIDs 作为“字符串"吗??数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为数据存储区中的每个实体分配一个唯一标识符.我找到了 AllocateIDs 但问题是它生成整数,我使用字符串作为键.将整数转换为字符串是否安全或存在冲突风险(即数据存储返回我当前用作字符串键的相同整数).?

解决方案

让我们先弄清楚一些事情:

标识符实体键的一部分可以是

  • 一个键名字符串
  • 或整数数字 ID

但不能两者兼而有之.所以当你保存一个实体时,它的键要么有一个名为 namestring id 或(异或)一个 int64 id 名为 intID.

这 2 个可选标识符字段是不同的:如果您有一个 name="1234" 的实体,则它与 intID=1234 的实体不同.

当您在未明确指定nameintID 的情况下保存新实体时,数据存储区会为其分配一个新的唯一intID 标识符.数据存储永远不会自行分配 string name.如果您自己指定 string name,则您只能拥有带有 string name 标识符的实体.

数据存储知道它自己生成的自动生成的 intID,并且永远不会生成相同的 intID 两次(良好行为).但是,如果您自己指定 intID 来保存新实体,则必须注意它的唯一性.这需要首先检查您要使用的 intID 是否尚未使用(例如,首先查询它以查看是否没有实体具有该 intID),但即使这并不能 100% 保证到您最终实际保存带有此实体的实体时仍将未使用.AllocateIDs() 函数可用于获取连续范围的 intIDs,数据存储区稍后将不会使用这些范围自行生成 intIDs,因此您可以自由使用分配的范围intID 安全.这也意味着,如果有并发请求也试图保存新实体(在同一个实例中或在其他实例中),如果标识符生成留给他们,它们也将永远不会使用这些 intIDs数据存储.

回到你的问题

您真的需要手动分配标识符吗?在大多数情况下,这仅在您已经拥有实体的唯一属性时才使用/需要,该属性对于 2 个不同的实体不能相同(例如,实体是 Person,它具有属性 IdentityCardId 已经是每个人唯一的).

如果你有这样一个独特的属性,你可以使用本质上确保唯一性的那个.如果您没有这样的属性,那么您首先不应该使用手动标识符分配,您可以使用/依赖数据存储的自动 intID 分配.

请注意,您可以拥有相同类型的不同实体的混合标识符(例如,您可以拥有一个带有 intIDPerson 和另一个 Personname).

如上所述,intIDs 和 names 是不同的.所以 AllocateIDs() 不会仅仅因为 name 包含有效数字而将带有 name 标识符的实体考虑在内.从数据存储中,您无法获得分配"数据的帮助.一个name标识符(类似于AllocateIDs()来分配intIDs),所以必须是应用逻辑来保证分配的name 是唯一的,否则您最终将覆盖"/替换现有实体.

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 intIDs 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 intIDs which the datastore will not use to generate intIDs by itself later on, so you are free to use the allocated range of intIDs 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 intIDs 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, intIDs and names 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 intIDs), so it must be application logic to ensure the assigned name is unique else you will end up "overwriting"/replacing an existing entity.

这篇关于我可以使用 AllocateIDs 作为“字符串"吗??数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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