-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdelta_cache.rs
259 lines (237 loc) · 8.22 KB
/
delta_cache.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use unleash_types::client_features::{ClientFeature, DeltaEvent, Segment};
#[derive(Debug, Clone)]
pub struct DeltaHydrationEvent {
pub event_id: u32,
pub features: Vec<ClientFeature>,
pub segments: Vec<Segment>,
}
#[derive(Debug, Clone)]
pub struct DeltaCache {
max_length: usize,
events: Vec<DeltaEvent>,
hydration_event: DeltaHydrationEvent,
}
impl DeltaCache {
pub fn new(hydration_event: DeltaHydrationEvent, max_length: usize) -> Self {
let mut cache = DeltaCache {
max_length,
events: Vec::new(),
hydration_event: hydration_event.clone(),
};
cache.add_base_event_from_hydration(&hydration_event);
cache
}
fn add_base_event_from_hydration(&mut self, hydration_event: &DeltaHydrationEvent) {
let last_feature = hydration_event
.features
.last()
.cloned()
.expect("Hydration event must have at least one feature");
self.add_events(&vec![DeltaEvent::FeatureUpdated {
event_id: hydration_event.event_id,
feature: last_feature,
}]);
}
pub fn has_revision(&self, revision: u32) -> bool {
self.get_events()
.iter()
.any(|e| e.get_event_id() == revision)
}
pub fn add_events(&mut self, events: &[DeltaEvent]) {
for event in events.iter() {
self.events.push(event.clone());
self.update_hydration_event(event);
if self.events.len() > self.max_length {
self.events.remove(0);
}
}
}
pub fn get_events(&self) -> &Vec<DeltaEvent> {
&self.events
}
pub fn is_missing_revision(&self, revision_id: u32) -> bool {
!self
.events
.iter()
.any(|event| event.get_event_id() == revision_id)
}
pub fn get_hydration_event(&self) -> &DeltaHydrationEvent {
&self.hydration_event
}
fn update_hydration_event(&mut self, event: &DeltaEvent) {
let event_id = event.get_event_id();
self.hydration_event.event_id = event_id;
match event {
DeltaEvent::FeatureUpdated { feature, .. } => {
if let Some(existing) = self
.hydration_event
.features
.iter_mut()
.find(|f| f.name == feature.name)
{
*existing = feature.clone();
} else {
self.hydration_event.features.push(feature.clone());
}
}
DeltaEvent::FeatureRemoved { feature_name, .. } => {
self.hydration_event
.features
.retain(|f| f.name != feature_name.clone());
}
DeltaEvent::SegmentUpdated { segment, .. } => {
if let Some(existing) = self
.hydration_event
.segments
.iter_mut()
.find(|s| s.id == segment.id)
{
*existing = segment.clone();
} else {
self.hydration_event.segments.push(segment.clone());
}
}
DeltaEvent::SegmentRemoved { segment_id, .. } => {
self.hydration_event
.segments
.retain(|s| s.id != *segment_id);
}
DeltaEvent::Hydration { .. } => {
// do nothing, as hydration will never end up in update events
}
}
}
}
#[cfg(test)]
mod tests {
use crate::delta_cache::{DeltaCache, DeltaHydrationEvent};
use unleash_types::client_features::{ClientFeature, DeltaEvent, Segment};
#[test]
fn test_update_hydration_event_and_remove_event_when_over_limit() {
let base_event = DeltaHydrationEvent {
event_id: 1,
features: vec![
ClientFeature {
name: "test-flag".to_string(),
..ClientFeature::default()
},
ClientFeature {
name: "my-feature-flag".to_string(),
..ClientFeature::default()
},
],
segments: vec![
Segment {
id: 1,
constraints: vec![],
},
Segment {
id: 2,
constraints: vec![],
},
],
};
let max_length = 2;
let mut delta_cache = DeltaCache::new(base_event.clone(), max_length);
let initial_events = &vec![DeltaEvent::FeatureUpdated {
event_id: 2,
feature: ClientFeature {
name: "my-feature-flag".to_string(),
..ClientFeature::default()
},
}];
delta_cache.add_events(initial_events);
let added_events = vec![
DeltaEvent::FeatureUpdated {
event_id: 3,
feature: ClientFeature {
name: "another-feature-flag".to_string(),
..ClientFeature::default()
},
},
DeltaEvent::FeatureRemoved {
event_id: 4,
feature_name: "test-flag".to_string(),
project: "default".to_string(),
},
DeltaEvent::SegmentUpdated {
event_id: 5,
segment: Segment {
id: 1,
constraints: vec![],
},
},
DeltaEvent::SegmentRemoved {
event_id: 6,
segment_id: 2,
},
DeltaEvent::SegmentUpdated {
event_id: 7,
segment: Segment {
id: 3,
constraints: vec![],
},
},
];
delta_cache.add_events(&added_events);
let events: Vec<_> = delta_cache.get_events().to_vec();
assert_eq!(events.len(), max_length);
assert_eq!(events, added_events[added_events.len() - max_length..]);
let hydration_event = delta_cache.get_hydration_event();
assert_eq!(hydration_event.features.len(), 2);
assert_eq!(hydration_event.event_id, 7);
assert!(
hydration_event
.features
.iter()
.any(|f| f.name == "my-feature-flag")
);
assert!(
hydration_event
.features
.iter()
.any(|f| f.name == "another-feature-flag")
);
assert!(hydration_event.segments.iter().any(|s| s.id == 1));
}
#[test]
fn test_prevent_mutation_of_previous_feature_updated_events() {
let base_event = DeltaHydrationEvent {
event_id: 1,
features: vec![ClientFeature {
name: "base-flag".to_string(),
..ClientFeature::default()
}],
segments: vec![],
};
let mut delta_cache = DeltaCache::new(base_event, 10);
let initial_feature_event = DeltaEvent::FeatureUpdated {
event_id: 129,
feature: ClientFeature {
name: "streaming-test".to_string(),
enabled: false,
..ClientFeature::default()
},
};
delta_cache.add_events(&vec![initial_feature_event.clone()]);
let updated_feature_event = DeltaEvent::FeatureUpdated {
event_id: 130,
feature: ClientFeature {
name: "streaming-test".to_string(),
enabled: true,
strategies: Some(vec![unleash_types::client_features::Strategy {
name: "new-strategy".into(),
sort_order: None,
segments: None,
variants: None,
constraints: None,
parameters: None,
}]),
..ClientFeature::default()
},
};
delta_cache.add_events(&vec![updated_feature_event.clone()]);
assert_eq!(delta_cache.get_events()[1], initial_feature_event);
assert_eq!(delta_cache.get_events()[2], updated_feature_event);
}
}