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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! The consumer may define declarative filters that are sent to the producer and evaluated on the producer side
//! before emitting the events.
//!
//! Consumer filters consists of exclude and include criteria. In short, the exclude criteria are evaluated first
//! and may be overridden by an include criteria. More precisely, they are evaluated according to the following rules:
//!
//! * Exclude criteria are evaluated first.
//! * If no matching exclude criteria the event is emitted.
//! * If an exclude criteria is matching the include criteria are evaluated.
//! * If no matching include criteria the event is discarded.
//! * If matching include criteria the event is emitted.
//!
//! The exclude criteria can be a combination of:
//!
//! * ExcludeTags - exclude events with any of the given tags
//! * ExcludeRegexEntityIds - exclude events for entities with entity ids matching the given regular expressions
//! * ExcludeEntityIds - exclude events for entities with the given entity ids
//!
//! To exclude all events you can use ExcludeRegexEntityIds with .*.
//!
//! The include criteria can be a combination of:
//!
//! * IncludeTopics - include events with any of the given matching topics
//! * IncludeTags - include events with any of the given tags
//! * IncludeRegexEntityIds - include events for entities with entity ids matching the given regular expressions
//! * IncludeEntityIds - include events for entities with the given entity ids

use std::fmt::Display;

use akka_persistence_rs::{EntityId, Tag, WithPersistenceId, WithTags};
use mqtt::{TopicFilter, TopicNameRef};
use regex::Regex;

#[derive(Clone)]
pub struct EntityIdOffset {
    pub entity_id: EntityId,
    // If this is defined (> 0) events are replayed from the given
    // sequence number (inclusive).
    pub seq_nr: u64,
}

#[derive(Clone)]
pub struct ComparableRegex(pub Regex);

impl PartialEq for ComparableRegex {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_str() == other.0.as_str()
    }
}

impl Eq for ComparableRegex {}

impl PartialOrd for ComparableRegex {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ComparableRegex {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.as_str().cmp(other.0.as_str())
    }
}

#[derive(Debug, Clone, Ord, Eq, PartialEq, PartialOrd)]
pub struct TopicMatcher(TopicFilter);

#[derive(Debug)]
pub struct BadTopicMatcher;

impl TopicMatcher {
    pub fn new<S: Into<String>>(matcher: S) -> Result<Self, BadTopicMatcher> {
        Ok(Self(
            TopicFilter::new(matcher).map_err(|_| BadTopicMatcher)?,
        ))
    }
}

impl Display for TopicMatcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

/// Exclude criteria are evaluated first.
/// If no matching exclude criteria the event is emitted.
/// If an exclude criteria is matching the include criteria are evaluated.
///   If no matching include criteria the event is discarded.
///   If matching include criteria the event is emitted.
#[derive(Clone)]
pub enum FilterCriteria {
    /// Exclude events with any of the given tags, unless there is a
    /// matching include filter that overrides the exclude.
    ExcludeTags { tags: Vec<Tag> },
    /// Remove a previously added `ExcludeTags`.
    RemoveExcludeTags { tags: Vec<Tag> },
    /// Include events with any of the given tags. A matching include overrides
    /// a matching exclude.
    IncludeTags { tags: Vec<Tag> },
    /// Remove a previously added `IncludeTags`.
    RemoveIncludeTags { tags: Vec<Tag> },
    /// Exclude events for entities with entity ids matching the given regular expressions,
    /// unless there is a matching include filter that overrides the exclude.
    ExcludeRegexEntityIds { matching: Vec<ComparableRegex> },
    /// Remove a previously added `ExcludeRegexEntityIds`.
    RemoveExcludeRegexEntityIds { matching: Vec<ComparableRegex> },
    /// Include events for entities with entity ids matching the given regular expressions.
    /// A matching include overrides a matching exclude.
    IncludeRegexEntityIds { matching: Vec<ComparableRegex> },
    /// Remove a previously added `IncludeRegexEntityIds`.
    RemoveIncludeRegexEntityIds { matching: Vec<ComparableRegex> },
    /// Exclude events for entities with the given entity ids,
    /// unless there is a matching include filter that overrides the exclude.
    ExcludeEntityIds { entity_ids: Vec<EntityId> },
    /// Remove a previously added `ExcludeEntityIds`.
    RemoveExcludeEntityIds { entity_ids: Vec<EntityId> },
    /// Include events for entities with the given entity ids. A matching include overrides
    /// a matching exclude.
    ///
    /// For the given entity ids a `seq_nr` can be defined to replay all events for the entity
    /// from the sequence number (inclusive). If `seq_nr` is 0 events will not be replayed.
    IncludeEntityIds {
        entity_id_offsets: Vec<EntityIdOffset>,
    },
    /// Remove a previously added `IncludeEntityIds`.
    RemoveIncludeEntityIds { entity_ids: Vec<EntityId> },
    /// Include events with any of the given matching topics. A matching include overrides
    /// a matching exclude.
    IncludeTopics { expressions: Vec<TopicMatcher> },
    /// Remove a previously added `IncludeTopics`.
    RemoveIncludeTopics { expressions: Vec<TopicMatcher> },
}

/// Exclude events from all entity ids, convenience for combining with for example a topic filter
/// to include only events matching the topic filter.
pub fn exclude_all() -> FilterCriteria {
    FilterCriteria::ExcludeRegexEntityIds {
        matching: vec![ComparableRegex(Regex::new(".*").unwrap())],
    }
}

/// A collection of criteria
pub struct Filter {
    topic_tag_prefix: Tag,
    max_tags: usize,
    exclude_tags: Vec<Tag>,
    include_tags: Vec<Tag>,
    max_regex_entity_ids: usize,
    exclude_regex_entity_ids: Vec<ComparableRegex>,
    include_regex_entity_ids: Vec<ComparableRegex>,
    max_entity_ids: usize,
    exclude_entity_ids: Vec<EntityId>,
    include_entity_ids: Vec<EntityId>,
    max_topics: usize,
    include_topics: Vec<TopicMatcher>,
}

impl Default for Filter {
    fn default() -> Self {
        Self {
            topic_tag_prefix: Tag::from("t:"),
            max_tags: 10,
            exclude_tags: vec![],
            include_tags: vec![],
            max_regex_entity_ids: 10,
            exclude_regex_entity_ids: vec![],
            include_regex_entity_ids: vec![],
            max_entity_ids: 10,
            exclude_entity_ids: vec![],
            include_entity_ids: vec![],
            max_topics: 10,
            include_topics: vec![],
        }
    }
}

impl Filter {
    pub fn new(
        topic_tag_prefix: Tag,
        max_tags: usize,
        max_regex_entity_ids: usize,
        max_entity_ids: usize,
        max_topics: usize,
    ) -> Self {
        Self {
            topic_tag_prefix,
            max_tags,
            exclude_tags: vec![],
            include_tags: vec![],
            max_regex_entity_ids,
            exclude_regex_entity_ids: vec![],
            include_regex_entity_ids: vec![],
            max_entity_ids,
            exclude_entity_ids: vec![],
            include_entity_ids: vec![],
            max_topics,
            include_topics: vec![],
        }
    }

    /// A function that matches an envelope with criteria and passes it through if matched.
    pub fn matches<Envelope>(&self, envelope: &Envelope) -> bool
    where
        Envelope: WithPersistenceId + WithTags,
    {
        let tags = envelope.tags();
        let persistence_id = envelope.persistence_id();
        let entity_id = &persistence_id.entity_id;

        if self.matches_exclude_tags(tags)
            || self.matches_exclude_entity_ids(entity_id)
            || self.matches_exclude_regex_entity_ids(entity_id)
        {
            self.matches_include_tags(tags)
                || self.matches_include_topics(tags)
                || self.matches_include_entity_ids(entity_id)
                || self.matches_include_regex_entity_ids(entity_id)
        } else {
            true
        }
    }

    fn matches_exclude_regex_entity_ids(&self, entity_id: &EntityId) -> bool {
        Self::matches_regex_entity_ids(&self.exclude_regex_entity_ids, entity_id)
    }

    fn matches_include_regex_entity_ids(&self, entity_id: &EntityId) -> bool {
        Self::matches_regex_entity_ids(&self.include_regex_entity_ids, entity_id)
    }

    fn matches_exclude_entity_ids(&self, entity_id: &EntityId) -> bool {
        Self::matches_entity_ids(&self.exclude_entity_ids, entity_id)
    }

    fn matches_include_entity_ids(&self, entity_id: &EntityId) -> bool {
        Self::matches_entity_ids(&self.include_entity_ids, entity_id)
    }

    fn matches_exclude_tags(&self, tags: &[Tag]) -> bool {
        Self::matches_tags(&self.exclude_tags, tags)
    }

    fn matches_include_tags(&self, tags: &[Tag]) -> bool {
        Self::matches_tags(&self.include_tags, tags)
    }

    fn matches_include_topics(&self, tags: &[Tag]) -> bool {
        Self::matches_topics(&self.include_topics, &self.topic_tag_prefix, tags)
    }

    fn matches_regex_entity_ids(matching: &[ComparableRegex], entity_id: &EntityId) -> bool {
        matching.iter().any(|r| r.0.is_match(entity_id))
    }

    fn matches_entity_ids(entity_ids: &[EntityId], entity_id: &EntityId) -> bool {
        entity_ids.iter().any(|pi| pi == entity_id)
    }

    fn matches_tags(match_tags: &[Tag], tags: &[Tag]) -> bool {
        match_tags.iter().any(|mt| tags.iter().any(|t| t == mt))
    }

    fn matches_topics(expressions: &[TopicMatcher], topic_tag_prefix: &Tag, tags: &[Tag]) -> bool {
        let topic_tag_prefix_len = topic_tag_prefix.len();
        expressions.iter().any(|r| {
            let matcher = r.0.get_matcher();
            tags.iter()
                .filter(|t| t.starts_with(topic_tag_prefix.as_str()))
                .any(|t| {
                    let topic_name = TopicNameRef::new(&t[topic_tag_prefix_len..]);
                    if let Ok(topic_name) = topic_name {
                        matcher.is_match(topic_name)
                    } else {
                        false
                    }
                })
        })
    }

    /// Updates the filter given commands to add or remove new criteria.
    pub fn update(&mut self, criteria: Vec<FilterCriteria>) {
        for criterion in criteria {
            match criterion {
                FilterCriteria::ExcludeTags { mut tags } => {
                    merge(&mut self.exclude_tags, &mut tags, self.max_tags)
                }

                FilterCriteria::RemoveExcludeTags { tags } => remove(&mut self.exclude_tags, &tags),

                FilterCriteria::IncludeTags { mut tags } => {
                    merge(&mut self.include_tags, &mut tags, self.max_tags)
                }

                FilterCriteria::RemoveIncludeTags { tags } => remove(&mut self.include_tags, &tags),

                FilterCriteria::ExcludeRegexEntityIds { mut matching } => merge(
                    &mut self.exclude_regex_entity_ids,
                    &mut matching,
                    self.max_regex_entity_ids,
                ),

                FilterCriteria::RemoveExcludeRegexEntityIds { matching } => {
                    remove(&mut self.exclude_regex_entity_ids, &matching)
                }

                FilterCriteria::IncludeRegexEntityIds { mut matching } => merge(
                    &mut self.include_regex_entity_ids,
                    &mut matching,
                    self.max_regex_entity_ids,
                ),

                FilterCriteria::RemoveIncludeRegexEntityIds { matching } => {
                    remove(&mut self.include_regex_entity_ids, &matching)
                }

                FilterCriteria::ExcludeEntityIds { mut entity_ids } => merge(
                    &mut self.exclude_entity_ids,
                    &mut entity_ids,
                    self.max_entity_ids,
                ),

                FilterCriteria::RemoveExcludeEntityIds { entity_ids } => {
                    remove(&mut self.exclude_entity_ids, &entity_ids)
                }

                FilterCriteria::IncludeEntityIds { entity_id_offsets } => merge(
                    &mut self.include_entity_ids,
                    &mut entity_id_offsets
                        .into_iter()
                        .map(|EntityIdOffset { entity_id, .. }| entity_id)
                        .collect(),
                    self.max_entity_ids,
                ),

                FilterCriteria::RemoveIncludeEntityIds { entity_ids } => {
                    remove(&mut self.include_entity_ids, &entity_ids)
                }

                FilterCriteria::IncludeTopics { mut expressions } => {
                    merge(&mut self.include_topics, &mut expressions, self.max_topics)
                }

                FilterCriteria::RemoveIncludeTopics { expressions } => {
                    remove(&mut self.include_topics, &expressions)
                }
            };
        }
    }
}

fn merge<T>(l: &mut Vec<T>, r: &mut Vec<T>, max_len: usize)
where
    T: Ord,
{
    if l.len() < max_len && r.len() < max_len {
        l.append(r);
        l.sort();
        l.dedup();
    }
}

fn remove<T>(l: &mut Vec<T>, r: &[T])
where
    T: PartialEq,
{
    l.retain(|existing| !r.contains(existing));
}

#[cfg(test)]
mod tests {

    use akka_persistence_rs::PersistenceId;

    use super::*;

    struct TestEnvelope {
        persistence_id: PersistenceId,
        tags: Vec<Tag>,
    }

    impl WithPersistenceId for TestEnvelope {
        fn persistence_id(&self) -> &PersistenceId {
            &self.persistence_id
        }
    }

    impl WithTags for TestEnvelope {
        fn tags(&self) -> &[Tag] {
            &self.tags
        }
    }

    #[test]
    fn exclude_include_and_remove_include_tag_and_remove_exclude_tag() {
        let persistence_id = "a|1".parse::<PersistenceId>().unwrap();
        let tag = Tag::from("a");

        let envelope = TestEnvelope {
            persistence_id: persistence_id.clone(),
            tags: vec![tag.clone()],
        };

        let mut filter = Filter::default();

        let criteria = vec![
            FilterCriteria::ExcludeTags {
                tags: vec![tag.clone()],
            },
            FilterCriteria::IncludeTags {
                tags: vec![tag.clone()],
            },
        ];
        filter.update(criteria);
        assert!(filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveIncludeTags {
            tags: vec![tag.clone()],
        }];
        filter.update(criteria);
        assert!(!filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveExcludeTags { tags: vec![tag] }];
        filter.update(criteria);
        assert!(filter.matches(&envelope));
    }

    #[test]
    fn exclude_include_and_remove_include_entity_id_and_remove_exclude_entity_id() {
        let persistence_id = "a|1".parse::<PersistenceId>().unwrap();
        let entity_id = persistence_id.entity_id.clone();

        let envelope = TestEnvelope {
            persistence_id: persistence_id.clone(),
            tags: vec![],
        };

        let mut filter = Filter::default();

        let criteria = vec![
            FilterCriteria::ExcludeEntityIds {
                entity_ids: vec![entity_id.clone()],
            },
            FilterCriteria::IncludeEntityIds {
                entity_id_offsets: vec![EntityIdOffset {
                    entity_id: entity_id.clone(),
                    seq_nr: 0,
                }],
            },
        ];
        filter.update(criteria);
        assert!(filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveIncludeEntityIds {
            entity_ids: vec![entity_id.clone()],
        }];
        filter.update(criteria);
        assert!(!filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveExcludeEntityIds {
            entity_ids: vec![entity_id.clone()],
        }];
        filter.update(criteria);
        assert!(filter.matches(&envelope));
    }

    #[test]
    fn exclude_include_and_remove_include_regex_entity_id_and_remove_exclude_regex_entity_id() {
        let persistence_id = "a|1".parse::<PersistenceId>().unwrap();
        let matching = ComparableRegex(Regex::new("1").unwrap());

        let envelope = TestEnvelope {
            persistence_id: persistence_id.clone(),
            tags: vec![],
        };

        let mut filter = Filter::default();

        let criteria = vec![
            FilterCriteria::ExcludeRegexEntityIds {
                matching: vec![matching.clone()],
            },
            FilterCriteria::IncludeRegexEntityIds {
                matching: vec![matching.clone()],
            },
        ];
        filter.update(criteria);
        assert!(filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveIncludeRegexEntityIds {
            matching: vec![matching.clone()],
        }];
        filter.update(criteria);
        assert!(!filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveExcludeRegexEntityIds {
            matching: vec![matching.clone()],
        }];
        filter.update(criteria);
        assert!(filter.matches(&envelope));
    }

    #[test]
    fn include_and_remove_include_topic() {
        let persistence_id = "a|1".parse::<PersistenceId>().unwrap();
        let tag = Tag::from("t:sport/abc/player1");
        let expression = TopicMatcher::new("sport/+/player1").unwrap();

        let envelope = TestEnvelope {
            persistence_id: persistence_id.clone(),
            tags: vec![tag.clone()],
        };

        let mut filter = Filter::default();

        let criteria = vec![
            exclude_all(),
            FilterCriteria::IncludeTopics {
                expressions: vec![expression.clone()],
            },
        ];
        filter.update(criteria);
        assert!(filter.matches(&envelope));

        let criteria = vec![FilterCriteria::RemoveIncludeTopics {
            expressions: vec![expression.clone()],
        }];
        filter.update(criteria);
        assert!(!filter.matches(&envelope));
    }
}