Class: Parse::BatchOperation
- Inherits:
-
Object
- Object
- Parse::BatchOperation
- Includes:
- Enumerable
- Defined in:
- lib/parse/client/batch.rb
Overview
This class provides a standard way to submit, manage and process batch operations for Parse::Objects and associations.
Batch requests are supported implicitly and intelligently through an extension of array. When an array of Parse::Object subclasses is saved, Parse-Stack will batch all possible save operations for the objects in the array that have changed. It will also batch save 50 at a time until all items in the array are saved. Note: Parse does not allow batch saving Parse::User objects.
songs = Songs.first 1000 #first 1000 songs songs.each do |song|
... modify song ...
end
will batch save 50 items at a time until all are saved.
songs.save
The objects do not have to be of the same collection in order to be supported in the batch request.
Constant Summary collapse
- DEFAULT_PARALLELISM =
Default number of threads used to dispatch batch segments concurrently. Raise via
Parse::BatchOperation.parallelism = N(or passparallelism:to#submit) for higher throughput on bulk writes; 2 is intentionally conservative to avoid overwhelming smaller Parse Server deployments. 2
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#requests ⇒ Object
Returns the value of attribute requests.
-
#responses ⇒ Object
Returns the value of attribute responses.
-
#transaction ⇒ Boolean
Whether this batch should be executed as a transaction.
Instance Method Summary collapse
-
#add(req) ⇒ Object
Add an additional request to this batch.
-
#as_json(*args) ⇒ Hash
A formatted payload for the batch request.
-
#change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
-
#clear! ⇒ Array
Remove all requests in this batch.
-
#client ⇒ Parse::Client
The client to be used for the request.
-
#count ⇒ Integer
The number of requests in the batch.
- #each(&block) ⇒ Array
-
#error? ⇒ Boolean
True if the request had an error.
-
#initialize(reqs = nil, transaction: false) ⇒ BatchOperation
constructor
A new instance of BatchOperation.
-
#submit(segment = 50, parallelism: self.class.parallelism, &block) ⇒ Array<Parse::Response>
(also: #save)
Submit the batch operation in chunks until they are all complete.
-
#success? ⇒ Boolean
True if the request was successful.
Constructor Details
#initialize(reqs = nil, transaction: false) ⇒ BatchOperation
Returns a new instance of BatchOperation.
70 71 72 73 74 75 76 |
# File 'lib/parse/client/batch.rb', line 70 def initialize(reqs = nil, transaction: false) @requests = [] @responses = [] @transaction = transaction reqs = [reqs] unless reqs.is_a?(Enumerable) reqs.each { |r| add(r) } if reqs.is_a?(Enumerable) end |
Class Attribute Details
.parallelism ⇒ Object
48 49 50 |
# File 'lib/parse/client/batch.rb', line 48 def parallelism @parallelism || DEFAULT_PARALLELISM end |
Instance Attribute Details
#requests ⇒ Object
Returns the value of attribute requests.
|
|
# File 'lib/parse/client/batch.rb', line 53
|
#responses ⇒ Object
Returns the value of attribute responses.
|
|
# File 'lib/parse/client/batch.rb', line 56
|
#transaction ⇒ Boolean
Returns whether this batch should be executed as a transaction.
61 |
# File 'lib/parse/client/batch.rb', line 61 attr_accessor :requests, :responses, :transaction |
Instance Method Details
#add(req) ⇒ Array<Parse::Request> #add(batch) ⇒ Array<Parse::Request>
Add an additional request to this batch.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/parse/client/batch.rb', line 85 def add(req) if req.respond_to?(:change_requests) requests = req.change_requests.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(Array) requests = req.select { |r| r.is_a?(Parse::Request) } @requests += requests elsif req.is_a?(BatchOperation) @requests += req.requests if req.is_a?(BatchOperation) else @requests.push(req) if req.is_a?(Parse::Request) end @requests end |
#as_json(*args) ⇒ Hash
Returns a formatted payload for the batch request.
113 114 115 116 117 |
# File 'lib/parse/client/batch.rb', line 113 def as_json(*args) payload = { requests: requests } payload[:transaction] = true if @transaction payload.as_json end |
#change_requests ⇒ Object
This method is for interoperability with Parse::Object instances.
102 103 104 |
# File 'lib/parse/client/batch.rb', line 102 def change_requests @requests end |
#clear! ⇒ Array
Remove all requests in this batch.
126 127 128 |
# File 'lib/parse/client/batch.rb', line 126 def clear! @requests.clear end |
#client ⇒ Parse::Client
Returns the client to be used for the request.
64 65 66 |
# File 'lib/parse/client/batch.rb', line 64 def client @client ||= Parse::Client.client end |
#count ⇒ Integer
Returns the number of requests in the batch.
120 121 122 |
# File 'lib/parse/client/batch.rb', line 120 def count @requests.count end |
#each(&block) ⇒ Array
107 108 109 110 |
# File 'lib/parse/client/batch.rb', line 107 def each(&block) return enum_for(:each) unless block_given? @requests.each(&block) end |
#error? ⇒ Boolean
Returns true if the request had an error.
137 138 139 140 |
# File 'lib/parse/client/batch.rb', line 137 def error? return false if @responses.empty? !success? end |
#submit(segment = 50, parallelism: self.class.parallelism, &block) ⇒ Array<Parse::Response> Also known as: save
Submit the batch operation in chunks until they are all complete. In general,
Parse limits requests in each batch to 50 and it is possible that a Parse::BatchOperation
instance contains more than 50 requests. This method will slice up the array of
request and send them based on the segment amount until they have all been submitted.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/parse/client/batch.rb', line 151 def submit(segment = 50, parallelism: self.class.parallelism, &block) @responses = [] @requests.uniq!(&:signature) parallelism = 1 if parallelism.nil? || parallelism < 1 @responses = @requests.each_slice(segment).to_a.threaded_map(parallelism) do |slice| client.batch_request(BatchOperation.new(slice)) end @responses.flatten! @requests.zip(@responses).each(&block) if block_given? @responses end |
#success? ⇒ Boolean
Returns true if the request was successful.
131 132 133 134 |
# File 'lib/parse/client/batch.rb', line 131 def success? return false if @responses.empty? @responses.compact.all?(&:success?) end |