Google Cloud Datastore唯一的自动生成的ID [英] Google Cloud Datastore unique autogenerated ids

查看:97
本文介绍了Google Cloud Datastore唯一的自动生成的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Cloud Datastore,并使用名称空间对数据进行分区.某些类型通过创建像这样的密钥来使用Cloud Datastore中自动生成的ID:

I'm using Google Cloud Datastore and using namespaces to partition data. Some kinds are using autogenerated IDs from Cloud Datastore by creating keys like this:

var key = Datastore.key([
    'example'
]);

此代码将生成一个类型为"example"的密钥,Cloud Datastore会自动为该实体分配一个整数数字ID. (来源: https://cloud.google.com/datastore/docs/concepts /entities#kinds_and_identifiers )

This code will generate a key with kind 'example' and Cloud Datastore automatically assign the entity an integer numeric ID. (Source: https://cloud.google.com/datastore/docs/concepts/entities#kinds_and_identifiers)

但是此唯一" ID仅对于其名称空间是唯一的.我在不同的命名空间中看到了相同的ID.

But this "unique" ID is only unique for its namespace. I have seen the same ID in different namespaces.

所以我的问题是,是否可以告诉Cloud Datastore自动生成的ID对于所有命名空间都必须是唯一的?

So my question is, is it possible to tell Cloud Datastore that autogenerated IDs must be unique for all the namespaces?

也许这个问题没有意义,但我希望在所有数据存储区中都具有唯一的ID(如果可能的话).

Maybe this question has not sense, but I would prefer to have unique IDs in all the datastore (if possible).

我在云中看到了"allocateIds"功能数据存储文档,但是我想知道此函数是否照顾名称空间,因为我已经看到我可以将它们包含在请求中,而且恐怕ID与由ID自动生成的ID相同.云数据存储.

I have seen "allocateIds" function in Cloud Datastore documentation, but I would like to know if this function take care about namespaces or not, because I've seen I can include them in the request and I'm afraid the IDs are the same than the ones autogenerated by Cloud Datastore.

提前谢谢!

推荐答案

否:您不能告诉数据存储区在所有实体组和名称空间之间分配唯一ID.

No: You can not tell Datastore to allocate unique IDs across all entity groups and namespaces.

但是有一个简单的解决方法:如果您相信统计信息并正确植入随机数生成器,那么通常您生成自己的 GUID (用于密钥).

However there is an easy fix: if you believe in statistics and correctly seeded random number generators you be generally better off if you generate your own GUIDs for keys.

如果您不相信统计信息和随机数,则仍然可以生成GUID,并在编写有问题的实体之前通过交易方式验证它在数据存储区中不存在.

If you don't believe in statistics and random numbers you can still generate a GUID and transactionally verify that it doesn't exist in your Datastore before writing the entity in question.

如果您真的很想让Datastore为您分配ID,则可以拨打

If you are truly desperate to have Datastore do id allocation for you it is possible to make a call AllocateIds manually and ask it to allocate an id for a constant key. (For example, ask it to allocate for an arbitrary (but unchanging) key in the default namespace, and it will return you an integer which will be unique to use somewhere else).

这篇关于Google Cloud Datastore唯一的自动生成的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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