Class: CouchRest::Streamer

Inherits:
Object
  • 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
23
24
25
26
27
28
29
# File 'lib/couchrest/helper/streamer.rb', line 9

def view name, params = nil, &block
  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|
    first = view.gets # discard header
    while line = view.gets
      row = parse_line(line)
      block.call row unless row.nil? # last line "}]" discarded
    end
  end
  parse_first(first)
end