From f648af01b5d6624f5777ec4b08eb1bcd1c1353ae Mon Sep 17 00:00:00 2001 From: "FOLIO3PK\\muhammadnoman" Date: Thu, 11 Oct 2018 13:50:17 +0500 Subject: [PATCH 1/2] Refact: Copying attributes into new copiedAttributes variable --- .../java/com/optimizely/ab/Optimizely.java | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/core-api/src/main/java/com/optimizely/ab/Optimizely.java b/core-api/src/main/java/com/optimizely/ab/Optimizely.java index e4070e681..386604b1b 100644 --- a/core-api/src/main/java/com/optimizely/ab/Optimizely.java +++ b/core-api/src/main/java/com/optimizely/ab/Optimizely.java @@ -209,9 +209,11 @@ Variation activate(@Nonnull ProjectConfig projectConfig, logger.info("Not activating user \"{}\" for experiment \"{}\".", userId, experiment.getKey()); return null; } + + Map copiedAttributes = copyAttributes(attributes); // determine whether all the given attributes are present in the project config. If not, filter out the unknown // attributes. - Map filteredAttributes = filterAttributes(projectConfig, attributes); + Map filteredAttributes = filterAttributes(projectConfig, copiedAttributes); // bucket the user to the given experiment and dispatch an impression event Variation variation = decisionService.getVariation(experiment, userId, filteredAttributes); @@ -300,9 +302,11 @@ public void track(@Nonnull String eventName, return; } + Map copiedAttributes = copyAttributes(attributes); + // determine whether all the given attributes are present in the project config. If not, filter out the unknown // attributes. - Map filteredAttributes = filterAttributes(currentConfig, attributes); + Map filteredAttributes = filterAttributes(currentConfig, copiedAttributes); if (eventTags == null) { logger.warn("Event tags is null when non-null was expected. Defaulting to an empty event tags map."); @@ -407,7 +411,9 @@ else if (userId == null) { return false; } - Map filteredAttributes = filterAttributes(projectConfig, attributes); + Map copiedAttributes = copyAttributes(attributes); + + Map filteredAttributes = filterAttributes(projectConfig, copiedAttributes); FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, filteredAttributes); if (featureDecision.variation != null) { @@ -655,8 +661,8 @@ else if (userId == null) { } String variableValue = variable.getDefaultValue(); - - FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, attributes); + Map copiedAttributes = copyAttributes(attributes); + FeatureDecision featureDecision = decisionService.getVariationForFeature(featureFlag, userId, copiedAttributes); if (featureDecision.variation != null) { LiveVariableUsageInstance liveVariableUsageInstance = featureDecision.variation.getVariableIdToLiveVariableUsageInstanceMap().get(variable.getId()); @@ -694,9 +700,10 @@ public List getEnabledFeatures(@Nonnull String userId, @Nonnull Map copiedAttributes = copyAttributes(attributes); for (FeatureFlag featureFlag : projectConfig.getFeatureFlags()){ String featureKey = featureFlag.getKey(); - if(isFeatureEnabled(featureKey, userId, attributes)) + if(isFeatureEnabled(featureKey, userId, copiedAttributes)) enabledFeaturesList.add(featureKey); } @@ -717,7 +724,8 @@ Variation getVariation(@Nonnull Experiment experiment, @Nonnull String userId, @Nonnull Map attributes) throws UnknownExperimentException { - Map filteredAttributes = filterAttributes(projectConfig, attributes); + Map copiedAttributes = copyAttributes(attributes); + Map filteredAttributes = filterAttributes(projectConfig, copiedAttributes); return decisionService.getVariation(experiment, userId, filteredAttributes); } @@ -755,7 +763,8 @@ Variation getVariation(@Nonnull String experimentKey, return null; } - Map filteredAttributes = filterAttributes(projectConfig, attributes); + Map copiedAttributes = copyAttributes(attributes); + Map filteredAttributes = filterAttributes(projectConfig, copiedAttributes); return decisionService.getVariation(experiment,userId,filteredAttributes); } @@ -818,6 +827,20 @@ public UserProfileService getUserProfileService() { //======== Helper methods ========// + /** + * Helper function to check that the provided attributes are null if not than it returns a copy + * + * @param attributes the attributes map being validated + * @return copy of attributes + */ + private Map copyAttributes(Map attributes) { + Map copiedAttributes = null; + if (attributes != null) { + copiedAttributes = new HashMap<>(attributes); + } + return copiedAttributes; + } + /** * Helper method to verify that the given attributes map contains only keys that are present in the * {@link ProjectConfig}. From 29c34e7f719cff6d32810489b1fab65cea80fba2 Mon Sep 17 00:00:00 2001 From: "FOLIO3PK\\muhammadnoman" Date: Mon, 15 Oct 2018 17:31:02 +0500 Subject: [PATCH 2/2] changed description of copyAttribute function --- core-api/src/main/java/com/optimizely/ab/Optimizely.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-api/src/main/java/com/optimizely/ab/Optimizely.java b/core-api/src/main/java/com/optimizely/ab/Optimizely.java index 386604b1b..0b2a500cf 100644 --- a/core-api/src/main/java/com/optimizely/ab/Optimizely.java +++ b/core-api/src/main/java/com/optimizely/ab/Optimizely.java @@ -828,9 +828,9 @@ public UserProfileService getUserProfileService() { //======== Helper methods ========// /** - * Helper function to check that the provided attributes are null if not than it returns a copy + * Helper method which makes separate copy of attributesMap variable and returns it * - * @param attributes the attributes map being validated + * @param attributes map to copy * @return copy of attributes */ private Map copyAttributes(Map attributes) {