Method: DynaModel::Query::ClassMethods#scan

Defined in:
lib/dyna_model/query.rb

#scan(options = {}) ⇒ Object

:count=>10, :scanned_count=>10, :last_evaluated_key=>“guid”=>{:s=>“11f82550-5c5d-11e3-9b55-d311a43114ca”}} :manual_batching => true|false

return results with last_evaluated_key instead of automatically looping through (useful to throttle or )


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dyna_model/query.rb', line 127

def scan(options={})
  aggregated_results = []

  batch_size = options.delete(:batch) || DEFAULT_BATCH_SIZE
  max_results_limit = options[:limit]
  options[:limit] = batch_size

  results = self.dynamo_db_table.scan(options)
  response = Response.new(results)

  results[:member].each do |result|
    attrs = Response.strip_attr_types(result)
    aggregated_results << self.obj_from_attrs(attrs, options)
  end

  if response.more_results? && !options[:manual_batching]
    results_returned = response.count
    batch_iteration = 0
    Timeout::timeout(QUERY_TIMEOUT) do
      while response.more_results?
        if max_results_limit && (delta_results_limit = (max_results_limit-results_returned)) < batch_size
          break if delta_results_limit == 0
          options.merge!(limit: delta_results_limit)
        else
          options.merge!(limit: batch_size)
        end

        results = dynamo_table.scan(options.merge(exclusive_start_key: response.last_evaluated_key))
        response = Response.new(results)
        results[:member].each do |result|
          attrs = Response.strip_attr_types(result)
          aggregated_results << self.obj_from_attrs(attrs, options)
        end
        results_returned += response.count
        batch_iteration += 1
      end
    end
  end

  if options[:manual_batching]
    response_hash = {
      results: aggregated_results,
      last_evaluated_key: results[:last_evaluated_key]
    }
    response_hash.merge!(consumed_capacity: results[:consumed_capacity]) if results[:consumed_capacity]
    response_hash
  else
    aggregated_results
  end
end