fix: handle multiple statements for a single event in projections (#2313)

* fix: handle multiple statements for a single event in projections

* export func type

* fix test

* Update internal/eventstore/handler/crdb/statement.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/eventstore/handler/crdb/statement.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* change to pointers

* add error test case

Co-authored-by: Silvan <silvan.reusser@gmail.com>
This commit is contained in:
Livio Amstutz
2021-09-08 13:54:31 +02:00
committed by GitHub
parent ec6a3a1847
commit 1ac1492fd3
11 changed files with 546 additions and 301 deletions

View File

@@ -18,11 +18,11 @@ type ProjectionHandlerConfig struct {
}
//Update updates the projection with the given statements
type Update func(context.Context, []Statement, Reduce) (unexecutedStmts []Statement, err error)
type Update func(context.Context, []*Statement, Reduce) (unexecutedStmts []*Statement, err error)
//Reduce reduces the given event to a statement
//which is used to update the projection
type Reduce func(eventstore.EventReader) ([]Statement, error)
type Reduce func(eventstore.EventReader) (*Statement, error)
//Lock is used for mutex handling if needed on the projection
type Lock func(context.Context, time.Duration) <-chan error
@@ -46,7 +46,7 @@ type ProjectionHandler struct {
ProjectionName string
lockMu sync.Mutex
stmts []Statement
stmts []*Statement
}
func NewProjectionHandler(config ProjectionHandlerConfig) *ProjectionHandler {
@@ -156,7 +156,7 @@ func (h *ProjectionHandler) processEvent(
event eventstore.EventReader,
reduce Reduce,
) error {
stmts, err := reduce(event)
stmt, err := reduce(event)
if err != nil {
logging.Log("EVENT-PTr4j").WithError(err).Warn("unable to process event")
return err
@@ -165,7 +165,7 @@ func (h *ProjectionHandler) processEvent(
h.lockMu.Lock()
defer h.lockMu.Unlock()
h.stmts = append(h.stmts, stmts...)
h.stmts = append(h.stmts, stmt)
return nil
}