-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.scala
347 lines (285 loc) · 11.1 KB
/
event.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
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
/*
* 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
import java.time.ZonedDateTime
import java.util.UUID
import cats.syntax.all._
import scodec.bits.ByteVector
import sec.utilities.{guardNonEmpty, guardNotStartsWith}
//======================================================================================================================
/** A persisted event in EventStoreDB. There are two variants:
*
* - [[EventRecord]] An event in an event stream.
* - [[ResolvedEvent]] A special event that contains a link and a linked event record.
*
* @tparam P
* Tells whether the event is retrieved from the global stream [[PositionInfo.Global]] or from an individual stream
* [[PositionInfo.Local]].
*/
sealed trait Event[P <: PositionInfo]
object Event {
implicit final class EventOps[P <: PositionInfo](val e: Event[P]) extends AnyVal {
def fold[A](f: EventRecord[P] => A, g: ResolvedEvent[P] => A): A = e match {
case er: EventRecord[P] => f(er)
case re: ResolvedEvent[P] => g(re)
}
/** The stream identifier of the stream the event belongs to.
*/
def streamId: StreamId = e.fold(_.streamId, _.event.streamId)
/** The stream position of the event in its stream.
*/
def streamPosition: StreamPosition.Exact =
e.fold(_.position.streamPosition, _.event.position.streamPosition)
/** The payload of the event.
*/
def eventData: EventData = e.fold(_.eventData, _.event.eventData)
/** The actual event record of this event. There are two options. Either the record points to a normal event or a
* resolved event.
*/
def record: EventRecord[P] = e.fold(identity, _.link)
/** The creation date of the event in [[java.time.ZonedDateTime]].
*/
def created: ZonedDateTime = e.fold(_.created, _.event.created)
def render: String = fold(EventRecord.render, ResolvedEvent.render)
}
implicit final class AllEventOps(val e: AllEvent) extends AnyVal {
/** The position of the event in the global stream.
*/
def logPosition: LogPosition.Exact = e.fold(_.position.log, _.event.position.log)
}
}
/** An event persisted in an event stream.
*
* @param streamId
* the stream identifier of the stream the event belongs to.
* @param position
* the position information about of the event.
* @param eventData
* the payload of the event.
* @param created
* the creation date of the event in [[java.time.ZonedDateTime]].
*/
final case class EventRecord[P <: PositionInfo](
streamId: StreamId,
position: P,
eventData: EventData,
created: ZonedDateTime
) extends Event[P]
object EventRecord {
def render[P <: PositionInfo](er: EventRecord[P]): String =
s"""
|EventRecord(
| streamId = ${er.streamId.render},
| eventId = ${er.eventData.eventId},
| type = ${er.eventData.eventType.render},
| position = ${er.position.renderPosition},
| data = ${er.eventData.renderData},
| metadata = ${er.eventData.renderMetadata},
| created = ${er.created}
|)
|""".stripMargin
}
/** Represents a [[EventType.LinkTo]] event that points to another event. Resolved events are common when reading or
* subscribing to system prefixed streams, for instance category streams like `$ce-` or `$et-`.
*
* @param event
* the original and linked to event record.
* @param link
* the link event to the resolved event.
*/
final case class ResolvedEvent[P <: PositionInfo](
event: EventRecord[P],
link: EventRecord[P]
) extends Event[P]
object ResolvedEvent {
def render[P <: PositionInfo](re: ResolvedEvent[P]): String =
s"""
|ResolvedEvent(
| event = ${EventRecord.render(re.event)},
| link = ${EventRecord.render(re.link)}
|)
|""".stripMargin
}
//======================================================================================================================
/** Event type for an [[Event]]. There are two event type variants:
*
* - [[EventType.System]] Type used for reserverd internal system events.
* - [[EventType.Normal]] Type used by users.
*
* @see
* [[https://ahjohannessen.github.io/sec/docs/types#eventtype]] for more information about event type usage.
*/
sealed trait EventType
object EventType {
sealed abstract case class System(name: String) extends EventType
private[sec] object System {
def unsafe(name: String): System = new System(name) {}
}
sealed abstract case class Normal(name: String) extends EventType
private[sec] object Normal {
def unsafe(name: String): Normal = new Normal(name) {}
}
// /
final val StreamDeleted: System = System.unsafe("streamDeleted")
final val StatsCollected: System = System.unsafe("statsCollected")
final val LinkTo = System.unsafe(">")
final val StreamReference: System = System.unsafe("@")
final val StreamMetadata: System = System.unsafe("metadata")
final val Settings: System = System.unsafe("settings")
/** @param name
* Constructs an event type for [[EventData]]. Provided value is validated for non-empty and not starting with the
* system reserved prefix `$`.
*/
def apply(name: String): Either[InvalidInput, Normal] =
normal(name).leftMap(InvalidInput(_))
// /
private[sec] val guardNonEmptyName: String => Attempt[String] = guardNonEmpty("Event type name")
private[sec] def system(name: String): Attempt[System] =
guardNonEmptyName(name) >>= guardNotStartsWith(systemPrefix) >>= (System.unsafe(_).asRight)
private[sec] def normal(name: String): Attempt[Normal] =
guardNonEmptyName(name) >>= guardNotStartsWith(systemPrefix) >>= (Normal.unsafe(_).asRight)
// /
private[sec] val eventTypeToString: EventType => String = {
case System(n) => s"$systemPrefix$n"
case Normal(n) => n
}
private[sec] val stringToEventType: String => Attempt[EventType] = {
case sd if sd.startsWith(systemPrefix) => system(sd.substring(systemPrefixLength))
case ud => normal(ud)
}
final private[sec] val systemPrefix: String = "$"
final private[sec] val systemPrefixLength: Int = systemPrefix.length
implicit final class EventTypeOps(val et: EventType) extends AnyVal {
def stringValue: String = eventTypeToString(et)
def render: String = stringValue
}
}
//======================================================================================================================
/** Event payload for an event. This is the actual data that you persist in EventStoreDB.
*
* @param eventType
* the [[EventType]] for the event.
* @param eventId
* unique identifier for the event.
* @param data
* a [[scodec.bits.ByteVector]] of encoded data.
* @param metadata
* a [[scodec.bits.ByteVector]] of encoded metadata.
* @param contentType
* the [[ContentType]] of encoded data and metadata.
*/
final case class EventData(
eventType: EventType,
eventId: UUID,
data: ByteVector,
metadata: ByteVector,
contentType: ContentType
)
object EventData {
/** Constructor for [[EventData]] when metadata is not required.
*
* @param eventType
* the [[EventType]] for the event.
* @param eventId
* unique identifier for the event.
* @param data
* a [[scodec.bits.ByteVector]] of encoded data.
* @param contentType
* the [[ContentType]] of encoded data and metadata.
*/
def apply(
eventType: EventType,
eventId: UUID,
data: ByteVector,
contentType: ContentType
): EventData =
EventData(eventType, eventId, data, ByteVector.empty, contentType)
/** Constructor for [[EventData]] when the event type is a string and metadata is not required. Returns either
* [[InvalidInput]] or [[EventType]].
*
* @param eventType
* string value for [[EventType]].
* @param eventId
* unique identifier for the event.
* @param data
* a [[scodec.bits.ByteVector]] of encoded data.
* @param contentType
* the [[ContentType]] of encoded data and metadata.
*/
def apply(
eventType: String,
eventId: UUID,
data: ByteVector,
contentType: ContentType
): Either[InvalidInput, EventData] =
EventType(eventType).map(EventData(_, eventId, data, ByteVector.empty, contentType))
/** Constructor for [[EventData]] when the event type is a string.
*
* @param eventType
* string value for [[EventType]].
* @param eventId
* unique identifier for the event.
* @param data
* a [[scodec.bits.ByteVector]] of encoded data.
* @param metadata
* a [[scodec.bits.ByteVector]] of encoded metadata.
* @param contentType
* the [[ContentType]] of encoded data and metadata.
*/
def apply(
eventType: String,
eventId: UUID,
data: ByteVector,
metadata: ByteVector,
contentType: ContentType
): Either[InvalidInput, EventData] =
EventType(eventType).map(EventData(_, eventId, data, metadata, contentType))
// /
implicit final private[sec] class EventDataOps(val ed: EventData) extends AnyVal {
def renderData: String = render(ed.data, ed.contentType)
def renderMetadata: String = render(ed.metadata, ed.contentType)
}
private[sec] def render(bv: ByteVector, ct: ContentType): String =
if (bv.isEmpty || ct.isBinary) s"${ct.render}(${renderByteVector(bv)})"
else s"${bv.decodeUtf8.getOrElse("Failed decoding utf8")}"
private[sec] def renderByteVector(bv: ByteVector): String = {
if (bv.isEmpty) s"empty"
else if (bv.size < 32) s"${bv.size} bytes, 0x${bv.toHex}"
else s"${bv.size} bytes, #${bv.hashCode}"
}
}
//======================================================================================================================
/** Content type for [[EventData]]. There are two variants:
*
* - [[ContentType.Json]] used when event data and metadata are encoded as JSON.
* - [[ContentType.Binary]] used when event data and metadata are encoded as binary. This can for instance be
* [[https://developers.google.com/protocol-buffers]] or utf8 encoded data like [[EventType.LinkTo]] events.
*/
sealed trait ContentType
object ContentType {
case object Binary extends ContentType
case object Json extends ContentType
implicit final class ContentTypeOps(val ct: ContentType) extends AnyVal {
def fold[A](binary: => A, json: => A): A = ct match {
case Binary => binary
case Json => json
}
def isJson: Boolean = ct.fold(false, true)
def isBinary: Boolean = !isJson
def render: String = fold("Binary", "Json")
}
}
//======================================================================================================================