class NATS::NUID
Constants
- BASE
- DIGITS
- INC
- MAX_INC
- MAX_SEQ
- MIN_INC
- PREFIX_LENGTH
- SEQ_LENGTH
- TOTAL_LENGTH
Public Class Methods
new()
click to toggle source
# File lib/nats/nuid.rb, line 28 def initialize @prand = Random.new @seq = @prand.rand(MAX_SEQ) @inc = MIN_INC + @prand.rand(INC) @prefix = '' randomize_prefix! end
Public Instance Methods
next()
click to toggle source
# File lib/nats/nuid.rb, line 36 def next @seq += @inc if @seq >= MAX_SEQ randomize_prefix! reset_sequential! end l = @seq # Do this inline 10 times to avoid even more extra allocs, # then use string interpolation of everything which works # faster for doing concat. s_10 = DIGITS[l % BASE]; # Ugly, but parallel assignment is slightly faster here... s_09, s_08, s_07, s_06, s_05, s_04, s_03, s_02, s_01 = \ (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\ (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]),\ (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]), (l /= BASE; DIGITS[l % BASE]) "#{@prefix}#{s_01}#{s_02}#{s_03}#{s_04}#{s_05}#{s_06}#{s_07}#{s_08}#{s_09}#{s_10}" end
randomize_prefix!()
click to toggle source
# File lib/nats/nuid.rb, line 57 def randomize_prefix! @prefix = \ SecureRandom.random_bytes(PREFIX_LENGTH).each_byte .reduce('') do |prefix, n| prefix << DIGITS[n % BASE] end end
Private Instance Methods
reset_sequential!()
click to toggle source
# File lib/nats/nuid.rb, line 67 def reset_sequential! @seq = @prand.rand(MAX_SEQ) @inc = MIN_INC + @prand.rand(INC) end