Firebase推送唯一性范围 [英] Firebase push uniqueness scope

查看:149
本文介绍了Firebase推送唯一性范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说,Firebase推送生成一个唯一的密钥,我想知道这个密钥在我的整个数据库(甚至是一个GuId)中是否是唯一的,或者只是在它被推送到的节点上。$ b $
$ b

 $ b $ p $ 用户{
-KIH-uFo_2jW16Ue8JNH {//用户键
用户名:Johnny Dummy
性别:male
}
}
项目{
-KIH-uFo_2jW16Ue8JNH {//用户键
-KIH6iaw5uAS856i6-u9:{// Item-Key
itemname:Item Dummy
count:3





$ p
$ b $ p $所以Item-Key的唯一性范围当我获取所有用户的所有项目时,对我来说很重要。解决方案

在统计学上几乎不可能得到重复的push-id。发生这种情况:


  1. 需要在客户端生成的时间戳中,生成id的前48位,相同毫秒

  2. 接下来的72个随机产生的位需要相同。所以你可以有重复的推式ID,但有2 ^ 72个可能的随机排列(超过4次,这个数字是世界上所有沙滩上估计的沙子的4倍)。因此,您需要在相同的毫秒内执行大量的推送请求。

核心开发人员之一写了一篇博客文章,描述他们是如何生成: https://firebase.googleblog.com /2015/02/the-2120-ways-to-ensure-unique_68.html



推送ID包含120位信息,前48位比特是一个时间戳,既减少了碰撞的机会,又允许连续创建的推送ID按时间顺序排序,时间戳之后是72比特的随机性,这确保了甚至两个人在相同的毫秒中创建推送ID是极不可能的生成相同的ID,一个需要注意的是,为了保持时间顺序,如果一个客户端在同一个毫秒内创建多个推送ID,我们只需要将这个随机位增加一次。

The docs say Firebase push generates a unique key, i want to know if this key is unique across my whole database (or even a GuId), or just in the node where it was pushed on

I am structuring my database like this ( I flattened it down, according to their guide ):

users {
    -KIH-uFo_2jW16Ue8JNH { //User-Key
       username : "Johnny Dummy"
       gender : "male"
    }
}
items {
    -KIH-uFo_2jW16Ue8JNH { //User-Key
        -KIH6iaw5uAS856i6-u9 : { //Item-Key
            itemname : "Item Dummy"
            count : 3
        }
    }
}

So the uniqueness scope of Item-Key is important to me when i fetch all Items of all users

解决方案

It is statistically nearly impossible to get duplicate push-Ids. For it to happen:

  1. The first 48 bit piece of the id, a time stamp generated by the client, would need to be generated in the same millisecond
  2. The next 72 randomly generated bits would need to be the same. So you could have duplicate push ids, but there are 2^72 possible random permutations ( over 4 sextillion which is 4 times the estimated grains of sand on all the beaches in the world ). So you would need a lot of push requests in the same millisecond.

One of the core developers wrote a blog post describing how they are generated: https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

"A push ID contains 120 bits of information. The first 48 bits are a timestamp, which both reduces the chance of collision and allows consecutively created push IDs to sort chronologically. The timestamp is followed by 72 bits of randomness, which ensures that even two people creating push IDs at the exact same millisecond are extremely unlikely to generate identical IDs. One caveat to the randomness is that in order to preserve chronological ordering if a client creates multiple push IDs in the same millisecond, we just 'increment' the random bits by one."

这篇关于Firebase推送唯一性范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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