class CouchbaseOrm::IdGenerator

Constants

B10
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

Skip46Years

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

Public Class Methods

next(model) click to toggle source

Generate a unique, orderable, ID using minimal bytes

# File lib/couchbase-orm/id_generator.rb, line 17
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(9999) + 1).to_s.rjust(4, '0')

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