CouchRest no longer uses POST for doc creation

This commit is contained in:
Chris Anderson 2008-09-07 13:51:26 -07:00
parent f87a6e4605
commit 54f260173f
4 changed files with 54 additions and 19 deletions

View file

@ -1,7 +1,8 @@
class CouchRest class CouchRest
attr_accessor :uri attr_accessor :uri, :uuid_batch_count
def initialize server = 'http://localhost:5984' def initialize server = 'http://localhost:5984', uuid_batch_count = 1000
@uri = server @uri = server
@uuid_batch_count = uuid_batch_count
end end
# ensure that a database exists # ensure that a database exists
@ -29,13 +30,13 @@ class CouchRest
end end
def database name def database name
CouchRest::Database.new(@uri, name) CouchRest::Database.new(self, name)
end end
# creates the database if it doesn't exist # creates the database if it doesn't exist
def database! name def database! name
create_db(path) rescue nil create_db(path) rescue nil
CouchRest::Database.new(@uri, name) database name
end end
# get the welcome message # get the welcome message
@ -43,15 +44,23 @@ class CouchRest
CouchRest.get "#{@uri}/" CouchRest.get "#{@uri}/"
end end
# create a database
def create_db name
CouchRest.put "#{@uri}/#{name}"
database name
end
# restart the couchdb instance # restart the couchdb instance
def restart! def restart!
CouchRest.post "#{@uri}/_restart" CouchRest.post "#{@uri}/_restart"
end end
# create a database def next_uuid count = @uuid_batch_count
def create_db name @uuids ||= []
CouchRest.put "#{@uri}/#{name}" if @uuids.empty?
database name @uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"]
end
@uuids.pop
end end
class << self class << self

View file

@ -3,10 +3,11 @@ require "base64"
class CouchRest class CouchRest
class Database class Database
attr_accessor :host, :name attr_accessor :server, :host, :name
def initialize host, name def initialize server, name
@name = name @name = name
@host = host @server = server
@host = server.uri
@root = "#{host}/#{name}" @root = "#{host}/#{name}"
end end
@ -62,13 +63,18 @@ class CouchRest
end end
if doc['_id'] if doc['_id']
slug = CGI.escape(doc['_id']) slug = CGI.escape(doc['_id'])
CouchRest.put "#{@root}/#{slug}", doc
else else
CouchRest.post "#{@root}", doc slug = doc['_id'] = @server.next_uuid
end end
CouchRest.put "#{@root}/#{slug}", doc
end end
def bulk_save docs def bulk_save docs
ids, noids = docs.partition{|d|d['_id']}
uuid_count = [noids.length, @server.uuid_batch_count].max
noids.each do |doc|
doc['_id'] = @server.next_uuid(uuid_count)
end
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs} CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
end end
@ -80,7 +86,9 @@ class CouchRest
def delete! def delete!
CouchRest.delete @root CouchRest.delete @root
end end
private private
def encode_attachments attachments def encode_attachments attachments
attachments.each do |k,v| attachments.each do |k,v|
next if v['stub'] next if v['stub']
@ -88,6 +96,7 @@ class CouchRest
end end
attachments attachments
end end
def base64 data def base64 data
Base64.encode64(data).gsub(/\s/,'') Base64.encode64(data).gsub(/\s/,'')
end end

View file

@ -26,10 +26,12 @@ describe CouchRest do
end end
end end
describe "description" do
it "should restart" do it "should restart" do
@cr.restart! @cr.restart!
end end
it "should provide one-time access to uuids" do
@cr.next_uuid.should_not be_nil
end end
describe "initializing a database" do describe "initializing a database" do

View file

@ -138,6 +138,16 @@ describe CouchRest::Database do
end end
end end
it "should use uuids when ids aren't provided" do
@db.server.stub!(:next_uuid).and_return('asdf6sgadkfhgsdfusdf')
docs = [{'key' => 'value'}, {'_id' => 'totally-uniq'}]
id_docs = [{'key' => 'value', '_id' => 'asdf6sgadkfhgsdfusdf'}, {'_id' => 'totally-uniq'}]
CouchRest.should_receive(:post).with("http://localhost:5984/couchrest-test/_bulk_docs", {:docs => id_docs})
@db.bulk_save(docs)
end
it "should add them with uniq ids" do it "should add them with uniq ids" do
rs = @db.bulk_save([ rs = @db.bulk_save([
{"_id" => "oneB", "wild" => "and random"}, {"_id" => "oneB", "wild" => "and random"},
@ -179,7 +189,7 @@ describe CouchRest::Database do
end end
end end
describe "POST (new document without an id)" do describe "new document without an id" do
it "should start empty" do it "should start empty" do
@db.documents["total_rows"].should == 0 @db.documents["total_rows"].should == 0
end end
@ -188,6 +198,11 @@ describe CouchRest::Database do
r2 = @db.get(r['id']) r2 = @db.get(r['id'])
r2["lemons"].should == "from texas" r2["lemons"].should == "from texas"
end end
it "should use PUT with UUIDs" do
CouchRest.should_receive(:put)
r = @db.save({'just' => ['another document']})
end
end end
describe "PUT document with attachment" do describe "PUT document with attachment" do