Skip to content

Commit b7013d4

Browse files
committed
polish
1 parent 2e54131 commit b7013d4

7 files changed

Lines changed: 88 additions & 57 deletions

File tree

lib/splitclient-rb/cache/repositories/splits_repository.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ def initialize(config, flag_sets_repository, flag_set_filter)
4343
end
4444
@flag_sets = flag_sets_repository
4545
@flag_set_filter = flag_set_filter
46-
unless @config.mode.equal?(:consumer)
47-
@adapter.set_string(namespace_key('.splits.till'), '-1')
48-
@adapter.initialize_map(namespace_key('.segments.registered'))
49-
end
46+
initialize_keys
5047
end
5148

5249
def update(to_add, to_delete, new_change_number)
@@ -127,10 +124,7 @@ def clear
127124
@tt_cache.clear
128125

129126
@adapter.clear(namespace_key)
130-
unless @config.mode.equal?(:consumer)
131-
@adapter.set_string(namespace_key('.splits.till'), '-1')
132-
@adapter.initialize_map(namespace_key('.segments.registered'))
133-
end
127+
initialize_keys
134128
end
135129

136130
def kill(change_number, split_name, default_treatment)
@@ -171,6 +165,13 @@ def flag_set_filter
171165

172166
private
173167

168+
def initialize_keys
169+
unless @config.mode.equal?(:consumer)
170+
@adapter.set_string(namespace_key('.splits.till'), '-1')
171+
@adapter.initialize_map(namespace_key('.segments.registered'))
172+
end
173+
end
174+
174175
def add_feature_flag(split)
175176
return unless split[:name]
176177
existing_split = get_split(split[:name])

lib/splitclient-rb/engine/api/splits.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def since(since, since_rbs, fetch_options = { cache_control_headers: false, till
5959
result = convert_to_newSPEC(result)
6060
end
6161

62+
result[:rbs][:d] = check_rbs_data(result[:rbs][:d])
6263
result = objects_with_segment_names(result)
6364

6465
if @spec_version == SplitIoClient::Spec::FeatureFlags::SPEC_VERSION
@@ -97,6 +98,15 @@ def clear_storage
9798

9899
private
99100

101+
def check_rbs_data(rbs_data)
102+
rbs_data.each do |rb_segment|
103+
rb_segment[:excluded] = {:keys => [], :segments => []} if rb_segment[:excluded].nil?
104+
rb_segment[:excluded][:keys] = [] if rb_segment[:excluded][:keys].nil?
105+
rb_segment[:excluded][:segments] = [] if rb_segment[:excluded][:segments].nil?
106+
end
107+
rbs_data
108+
end
109+
100110
def objects_with_segment_names(parsed_objects)
101111
parsed_objects[:segment_names] = Set.new
102112
parsed_objects[:segment_names] =

lib/splitclient-rb/engine/matchers/rule_based_segment_matcher.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,10 @@ def match?(args)
2525
rule_based_segment = @rule_based_segments_repository.get_rule_based_segment(@segment_name)
2626
return false if rule_based_segment.nil?
2727

28-
if args[:value].nil? or args[:value].empty?
29-
key = args[:matching_key]
30-
else
31-
key = args[:value]
32-
end
28+
key = update_key(args)
3329
return false if rule_based_segment[:excluded][:keys].include?(key)
3430

35-
rule_based_segment[:excluded][:segments].each do |segment|
36-
return false if segment[:type] == 'standard' and @segments_repository.in_segment?(segment[:name], key)
37-
38-
if segment[:type] == 'rule-based'
39-
return false if match_rbs(@rule_based_segments_repository.get_rule_based_segment(segment[:name]), args)
40-
end
41-
end
31+
return false unless check_excluded_segments(rule_based_segment)
4232

4333
matches = false
4434
rule_based_segment[:conditions].each do |c|
@@ -54,13 +44,31 @@ def match?(args)
5444

5545
private
5646

57-
def match_rbs(rule_based_segment, args)
58-
rbs_matcher = RuleBasedSegmentMatcher.new(@segments_repository, @rule_based_segments_repository, rule_based_segment[:name], @config)
59-
return rbs_matcher.match?(
60-
matching_key: args[:matching_key],
61-
bucketing_key: args[:value],
62-
attributes: args[:attributes]
63-
)
47+
def check_excluded_segments(rule_based_segment)
48+
rule_based_segment[:excluded][:segments].each do |segment|
49+
return false if segment[:type] == 'standard' && @segments_repository.in_segment?(segment[:name], key)
50+
51+
return false if segment[:type] == 'rule-based' && match_rbs(
52+
@rule_based_segments_repository.get_rule_based_segment(segment[:name]), args
53+
)
54+
end
55+
True
56+
end
57+
58+
def update_key(args)
59+
if args[:value].nil? || args[:value].empty?
60+
args[:matching_key]
61+
else
62+
args[:value]
63+
end
64+
end
65+
66+
def match_rbs(rule_based_segment, args)
67+
rbs_matcher = RuleBasedSegmentMatcher.new(@segments_repository, @rule_based_segments_repository,
68+
rule_based_segment[:name], @config)
69+
rbs_matcher.match?(matching_key: args[:matching_key],
70+
bucketing_key: args[:value],
71+
attributes: args[:attributes])
6472
end
6573
end
6674
end

lib/splitclient-rb/helpers/evaluator_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def self.matcher_type(condition, segments_repository, rb_segment_repository)
88
segments_repository.adapter.pipelined do
99
condition.matchers.each do |matcher|
1010
matchers << if matcher[:negate]
11-
condition.negation_matcher(matcher_instance(matcher[:matcherType], condition, matcher, segments_repository, rb_segment_repository))
11+
condition.negation_matcher(matcher_instance(matcher[:matcherType], condition,
12+
matcher, segments_repository,
13+
rb_segment_repository))
1214
else
1315
matcher_instance(matcher[:matcherType], condition, matcher, segments_repository, rb_segment_repository)
1416
end

lib/splitclient-rb/helpers/repository_helper.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ def self.update_feature_flag_repository(feature_flag_repository, feature_flags,
1313
next
1414
end
1515

16-
unless feature_flag.key?(:impressionsDisabled)
17-
feature_flag[:impressionsDisabled] = false
18-
if config.debug_enabled
19-
config.logger.debug("feature flag (#{feature_flag[:name]}) does not have impressionsDisabled field, setting it to false")
20-
end
21-
end
16+
feature_flag = self.check_impressions_disabled(feature_flag, config)
2217

2318
config.logger.debug("storing feature flag (#{feature_flag[:name]})") if config.debug_enabled
2419
to_add.push(feature_flag)
@@ -27,6 +22,16 @@ def self.update_feature_flag_repository(feature_flag_repository, feature_flags,
2722
feature_flag_repository.update(to_add, to_delete, change_number)
2823
end
2924

25+
def self.check_impressions_disabled(feature_flag, config)
26+
unless feature_flag.key?(:impressionsDisabled)
27+
feature_flag[:impressionsDisabled] = false
28+
if config.debug_enabled
29+
config.logger.debug("feature flag (#{feature_flag[:name]}) does not have impressionsDisabled field, setting it to false")
30+
end
31+
end
32+
feature_flag
33+
end
34+
3035
def self.update_rule_based_segment_repository(rule_based_segment_repository, rule_based_segments, change_number, config)
3136
to_add = []
3237
to_delete = []

lib/splitclient-rb/helpers/util.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Util
66
def self.segment_names_by_object(object, matcher_type)
77
object[:conditions].each_with_object(Set.new) do |condition, names|
88
condition[:matcherGroup][:matchers].each do |matcher|
9-
next if matcher[:userDefinedSegmentMatcherData].nil? or matcher[:matcherType] != matcher_type
9+
next if matcher[:userDefinedSegmentMatcherData].nil? || matcher[:matcherType] != matcher_type
10+
1011
names << matcher[:userDefinedSegmentMatcherData][:segmentName]
1112
end
1213
end

lib/splitclient-rb/sse/workers/splits_worker.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module SplitIoClient
44
module SSE
55
module Workers
66
class SplitsWorker
7-
def initialize(synchronizer, config, feature_flags_repository, telemetry_runtime_producer, segment_fetcher, rule_based_segment_repository)
7+
def initialize(synchronizer, config, feature_flags_repository, telemetry_runtime_producer,
8+
segment_fetcher, rule_based_segment_repository)
89
@synchronizer = synchronizer
910
@config = config
1011
@feature_flags_repository = feature_flags_repository
@@ -68,12 +69,11 @@ def perform
6869
def update_feature_flag(notification)
6970
return true if @feature_flags_repository.get_change_number.to_i >= notification.data['changeNumber']
7071
return false unless !notification.data['d'].nil? && @feature_flags_repository.get_change_number == notification.data['pcn']
71-
new_split = return_object_from_json(notification)
72-
SplitIoClient::Helpers::RepositoryHelper.update_feature_flag_repository(@feature_flags_repository,
73-
[new_split],
74-
notification.data['changeNumber'], @config, false)
75-
fetch_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_split, "IN_SEGMENT"), @feature_flags_repository)
76-
if fetch_rule_based_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_split, "IN_RULE_BASED_SEGMENT"), notification.data['changeNumber'])
72+
73+
update_feature_flag_repository(notification)
74+
fetch_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_split, 'IN_SEGMENT'), @feature_flags_repository)
75+
if fetch_rule_based_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_split, 'IN_RULE_BASED_SEGMENT'),
76+
notification.data['changeNumber'])
7777
return true
7878
end
7979

@@ -86,18 +86,26 @@ def update_feature_flag(notification)
8686
false
8787
end
8888

89+
def update_feature_flag_repository(notification)
90+
new_split = return_object_from_json(notification)
91+
SplitIoClient::Helpers::RepositoryHelper.update_feature_flag_repository(@feature_flags_repository, [new_split],
92+
notification.data['changeNumber'], @config, false)
93+
end
94+
8995
def update_rule_based_segment(notification)
9096
return true if @rule_based_segment_repository.get_change_number.to_i >= notification.data['changeNumber']
91-
return false unless !notification.data['d'].nil? && @rule_based_segment_repository.get_change_number == notification.data['pcn']
97+
return false unless !notification.data['d'].nil? &&
98+
@rule_based_segment_repository.get_change_number == notification.data['pcn']
9299

93100
new_rb_segment = return_object_from_json(notification)
94101
SplitIoClient::Helpers::RepositoryHelper.update_rule_based_segment_repository(@rule_based_segment_repository,
95-
[new_rb_segment],
96-
notification.data['changeNumber'], @config)
97-
fetch_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_rb_segment, "IN_SEGMENT"), @rule_based_segment_repository)
102+
[new_rb_segment],
103+
notification.data['changeNumber'], @config)
104+
fetch_segments_if_not_exists(Helpers::Util.segment_names_by_object(new_rb_segment, 'IN_SEGMENT'),
105+
@rule_based_segment_repository)
98106

99-
# TODO: enable when telemetry spec is added
100-
# @telemetry_runtime_producer.record_updates_from_sse(Telemetry::Domain::Constants::SPLITS)
107+
# TODO: enable when telemetry spec is added
108+
# @telemetry_runtime_producer.record_updates_from_sse(Telemetry::Domain::Constants::SPLITS)
101109

102110
true
103111
rescue StandardError => e
@@ -110,11 +118,9 @@ def kill_feature_flag(notification)
110118
return if @feature_flags_repository.get_change_number.to_i > notification.data['changeNumber']
111119

112120
@config.logger.debug("feature_flags_worker kill #{notification.data['splitName']}, #{notification.data['changeNumber']}")
113-
@feature_flags_repository.kill(
114-
notification.data['changeNumber'],
115-
notification.data['splitName'],
116-
notification.data['defaultTreatment']
117-
)
121+
@feature_flags_repository.kill(notification.data['changeNumber'],
122+
notification.data['splitName'],
123+
notification.data['defaultTreatment'])
118124
@synchronizer.fetch_splits(notification.data['changeNumber'], 0)
119125
end
120126

@@ -124,17 +130,15 @@ def return_object_from_json(notification)
124130
end
125131

126132
def fetch_segments_if_not_exists(segment_names, object_repository)
127-
128133
return if segment_names.nil?
129134

130135
object_repository.set_segment_names(segment_names)
131136
@segment_fetcher.fetch_segments_if_not_exists(segment_names)
132137
end
133138

134139
def fetch_rule_based_segments_if_not_exists(segment_names, change_number)
135-
if segment_names.nil? or segment_names.empty? or @rule_based_segment_repository.contains?(segment_names.to_a)
136-
return false
137-
end
140+
return false if segment_names.nil? || segment_names.empty? || @rule_based_segment_repository.contains?(segment_names.to_a)
141+
138142
@synchronizer.fetch_splits(0, change_number)
139143

140144
true

0 commit comments

Comments
 (0)