Class: RelaxDB::UuidGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/uuid_generator.rb

Overview

UUIDs have a significant impact on CouchDB file size and performance See issues.apache.org/jira/browse/COUCHDB-465 and mail-archives.apache.org/mod_mbox/couchdb-dev/201001.mbox/%[email protected]%3e

This approach uses the default CouchDB UUID generator (sequential) and reduces its size by converting to base 36. Converting to base 36 results in a UUID 7 chars shorter than hex.

The default size of 200 is arbitrary. Brian Candler’s UUID generator in couchtiny may also be of interest.

Class Method Summary collapse

Class Method Details

.count=(c) ⇒ Object



39
40
41
# File 'lib/relaxdb/uuid_generator.rb', line 39

def self.count=(c)
  @count = c
end

.id_length=(length) ⇒ Object

 Convenience that helps relationship debuggging and model exploration



44
45
46
# File 'lib/relaxdb/uuid_generator.rb', line 44

def self.id_length=(length)
  @length = length
end

.refillObject



34
35
36
37
# File 'lib/relaxdb/uuid_generator.rb', line 34

def self.refill
  resp = RelaxDB.db.uuids(@count)
  @uuids = JSON.parse(resp.body)["uuids"]      
end

.resetObject

To be invoked by tests, or clients after temp changes



49
50
51
52
53
# File 'lib/relaxdb/uuid_generator.rb', line 49

def self.reset
  @uuids = []
  @count = 200
  @length = nil
end

.uuidObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/relaxdb/uuid_generator.rb', line 21

def self.uuid
  unless @length
    uuid = @uuids.pop
    if uuid.nil?
      refill
      uuid = @uuids.pop
    end
    uuid.hex.to_s(36)
  else
    rand.to_s[2, @length]
  end
end