1
1
//
2
- // Copyright 2022, Optimizely, Inc. and contributors
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
2
+ // Copyright 2022, Optimizely, Inc. and contributors
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
5
// you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
6
+ // You may obtain a copy of the License at
7
+ //
8
8
// http://www.apache.org/licenses/LICENSE-2.0
9
9
//
10
10
// Unless required by applicable law or agreed to in writing, software
@@ -24,11 +24,12 @@ struct HoldoutConfig {
24
24
}
25
25
private( set) var holdoutIdMap : [ String : Holdout ] = [ : ]
26
26
private( set) var global : [ Holdout ] = [ ]
27
- private( set) var others : [ Holdout ] = [ ]
28
27
private( set) var includedHoldouts : [ String : [ Holdout ] ] = [ : ]
29
28
private( set) var excludedHoldouts : [ String : [ Holdout ] ] = [ : ]
30
29
private( set) var flagHoldoutsMap : [ String : [ Holdout ] ] = [ : ]
31
30
31
+ let logger = OPTLoggerFactory . getLogger ( )
32
+
32
33
init ( allholdouts: [ Holdout ] = [ ] ) {
33
34
self . allHoldouts = allholdouts
34
35
updateHoldoutProperties ( )
@@ -42,15 +43,19 @@ struct HoldoutConfig {
42
43
} ( )
43
44
flagHoldoutsMap = [ : ]
44
45
global = [ ]
45
- others = [ ]
46
+
46
47
includedHoldouts = [ : ]
47
48
excludedHoldouts = [ : ]
48
49
49
50
for holdout in allHoldouts {
50
51
switch ( holdout. includedFlags. isEmpty, holdout. excludedFlags. isEmpty) {
51
52
case ( true , true ) :
52
53
global. append ( holdout)
53
- case ( false , _) :
54
+
55
+ case ( false , false ) :
56
+ logger. e ( . holdoutToFlagMappingError)
57
+
58
+ case ( false , true ) :
54
59
holdout. includedFlags. forEach { flagId in
55
60
if var existing = includedHoldouts [ flagId] {
56
61
existing. append ( holdout)
@@ -59,8 +64,10 @@ struct HoldoutConfig {
59
64
includedHoldouts [ flagId] = [ holdout]
60
65
}
61
66
}
62
- case ( _, false ) :
63
- others. append ( holdout)
67
+
68
+ case ( true , false ) :
69
+ global. append ( holdout)
70
+
64
71
holdout. excludedFlags. forEach { flagId in
65
72
if var existing = excludedHoldouts [ flagId] {
66
73
existing. append ( holdout)
@@ -76,19 +83,29 @@ struct HoldoutConfig {
76
83
mutating func getHoldoutForFlag( id: String ) -> [ Holdout ] {
77
84
guard !allHoldouts. isEmpty else { return [ ] }
78
85
86
+ // Check cache and return if persist holdouts
79
87
if let holdouts = flagHoldoutsMap [ id] {
80
88
return holdouts
81
89
}
82
90
83
- if let included = includedHoldouts [ id] , !included. isEmpty {
84
- flagHoldoutsMap [ id] = global + included
85
- } else {
86
- let excluded = excludedHoldouts [ id] ?? [ ]
87
- let filteredHoldouts = others. filter { holdout in
91
+ var activeHoldouts : [ Holdout ] = [ ]
92
+
93
+ let excluded = excludedHoldouts [ id] ?? [ ]
94
+
95
+ if !excluded. isEmpty {
96
+ activeHoldouts = global. filter { holdout in
88
97
return !excluded. contains ( holdout)
89
98
}
90
- flagHoldoutsMap [ id] = global + filteredHoldouts
99
+ } else {
100
+ activeHoldouts = global
91
101
}
102
+
103
+ let includedHoldouts = includedHoldouts [ id] ?? [ ]
104
+
105
+ activeHoldouts += includedHoldouts
106
+
107
+ flagHoldoutsMap [ id] = activeHoldouts
108
+
92
109
return flagHoldoutsMap [ id] ?? [ ]
93
110
}
94
111
0 commit comments