From 785dca5d23d7897e9026f673cdb1831881264edf Mon Sep 17 00:00:00 2001 From: Mauro Antonio Sanz Date: Fri, 2 May 2025 10:50:32 -0300 Subject: [PATCH 1/2] fixing excluded rbs --- .../split/client/dtos/ExcludedSegments.java | 11 ++++++++ .../matchers/RuleBasedSegmentMatcher.java | 25 +++++++++++-------- .../matchers/RuleBasedSegmentMatcherTest.java | 12 +++------ .../test/resources/rule_base_segments2.json | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/client/src/main/java/io/split/client/dtos/ExcludedSegments.java b/client/src/main/java/io/split/client/dtos/ExcludedSegments.java index 84dc3cf0..721a802d 100644 --- a/client/src/main/java/io/split/client/dtos/ExcludedSegments.java +++ b/client/src/main/java/io/split/client/dtos/ExcludedSegments.java @@ -1,6 +1,9 @@ package io.split.client.dtos; public class ExcludedSegments { + static final String STANDARD_TYPE = "standard"; + static final String RULE_BASED_TYPE = "rule-based"; + public ExcludedSegments() {} public ExcludedSegments(String type, String name) { this.type = type; @@ -9,4 +12,12 @@ public ExcludedSegments(String type, String name) { public String type; public String name; + + public boolean isStandard() { + return STANDARD_TYPE.equals(type); + } + + public boolean isRuleBased() { + return RULE_BASED_TYPE.equals(type); + } } diff --git a/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java index e069493c..7b602292 100644 --- a/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java @@ -18,9 +18,6 @@ * @author adil */ public class RuleBasedSegmentMatcher implements Matcher { - private final String standardType = "standard"; - private final String ruleBasedType = "rule-based"; - private final String _segmentName; public RuleBasedSegmentMatcher(String segmentName) { @@ -41,20 +38,28 @@ public boolean match(Object matchValue, String bucketingKey, Map return false; } - for (ExcludedSegments segment: parsedRuleBasedSegment.excludedSegments()) { - if (segment.type.equals(standardType) && evaluationContext.getSegmentCache().isInSegment(segment.name, (String) matchValue)) { - return false; + if (matchExcludedSegments(parsedRuleBasedSegment.excludedSegments(), matchValue, bucketingKey, attributes, evaluationContext)) { + return false; + } + + return matchConditions(parsedRuleBasedSegment.parsedConditions(), matchValue, bucketingKey, attributes, evaluationContext); + } + + private boolean matchExcludedSegments(List excludedSegments, Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + for (ExcludedSegments excludedSegment: excludedSegments) { + if (excludedSegment.isStandard() && evaluationContext.getSegmentCache().isInSegment(excludedSegment.name, (String) matchValue)) { + return true; } - if (segment.type.equals(ruleBasedType)) { - List conditions = evaluationContext.getRuleBasedSegmentCache().get(segment.name).parsedConditions(); - if (matchConditions(conditions, matchValue, bucketingKey, attributes, evaluationContext)) { + if (excludedSegment.isRuleBased()) { + RuleBasedSegmentMatcher excludedRbsMatcher = new RuleBasedSegmentMatcher(excludedSegment.name); + if (excludedRbsMatcher.match(matchValue, bucketingKey, attributes, evaluationContext)) { return true; } } } - return matchConditions(parsedRuleBasedSegment.parsedConditions(), matchValue, bucketingKey, attributes, evaluationContext); + return false; } private boolean matchConditions(List conditions, Object matchValue, String bucketingKey, diff --git a/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java b/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java index c3608722..7d5d0c48 100644 --- a/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java +++ b/client/src/test/java/io/split/engine/matchers/RuleBasedSegmentMatcherTest.java @@ -1,7 +1,6 @@ package io.split.engine.matchers; import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import io.split.client.dtos.ConditionType; import io.split.client.dtos.MatcherCombiner; import io.split.client.dtos.SplitChange; @@ -14,11 +13,9 @@ import io.split.engine.experiments.RuleBasedSegmentParser; import io.split.engine.matchers.strings.WhitelistMatcher; import io.split.storages.RuleBasedSegmentCache; -import io.split.storages.RuleBasedSegmentCacheConsumer; import io.split.storages.SegmentCache; import io.split.storages.memory.RuleBasedSegmentCacheInMemoryImp; import io.split.storages.memory.SegmentCacheInMemoryImpl; -import okhttp3.mockwebserver.MockResponse; import org.junit.Test; import org.mockito.Mockito; @@ -29,7 +26,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.Set; import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges; import static org.hamcrest.Matchers.is; @@ -128,14 +124,14 @@ public void usingRbsInExcludedTest() throws IOException { RuleBasedSegmentsToUpdate ruleBasedSegmentsToUpdate = processRuleBasedSegmentChanges(ruleBasedSegmentParser, change.ruleBasedSegments.d); ruleBasedSegmentCache.update(ruleBasedSegmentsToUpdate.getToAdd(), null, 123); - RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("no_excludes"); + RuleBasedSegmentMatcher matcher = new RuleBasedSegmentMatcher("sample_rule_based_segment"); HashMap attrib1 = new HashMap() {{ put("email", "mauro@split.io"); }}; HashMap attrib2 = new HashMap() {{ - put("email", "bilal@split.io"); + put("email", "bilal@harness.io"); }}; - assertThat(matcher.match("mauro@split.io", null, attrib1, evaluationContext), is(true)); - assertThat(matcher.match("bilal@split.io", null, attrib2, evaluationContext), is(true)); + assertThat(matcher.match("mauro", null, attrib1, evaluationContext), is(false)); + assertThat(matcher.match("bilal", null, attrib2, evaluationContext), is(true)); } } diff --git a/client/src/test/resources/rule_base_segments2.json b/client/src/test/resources/rule_base_segments2.json index fa2b006b..991fa81b 100644 --- a/client/src/test/resources/rule_base_segments2.json +++ b/client/src/test/resources/rule_base_segments2.json @@ -23,7 +23,7 @@ "negate": false, "whitelistMatcherData": { "whitelist": [ - "@split.io" + "@harness.io" ] } } From c9ec506cb80a9069c360d622c680b36a8ba4518d Mon Sep 17 00:00:00 2001 From: Mauro Antonio Sanz Date: Fri, 2 May 2025 10:54:20 -0300 Subject: [PATCH 2/2] fixing build --- .../java/io/split/engine/matchers/RuleBasedSegmentMatcher.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java index 7b602292..4c74527b 100644 --- a/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java +++ b/client/src/main/java/io/split/engine/matchers/RuleBasedSegmentMatcher.java @@ -45,7 +45,8 @@ public boolean match(Object matchValue, String bucketingKey, Map return matchConditions(parsedRuleBasedSegment.parsedConditions(), matchValue, bucketingKey, attributes, evaluationContext); } - private boolean matchExcludedSegments(List excludedSegments, Object matchValue, String bucketingKey, Map attributes, EvaluationContext evaluationContext) { + private boolean matchExcludedSegments(List excludedSegments, Object matchValue, String bucketingKey, + Map attributes, EvaluationContext evaluationContext) { for (ExcludedSegments excludedSegment: excludedSegments) { if (excludedSegment.isStandard() && evaluationContext.getSegmentCache().isInSegment(excludedSegment.name, (String) matchValue)) { return true;