Sha256: cef74b3c5b6ef68112ac05c41bc79a9de593847388c9d5a833aaa5c2b8a92b13

Contents?: true

Size: 749 Bytes

Versions: 3

Compression:

Stored size: 749 Bytes

Contents

module CouchRest
  class Streamer
    attr_accessor :db
    def initialize db
      @db = db
    end
    
    # 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
      urlst = /^_/.match(name) ? "#{@db.root}/#{name}" : "#{@db.root}/_view/#{name}"
      url = CouchRest.paramify_url urlst, params
      IO.popen("curl --silent #{url}") do |view|
        view.gets # discard header
        while row = parse_line(view.gets)
          yield row
        end
      end
    end
    
    private
    
    def parse_line line
      return nil unless line
      if /(\{.*\}),?/.match(line.chomp)
        JSON.parse($1)
      end
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jchris-couchrest-0.9.10 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.9.11 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.9.9 lib/couchrest/helper/streamer.rb