From 8a0e1a47a299774092e2be1e22d597bdd34bc054 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 22 Nov 2010 13:28:19 +0000 Subject: [PATCH] added proper support for hashed_label option, and disabled it by default --- lib/redistat/key.rb | 14 +++++++------- lib/redistat/label.rb | 23 +++++++++++++++-------- lib/redistat/model.rb | 8 ++++++++ spec/key_spec.rb | 12 ++++++------ spec/label_spec.rb | 14 +++++++------- spec/model_helper.rb | 1 + spec/model_spec.rb | 6 ++++++ 7 files changed, 50 insertions(+), 28 deletions(-) diff --git a/lib/redistat/key.rb b/lib/redistat/key.rb index fbcaf4c..d253a5d 100644 --- a/lib/redistat/key.rb +++ b/lib/redistat/key.rb @@ -5,20 +5,20 @@ module Redistat attr_accessor :date attr_accessor :options - def initialize(scope, label = nil, date = nil, options = {}) + def initialize(scope, label_name = nil, time_stamp = nil, options = {}) + @options = default_options.merge(options || {}) @scope = scope - self.label = label if !label.nil? - self.date = date ||= Time.now - @options = default_options.merge(options ||= {}) + self.label = label_name if !label_name.nil? + self.date = time_stamp ||= Time.now end def default_options - { :depth => :day } + { :depth => :hour, :hashed_label => false } end def prefix key = "#{@scope}" - key << "/" + ((@options[:label_hash].nil? || @options[:label_hash] == true) ? @label.hash : @label.name) if !label.nil? + key << "/#{label}" if !label.nil? key << ":" key end @@ -40,7 +40,7 @@ module Redistat end def label=(input) - @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input) + @label = (input.instance_of?(Redistat::Label)) ? input : Label.create(input, @options) end def to_s(depth = nil) diff --git a/lib/redistat/label.rb b/lib/redistat/label.rb index 1a9c53d..1236bd4 100644 --- a/lib/redistat/label.rb +++ b/lib/redistat/label.rb @@ -2,16 +2,23 @@ module Redistat class Label include Database - attr_reader :name - attr_reader :hash + attr_reader :raw - def initialize(str) - @name = str.to_s - @hash = Digest::SHA1.hexdigest(@name) + def initialize(str, options = {}) + @options = options + @raw = str.to_s + end + + def name + @options[:hashed_label] ? hash : @raw + end + + def hash + @hash ||= Digest::SHA1.hexdigest(@raw) end def save - @saved = (db.set("#{KEY_LEBELS}#{@hash}", @name) == "OK") + @saved = (db.set("#{KEY_LEBELS}#{hash}", @raw) == "OK") if @options[:hashed_label] self end @@ -19,8 +26,8 @@ module Redistat @saved ||= false end - def self.create(name) - self.new(name).save + def self.create(name, options = {}) + self.new(name, options).save end end diff --git a/lib/redistat/model.rb b/lib/redistat/model.rb index 677c524..1bd1e60 100644 --- a/lib/redistat/model.rb +++ b/lib/redistat/model.rb @@ -20,6 +20,14 @@ module Redistat end alias :find :fetch + def hashed_label(boolean = nil) + if !boolean.nil? + options[:hashed_label] = boolean + else + options[:hashed_label] || nil + end + end + def depth(depth = nil) if !depth.nil? options[:depth] = depth diff --git a/spec/key_spec.rb b/spec/key_spec.rb index 319d9f8..bb2cd20 100644 --- a/spec/key_spec.rb +++ b/spec/key_spec.rb @@ -19,24 +19,24 @@ describe Redistat::Key do end it "should convert to string properly" do - @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" + @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}" props = [:year, :month, :day, :hour, :min, :sec] props.each do - @key.to_s(props.last).should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(props.last)}" + @key.to_s(props.last).should == "#{@scope}/#{@label}:#{@key.date.to_s(props.last)}" props.pop end end - it "should abide to hash_label option" do - @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => true}) + it "should abide to hashed_label option" do + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => true}) @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}" - @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :label_hash => false}) + @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => false}) @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}" end it "should have default depth option" do @key = Redistat::Key.new(@scope, @label, @date) - @key.depth.should == :day + @key.depth.should == :hour end it "should allow changing attributes" do diff --git a/spec/label_spec.rb b/spec/label_spec.rb index 32c7681..abe277c 100644 --- a/spec/label_spec.rb +++ b/spec/label_spec.rb @@ -15,14 +15,14 @@ describe Redistat::Label do end it "should store a label hash lookup key" do - @label.save - @label.saved?.should be_true - db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name + label = Redistat::Label.new(@name, {:hashed_label => true}).save + label.saved?.should be_true + db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == @name - @name = "contact_us" - @label = Redistat::Label.create(@name) - @label.saved?.should be_true - db.get("#{Redistat::KEY_LEBELS}#{@label.hash}").should == @name + name = "contact_us" + label = Redistat::Label.create(name, {:hashed_label => true}) + label.saved?.should be_true + db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name end end \ No newline at end of file diff --git a/spec/model_helper.rb b/spec/model_helper.rb index fd31251..87124c0 100644 --- a/spec/model_helper.rb +++ b/spec/model_helper.rb @@ -11,5 +11,6 @@ class ModelHelper2 depth :day store_event true + hashed_label true end \ No newline at end of file diff --git a/spec/model_spec.rb b/spec/model_spec.rb index 3894cf8..3326257 100644 --- a/spec/model_spec.rb +++ b/spec/model_spec.rb @@ -16,17 +16,23 @@ describe Redistat::Model do it "should listen to model-defined options" do ModelHelper2.depth.should == :day ModelHelper2.store_event.should == true + ModelHelper2.hashed_label.should == true ModelHelper.depth.should == nil ModelHelper.store_event.should == nil + ModelHelper.hashed_label.should == nil ModelHelper.depth(:hour) ModelHelper.depth.should == :hour ModelHelper.store_event(true) ModelHelper.store_event.should == true + ModelHelper.hashed_label(true) + ModelHelper.hashed_label.should == true ModelHelper.options[:depth] = nil ModelHelper.options[:store_event] = nil + ModelHelper.options[:hashed_label] = nil ModelHelper.depth.should == nil ModelHelper.store_event.should == nil + ModelHelper.hashed_label.should == nil end it "should store and fetch stats" do