Sha256: 210b5fe0a0eb401fddcd3573054c2b6e1332eacfd580b5aa64377979fe3502ba

Contents?: true

Size: 1.03 KB

Versions: 94

Compression:

Stored size: 1.03 KB

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, &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
    
    private
    
    def parse_line line
      return nil unless line
      if /(\{.*\}),?/.match(line.chomp)
        JSON.parse($1)
      end
    end

    def parse_first first
      return nil unless first
      parts = first.split(',')
      parts.pop
      line = parts.join(',')
      JSON.parse("#{line}}")
    rescue
      nil
    end
    
  end
end

Version data entries

94 entries across 94 versions & 23 rubygems

Version Path
derfred-couchrest-0.12.6.3 lib/couchrest/helper/streamer.rb
derfred-couchrest-0.12.6 lib/couchrest/helper/streamer.rb
gbuesing-couchrest-0.23 lib/couchrest/helper/streamer.rb
glasner-couchrest-0.2.2 lib/couchrest/helper/streamer.rb
gohanlonllc-couchrest-0.2.3.1 lib/couchrest/helper/streamer.rb
halfninja-couchrest-0.23.2 lib/couchrest/helper/streamer.rb
halfninja-couchrest-0.23.3 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.12.2 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.12.4 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.12.5 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.12.6 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.16 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.17.0 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.2.1 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.2.2 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.2 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.22 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.23 lib/couchrest/helper/streamer.rb
jchris-couchrest-0.9.12 lib/couchrest/helper/streamer.rb
jgre-couchrest-0.12.6 lib/couchrest/helper/streamer.rb