Class: Teakflake::Id

Inherits:
Object
  • Object
show all
Defined in:
lib/teakflake/id.rb

Constant Summary collapse

EPOCH =
1288834974657
SEQUENCE_BITS =
12
WORKER_ID_BITS =
5
DATACENTER_ID_BITS =
5
MAX_WORKER_ID =
2 ** WORKER_ID_BITS
MAX_DATACENTER_ID =
2 ** DATACENTER_ID_BITS
MAX_SEQUENCE =
2 ** SEQUENCE_BITS
WORKER_ID_SHIFT =
SEQUENCE_BITS
DATACENTER_ID_SHIFT =
WORKER_ID_SHIFT + WORKER_ID_BITS
TIMESTAMP_SHIFT =
DATACENTER_ID_SHIFT + DATACENTER_ID_BITS
SEQUENCE_MASK =
-1 ^ (-1 << SEQUENCE_BITS)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Id

Returns a new instance of Id.



45
46
47
48
49
50
51
# File 'lib/teakflake/id.rb', line 45

def initialize(id)
  @id = id
  @timestamp = (id >> TIMESTAMP_SHIFT) + EPOCH
  @datacenter_id = id >> DATACENTER_ID_SHIFT & (-1 ^ (-1 << DATACENTER_ID_BITS))
  @worker_id = id >> WORKER_ID_SHIFT & (-1 ^ (-1 << WORKER_ID_BITS))
  @sequence = id & SEQUENCE_MASK
end

Instance Attribute Details

#datacenter_idObject (readonly)

Returns the value of attribute datacenter_id.



34
35
36
# File 'lib/teakflake/id.rb', line 34

def datacenter_id
  @datacenter_id
end

#idObject (readonly)

Returns the value of attribute id.



34
35
36
# File 'lib/teakflake/id.rb', line 34

def id
  @id
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



34
35
36
# File 'lib/teakflake/id.rb', line 34

def sequence
  @sequence
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



34
35
36
# File 'lib/teakflake/id.rb', line 34

def timestamp
  @timestamp
end

#worker_idObject (readonly)

Returns the value of attribute worker_id.



34
35
36
# File 'lib/teakflake/id.rb', line 34

def worker_id
  @worker_id
end

Class Method Details

.from_parts(timestamp, datacenter_id, worker_id, sequence) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/teakflake/id.rb', line 36

def self.from_parts(timestamp, datacenter_id, worker_id, sequence)
  new(
    timestamp << TIMESTAMP_SHIFT |
    datacenter_id << DATACENTER_ID_SHIFT |
    worker_id << WORKER_ID_SHIFT |
    sequence
  )
end