module Couch::BulkRequest::Get

Public Instance Methods

all_docs(db, limit=750, opts={}, &block) click to toggle source

If a block is given, performs the block for each limit-sized slice of _all_docs. If no block is given, returns all docs by appending limit-sized slices of _all_docs.

This method assumes your docs don’t have the high-value Unicode character ufff0. If it does, then behaviour is undefined. The reason why we use the startkey parameter instead of skip is that startkey is faster.

# File lib/couch.rb, line 80
def all_docs(db, limit=750, opts={}, &block)
  all_docs = []
  start_key = nil
  loop do
    opts = opts.merge({limit: limit})
    if start_key
      opts[:startkey]=start_key
    end
    docs = (lambda { |options| get_all_docs(db, options) }).call(opts)
    if docs.length <= 0
      break
    else
      if block
        block.call(docs)
      else
        all_docs < docs
      end
      start_key ="\"#{docs.last['_id']}\\ufff0\""
    end
  end
  all_docs.flatten
end
all_ids(db, limit=500, opts={}, &block) click to toggle source

Returns an array of all ids in the database

# File lib/couch.rb, line 140
def all_ids(db, limit=500, opts={}, &block)
  all_docs = []
  start_key = nil
  loop do
    opts = opts.merge({limit: limit})
    if start_key
      opts[:startkey]=start_key
    end
    docs = (lambda { |options| get_all_ids(db, options) }).call(opts)
    if docs.length <= 0
      break
    else
      if block
        block.call(docs)
      else
        all_docs < docs
      end
      start_key ="\"#{docs.last}\\ufff0\""
    end
  end
  all_docs.flatten
end
docs_for_view(db, design_doc, view, limit=750, opts={}, &block) click to toggle source

If a block is given, performs the block for each limit-sized slice of documents for the given view. If no block is given, returns all docs by appending limit-sized slices of the given view.

# File lib/couch.rb, line 178
def docs_for_view(db, design_doc, view, limit=750, opts={}, &block)
  get_all_views(lambda { |options| get_docs_for_view(db, design_doc, view, options) }, limit, opts, block)
end
get_all_docs(database, params) click to toggle source

Returns an array of the full documents for given database, possibly filtered with given parameters. We recommend you use all_docs instead.

Note that the ‘include_docs’ parameter must be set to true for this.

# File lib/couch.rb, line 65
def get_all_docs(database, params)
  # unless params.include_symbol_or_string? :include_docs
  #   params.merge!({:include_docs => true})
  # end
  postfix = create_postfix(params)
  uri = URI::encode "/#{database}/_all_docs#{postfix}"
  res = get(uri)
  JSON.parse(res.body)['rows']
end
get_all_ids(database, params) click to toggle source

Returns an array of all ids in the database

# File lib/couch.rb, line 121
def get_all_ids(database, params)
  ids=[]
  postfix = create_postfix(params)

  uri = URI::encode "/#{database}/_all_docs#{postfix}"
  res = get(uri)
  result = JSON.parse(res.body)
  result['rows'].each do |row|
    if row['error']
      puts "#{row['key']}: #{row['error']}"
      puts "#{row['reason']}"
    else
      ids << row['id']
    end
  end
  ids
end
get_docs_for_view(db, design_doc, view, params={}) click to toggle source

Returns an array of the full documents for given view, possibly filtered with given parameters. Note that the ‘include_docs’ parameter must be set to true for this.

Also consider using ‘docs_for_view`

# File lib/couch.rb, line 166
def get_docs_for_view(db, design_doc, view, params={})
  params.merge!({:include_docs => true})
  rows = get_rows_for_view(db, design_doc, view, params)
  docs = []
  rows.each do |row|
    docs << row['doc']
  end
  docs
end
get_rows_for_view(database, design_doc, view, query_params=nil) click to toggle source

Returns an array of all rows for given view.

We recommend you use rows_for_view instead.

# File lib/couch.rb, line 106
def get_rows_for_view(database, design_doc, view, query_params=nil)
  postfix = create_postfix(query_params)
  uri = URI::encode "/#{database}/_design/#{design_doc}/_view/#{view}#{postfix}"
  res = get(uri)
  JSON.parse(res.body.force_encoding('utf-8'))['rows']
end
rows_for_view(db, design_doc, view, limit=500, opts={}, &block) click to toggle source

If a block is given, performs the block for each limit-sized slice of rows for the given view. If no block is given, returns all rows by appending limit-sized slices of the given view.

# File lib/couch.rb, line 115
def rows_for_view(db, design_doc, view, limit=500, opts={}, &block)
  get_all_views(lambda { |options| get_rows_for_view(db, design_doc, view, options) }, limit, opts, block)
end

Private Instance Methods

get_all_views(next_results, limit, opts, block) click to toggle source
# File lib/couch.rb, line 184
def get_all_views(next_results, limit, opts, block)
  all = []
  offset = 0
  loop do
    opts = opts.merge({
                          limit: limit,
                          skip: offset,
                      })
    docs = next_results.call(opts)
    if docs.length <= 0
      break
    else
      if block
        block.call(docs)
      else
        all < docs
      end
      offset += limit
    end
  end
  all.flatten
end