Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Set for users and groups, for better lookup performance #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/rollout.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "rollout/version"
require "rollout/legacy"
require "zlib"
require 'set'

class Rollout
class Feature
Expand All @@ -14,15 +15,15 @@ def initialize(name, string = nil, opts = {})
if string
raw_percentage,raw_users,raw_groups = string.split("|")
@percentage = raw_percentage.to_i
@users = (raw_users || "").split(",").map(&:to_s)
@groups = (raw_groups || "").split(",").map(&:to_sym)
@users = (raw_users || "").split(",").map(&:to_s).to_set
@groups = (raw_groups || "").split(",").map(&:to_sym).to_set
else
clear
end
end

def serialize
"#{@percentage}|#{@users.join(",")}|#{@groups.join(",")}"
"#{@percentage}|#{@users.to_a.join(",")}|#{@groups.to_a.join(",")}"
end

def add_user(user)
Expand All @@ -43,8 +44,8 @@ def remove_group(group)
end

def clear
@groups = []
@users = []
@groups = Set.new
@users = Set.new
@percentage = 0
end

Expand Down
20 changes: 10 additions & 10 deletions spec/rollout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
end

it "leaves the other groups active" do
@rollout.get(:chat).groups.should == [:fivesonly]
@rollout.get(:chat).groups.should == [:fivesonly].to_set
end
end

Expand Down Expand Up @@ -139,7 +139,7 @@
end

it "remains active for other active users" do
@rollout.get(:chat).users.should == %w(24)
@rollout.get(:chat).users.should == %w(24).to_set
end
end

Expand Down Expand Up @@ -291,13 +291,13 @@

it "returns the feature object" do
feature = @rollout.get(:chat)
feature.groups.should == [:caretakers, :greeters]
feature.groups.should == [:caretakers, :greeters].to_set
feature.percentage.should == 10
feature.users.should == %w(42)
feature.users.should == %w(42).to_set
feature.to_hash.should == {
:groups => [:caretakers, :greeters],
:groups => [:caretakers, :greeters].to_set,
:percentage => 10,
:users => %w(42)
:users => %w(42).to_set
}

feature = @rollout.get(:signup)
Expand All @@ -320,8 +320,8 @@
features.each do |feature|
@rollout.get(feature).to_hash.should == {
:percentage => 0,
:users => [],
:groups => []
:users => Set.new,
:groups => Set.new
}
end
end
Expand Down Expand Up @@ -350,8 +350,8 @@
@legacy.deactivate_all(:chat)
@rollout.get(:chat).to_hash.should == {
:percentage => 12,
:users => %w(24 42),
:groups => [:dope_people]
:users => %w(24 42).to_set,
:groups => [:dope_people].to_set
}
@redis.get("feature:chat").should_not be_nil
end
Expand Down