mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-14 11:58:02 +00:00
b522588d98
# Which Problems Are Solved Float64 which was used for the event.Position field is [not precise in go and gets rounded](https://github.com/golang/go/issues/47300). This can lead to unprecies position tracking of events and therefore projections especially on cockcoachdb as the position used there is a big number. example of a unprecies position: exact: 1725257931223002628 float64: 1725257931223002624.000000 # How the Problems Are Solved The float64 was replaced by [github.com/jackc/pgx-shopspring-decimal](https://github.com/jackc/pgx-shopspring-decimal). # Additional Changes Correct behaviour of makefile for load tests. Rename `latestSequence`-queries to `latestPosition`
56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package eventstore
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
// ReadModel is the minimum representation of a read model.
|
|
// It implements a basic reducer
|
|
// it might be saved in a database or in memory
|
|
type ReadModel struct {
|
|
AggregateID string `json:"-"`
|
|
ProcessedSequence uint64 `json:"-"`
|
|
CreationDate time.Time `json:"-"`
|
|
ChangeDate time.Time `json:"-"`
|
|
Events []Event `json:"-"`
|
|
ResourceOwner string `json:"-"`
|
|
InstanceID string `json:"-"`
|
|
Position decimal.Decimal `json:"-"`
|
|
}
|
|
|
|
// AppendEvents adds all the events to the read model.
|
|
// The function doesn't compute the new state of the read model
|
|
func (rm *ReadModel) AppendEvents(events ...Event) {
|
|
rm.Events = append(rm.Events, events...)
|
|
}
|
|
|
|
// Reduce is the basic implementation of reducer
|
|
// If this function is extended the extending function should be the last step
|
|
func (rm *ReadModel) Reduce() error {
|
|
if len(rm.Events) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if rm.AggregateID == "" {
|
|
rm.AggregateID = rm.Events[0].Aggregate().ID
|
|
}
|
|
if rm.ResourceOwner == "" {
|
|
rm.ResourceOwner = rm.Events[0].Aggregate().ResourceOwner
|
|
}
|
|
if rm.InstanceID == "" {
|
|
rm.InstanceID = rm.Events[0].Aggregate().InstanceID
|
|
}
|
|
|
|
if rm.CreationDate.IsZero() {
|
|
rm.CreationDate = rm.Events[0].CreatedAt()
|
|
}
|
|
rm.ChangeDate = rm.Events[len(rm.Events)-1].CreatedAt()
|
|
rm.ProcessedSequence = rm.Events[len(rm.Events)-1].Sequence()
|
|
rm.Position = rm.Events[len(rm.Events)-1].Position()
|
|
// all events processed and not needed anymore
|
|
rm.Events = rm.Events[0:0]
|
|
return nil
|
|
}
|