Skip to content

Commit

Permalink
Streamer fixes
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Julien Sanchez committed Oct 8, 2009
1 parent b01b49d commit 54dee07
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
15 changes: 11 additions & 4 deletions lib/couchrest/helper/streamer.rb
Expand Up @@ -7,15 +7,22 @@ def initialize db

# 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
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
# puts "stream #{url}"
first = nil
IO.popen("curl --silent #{url}") do |view|
IO.popen("curl --silent \"#{url}\"") do |view|
first = view.gets # discard header
while line = view.gets
row = parse_line(line)
block.call row
block.call row unless row.nil? # last line "}]" discarded
end
end
parse_first(first)
Expand All @@ -41,4 +48,4 @@ def parse_first first
end

end
end
end
11 changes: 9 additions & 2 deletions spec/couchrest/core/database_spec.rb
Expand Up @@ -131,9 +131,16 @@
rs = @db.view('first/test', :include_docs => true) do |row|
rows << row
end
rows.length.should == 4
rows.length.should == 3
rs["total_rows"].should == 3
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

describe "GET (document by id) when the doc exists" do
Expand Down Expand Up @@ -711,4 +718,4 @@
end


end
end
33 changes: 31 additions & 2 deletions spec/couchrest/helpers/streamer_spec.rb
Expand Up @@ -9,6 +9,14 @@
@streamer = CouchRest::Streamer.new(@db)
@docs = (1..1000).collect{|i| {:integer => i, :string => i.to_s}}
@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

it "should yield each row in a view" do
Expand All @@ -19,5 +27,26 @@
end
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

0 comments on commit 54dee07

Please sign in to comment.