From 2f95aab874c995a091af438a476e26cebb7a8b92 Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Tue, 19 Nov 2013 19:23:13 +0100 Subject: [PATCH 1/2] Don't increase opslog count when overwriting existing binary file --- lib/remote_storage/riak.rb | 2 +- spec/riak_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index c237d2b..d32ffd3 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -92,7 +92,7 @@ module RemoteStorage server.halt 412 unless required_match == object.etag end - object_exists = !object.raw_data.nil? + object_exists = !object.raw_data.nil? || !object.meta["binary_key"].nil? existing_object_size = object_size(object) server.halt 412 if object_exists && server.env["HTTP_IF_NONE_MATCH"] == "*" diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index 1e1c3ba..6b3b9f3 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -397,6 +397,27 @@ describe "App with Riak backend" do log_entry.data["category"].must_equal "documents" log_entry.indexes["user_id_bin"].must_include "jimmy" end + + context "overwriting existing file with different file" do + before do + header "Content-Type", "image/jpeg; charset=binary" + filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg") + @image = File.open(filename, "r").read + put "/jimmy/documents/jaypeg", @image+"foo" + end + + it "logs the operation changing only the size" do + objects = [] + opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil } + + objects.size.must_equal 2 + + log_entry = objects.select{|o| o.data["count"] == 0}.first + log_entry.data["size"].must_equal 3 + log_entry.data["category"].must_equal "documents" + log_entry.indexes["user_id_bin"].must_include "jimmy" + end + end end context "no binary charset in content-type header" do From 17068f33dfe1fe0f2e08cc8f08d4de0c6bce0439 Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Tue, 19 Nov 2013 19:35:04 +0100 Subject: [PATCH 2/2] Don't write any opslog when count and size didn't change (same file) --- lib/remote_storage/riak.rb | 5 ++++- spec/riak_spec.rb | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index d32ffd3..2ef3c39 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -173,11 +173,14 @@ module RemoteStorage end def log_operation(user, directory, count, new_size=0, old_size=0) + size = (-old_size + new_size) + return if count == 0 && size == 0 + log_entry = opslog_bucket.new log_entry.content_type = "application/json" log_entry.data = { "count" => count, - "size" => (-old_size + new_size), + "size" => size, "category" => extract_category(directory) } log_entry.indexes.merge!({:user_id_bin => [user]}) diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index 6b3b9f3..bc3ea04 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -398,6 +398,22 @@ describe "App with Riak backend" do log_entry.indexes["user_id_bin"].must_include "jimmy" end + context "overwriting existing file with same file" do + before do + header "Content-Type", "image/jpeg; charset=binary" + filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg") + @image = File.open(filename, "r").read + put "/jimmy/documents/jaypeg", @image + end + + it "doesn't log the operation" do + objects = [] + opslog_bucket.keys.each { |k| objects << opslog_bucket.get(k) rescue nil } + + objects.size.must_equal 1 + end + end + context "overwriting existing file with different file" do before do header "Content-Type", "image/jpeg; charset=binary"