From 6907650bcbeecfea2052e4c2cb3920d63f31e0c4 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Tue, 20 Feb 2018 15:16:34 +0100 Subject: [PATCH] Use bulk delete in CellarService when possible --- lib/active_storage/service/cellar_service.rb | 24 ++++++++++--------- .../service/cellar_service_spec.rb | 1 - 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/active_storage/service/cellar_service.rb b/lib/active_storage/service/cellar_service.rb index 25cc6854a..e7cc5a85e 100644 --- a/lib/active_storage/service/cellar_service.rb +++ b/lib/active_storage/service/cellar_service.rb @@ -61,7 +61,9 @@ module ActiveStorage # TODO: error handling instrument :delete, key: key do http_start do |http| - perform_delete(http, key) + request = Net::HTTP::Delete.new(URI::join(@endpoint, "/#{key}")) + sign(request, key) + http.request(request) end end end @@ -71,10 +73,16 @@ module ActiveStorage # TODO: handle pagination if more than 1000 keys instrument :delete_prefixed, prefix: prefix do http_start do |http| - list_prefixed(http, prefix).each do |key| - # TODO: use bulk delete instead - perform_delete(http, key) - end + keys = list_prefixed(http, prefix) + request_body = bulk_deletion_request_body(keys) + checksum = Digest::MD5.base64digest(request_body) + request = Net::HTTP::Post.new(URI::join(@endpoint, "/?delete")) + request.content_type = 'text/xml' + request['Content-MD5'] = checksum + request['Content-Length'] = request_body.length + request.body = request_body + sign(request, "?delete", checksum: checksum) + http.request(request) end end end @@ -198,12 +206,6 @@ module ActiveStorage builder.to_xml end - def perform_delete(http, key) - request = Net::HTTP::Delete.new(URI::join(@endpoint, "/#{key}")) - sign(request, key) - http.request(request) - end - def with_io_length(io) if io.respond_to?(:size) && io.respond_to?(:pos) yield(io, io.size - io.pos) diff --git a/spec/lib/active_storage/service/cellar_service_spec.rb b/spec/lib/active_storage/service/cellar_service_spec.rb index eae73bd32..88e965cc5 100644 --- a/spec/lib/active_storage/service/cellar_service_spec.rb +++ b/spec/lib/active_storage/service/cellar_service_spec.rb @@ -148,7 +148,6 @@ describe 'CellarService' do let(:expected_response) do ' - true chapi