multi-key support for views, temp-views, and alldocs

This commit is contained in:
Chris Anderson 2008-10-08 12:19:28 -07:00
parent d7e3c7931f
commit e2f7163816
3 changed files with 36 additions and 13 deletions

View file

@ -108,8 +108,8 @@ module CouchRest
JSON.parse(RestClient.delete(uri)) JSON.parse(RestClient.delete(uri))
end end
def paramify_url url, params = nil def paramify_url url, params = {}
if params if params && !params.empty?
query = params.collect do |k,v| query = params.collect do |k,v|
v = v.to_json if %w{key startkey endkey}.include?(k.to_s) v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
"#{k}=#{CGI.escape(v.to_s)}" "#{k}=#{CGI.escape(v.to_s)}"

View file

@ -29,21 +29,33 @@ module CouchRest
end end
# Query the <tt>_all_docs</tt> view. Accepts all the same arguments as view. # Query the <tt>_all_docs</tt> 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 url = CouchRest.paramify_url "#{@root}/_all_docs", params
CouchRest.get url if keys
CouchRest.post(url, {:keys => keys})
else
CouchRest.get url
end
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. # 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 url = CouchRest.paramify_url "#{@root}/_temp_view", params
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'})) JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
end end
# Query a CouchDB view as defined by a <tt>_design</tt> document. Accepts paramaters as described in http://wiki.apache.org/couchdb/HttpViewApi # Query a CouchDB view as defined by a <tt>_design</tt> 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 url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
CouchRest.get url if keys
CouchRest.post(url, {:keys => keys})
else
CouchRest.get url
end
end end
# GET a document from CouchDB, by id. Returns a Ruby Hash. # GET a document from CouchDB, by id. Returns a Ruby Hash.

View file

@ -22,17 +22,21 @@ describe CouchRest::Database do
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1 rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
end end
it "should work with a range" do 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 rs['rows'].length.should == 2
end end
it "should work with a key" do 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 rs['rows'].length.should == 1
end end
it "should work with a count" do 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 rs['rows'].length.should == 1
end end
it "should work with multi-keys" do
rs = @db.temp_view(@temp_view, :keys => ["another", "wild"])
rs['rows'].length.should == 2
end
end end
describe "map/reduce query with _temp_view in Javascript" do 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 rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
end end
it "should work with a range" do 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 rs['rows'].length.should == 2
end end
it "should work with a key" do 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 rs['rows'].length.should == 1
end end
it "should work with a count" do 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 rs['rows'].length.should == 1
end end
it "should work with multi-keys" do
rs = @db.view('first/test', :keys => ["another", "wild"])
rs['rows'].length.should == 2
end
end end
describe "GET (document by id) when the doc exists" do describe "GET (document by id) when the doc exists" do
@ -426,6 +434,9 @@ describe CouchRest::Database do
ds['rows'].length.should == 4 ds['rows'].length.should == 4
ds = @db.documents(:key => 'doc0') ds = @db.documents(:key => 'doc0')
ds['rows'].length.should == 1 ds['rows'].length.should == 1
rs = @db.documents :keys => ["doc0", "doc7"]
rs['rows'].length.should == 2
end end
describe "deleting a database" do describe "deleting a database" do