Class: Dynamoid::AdapterPlugin::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/adapter_plugin/query.rb

Constant Summary collapse

OPTIONS_KEYS =
[
  :limit, :hash_key, :hash_value, :range_key, :consistent_read, :scan_index_forward,
  :select, :index_name, :batch_size, :exclusive_start_key, :record_limit, :scan_limit
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, table, opts = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
# File 'lib/dynamoid/adapter_plugin/query.rb', line 11

def initialize(client, table, opts = {})
  @client = client
  @table = table

  opts = opts.symbolize_keys
  @options = opts.slice(*OPTIONS_KEYS)
  @conditions = opts.except(*OPTIONS_KEYS)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/dynamoid/adapter_plugin/query.rb', line 9

def client
  @client
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



9
10
11
# File 'lib/dynamoid/adapter_plugin/query.rb', line 9

def conditions
  @conditions
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/dynamoid/adapter_plugin/query.rb', line 9

def options
  @options
end

#tableObject (readonly)

Returns the value of attribute table.



9
10
11
# File 'lib/dynamoid/adapter_plugin/query.rb', line 9

def table
  @table
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dynamoid/adapter_plugin/query.rb', line 20

def call
  request = build_request

  Enumerator.new do |yielder|
    record_count = 0
    scan_count = 0

    backoff = Dynamoid.config.backoff ? Dynamoid.config.build_backoff : nil

    loop do
      # Adjust the limit down if the remaining record and/or scan limit are
      # lower to obey limits. We can assume the difference won't be
      # negative due to break statements below but choose smaller limit
      # which is why we have 2 separate if statements.
      # NOTE: Adjusting based on record_limit can cause many HTTP requests
      # being made. We may want to change this behavior, but it affects
      # filtering on data with potentially large gaps.
      # Example:
      #    User.where('created_at.gte' => 1.day.ago).record_limit(1000)
      #    Records 1-999 User's that fit criteria
      #    Records 1000-2000 Users's that do not fit criteria
      #    Record 2001 fits criteria
      # The underlying implementation will have 1 page for records 1-999
      # then will request with limit 1 for records 1000-2000 (making 1000
      # requests of limit 1) until hit record 2001.
      if request[:limit] && record_limit && record_limit - record_count < request[:limit]
        request[:limit] = record_limit - record_count
      end
      if request[:limit] && scan_limit && scan_limit - scan_count < request[:limit]
        request[:limit] = scan_limit - scan_count
      end

      response = client.query(request)

      yielder << response

      record_count += response.count
      break if record_limit && record_count >= record_limit

      scan_count += response.scanned_count
      break if scan_limit && scan_count >= scan_limit

      if response.last_evaluated_key
        request[:exclusive_start_key] = response.last_evaluated_key
      else
        break
      end

      backoff.call if backoff
    end
  end
end