Class: CouchRest::Streamer

Inherits:
Object show all
Defined in:
lib/couchrest/helper/streamer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Streamer

Returns a new instance of Streamer.



4
5
6
# File 'lib/couchrest/helper/streamer.rb', line 4

def initialize db
  @db = db
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



3
4
5
# File 'lib/couchrest/helper/streamer.rb', line 3

def db
  @db
end

Instance Method Details

#view(name, params = nil, &block) ⇒ Object

Stream a view, yielding one row at a time. Shells out to curl to keep RAM usage low when you have millions of rows.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/couchrest/helper/streamer.rb', line 9

def view name, params = nil, &block
  urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
  url = CouchRest.paramify_url urlst, params
  # puts "stream #{url}"
  first = nil
  IO.popen("curl --silent #{url}") do |view|
    first = view.gets # discard header
    while line = view.gets 
      row = parse_line(line)
      block.call row
    end
  end
  parse_first(first)
end