Caloriosa / caloriosa

Documentation, Wiki, Analysis, issues and reports for Caloriosa
https://caloriosa.com
0 stars 0 forks source link

Generate device name #13

Open purrplingcat opened 6 years ago

purrplingcat commented 6 years ago

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

64 63                                         22            10        0
 1  00000010011001101001011111010011101010101  101010101110  0000000001                       
 |              |                                    |            |
 \_ Prefix      \_ Date NOW - EPOCH                  \_ Random    \_ Increment

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 (bin 1). 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:

010000001001100101001011111010011111100000000001

Represented as long:

71027147798529

We encode this number with Base62 and we gets:

katjjMQN

And this string is a final device name


Related: #6 Following: Source:

CallMeFoxie commented 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.