Sha256: 767c538386aff116a3a5665600d27866b2071de30d8d7797ddb6dabac6f3e4b9

Contents?: true

Size: 1.09 KB

Versions: 24

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true, encoding: ASCII-8BIT

require 'radix/base'

module CouchbaseOrm
    class IdGenerator
        # Using base 65 as a form of compression (reduced length of ID string)
        # No escape characters are required to display these in a URL
        B65 = ::Radix::Base.new(::Radix::BASE::B62 + ['-', '_', '~'])
        B10 = ::Radix::Base.new(10)

        # We don't really care about dates before this library was created
        # This reduces the length of the ID significantly
        Skip46Years = 1451649600  # 46.years.to_i

        # Generate a unique, orderable, ID using minimal bytes
        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
    end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
couchbase-orm-2.0.4 lib/couchbase-orm/id_generator.rb
couchbase-orm-2.0.3 lib/couchbase-orm/id_generator.rb
couchbase-orm-2.0.2 lib/couchbase-orm/id_generator.rb
couchbase-orm-2.0.1 lib/couchbase-orm/id_generator.rb
couchbase-orm-2.0.0 lib/couchbase-orm/id_generator.rb
couchbase-orm-1.1.1 lib/couchbase-orm/id_generator.rb
couchbase-orm-1.1.0 lib/couchbase-orm/id_generator.rb
couchbase-orm-1.0.0 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.2.1 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.2.0 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.1.2 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.1.1 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.1.0 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.12 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.11 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.10 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.9 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.8 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.6 lib/couchbase-orm/id_generator.rb
couchbase-orm-0.0.5 lib/couchbase-orm/id_generator.rb