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
|
@ -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.
|
||||
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)
|
||||
|
@ -41,4 +48,4 @@ module CouchRest
|
|||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -131,9 +131,16 @@ describe CouchRest::Database do
|
|||
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
|
||||
|
@ -711,4 +718,4 @@ describe CouchRest::Database do
|
|||
end
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,6 +9,14 @@ describe CouchRest::Streamer do
|
|||
@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
|
||||
|
@ -19,5 +27,26 @@ describe CouchRest::Streamer do
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue