Module: TwitterSnowflake
- Defined in:
- lib/twitter_snowflake.rb,
lib/twitter_snowflake/version.rb,
lib/twitter_snowflake/snowflake.rb
Overview
TwitterSnowflake is a library to parse snowflake IDs.
Defined Under Namespace
Classes: Snowflake
Constant Summary collapse
- TWITTER_EPOCH =
Twitter’s epoch in milliseconds. Used as default epoch to parse IDs.
1_288_834_974_657
- TIMESTAMP_BINARY_OFFSET =
Timestamp offset (low-order bit of the timestamp collection)
22
- WORKER_ID_BINARY_OFFSET =
Worker ID offset (low-order bit of the worker ID collection)
17
- WORKER_ID_MASK =
Mask to extract worker ID.
0x3E000
- PROCESS_ID_BINARY_OFFSET =
Process ID offset (low-order bit of the process ID collection)
12
- PROCESS_ID_MASK =
Mask to extract process ID.
0x1F000
- INCREMENT_MASK =
Mask to extract increment.
0xFFF
- VERSION =
'0.0.2'
Class Method Summary collapse
-
.increment(id) ⇒ Integer
Extracts the increment from a snowflake ID.
-
.parse(id, epoch: TWITTER_EPOCH) ⇒ Snowflake
Parses a snowflake ID.
-
.process_id(id) ⇒ Integer
Extracts the process ID from a snowflake ID.
-
.synthesize(timestamp:, worker_id: 0, process_id: 0, increment: 0, epoch: TWITTER_EPOCH) ⇒ Snowflake
Creates a snowflake object for any given timestamp, worker ID, process ID and increment.
-
.timestamp(id, epoch: TWITTER_EPOCH) ⇒ Integer
Extracts the timestamp from a snowflake ID.
-
.worker_id(id) ⇒ Integer
Extracts the worker ID from a snowflake ID.
Class Method Details
.increment(id) ⇒ Integer
Extracts the increment from a snowflake ID.
88 89 90 |
# File 'lib/twitter_snowflake.rb', line 88 def increment(id) id & INCREMENT_MASK end |
.parse(id, epoch: TWITTER_EPOCH) ⇒ Snowflake
Parses a snowflake ID.
51 52 53 |
# File 'lib/twitter_snowflake.rb', line 51 def parse(id, epoch: TWITTER_EPOCH) Snowflake.new(id: id, epoch: epoch) end |
.process_id(id) ⇒ Integer
Extracts the process ID from a snowflake ID.
79 80 81 |
# File 'lib/twitter_snowflake.rb', line 79 def process_id(id) (id & PROCESS_ID_MASK) >> PROCESS_ID_BINARY_OFFSET end |
.synthesize(timestamp:, worker_id: 0, process_id: 0, increment: 0, epoch: TWITTER_EPOCH) ⇒ Snowflake
Creates a snowflake object for any given timestamp, worker ID, process ID and increment.
36 37 38 39 40 41 42 43 |
# File 'lib/twitter_snowflake.rb', line 36 def synthesize(timestamp:, worker_id: 0, process_id: 0, increment: 0, epoch: TWITTER_EPOCH) id = ( - epoch) << TIMESTAMP_BINARY_OFFSET id += (worker_id << WORKER_ID_BINARY_OFFSET) id += (process_id << PROCESS_ID_BINARY_OFFSET) id += increment Snowflake.new(id: id, epoch: epoch) end |
.timestamp(id, epoch: TWITTER_EPOCH) ⇒ Integer
Extracts the timestamp from a snowflake ID.
61 62 63 |
# File 'lib/twitter_snowflake.rb', line 61 def (id, epoch: TWITTER_EPOCH) (id >> TIMESTAMP_BINARY_OFFSET) + epoch end |
.worker_id(id) ⇒ Integer
Extracts the worker ID from a snowflake ID.
70 71 72 |
# File 'lib/twitter_snowflake.rb', line 70 def worker_id(id) (id & WORKER_ID_MASK) >> WORKER_ID_BINARY_OFFSET end |