Module: Couch::BulkRequest::Get

Included in:
Server
Defined in:
lib/couch.rb

Instance Method Summary collapse

Instance Method Details

#all_docs(db, limit = 750, opts = {}, &block) ⇒ Object

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object

Returns an array of all ids in the database



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object

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.



178
179
180
# 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) ⇒ Object

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.



65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

Returns an array of all ids in the database



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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 = {}) ⇒ Object

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



166
167
168
169
170
171
172
173
174
# 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) ⇒ Object

Returns an array of all rows for given view.

We recommend you use rows_for_view instead.



106
107
108
109
110
111
# 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) ⇒ Object

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.



115
116
117
# 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