|
| 1 | +/* |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using OptimizelySDK.Entity; |
| 20 | + |
| 21 | +namespace OptimizelySDK.Utils |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Configuration manager for holdouts, providing flag-to-holdout relationship mapping and optimization logic. |
| 25 | + /// </summary> |
| 26 | + public class HoldoutConfig |
| 27 | + { |
| 28 | + private readonly List<Holdout> _allHoldouts; |
| 29 | + private readonly List<Holdout> _globalHoldouts; |
| 30 | + private readonly Dictionary<string, Holdout> _holdoutIdMap; |
| 31 | + private readonly Dictionary<string, List<Holdout>> _includedHoldouts; |
| 32 | + private readonly Dictionary<string, List<Holdout>> _excludedHoldouts; |
| 33 | + private readonly Dictionary<string, List<Holdout>> _flagHoldoutCache; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Initializes a new instance of the HoldoutConfig class. |
| 37 | + /// </summary> |
| 38 | + /// <param name="allHoldouts">Array of all holdouts from the datafile</param> |
| 39 | + public HoldoutConfig(Holdout[] allHoldouts = null) |
| 40 | + { |
| 41 | + _allHoldouts = allHoldouts?.ToList() ?? new List<Holdout>(); |
| 42 | + _globalHoldouts = new List<Holdout>(); |
| 43 | + _holdoutIdMap = new Dictionary<string, Holdout>(); |
| 44 | + _includedHoldouts = new Dictionary<string, List<Holdout>>(); |
| 45 | + _excludedHoldouts = new Dictionary<string, List<Holdout>>(); |
| 46 | + _flagHoldoutCache = new Dictionary<string, List<Holdout>>(); |
| 47 | + |
| 48 | + UpdateHoldoutMapping(); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Updates internal mappings of holdouts including the id map, global list, and per-flag inclusion/exclusion maps. |
| 53 | + /// </summary> |
| 54 | + private void UpdateHoldoutMapping() |
| 55 | + { |
| 56 | + // Clear existing mappings |
| 57 | + _holdoutIdMap.Clear(); |
| 58 | + _globalHoldouts.Clear(); |
| 59 | + _includedHoldouts.Clear(); |
| 60 | + _excludedHoldouts.Clear(); |
| 61 | + _flagHoldoutCache.Clear(); |
| 62 | + |
| 63 | + foreach (var holdout in _allHoldouts) |
| 64 | + { |
| 65 | + // Build ID mapping |
| 66 | + _holdoutIdMap[holdout.Id] = holdout; |
| 67 | + |
| 68 | + var hasIncludedFlags = holdout.IncludedFlags != null && holdout.IncludedFlags.Length > 0; |
| 69 | + var hasExcludedFlags = holdout.ExcludedFlags != null && holdout.ExcludedFlags.Length > 0; |
| 70 | + |
| 71 | + if (!hasIncludedFlags && !hasExcludedFlags) |
| 72 | + { |
| 73 | + // Global holdout (no included or excluded flags) |
| 74 | + _globalHoldouts.Add(holdout); |
| 75 | + } |
| 76 | + else if (hasIncludedFlags) |
| 77 | + { |
| 78 | + // Holdout with specific included flags |
| 79 | + foreach (var flagId in holdout.IncludedFlags) |
| 80 | + { |
| 81 | + if (!_includedHoldouts.ContainsKey(flagId)) |
| 82 | + _includedHoldouts[flagId] = new List<Holdout>(); |
| 83 | + |
| 84 | + _includedHoldouts[flagId].Add(holdout); |
| 85 | + } |
| 86 | + } |
| 87 | + else if (hasExcludedFlags) |
| 88 | + { |
| 89 | + // Global holdout with excluded flags |
| 90 | + _globalHoldouts.Add(holdout); |
| 91 | + |
| 92 | + foreach (var flagId in holdout.ExcludedFlags) |
| 93 | + { |
| 94 | + if (!_excludedHoldouts.ContainsKey(flagId)) |
| 95 | + _excludedHoldouts[flagId] = new List<Holdout>(); |
| 96 | + |
| 97 | + _excludedHoldouts[flagId].Add(holdout); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Returns the applicable holdouts for the given flag ID by combining global holdouts (excluding any specified) and included holdouts, in that order. |
| 105 | + /// Caches the result for future calls. |
| 106 | + /// </summary> |
| 107 | + /// <param name="flagId">The flag identifier</param> |
| 108 | + /// <returns>A list of Holdout objects relevant to the given flag</returns> |
| 109 | + public List<Holdout> GetHoldoutsForFlag(string flagId) |
| 110 | + { |
| 111 | + if (_allHoldouts.Count == 0) |
| 112 | + return new List<Holdout>(); |
| 113 | + |
| 114 | + // Check cache first |
| 115 | + if (_flagHoldoutCache.ContainsKey(flagId)) |
| 116 | + return _flagHoldoutCache[flagId]; |
| 117 | + |
| 118 | + var activeHoldouts = new List<Holdout>(); |
| 119 | + |
| 120 | + // Start with global holdouts, excluding any that are specifically excluded for this flag |
| 121 | + var excludedForFlag = _excludedHoldouts.ContainsKey(flagId) ? _excludedHoldouts[flagId] : new List<Holdout>(); |
| 122 | + |
| 123 | + foreach (var globalHoldout in _globalHoldouts) |
| 124 | + { |
| 125 | + if (!excludedForFlag.Contains(globalHoldout)) |
| 126 | + { |
| 127 | + activeHoldouts.Add(globalHoldout); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // Add included holdouts for this flag |
| 132 | + if (_includedHoldouts.ContainsKey(flagId)) |
| 133 | + { |
| 134 | + activeHoldouts.AddRange(_includedHoldouts[flagId]); |
| 135 | + } |
| 136 | + |
| 137 | + // Cache the result |
| 138 | + _flagHoldoutCache[flagId] = activeHoldouts; |
| 139 | + |
| 140 | + return activeHoldouts; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Get a Holdout object for an ID. |
| 145 | + /// </summary> |
| 146 | + /// <param name="holdoutId">The holdout identifier</param> |
| 147 | + /// <returns>The Holdout object if found, null otherwise</returns> |
| 148 | + public Holdout GetHoldout(string holdoutId) |
| 149 | + { |
| 150 | + return _holdoutIdMap.ContainsKey(holdoutId) ? _holdoutIdMap[holdoutId] : null; |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Gets the total number of holdouts. |
| 155 | + /// </summary> |
| 156 | + public int HoldoutCount => _allHoldouts.Count; |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// Gets the number of global holdouts. |
| 160 | + /// </summary> |
| 161 | + public int GlobalHoldoutCount => _globalHoldouts.Count; |
| 162 | + } |
| 163 | +} |
0 commit comments