Class: Dynamoid::AdapterPlugin::AwsSdkV3::Middleware::Limit

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/limit.rb

Instance Method Summary collapse

Constructor Details

#initialize(next_chain, record_limit: nil, scan_limit: nil) ⇒ Limit

Returns a new instance of Limit.


9
10
11
12
13
14
15
16
17
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/limit.rb', line 9

def initialize(next_chain, record_limit: nil, scan_limit: nil)
  @next_chain = next_chain

  @record_limit = record_limit
  @scan_limit = scan_limit

  @record_count = 0
  @scan_count = 0
end

Instance Method Details

#call(request) ⇒ Object


19
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
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/limit.rb', line 19

def call(request)
  # 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 = @next_chain.call(request)

  @record_count += response.count
  throw :stop_pagination if @record_limit && @record_count >= @record_limit

  @scan_count += response.scanned_count
  throw :stop_pagination if @scan_limit && @scan_count >= @scan_limit

  response
end