diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 715d5e0..9fa0ba3 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -158,27 +158,22 @@ module RemoteStorage def log_object_size(user, directory, new_size=0, old_size=0) category = extract_category(directory) - info = info_bucket.get_or_new("usage:size:#{user}:#{category}") - info.content_type = "text/plain" - - size = -old_size + new_size - size += info.data.to_i - - info.data = size.to_s + info = info_bucket.get_or_new("usage:#{user}:#{category}") + info.content_type = "application/json" + info.data ||= {} + info.data["size"] ||= 0 + info.data["size"] += (-old_size + new_size) info.indexes.merge!({:user_id_bin => [user]}) info.store end def log_object_count(user, directory, change) category = extract_category(directory) - - info = info_bucket.get_or_new("usage:count:#{user}:#{category}") - info.content_type = "text/plain" - - count = change.to_i - count += info.data.to_i - - info.data = count.to_s + info = info_bucket.get_or_new("usage:#{user}:#{category}") + info.content_type = "application/json" + info.data ||= {} + info.data["count"] ||= 0 + info.data["count"] += change info.indexes.merge!({:user_id_bin => [user]}) info.store end diff --git a/spec/riak_spec.rb b/spec/riak_spec.rb index c985fa8..337904e 100644 --- a/spec/riak_spec.rb +++ b/spec/riak_spec.rb @@ -1,9 +1,11 @@ require_relative "spec_helper" def set_usage_info(user, category, type, value) - object = info_bucket.get_or_new("usage:#{type}:#{user}:#{category}") - object.content_type = "text/plain" - object.data = value.to_s + object = info_bucket.get_or_new("usage:#{user}:#{category}") + object.content_type = "application/json" + data = object.data || {} + data.merge!(type => value) + object.data = data object.store end @@ -93,7 +95,7 @@ describe "App with Riak backend" do describe "PUT" do before do header "Authorization", "Bearer 123" - set_usage_info "jimmy", "documents", "size", "23" + set_usage_info "jimmy", "documents", "size", 23 end describe "with implicit content type" do @@ -112,10 +114,9 @@ describe "App with Riak backend" do end it "increases the usage size counter" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "35" - - indexes = info_bucket.get("usage:size:jimmy:documents").indexes - indexes["user_id_bin"].must_include "jimmy" + usage = info_bucket.get("usage:jimmy:documents") + usage.data["size"].must_equal 35 + usage.indexes["user_id_bin"].must_include "jimmy" end it "indexes the data set" do @@ -143,8 +144,8 @@ describe "App with Riak backend" do data_bucket.get("jimmy:documents:jason").content_type.must_equal "application/json" end - it "increases the overall category size" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "49" + it "increases the category size counter" do + info_bucket.get("usage:jimmy:documents").data["size"].must_equal 49 end it "delivers the data correctly" do @@ -206,7 +207,7 @@ describe "App with Riak backend" do describe "with existing content" do before do - set_usage_info "jimmy", "documents", "size", "10" + set_usage_info "jimmy", "documents", "size", 10 put "/jimmy/documents/archive/foo", "lorem ipsum" put "/jimmy/documents/archive/foo", "some awesome content" end @@ -216,33 +217,29 @@ describe "App with Riak backend" do data_bucket.get("jimmy:documents/archive:foo").data.must_equal "some awesome content" end - it "increases the overall category size" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "30" + it "increases the category size counter" do + info_bucket.get("usage:jimmy:documents").data["size"].must_equal 30 end end describe "public data" do before do - set_usage_info "jimmy", "public/documents", "size", "10" - set_usage_info "jimmy", "public/documents", "count", "100" + set_usage_info "jimmy", "public/documents", "size", 10 + set_usage_info "jimmy", "public/documents", "count", 100 put "/jimmy/public/documents/notes/foo", "note to self" end - # after do - # info_bucket.delete "usage:size:jimmy:public/documents" - # end - it "saves the value" do last_response.status.must_equal 200 data_bucket.get("jimmy:public/documents/notes:foo").data.must_equal "note to self" end it "increases the category size counter" do - info_bucket.get("usage:size:jimmy:public/documents").data.must_equal "22" + info_bucket.get("usage:jimmy:public/documents").data["size"].must_equal 22 end it "increases the category object counter" do - info_bucket.get("usage:count:jimmy:public/documents").data.must_equal "101" + info_bucket.get("usage:jimmy:public/documents").data["count"].must_equal 101 end end @@ -269,8 +266,8 @@ describe "App with Riak backend" do last_response.body.must_equal @image end - it "increases the overall category size" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "16067" + it "increases the category size counter" do + info_bucket.get("usage:jimmy:documents").data["size"].must_equal 16067 end it "indexes the binary set" do @@ -357,8 +354,8 @@ describe "App with Riak backend" do describe "DELETE" do before do header "Authorization", "Bearer 123" - set_usage_info "jimmy", "documents", "size", "123" - set_usage_info "jimmy", "documents", "count", "1000" + set_usage_info "jimmy", "documents", "size", 123 + set_usage_info "jimmy", "documents", "count", 1000 delete "/jimmy/documents/foo" end @@ -370,11 +367,11 @@ describe "App with Riak backend" do end it "decreases the category size counter" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "101" + info_bucket.get("usage:jimmy:documents").data["size"].must_equal 101 end it "decreases the category object counter" do - info_bucket.get("usage:count:jimmy:documents").data.must_equal "999" + info_bucket.get("usage:jimmy:documents").data["count"].must_equal 999 end context "binary data" do @@ -383,7 +380,8 @@ describe "App with Riak backend" do filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg") @image = File.open(filename, "r").read put "/jimmy/documents/jaypeg", @image - set_usage_info "jimmy", "documents", "size", "100000" + set_usage_info "jimmy", "documents", "size", 100000 + set_usage_info "jimmy", "documents", "count", 10 delete "/jimmy/documents/jaypeg" end @@ -402,8 +400,12 @@ describe "App with Riak backend" do }.must_raise Riak::HTTPFailedRequest end - it "decreases the overall category size" do - info_bucket.get("usage:size:jimmy:documents").data.must_equal "83956" + it "decreases the category size counter" do + info_bucket.get("usage:jimmy:documents").data["size"].must_equal 83956 + end + + it "decreases the category object counter" do + info_bucket.get("usage:jimmy:documents").data["count"].must_equal 9 end end end