fix(query): query event editors only once per call (#7776)

Co-authored-by: Livio Spring <livio.a@gmail.com>
(cherry picked from commit f412687427)
This commit is contained in:
Silvan 2024-04-16 12:42:31 +02:00 committed by Livio Spring
parent 86e6c2a5df
commit 288525f180
No known key found for this signature in database
GPG Key ID: 26BB1C2FA5952CF0

View File

@ -28,13 +28,14 @@ type EventEditor struct {
} }
type eventsReducer struct { type eventsReducer struct {
ctx context.Context ctx context.Context
q *Queries q *Queries
events []*Event events []*Event
editors map[string]*EventEditor
} }
func (r *eventsReducer) AppendEvents(events ...eventstore.Event) { func (r *eventsReducer) AppendEvents(events ...eventstore.Event) {
r.events = append(r.events, r.q.convertEvents(r.ctx, events)...) r.events = append(r.events, r.convertEvents(r.ctx, events)...)
} }
func (r *eventsReducer) Reduce() error { return nil } func (r *eventsReducer) Reduce() error { return nil }
@ -49,7 +50,7 @@ func (q *Queries) SearchEvents(ctx context.Context, query *eventstore.SearchQuer
if auditLogRetention != 0 { if auditLogRetention != 0 {
query = filterAuditLogRetention(ctx, auditLogRetention, query) query = filterAuditLogRetention(ctx, auditLogRetention, query)
} }
reducer := &eventsReducer{ctx: ctx, q: q} reducer := &eventsReducer{ctx: ctx, q: q, editors: make(map[string]*EventEditor, query.GetLimit())}
if err = q.eventstore.FilterToReducer(ctx, query, reducer); err != nil { if err = q.eventstore.FilterToReducer(ctx, query, reducer); err != nil {
return nil, err return nil, err
} }
@ -78,24 +79,23 @@ func (q *Queries) SearchAggregateTypes(ctx context.Context) []string {
return q.eventstore.AggregateTypes() return q.eventstore.AggregateTypes()
} }
func (q *Queries) convertEvents(ctx context.Context, events []eventstore.Event) []*Event { func (er *eventsReducer) convertEvents(ctx context.Context, events []eventstore.Event) []*Event {
result := make([]*Event, len(events)) result := make([]*Event, len(events))
users := make(map[string]*EventEditor)
for i, event := range events { for i, event := range events {
result[i] = q.convertEvent(ctx, event, users) result[i] = er.convertEvent(ctx, event)
} }
return result return result
} }
func (q *Queries) convertEvent(ctx context.Context, event eventstore.Event, users map[string]*EventEditor) *Event { func (er *eventsReducer) convertEvent(ctx context.Context, event eventstore.Event) *Event {
ctx, span := tracing.NewSpan(ctx) ctx, span := tracing.NewSpan(ctx)
var err error var err error
defer func() { span.EndWithError(err) }() defer func() { span.EndWithError(err) }()
editor, ok := users[event.Creator()] editor, ok := er.editors[event.Creator()]
if !ok { if !ok {
editor = q.editorUserByID(ctx, event.Creator()) editor = er.q.editorUserByID(ctx, event.Creator())
users[event.Creator()] = editor er.editors[event.Creator()] = editor
} }
return &Event{ return &Event{