Module: Uniqid::ClassMethods

Defined in:
lib/uniqid.rb

Constant Summary collapse

TOTAL_LEN =

64 bits

64
TIMESTAMP_LEN =

40 bits for time in milliseconds

40
TIMESTAMP_START =

Start timestamp, 2018-01-01 00:00:00

1_514_736_000_000
MAX_TIMESTAMP =
-1 ^ (-1 << TIMESTAMP_LEN)
WORKER_ID_LEN =

9 bits for worker ID, (0-511)

9
MAX_WORKER_NUM =

The maximum number of workers supported, -1:complement0b111111111

-1 ^ (-1 << WORKER_ID_LEN)
SERVER_ID_LEN =

6 bits for server ID, (0-63)

6
MAX_SERVER_NUM =

The maximum number of servers supported, result is 63

-1 ^ (-1 << SERVER_ID_LEN)
BAK_ID_LEN =

2 bits reserved, (0-3)

2
MAX_BAK_NUM =
-1 ^ (-1 << BAK_ID_LEN)
LOCAL_ID_LEN =

7 bits for serial number in milliseconds, (0-127)

7
MAX_LOCAL_NUM =
-1 ^ (-1 << LOCAL_ID_LEN)

Instance Method Summary collapse

Instance Method Details

#generate(worker_value, server_value, timestamp = nil) ⇒ Object

Generate ID



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/uniqid.rb', line 88

def generate(worker_value, server_value, timestamp = nil)
  server_value = server_value(server_value)
  worker_value = worker_value(worker_value)

  # The reserved position is temporarily random, shifted by 7 bits to the left
  bak_value = (rand(MAX_BAK_NUM) << LOCAL_ID_LEN)

  local_timestamp =
    if timestamp
      (timestamp * 1000).to_i
    else
      (Time.now.to_f * 1000).to_i
    end

  # If the last generation time is the same as the current time, the sequence within milliseconds
  if local_timestamp == @last_timestamp

    # The sequence is self-increasing and only has 7 bits,
    # so it is ANDed with MAX_LOCAL_NUM and removes the high bits
    sequence = (@sequence + 1) & MAX_LOCAL_NUM

    # Check for overflow: whether the sequence exceeds 127 per millisecond,
    # when 127, it is equal to 0 after AND with MAX_LOCAL_NUM
    if sequence.zero?
      # Wait until the next millisecond
      local_timestamp = next_timestamp(@last_timestamp)
    end

  else
    # If it is different from the last generation time, reset the sequence
    # In order to ensure that the mantissa is more random, set a random number in the last digit
    @sequence = rand(1 << LOCAL_ID_LEN)
    sequence = @sequence
  end

  @last_timestamp = local_timestamp

  # Save the difference of timestamp(current timestamp - start timestamp)
  local_timestamp -= TIMESTAMP_START

  (local_timestamp << (TOTAL_LEN - TIMESTAMP_LEN)) | server_value | worker_value | bak_value | sequence
end

#get_timestamp(id) ⇒ Object

Reverse check timestamp



132
133
134
135
# File 'lib/uniqid.rb', line 132

def get_timestamp(id)
  timestamp = (id >> (TOTAL_LEN - TIMESTAMP_LEN)) / 1000.0
  Time.at(timestamp + TIMESTAMP_START / 1000.0)
end

#next_timestamp(last_timestamp) ⇒ Object

Prevent the generation time is smaller than the previous time(due to issues such as NTP callback), and keep the incremental trend.



56
57
58
59
60
# File 'lib/uniqid.rb', line 56

def next_timestamp(last_timestamp)
  timestamp = (Time.now.to_f * 1000).to_i
  timestamp = (Time.now.to_f * 1000).to_i while timestamp <= last_timestamp
  timestamp
end

#server_value(value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/uniqid.rb', line 70

def server_value(value)
  left_num = BAK_ID_LEN + LOCAL_ID_LEN

  value = 0 if value.to_i > MAX_SERVER_NUM || value.to_i.negative?

  unless value.present?
    # In development mode, take the random number of the largest supported number
    if Rails.env == 'development'
      rand(MAX_SERVER_NUM) << left_num
    else
      value = 0
    end
  end

  value.to_i << left_num
end

#worker_value(value) ⇒ Object



63
64
65
66
67
68
# File 'lib/uniqid.rb', line 63

def worker_value(value)
  # Handling parameter exception
  value = 0 if value.to_i > MAX_WORKER_NUM || value.to_i.negative?

  value.to_i << SERVER_ID_LEN + BAK_ID_LEN + LOCAL_ID_LEN # 15 bits left
end