Class: Parse::BatchOperation

Inherits:
Object
  • Object
show all
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 pass `parallelism:` 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

Instance Method Summary collapse

Constructor Details

#initialize(reqs = nil, transaction: false) ⇒ BatchOperation

Returns a new instance of BatchOperation.

Parameters:

  • reqs (Array<Parse::Request>) (defaults to: nil)

    an array of requests.

  • transaction (Boolean) (defaults to: false)

    whether to execute as a transaction.



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

.parallelismObject



48
49
50
# File 'lib/parse/client/batch.rb', line 48

def parallelism
  @parallelism || DEFAULT_PARALLELISM
end

Instance Attribute Details

#requestsObject

Returns the value of attribute requests.



# File 'lib/parse/client/batch.rb', line 53

#responsesObject

Returns the value of attribute responses.



# File 'lib/parse/client/batch.rb', line 56

#transactionBoolean

Returns whether this batch should be executed as a transaction.

Returns:

  • (Boolean)

    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.

Overloads:



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.

Returns:

  • (Hash)

    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_requestsObject

This method is for interoperability with Parse::Object instances.

See Also:

  • Object#change_requests


102
103
104
# File 'lib/parse/client/batch.rb', line 102

def change_requests
  @requests
end

#clear!Array

Remove all requests in this batch.

Returns:



126
127
128
# File 'lib/parse/client/batch.rb', line 126

def clear!
  @requests.clear
end

#clientParse::Client

Returns the client to be used for the request.

Returns:



64
65
66
# File 'lib/parse/client/batch.rb', line 64

def client
  @client ||= Parse::Client.client
end

#countInteger

Returns the number of requests in the batch.

Returns:

  • (Integer)

    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

Returns:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • segment (Integer) (defaults to: 50)

    the number of requests to send in each batch. Default 50.

  • parallelism (Integer) (defaults to: self.class.parallelism)

    the number of segments dispatched in parallel. Defaults to ‘Parse::BatchOperation.parallelism` (2).

Returns:



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.

Returns:

  • (Boolean)

    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