Class: Azure::Table::Batch
- Inherits:
-
Object
- Object
- Azure::Table::Batch
- Defined in:
- lib/azure/table/batch.rb
Overview
Represents a batch of table operations.
Example usage (block syntax):
results = Batch.new “table”, “partition” do
insert "row1", {"meta"=>"data"}
insert "row2", {"meta"=>"data"}
end.execute
which is equivalent to (fluent syntax):
results = Batch.new(“table”, “partition”)
.insert("row1", {"meta"=>"data"})
.insert("row2", {"meta"=>"data"})
.execute
which is equivalent to (as class):
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
-
#batch_id ⇒ Object
Returns the value of attribute batch_id.
Instance Method Summary collapse
- #delete(row_key, options = {}) ⇒ Object
-
#initialize(table, partition, &block) ⇒ Batch
constructor
A new instance of Batch.
- #insert(row_key, entity_values) ⇒ Object
- #insert_or_merge(row_key, entity_values) ⇒ Object
- #insert_or_replace(row_key, entity_values) ⇒ Object
- #merge(row_key, entity_values, options = {}) ⇒ Object
- #parse_response(response) ⇒ Object
- #to_body ⇒ Object
- #update(row_key, entity_values, options = {}) ⇒ Object
Constructor Details
#initialize(table, partition, &block) ⇒ Batch
Returns a new instance of Batch.
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/azure/table/batch.rb', line 51 def initialize(table, partition, &block) @table = table @partition = partition @operations = [] @entity_keys = [] @table_service = Azure::Table::TableService.new @batch_id = "batch_" + SecureRandom.uuid @changeset_id = "changeset_" + SecureRandom.uuid self.instance_eval(&block) if block_given? end |
Instance Attribute Details
#batch_id ⇒ Object
Returns the value of attribute batch_id.
73 74 75 |
# File 'lib/azure/table/batch.rb', line 73 def batch_id @batch_id end |
Instance Method Details
#delete(row_key, options = {}) ⇒ Object
323 324 325 326 |
# File 'lib/azure/table/batch.rb', line 323 def delete(row_key, ={}) add_operation(:delete, @table_service.entities_uri(table, partition, row_key), nil, {"If-Match"=> [:if_match] || "*"}) self end |
#insert(row_key, entity_values) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/azure/table/batch.rb', line 205 def insert(row_key, entity_values) check_entity_key(row_key) body = Azure::Table::Serialization.hash_to_entry_xml({ "PartitionKey" => partition, "RowKey" => row_key }.merge(entity_values) ).to_xml add_operation(:post, @table_service.entities_uri(table), body) self end |
#insert_or_merge(row_key, entity_values) ⇒ Object
290 291 292 293 |
# File 'lib/azure/table/batch.rb', line 290 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
304 305 306 307 |
# File 'lib/azure/table/batch.rb', line 304 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
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/azure/table/batch.rb', line 267 def merge(row_key, entity_values, ={}) check_entity_key(row_key) uri = @table_service.entities_uri(table, partition, row_key) headers = {} headers["If-Match"] = [:if_match] || "*" unless [:create_if_not_exists] body = Azure::Table::Serialization.hash_to_entry_xml(entity_values).to_xml add_operation(:merge, uri, body, headers) self end |
#parse_response(response) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/azure/table/batch.rb', line 117 def parse_response(response) responses = BatchResponse.parse response.body 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 # entity from body result = Azure::Table::Serialization.hash_from_entry_xml(response[:body]) entity = Azure::Table::Entity.new entity.table = table entity.updated = result[:updated] entity.etag = response[:headers]["etag"] || result[:etag] entity.properties = result[:properties] 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 ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/azure/table/batch.rb', line 158 def to_body body = "" body.define_singleton_method(:add_line) do |a| self << (a||nil) + "\n" end body.add_line "--#{batch_id}" body.add_line "Content-Type: multipart/mixed; boundary=#{changeset_id}" body.add_line "" content_id = 1 operations.each { |op| body.add_line "--#{changeset_id}" 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} #{op[:uri]} HTTP/1.1" body.add_line "Content-ID: #{content_id}" if op[:headers] op[:headers].each { |k,v| body.add_line "#{k}: #{v}" } end if op[:body] body.add_line "Content-Type: application/atom+xml;type=entry" body.add_line "Content-Length: #{op[:body].bytesize.to_s}" body.add_line "" body.add_line op[:body] else body.add_line "" end content_id += 1 } body.add_line "--#{changeset_id}--" body.add_line "--#{batch_id}--" end |
#update(row_key, entity_values, options = {}) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/azure/table/batch.rb', line 235 def update(row_key, entity_values, ={}) check_entity_key(row_key) uri = @table_service.entities_uri(table, partition, row_key) headers = {} headers["If-Match"] = [:if_match] || "*" unless [:create_if_not_exists] body = Azure::Table::Serialization.hash_to_entry_xml(entity_values).to_xml add_operation(:put, uri, body, headers) self end |