An implementation of Snowflake for Lua. Snowflake is an algorithm which supports ordered, distributed id generation.
$ luarocks-5.4 make rockspec/snowflake-1.0-1.rockspec
local sf = require "snowflake"
sf.init(0x01, 0x01)
local uuid = sf.next_id()
uuid will be a 64-bit number represented as a string that is structured as follows:
6 6 5 4 3 2 1
3210987654321098765432109876543210987654321098765432109876543210
tttttttttttttttttttttttttttttttttttttttttdddddnnnnnsssssssssssss
where
sis a 12-bit integer that increments when next_id() is called multiple times for the same millisecondnis a 5-bit integer representing the node within a given data centerdis a 5-bit integer representing a unique data center or group of serverstis a 42-bit integer representing the current timestamp in milliseconds- the number of milliseconds to have elapsed since 1413817200000 or 2014-10-20T15:00:00.000Z
sf.init(datacenter_id, node_id) is used to initialize snowflake and set values for ddddd and nnnnn as follows:
datacenter_idis an integer n, where 0 ≤ n ≤ 0x1f and specifies thedddddportion of the idnode_idis an integer n, where 0 ≤ n ≤ 0x1f and specifies thennnnnportion of the id