2016年12月27日 星期二

[tech_note] firebase push id Unique Identifiers method guid 產生方法


The Firebase Blog: The 2^120 Ways to Ensure Unique Identifiers https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html

昨天剛好看到一篇 firebase 提到,怎麼產生不重複的 guid的方式。


What's in a Push ID?

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.
To turn our 120 bits of information (timestamp + randomness) into an ID that can be used as a Firebase key, we basically base64 encode it into ASCII characters, but we use a modified base64 alphabet that ensures the IDs will still sort correctly when ordered lexicographically (since Firebase keys are ordered lexicographically).

使用 timestamp + randomness 在使用 base64 encode 而成,實作上,只要是產生的時候, timestamp 還沒改變,就把最後一個 bit +1 。 
這些gits 是個版本的實作方式。
JavaScript code for generating Firebase Push IDs https://gist.github.com/mikelehen/3596a30bd69384624c11
Firebase push algorithm in Python (ref: https://www.firebase.com/blog/2015-02-11-firebase-unique-identifiers.html) https://gist.github.com/risent/4cab3878d995bec7d1c2


ref
[tech_note] hackpad 的 unique padid 產生方式 / etherpad 與 appjet - peicheng note
http://peichengnote.blogspot.tw/2015/10/technote-hackpad-unique-padid-etherpad.html
Parse.com ObjectId 產生方法 unique id - peicheng note http://peichengnote.blogspot.tw/2016/01/parsecom-objectid-unique-id.html

沒有留言:

張貼留言