Class: Azure::Storage::Table::Batch

Inherits:
Object
  • Object
show all
Defined in:
table/lib/azure/storage/table/batch.rb

Overview

Represents a batch of table operations.

Example usage:

svc = TableSerice.new

batch = Batch.new “table”, “partition” batch.insert “row1”, “meta”=>“data” batch.insert “row2”, “meta”=>“data”

results = svc.execute_batch batch

Defined Under Namespace

Classes: ResponseWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, partition, &block) ⇒ Batch

Returns a new instance of Batch.



42
43
44
45
46
47
48
49
50
51
# File 'table/lib/azure/storage/table/batch.rb', line 42

def initialize(table, partition, &block)
  @table = table
  @partition = partition
  @operations = []
  @entity_keys = []
  @batch_id = "batch_" + SecureRandom.uuid
  @changeset_id = "changeset_" + SecureRandom.uuid

  self.instance_eval(&block) if block_given?
end

Instance Attribute Details

#batch_idObject

Returns the value of attribute batch_id.



62
63
64
# File 'table/lib/azure/storage/table/batch.rb', line 62

def batch_id
  @batch_id
end

Instance Method Details

#delete(row_key, options = {}) ⇒ Object



356
357
358
359
360
361
362
363
# File 'table/lib/azure/storage/table/batch.rb', line 356

def delete(row_key, options = {})
  headers = {
    Azure::Storage::Common::HeaderConstants::ACCEPT => Serialization.get_accept_string(options[:accept]),
    "If-Match" => options[:if_match] || "*"
  }
  add_operation(:delete, row_key, nil, headers)
  self
end

#get(row_key, options = {}) ⇒ Object



233
234
235
236
237
238
239
240
# File 'table/lib/azure/storage/table/batch.rb', line 233

def get(row_key, options = {})
  check_entity_key(row_key)

  headers = { Azure::Storage::Common::HeaderConstants::ACCEPT => Serialization.get_accept_string(options[:accept]) }

  add_operation(:get, row_key, nil, headers)
  self
end

#insert(row_key, entity_values, options = {}) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'table/lib/azure/storage/table/batch.rb', line 200

def insert(row_key, entity_values, options = {})
  check_entity_key(row_key)

  headers = { Azure::Storage::Common::HeaderConstants::ACCEPT => Serialization.get_accept_string(options[:accept]) }
  headers[Azure::Storage::Common::HeaderConstants::PREFER] = options[:prefer] unless options[:prefer].nil?

  body = Serialization.hash_to_json({
      "PartitionKey" => partition,
      "RowKey" => row_key
    }.merge(entity_values)
  )

  add_operation(:post, nil, body, headers)
  self
end

#insert_or_merge(row_key, entity_values) ⇒ Object



319
320
321
322
# File 'table/lib/azure/storage/table/batch.rb', line 319

def insert_or_merge(row_key, entity_values)
  merge(row_key, entity_values, create_if_not_exists: true)
  self
end

#insert_or_replace(row_key, entity_values) ⇒ Object



333
334
335
336
# File 'table/lib/azure/storage/table/batch.rb', line 333

def insert_or_replace(row_key, entity_values)
  update(row_key, entity_values, create_if_not_exists: true)
  self
end

#merge(row_key, entity_values, options = {}) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
# File 'table/lib/azure/storage/table/batch.rb', line 298

def merge(row_key, entity_values, options = {})
  check_entity_key(row_key)

  headers = { Azure::Storage::Common::HeaderConstants::ACCEPT => Serialization.get_accept_string(options[:accept]) }
  headers["If-Match"] = options[:if_match] || "*" unless options[:create_if_not_exists]

  body = Serialization.hash_to_json(entity_values)

  add_operation(:merge, row_key, body, headers)
  self
end

#parse_response(response) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'table/lib/azure/storage/table/batch.rb', line 105

def parse_response(response)
  responses = BatchResponse.parse response.body, (!operations.empty? && operations[0][:method] == :get)
  new_responses = []

  (0..responses.length - 1).each { |index|
    operation = operations[index]
    response = responses[index]

    if response[:status_code].to_i > 299
      # failed
      error = Azure::Core::Http::HTTPError.new(ResponseWrapper.new(response.merge(uri: operation[:uri])))
      error.description = response[:message] if (error.description || "").strip == ""
      raise error
    else
      # success
      case operation[:method]
      when :post, :get
        # entity from body
        entity = Serialization.entity_from_json(response[:body])

        entity.etag = response[:headers]["etag"] if entity.etag.nil?

        new_responses.push entity
      when :put, :merge
        # etag from headers
        new_responses.push response[:headers]["etag"]
      when :delete
        # true
        new_responses.push nil
      end
    end
  }

  new_responses
end

#to_body(table_service) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'table/lib/azure/storage/table/batch.rb', line 142

def to_body(table_service)
  body = ""
  body.define_singleton_method(:add_line) do |a| self << (a || nil) + "\n" end

  is_get = true if !operations.empty? && operations[0][:method] == :get

  body.add_line "--#{batch_id}"
  body.add_line "Content-Type: multipart/mixed; boundary=#{changeset_id}" unless is_get
  body.add_line "" unless is_get

  operations.each { |op|
    uri = table_service.entities_uri(@table, @partition, op[:row_key])
    body.add_line "--#{changeset_id}" unless is_get
    body.add_line "Content-Type: application/http"
    body.add_line "Content-Transfer-Encoding: binary"
    body.add_line ""
    body.add_line "#{op[:method].to_s.upcase} #{uri} HTTP/1.1"

    if op[:headers]
      op[:headers].each { |k, v|
        body.add_line "#{k}: #{v}"
      }
    end

    if op[:body]
      body.add_line "Content-Length: #{op[:body].bytesize}"
      body.add_line ""
      body.add_line op[:body]
    else
      body.add_line ""
    end

  }
  body.add_line "--#{changeset_id}--" unless is_get
  body.add_line "--#{batch_id}--"
end

#update(row_key, entity_values, options = {}) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
# File 'table/lib/azure/storage/table/batch.rb', line 264

def update(row_key, entity_values, options = {})
  check_entity_key(row_key)

  headers = { Azure::Storage::Common::HeaderConstants::ACCEPT => Serialization.get_accept_string(options[:accept]) }
  headers["If-Match"] = options[:if_match] || "*" unless options[:create_if_not_exists]

  body = Serialization.hash_to_json(entity_values)

  add_operation(:put, row_key, body, headers)
  self
end