-
Notifications
You must be signed in to change notification settings - Fork 11
/
query.go
284 lines (252 loc) · 6.85 KB
/
query.go
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
// Package query provides an event query builder.
package query
import (
"github.com/google/uuid"
"github.com/modernice/goes/event"
"github.com/modernice/goes/event/query/time"
"github.com/modernice/goes/event/query/version"
)
// A Query is used by event stores to query events.
type Query struct {
names []string
ids []uuid.UUID
aggregateNames []string
aggregateIDs []uuid.UUID
aggregates []event.AggregateRef
sortings []event.SortOptions
times time.Constraints
aggregateVersions version.Constraints
}
// Option is an option for building a query.
type Option func(*builder)
type builder struct {
Query
timeConstraints []time.Option
versionConstraints []version.Option
}
// New builds the query from the provided options.
func New(opts ...Option) Query {
var b builder
for _, opt := range opts {
opt(&b)
}
return b.build()
}
// Name returns an Option that filters events by their names.
func Name(names ...string) Option {
return func(b *builder) {
L:
for _, name := range names {
for _, n := range b.names {
if n == name {
continue L
}
}
b.names = append(b.names, names...)
}
}
}
// ID returns an Option that filters events by their ids.
func ID(ids ...uuid.UUID) Option {
return func(b *builder) {
L:
for _, id := range ids {
for _, id2 := range b.ids {
if id2 == id {
continue L
}
}
b.ids = append(b.ids, ids...)
}
}
}
// Time returns an Option that filters events by time constraints.
func Time(constraints ...time.Option) Option {
return func(b *builder) {
b.timeConstraints = append(b.timeConstraints, constraints...)
}
}
// AggregateName returns an Option that filters events by their aggregate names.
func AggregateName(names ...string) Option {
return func(b *builder) {
L:
for _, name := range names {
for _, n := range b.aggregateNames {
if n == name {
continue L
}
}
b.aggregateNames = append(b.aggregateNames, name)
}
}
}
// AggregateID returns an Option that filters events by their aggregate ids.
func AggregateID(ids ...uuid.UUID) Option {
return func(b *builder) {
L:
for _, id := range ids {
for _, id2 := range b.aggregateIDs {
if id2 == id {
continue L
}
}
b.aggregateIDs = append(b.aggregateIDs, id)
}
}
}
// AggregateVersion returns an Option that filters events by their aggregate
// versions.
func AggregateVersion(constraints ...version.Option) Option {
return func(b *builder) {
b.versionConstraints = append(b.versionConstraints, constraints...)
}
}
// Aggregate returns an Option that filters events by a specific aggregate.
func Aggregate(name string, id uuid.UUID) Option {
return func(b *builder) {
for _, at := range b.aggregates {
if at.Name == name && at.ID == id {
return
}
}
b.aggregates = append(b.aggregates, event.AggregateRef{Name: name, ID: id})
}
}
// Aggregates returns an Option that filters events by specific aggregates.
func Aggregates(aggregates ...event.AggregateRef) Option {
return func(b *builder) {
L:
for _, at := range aggregates {
for _, at2 := range b.aggregates {
if at2 == at {
continue L
}
}
b.aggregates = append(b.aggregates, at)
}
}
}
// SortBy returns an Option that sorts a Query by the given Sorting and
// SortDirection.
func SortBy(sort event.Sorting, dir event.SortDirection) Option {
return SortByMulti(event.SortOptions{Sort: sort, Dir: dir})
}
// SortByMulti return an Option that sorts a Query by multiple Sortings and
// SortDirections.
func SortByMulti(sorts ...event.SortOptions) Option {
return func(b *builder) {
L:
for _, s := range sorts {
for _, s2 := range b.sortings {
if s2 == s {
continue L
}
}
b.sortings = append(b.sortings, s)
}
}
}
// SortByAggregate returns an Option that sorts the a Query by aggregates.
//
// Order of sortings is:
// 1. aggregateName (ascending)
// 2. aggregateID (ascending)
// 3. aggregateVersion (ascending)
func SortByAggregate() Option {
return SortByMulti(
event.SortOptions{Sort: event.SortAggregateName, Dir: event.SortAsc},
event.SortOptions{Sort: event.SortAggregateID, Dir: event.SortAsc},
event.SortOptions{Sort: event.SortAggregateVersion, Dir: event.SortAsc},
)
}
// SortByTime returns an Option that sorts a Query by event time.
func SortByTime() Option {
return SortBy(event.SortTime, event.SortAsc)
}
// Test tests the event evt against the Query q and returns true if q should
// include evt in its results. Test can be used by in-memory event.Store
// implementations to filter events based on the query.
func Test[D any](q event.Query, evt event.Of[D]) bool {
return event.Test(q, evt)
}
// Apply tests events against the provided Query and returns only those events
// that match the Query.
func Apply[D any](q event.Query, events ...event.Of[D]) []event.Of[D] {
if events == nil {
return nil
}
out := make([]event.Of[D], 0, len(events))
for _, evt := range events {
if Test(q, evt) {
out = append(out, evt)
}
}
return out
}
// Merge merges multiple queries into a single query.
//
// In cases where only a single value can be assigned to a filter, the last
// provided query that provides that filter is used.
func Merge(queries ...event.Query) Query {
var opts []Option
for _, q := range queries {
if q == nil {
continue
}
versionOpts := version.DryMerge(q.AggregateVersions())
timeOpts := time.DryMerge(q.Times())
opts = append(
opts,
ID(q.IDs()...),
Name(q.Names()...),
AggregateID(q.AggregateIDs()...),
AggregateName(q.AggregateNames()...),
AggregateVersion(versionOpts...),
Aggregates(q.Aggregates()...),
Time(timeOpts...),
SortByMulti(q.Sortings()...),
)
}
return New(opts...)
}
// Names returns the event names to query for.
func (q Query) Names() []string {
return q.names
}
// IDs returns the event ids to query for.
func (q Query) IDs() []uuid.UUID {
return q.ids
}
// Time returns the time constraints. The returned Constraints are guaranteed to
// be non-nil.
func (q Query) Times() time.Constraints {
if q.times == nil {
return time.Filter()
}
return q.times
}
// AggregateNames returns the aggregate names to query for.
func (q Query) AggregateNames() []string {
return q.aggregateNames
}
// AggregateIDs returns the aggregate ids to query for.
func (q Query) AggregateIDs() []uuid.UUID {
return q.aggregateIDs
}
// AggregateVersions returns the aggregate versions to query for.
func (q Query) AggregateVersions() version.Constraints {
return q.aggregateVersions
}
// Aggregates returns a slice of specific aggregates to query for.
func (q Query) Aggregates() []event.AggregateRef {
return q.aggregates
}
// Sortings returns the sorting options for the query.
func (q Query) Sortings() []event.SortOptions {
return q.sortings
}
func (b builder) build() Query {
b.times = time.Filter(b.timeConstraints...)
b.aggregateVersions = version.Filter(b.versionConstraints...)
return b.Query
}