生成用于IndexedDB密钥的UUID? [英] Generating UUIDs for IndexedDB keys?

查看:76
本文介绍了生成用于IndexedDB密钥的UUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IndexedDB的W3C规范将密钥生成器定义为:

The W3C spec for IndexedDB defines a key generator as:

每次需要密钥时,密钥生成器都会生成单调递增的数字[sic].

A key generator generates a monotonically increasing numbers [sic] every time a key is needed.

现在,在我看来,IndexedDB(或就此而言,任何HTML5客户端存储选项:WebSQL,localStorage等)的通用用例将是旨在脱机工作的应用程序(在与HTML5 ApplicationCache结合使用.

Now, it seems (to me) that a common use case for IndexedDB (or, for that matter, any of the HTML5 client-side storage options: WebSQL, localStorage etc.) would be apps designed to work offline (in conjunction with HTML5 ApplicationCache).

在这种情况下,断开连接的 Web应用程序可能会在其本地数据存储中生成新的对象/记录,这些新对象/记录将在与服务器的连接可用时稍后同步到集中式数据库.

In this scenario, a disconnected web application might generate new objects/records in it's local data store that are later synced to a centralised database when a connection to the server becomes available.

此外,任何将多个客户端同步到相同集中式数据库的应用程序,通常都需要一种机制来防止ID冲突.

Further, any application where multiple clients sync to the same centralised database typically requires a mechanism to prevent ID collisions.

UUID(或GUID)是一个不错的选择,因为它可以在无需任何中央协调的情况下生成唯一的密钥.相比之下,单调递增数字" 是一个较差的解决方案(除非每个客户端"都以不大可能与其他用户冲突的起始值播种").

UUID (or GUID) is a good choice, as it enables unique key generation without any central coordination. By contrast, "monotonically incrementing numbers" is a poor solution (unless each client is "seeded" with a starting value that is unlikely to collide with other users).

令人惊讶的是,IndexedDB规范未指定(甚至不允许将来支持)备用密钥生成器,例如UUID生成器.有人可能会提出答案,就是根本不使用IndexedDB的内置密钥生成器,而是让您的应用程序生成它自己的密钥.

I find it surprising that the IndexedDB spec does not specify (or even allow for future support of) alternate key generators, such as a UUID generator. Some may suggest that the answer is simply not to use IndexedDB's builtin key generator at all, instead have your application generate it's own keys.

但是,尽管有很多基于Java的UUID生成器可用,但其中许多似乎都基于Math.random(),它在 randomness 方面具有已知的局限性,因此可能不会如果必须保证绝对唯一的密钥,那么它是一个不错的选择.

However, while there are plenty of Javascript-based UUID generators available, many of them seem to be based on Math.random() which has known limitations in terms of it's randomness, so might not be a good choice if absolute unique keys must be guaranteed.

(可能)由IndexedDB实现者提供的本地UUID生成器比应用程序实现/导入的脚本更健壮,并且性能更好.人们会认为.

A native UUID generator provided by the IndexedDB implementors would (presumably) be more robust and perform better than a script implemented/imported by the application; one would think.

所以我在这里错过了什么吗,或者这是W3C IndexedDB工作组错过的机会吗?

So am I missing something here, or is this a missed opportunity by the W3C IndexedDB working group?

推荐答案

objectStore键对于本地数据库中的objectStore而言是唯一的,除此之外别无其他.关于唯一性的任何更高级别的语义确实超出了IndexedDB的范围.您描述的是UUID,但这仅仅是唯一键的一种形式.生成具有不同属性的UUID的方式有很多,包括将特定前缀与单调递增的低位组合在一起,随机生成整个内容,通过以太网卡地址对其进行作用域确定等.

The objectStore keys are meant to be unique to the objectStore in the local database, and nothing else. Any higher level semantics about uniqueness are really beyond the scope of IndexedDB. You describe UUID, but even that is just one form of unique keys. There are lots of different ways to generate a UUID that have different properties, including a specific prefix combined with monotonically increasing lower-bits, randomly generating the whole thing, scoping it by ethernet card address, etc.

通过选择仅适用于本地实例的内容,IDB(无论好坏)迫使开发人员对全局"唯一密钥进行谨慎选择.

By picking something that is only scoped to the local instance, IDB (for better or worse) forces developers to make a careful choice about a "globally" unique key.

这篇关于生成用于IndexedDB密钥的UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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