flexid v0.1.1 FlexId
Generator for distributed time-based ids. Ids are composed of 4 value:
- time in ms
- rolling sequence counter
- partition used to segratate ids; this could be hard-coded by node or geo-region or be a hash to shard data by user
- Luhn mod 16 checksum to check for incorrectly typed ids
The number of bits for each value is flexible and can be tuned to your requirements.
Summary
Functions
Adds a Luhn mod 16 checksum to the input
Returns the checksum component of an id
Returns the millisecond component of the id, adjusted for epoch
Returns the checksum component of an id
Returns the millisecond component of the id, without adding the epoch
Returns the sequence component of the id
Generate an Id value
Used by the agent to get the next id and next agent state
Logs information about the agent state
Helper for generating a 16-bit partition value from the last two bytes of the sha1 hash of a text value. Use this to generate a shard value based on the data passed in
Start an agent that can be used generate IDs with the given parameters
Used by the agent to construct the initial state
Returns true if the value has a valid checksum
Functions
Returns the checksum component of an id.
This is useful for copying shard values from parent to child.
Returns the millisecond component of the id, without adding the epoch.
Generate an Id value.
Examples
iex> {:ok, fid} = FlexId.start_link
value = FlexId.generate(fid, 0xB1)
Used by the agent to get the next id and next agent state.
This is not meant to be called directly.
Helper for generating a 16-bit partition value from the last two bytes of the sha1 hash of a text value. Use this to generate a shard value based on the data passed in.
Examples
partition = FlexId.make_partition(username)
Start an agent that can be used generate IDs with the given parameters.
Parameters
- epoch: the epoch to start the ids at in ms; defaults to
946_684_800_000
(2000-01-01 0:00 UTC) - sequence_bits: how many bits should be used for sub-millisecond precision; defaults to 6 for 64 ids/ms
- partition_bits: how many bits should be used to identify partitions in the ids; defaults to 6 for 64 partitions
- checksum_bits: how many bits should be used for checksum to detect invalid ids; must be 0 or 4, defaults to 4
Usage
The default parameters have the following characteristics
- a time range of 2231 years, from 2000 to 4229 (afterwhich values become 64-bits and will be negative when using signed integers)
- a theoretical maximum of 64 ids/ms
- 64 partitions (shards)
- a 4-bit checksum to catch errors should ids need to be typed in manually
Examples
iex> {:ok, fid} = FlexId.start_link()
id = FlexId.generate(fid, FlexId.make_partition("test"))
Used by the agent to construct the initial state.
This is not meant to be called directly.