Open purrplingcat opened 6 years ago
changed the spec to accomodate more random bits:
63 22 10 0
1 0000001001100101001011111010011101010101 101010101110 0000000001
| | | |
\_ Prefix \_ Date NOW - EPOCH \_ Random \_ Increment
which makes ie full uint64 with Date field being in miliseconds rather than whole seconds and Random being 12bits rather than only 4 bits. The rest remains.
Generate device name
Device name is a snowflake encoded to base62 with pattern
[A-z0-9]
with lenght 11 characters. In background it's 64 bit number. Device name is generated and assignet to device on REST server.Snowflake
1
(bin1
)0
and16
(bin0000
-1111
)1
Epoch
Epoch is a refererence date and time with value 2017-01-01 00:00 UTC. This constant is subtracted from the current date and time. The result is a number in ms since Caloriosa epoch
Random number
This is a light random salt. It's is for avoid colision and humanic force next number. Number is in range 0 - 4095
Increment
A incremental number from 0 increments about 1. Increment size is 10 bits - max value:
1023
. If increment reached this value, then overflow and increments starts at 0 again.Prefix
Is a constant number
1
(bin1
). It's for avoid base62 encoder to overflow or underflow 11 chars.Base62 encode
Whole snowflake is encoded by Base62 algorithm. Encoded string has lenght 11 chars.
Example
We have a generated device name as binary snowflake:
Represented as long:
We encode this number with Base62 and we gets:
And this string is a final device name
Related: #6 Following: Source: