From e2f71638163cffc74075fdf261f649e587a4e690 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Wed, 8 Oct 2008 12:19:28 -0700 Subject: [PATCH] multi-key support for views, temp-views, and alldocs --- lib/couchrest.rb | 4 ++-- lib/couchrest/core/database.rb | 22 +++++++++++++++++----- spec/couchrest/core/database_spec.rb | 23 +++++++++++++++++------ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/couchrest.rb b/lib/couchrest.rb index d8bd3f0..c7e77c1 100644 --- a/lib/couchrest.rb +++ b/lib/couchrest.rb @@ -108,8 +108,8 @@ module CouchRest JSON.parse(RestClient.delete(uri)) end - def paramify_url url, params = nil - if params + def paramify_url url, params = {} + if params && !params.empty? query = params.collect do |k,v| v = v.to_json if %w{key startkey endkey}.include?(k.to_s) "#{k}=#{CGI.escape(v.to_s)}" diff --git a/lib/couchrest/core/database.rb b/lib/couchrest/core/database.rb index da13219..d8d8cc3 100644 --- a/lib/couchrest/core/database.rb +++ b/lib/couchrest/core/database.rb @@ -29,21 +29,33 @@ module CouchRest end # Query the _all_docs view. Accepts all the same arguments as view. - def documents params = nil + def documents params = {} + keys = params.delete(:keys) url = CouchRest.paramify_url "#{@root}/_all_docs", params - CouchRest.get url + if keys + CouchRest.post(url, {:keys => keys}) + else + CouchRest.get url + end end # POST a temporary view function to CouchDB for querying. This is not recommended, as you don't get any performance benefit from CouchDB's materialized views. Can be quite slow on large databases. - def temp_view funcs, params = nil + def temp_view funcs, params = {} + keys = params.delete(:keys) + funcs = funcs.merge({:keys => keys}) if keys url = CouchRest.paramify_url "#{@root}/_temp_view", params JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'})) end # Query a CouchDB view as defined by a _design document. Accepts paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi - def view name, params = nil + def view name, params = {} + keys = params.delete(:keys) url = CouchRest.paramify_url "#{@root}/_view/#{name}", params - CouchRest.get url + if keys + CouchRest.post(url, {:keys => keys}) + else + CouchRest.get url + end end # GET a document from CouchDB, by id. Returns a Ruby Hash. diff --git a/spec/couchrest/core/database_spec.rb b/spec/couchrest/core/database_spec.rb index 8ca97ca..29608d2 100644 --- a/spec/couchrest/core/database_spec.rb +++ b/spec/couchrest/core/database_spec.rb @@ -22,17 +22,21 @@ describe CouchRest::Database do rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1 end it "should work with a range" do - rs = @db.temp_view(@temp_view,{:startkey => "b", :endkey => "z"}) + rs = @db.temp_view(@temp_view, :startkey => "b", :endkey => "z") rs['rows'].length.should == 2 end it "should work with a key" do - rs = @db.temp_view(@temp_view,{:key => "wild"}) + rs = @db.temp_view(@temp_view, :key => "wild") rs['rows'].length.should == 1 end it "should work with a count" do - rs = @db.temp_view(@temp_view,{:count => 1}) + rs = @db.temp_view(@temp_view, :count => 1) rs['rows'].length.should == 1 end + it "should work with multi-keys" do + rs = @db.temp_view(@temp_view, :keys => ["another", "wild"]) + rs['rows'].length.should == 2 + end end describe "map/reduce query with _temp_view in Javascript" do @@ -98,17 +102,21 @@ describe CouchRest::Database do rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1 end it "should work with a range" do - rs = @db.view('first/test',{:startkey => "b", :endkey => "z"}) + rs = @db.view('first/test', :startkey => "b", :endkey => "z") rs['rows'].length.should == 2 end it "should work with a key" do - rs = @db.view('first/test',{:key => "wild"}) + rs = @db.view('first/test', :key => "wild") rs['rows'].length.should == 1 end it "should work with a count" do - rs = @db.view('first/test',{:count => 1}) + rs = @db.view('first/test', :count => 1) rs['rows'].length.should == 1 end + it "should work with multi-keys" do + rs = @db.view('first/test', :keys => ["another", "wild"]) + rs['rows'].length.should == 2 + end end describe "GET (document by id) when the doc exists" do @@ -426,6 +434,9 @@ describe CouchRest::Database do ds['rows'].length.should == 4 ds = @db.documents(:key => 'doc0') ds['rows'].length.should == 1 + + rs = @db.documents :keys => ["doc0", "doc7"] + rs['rows'].length.should == 2 end describe "deleting a database" do