Permalink
Browse files

Updated UuidGenerator to use that provided by CouchDB.

  • Loading branch information...
1 parent 7c7bbca commit 98ec24eac8cd75341676deeb25c77a920c667782 @paulcarey committed Apr 15, 2010
Showing with 47 additions and 3 deletions.
  1. +7 −0 lib/relaxdb/server.rb
  2. +38 −2 lib/relaxdb/uuid_generator.rb
  3. +1 −0 relaxdb.gemspec
  4. +1 −1 spec/doc_inheritable_spec.rb
View
@@ -68,6 +68,13 @@ def put(path=nil, json=nil)
@server.put("/#{@db}/#{path}", json)
end
+ def uuids(count=1)
+ @get_count += 1
+ uri = "/_uuids?count=#{count}"
+ @logger.info "GET #{uri}"
+ @server.get uri
+ end
+
def unesc(path)
# path
path ? ::CGI::unescape(path) : ""
@@ -1,20 +1,56 @@
module RelaxDB
+ #
+ # UUIDs have a significant impact on CouchDB file size and performance
+ # See https://issues.apache.org/jira/browse/COUCHDB-465 and
+ # http://mail-archives.apache.org/mod_mbox/couchdb-dev/201001.mbox/%3chi57et$19n$1@ger.gmane.org%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 UuidGenerator
+
+ @uuids = []
+ @count = 200
def self.uuid
unless @length
- @uuid ||= UUID.new
- @uuid.generate
+ uuid = @uuids.pop
+ if uuid.nil?
+ refill
+ uuid = @uuids.pop
+ end
+ uuid.hex.to_s(36)
else
rand.to_s[2, @length]
end
end
+
+ def self.refill
+ resp = RelaxDB.db.uuids(@count)
+ @uuids = JSON.parse(resp.body)["uuids"]
+ end
+
+ def self.count=(c)
+ @count = c
+ end
# Convenience that helps relationship debuggging and model exploration
def self.id_length=(length)
@length = length
end
+
+ # To be invoked by tests, or clients after temp changes
+ def self.reset
+ @uuids = []
+ @count = 200
+ @length = nil
+ end
end
View
@@ -56,6 +56,7 @@ Gem::Specification.new do |s|
"spec/spec.opts",
"spec/spec_helper.rb",
"spec/spec_models.rb",
+ "spec/uuid_generator_spec.rb",
"spec/view_docs_by_spec.rb",
"spec/view_object_spec.rb"]
s.bindir = "bin"
@@ -34,7 +34,7 @@
a = Ancestor.new(:x => 0).save!
d = Descendant.new(:x => 1).save!
- Ancestor.all.load!.should == [a, d]
+ Ancestor.all.load!.should include(a, d)
Descendant.all.load!.should == [d]
end

0 comments on commit 98ec24e

Please sign in to comment.