added label indexing features when using label groupings

This commit is contained in:
2011-03-09 10:59:53 +00:00
parent ac338bb4f0
commit d74dc41110
5 changed files with 70 additions and 23 deletions

View File

@@ -36,6 +36,7 @@ module Redistat
KEY_EVENT = ".event:"
KEY_LEBELS = "Redistat.lables:"
KEY_EVENT_IDS = ".event_ids"
LABEL_INDEX = ".label_index:"
GROUP_SEPARATOR = "/"
class InvalidOptions < ArgumentError; end

View File

@@ -57,6 +57,10 @@ module Redistat
@label.parent_group
end
def update_label_index
@label.update_index
end
def to_s(depth = nil)
depth ||= @options[:depth]
key = self.prefix

View File

@@ -49,8 +49,24 @@ module Redistat
@groups.reverse!
end
def parent
@parent ||= self.class.new(parent_group)
end
def parent_group
groups[-2]
groups[1]
end
def sub_group
@raw.split(GROUP_SEPARATOR).last
end
def update_index
groups.each do |group|
label = self.class.new(group)
break if label.parent_group.nil?
db.sadd("#{LABEL_INDEX}#{label.parent_group}", label.sub_group) == "OK" ? true : false
end
end
end

View File

@@ -13,6 +13,7 @@ module Redistat
stats = inject_group_summaries(stats)
key.groups.each { |k|
update_key(k, stats, depth_limit, connection_ref)
k.update_label_index # TODO: add a label_indexing option
}
else
update_key(key, stats, depth_limit, connection_ref)

View File

@@ -25,30 +25,55 @@ describe Redistat::Label do
db.get("#{Redistat::KEY_LEBELS}#{label.hash}").should == name
end
it "should separate label names into groups" do
name = "message/public/offensive"
label = Redistat::Label.new(name)
label.name.should == name
label.groups.should == [ "message/public/offensive",
"message/public",
"message" ]
describe "Grouping" do
before(:each) do
@name = "message/public/offensive"
@label = Redistat::Label.new(@name)
end
it "should separate label names into groups" do
@label.name.should == @name
@label.groups.should == [ "message/public/offensive",
"message/public",
"message" ]
name = "/message/public/"
label = Redistat::Label.new(name)
label.name.should == name
label.groups.should == [ "message/public",
"message" ]
@name = "/message/public/"
@label = Redistat::Label.new(@name)
@label.name.should == @name
@label.groups.should == [ "message/public",
"message" ]
name = "message"
label = Redistat::Label.new(name)
label.name.should == name
label.groups.should == [ "message" ]
end
it "should know it's parent label group" do
name = "message/public/offensive"
label = Redistat::Label.new(name)
label.parent_group.should == 'message/public'
@name = "message"
@label = Redistat::Label.new(@name)
@label.name.should == @name
@label.groups.should == [ "message" ]
end
it "should know it's parent label group" do
@label.parent_group.should == 'message/public'
Redistat::Label.new('hello').parent_group.should be_nil
end
it "should update label index" do
db.smembers("#{Redistat::LABEL_INDEX}#{@label.parent_group}").should == []
@label.update_index
members = db.smembers("#{Redistat::LABEL_INDEX}#{@label.parent_group}") # checking 'message/public'
members.should have(1).item
members.should include('offensive')
name = "message/public/nice"
label = Redistat::Label.new(name)
label.update_index
members = db.smembers("#{Redistat::LABEL_INDEX}#{label.parent_group}") # checking 'message/public'
members.should have(2).items
members.should include('offensive')
members.should include('nice')
label = @label.parent
members = db.smembers("#{Redistat::LABEL_INDEX}#{label.parent_group}") # checking 'message'
members.should have(1).item
members.should include('public')
end
end
end