-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.scala
99 lines (82 loc) · 3.97 KB
/
filter.scala
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
/*
* Copyright 2020 Scala EventStoreDB Client
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sec
package api
import scala.util.matching.Regex
import cats.data.NonEmptyList
import cats.syntax.all.*
import EventFilter.*
//======================================================================================================================
/** Used for server-side event stream filtering. There are two *kinds* of filters:
*
* - [[EventFilter.ByStreamId]] when you wish to filter by stream identifier.
* - [[EventFilter.ByEventType]] when you wish to filter by event type.
*
* In combination with [[EventFilter.Kind]] you can choose between two types of filter expressions:
*
* - [[EventFilter.PrefixFilter]] when you wish to filter for prefixes. An example of this is
* `PrefixFilter("user_stream")` for streams starting with the string value `"user_stream"` like `"user_stream-a"`
* and `"user_stream-b"`.
*
* - [[EventFilter.RegexFilter]] when you wish to filter with a regular expression. An example of this is
* `RegexFilter("^[^$].*")` when you for do not wish to retrieve events starting with `$`.
*/
final case class EventFilter(
kind: Kind,
option: Either[NonEmptyList[PrefixFilter], RegexFilter]
)
object EventFilter:
sealed trait Kind
case object ByStreamId extends Kind
case object ByEventType extends Kind
def streamIdPrefix(fst: String, rest: String*): EventFilter = prefix(ByStreamId, fst, rest*)
def streamIdRegex(filter: String): EventFilter = regex(ByStreamId, filter)
def eventTypePrefix(fst: String, rest: String*): EventFilter = prefix(ByEventType, fst, rest*)
def eventTypeRegex(filter: String): EventFilter = regex(ByEventType, filter)
def prefix(kind: Kind, fst: String, rest: String*): EventFilter =
EventFilter(kind, NonEmptyList(PrefixFilter(fst), rest.toList.map(PrefixFilter(_))).asLeft)
def regex(kind: Kind, filter: String): EventFilter =
EventFilter(kind, RegexFilter(filter).asRight)
//
sealed trait Expression
final case class PrefixFilter(value: String) extends Expression
final case class RegexFilter(value: String) extends Expression
object RegexFilter:
val excludeSystemEvents: RegexFilter = apply("^[^$].*".r)
def apply(regex: Regex): RegexFilter = RegexFilter(regex.pattern.toString)
//======================================================================================================================
sealed abstract case class SubscriptionFilterOptions(
filter: EventFilter,
maxSearchWindow: Option[Int],
checkpointIntervalMultiplier: Int
)
object SubscriptionFilterOptions:
/** @param filter
* See [[EventFilter]].
* @param maxSearchWindow
* Maximum number of events to read that do not match the filter. Minimum valid value is 1 and if provided value is
* less then 1 is used.
* @param checkpointIntervalMultiplier
* The checkpoint interval is multiplied by the max search window to determine the number of events after which to
* checkpoint. Minimum valid value is 1 and if provided value is less then 1 is used.
*/
def apply(
filter: EventFilter,
maxSearchWindow: Option[Int] = 32.some,
checkpointIntervalMultiplier: Int = 1
): SubscriptionFilterOptions =
new SubscriptionFilterOptions(filter, maxSearchWindow.map(_ max 1), checkpointIntervalMultiplier max 1) {}
//======================================================================================================================