Module: AWS::Core::Collection::Simple
- Includes:
- AWS::Core::Collection, Model, Enumerable
- Included in:
- AWS::CloudFormation::StackCollection, AWS::CloudFormation::StackEventCollection, AWS::CloudFormation::StackResourceCollection, AWS::CloudFormation::StackResourceSummaryCollection, AWS::CloudFormation::StackSummaryCollection, EC2::CustomerGatewayCollection, EC2::DHCPOptionsCollection, EC2::InternetGatewayCollection, EC2::NetworkACLCollection, EC2::NetworkInterfaceCollection, EC2::RouteTableCollection, EC2::SubnetCollection, EC2::VPCCollection, EC2::VPNConnectionCollection, EC2::VPNGatewayCollection, ELB::AvailabilityZoneCollection, ELB::BackendServerPolicyCollection, ELB::InstanceCollection, ELB::ListenerCollection, ELB::LoadBalancerCollection, ELB::LoadBalancerPolicyCollection, IAM::GroupUserCollection
- Defined in:
- lib/aws/core/collection/simple.rb
Overview
AWS::Core::Collection::Simple is used by collections that always recieve every matching items in a single response.
This means:
-
Paging methods are simulated
-
Next tokens are artificial (guessable numeric offsets)
AWS services generally return all items only for requests with a small maximum number of results.
See AWS::Core::Collection for documentation on the available collection methods.
Instance Attribute Summary
Attributes included from Model
Instance Method Summary collapse
-
#each_batch(options = {}) {|batch| ... } ⇒ nil_or_next_token
Yields items from this collection in batches.
Methods included from AWS::Core::Collection
#each, #enum, #first, #in_groups_of, #page
Methods included from Model
#client, #config_prefix, #initialize, #inspect
Instance Method Details
#each_batch(options = {}) {|batch| ... } ⇒ nil_or_next_token
If you require fixed size batches, see AWS::Core::Collection#in_groups_of.
Yields items from this collection in batches.
collection.each_batch do |batch|
batch.each do |item|
# ...
end
end
Variable Batch Sizes
Each AWS service has its own rules on how it returns results. Because of this batch size may very based on:
-
Service limits (e.g. S3 limits keys to 1000 per response)
-
The size of response objects (SimpleDB limits responses to 1MB)
-
Time to process the request
Because of these variables, batch sizes may not be consistent for a single collection. Each batch represents all of the items returned in a single resopnse.
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 72 73 74 75 76 77 78 79 |
# File 'lib/aws/core/collection/simple.rb', line 39 def each_batch = {}, &block each_opts = .dup limit = each_opts.delete(:limit) limit = limit.to_i if limit next_token = each_opts.delete(:next_token) offset = next_token ? next_token.to_i - 1 : 0 total = 0 nil_or_next_token = nil batch = [] _each_item(each_opts.dup) do |item| total += 1 # skip until we reach our offset (derived from the "next token") next if total <= offset if limit if batch.size < limit batch << item else # allow _each_item to yield one more item than needed # so we can determine if we should return a "next token" nil_or_next_token = total break end else batch << item end end yield(batch) nil_or_next_token end |