Streamer fixes
* url must be quoted inside the curl command otherwise '&' between params are interpreted by shells * View url format updated * Streamer yielded the last row returned by curl ("}]") as nil * Specs
This commit is contained in:
parent
d8e7652680
commit
1c43a8f3d3
3 changed files with 51 additions and 8 deletions
|
@ -7,15 +7,22 @@ module CouchRest
|
||||||
|
|
||||||
# Stream a view, yielding one row at a time. Shells out to <tt>curl</tt> to keep RAM usage low when you have millions of rows.
|
# Stream a view, yielding one row at a time. Shells out to <tt>curl</tt> to keep RAM usage low when you have millions of rows.
|
||||||
def view name, params = nil, &block
|
def view name, params = nil, &block
|
||||||
urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
|
urlst = if /^_/.match(name) then
|
||||||
|
"#{@db.root}/#{name}"
|
||||||
|
else
|
||||||
|
name = name.split('/')
|
||||||
|
dname = name.shift
|
||||||
|
vname = name.join('/')
|
||||||
|
"#{@db.root}/_design/#{dname}/_view/#{vname}"
|
||||||
|
end
|
||||||
url = CouchRest.paramify_url urlst, params
|
url = CouchRest.paramify_url urlst, params
|
||||||
# puts "stream #{url}"
|
# puts "stream #{url}"
|
||||||
first = nil
|
first = nil
|
||||||
IO.popen("curl --silent #{url}") do |view|
|
IO.popen("curl --silent \"#{url}\"") do |view|
|
||||||
first = view.gets # discard header
|
first = view.gets # discard header
|
||||||
while line = view.gets
|
while line = view.gets
|
||||||
row = parse_line(line)
|
row = parse_line(line)
|
||||||
block.call row
|
block.call row unless row.nil? # last line "}]" discarded
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
parse_first(first)
|
parse_first(first)
|
||||||
|
|
|
@ -131,9 +131,16 @@ describe CouchRest::Database do
|
||||||
rs = @db.view('first/test', :include_docs => true) do |row|
|
rs = @db.view('first/test', :include_docs => true) do |row|
|
||||||
rows << row
|
rows << row
|
||||||
end
|
end
|
||||||
rows.length.should == 4
|
rows.length.should == 3
|
||||||
rs["total_rows"].should == 3
|
rs["total_rows"].should == 3
|
||||||
end
|
end
|
||||||
|
it "should accept a block with several params" do
|
||||||
|
rows = []
|
||||||
|
rs = @db.view('first/test', :include_docs => true, :limit => 2) do |row|
|
||||||
|
rows << row
|
||||||
|
end
|
||||||
|
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
|
||||||
|
|
|
@ -9,6 +9,14 @@ describe CouchRest::Streamer do
|
||||||
@streamer = CouchRest::Streamer.new(@db)
|
@streamer = CouchRest::Streamer.new(@db)
|
||||||
@docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
|
@docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
|
||||||
@db.bulk_save(@docs)
|
@db.bulk_save(@docs)
|
||||||
|
@db.save_doc({
|
||||||
|
"_id" => "_design/first",
|
||||||
|
:views => {
|
||||||
|
:test => {
|
||||||
|
:map => "function(doc){for(var w in doc){ if(!w.match(/^_/))emit(w,doc[w])}}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should yield each row in a view" do
|
it "should yield each row in a view" do
|
||||||
|
@ -20,4 +28,25 @@ describe CouchRest::Streamer do
|
||||||
count.should == 1001
|
count.should == 1001
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should accept several params" do
|
||||||
|
count = 0
|
||||||
|
@streamer.view("_design/first/_view/test", :include_docs => true, :limit => 5) do |row|
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
count.should == 5
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should accept both view formats" do
|
||||||
|
count = 0
|
||||||
|
@streamer.view("_design/first/_view/test") do |row|
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
count.should == 2000
|
||||||
|
count = 0
|
||||||
|
@streamer.view("first/test") do |row|
|
||||||
|
count += 1
|
||||||
|
end
|
||||||
|
count.should == 2000
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue