Class: CouchbaseOrm::IdGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase-orm/id_generator.rb

Constant Summary collapse

B65 =

Using base 65 as a form of compression (reduced length of ID string) No escape characters are required to display these in a URL

::Radix::Base.new(::Radix::BASE::B62 + ['-', '_', '~'])
B10 =
::Radix::Base.new(10)
Skip46Years =

We don't really care about dates before this library was created This reduces the length of the ID significantly

1451649600

Class Method Summary collapse

Class Method Details

.next(model) ⇒ Object

Generate a unique, orderable, ID using minimal bytes



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/couchbase-orm/id_generator.rb', line 18

def self.next(model)
    # We are unlikely to see a clash here
  now = Time.now
  time = (now.to_i - Skip46Years) * 1_000_000 + now.usec

    # This makes it very very improbable that there will ever be an ID clash
    # Distributed system safe!
  prefix = time.to_s
  tail = rand(1..9999).to_s.rjust(4, '0')

  "#{model.class.design_document}-#{Radix.convert("#{prefix}#{tail}", B10, B65)}"
end