2020-04-06 04:42:21 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/internal/repository"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-11-12 21:50:01 +00:00
|
|
|
es_v2 "github.com/caos/zitadel/internal/eventstore/v2"
|
2020-04-06 04:42:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Eventstore interface {
|
|
|
|
AggregateCreator() *models.AggregateCreator
|
|
|
|
Health(ctx context.Context) error
|
|
|
|
PushAggregates(ctx context.Context, aggregates ...*models.Aggregate) error
|
|
|
|
FilterEvents(ctx context.Context, searchQuery *models.SearchQuery) (events []*models.Event, err error)
|
2020-07-28 07:42:21 +00:00
|
|
|
LatestSequence(ctx context.Context, searchQuery *models.SearchQueryFactory) (uint64, error)
|
2020-11-12 21:50:01 +00:00
|
|
|
V2() *es_v2.Eventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
Subscribe(aggregates ...models.AggregateType) *Subscription
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Eventstore = (*eventstore)(nil)
|
|
|
|
|
|
|
|
type eventstore struct {
|
|
|
|
repo repository.Repository
|
|
|
|
aggregateCreator *models.AggregateCreator
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
esV2 *es_v2.Eventstore
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *eventstore) AggregateCreator() *models.AggregateCreator {
|
|
|
|
return es.aggregateCreator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *eventstore) PushAggregates(ctx context.Context, aggregates ...*models.Aggregate) (err error) {
|
|
|
|
for _, aggregate := range aggregates {
|
|
|
|
if len(aggregate.Events) == 0 {
|
|
|
|
return errors.ThrowInvalidArgument(nil, "EVENT-cNhIj", "no events in aggregate")
|
|
|
|
}
|
|
|
|
for _, event := range aggregate.Events {
|
|
|
|
if err = event.Validate(); err != nil {
|
2020-04-07 16:36:37 +00:00
|
|
|
return errors.ThrowInvalidArgument(err, "EVENT-tzIhl", "validate event failed")
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = es.repo.PushAggregates(ctx, aggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
|
|
|
|
go notify(aggregates)
|
2020-04-06 04:42:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *eventstore) FilterEvents(ctx context.Context, searchQuery *models.SearchQuery) ([]*models.Event, error) {
|
|
|
|
if err := searchQuery.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-28 07:42:21 +00:00
|
|
|
return es.repo.Filter(ctx, models.FactoryFromSearchQuery(searchQuery))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *eventstore) LatestSequence(ctx context.Context, queryFactory *models.SearchQueryFactory) (uint64, error) {
|
|
|
|
sequenceFactory := *queryFactory
|
|
|
|
sequenceFactory = *(&sequenceFactory).Columns(models.Columns_Max_Sequence)
|
|
|
|
sequenceFactory = *(&sequenceFactory).SequenceGreater(0)
|
|
|
|
return es.repo.LatestSequence(ctx, &sequenceFactory)
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
|
|
|
|
func (es *eventstore) Health(ctx context.Context) error {
|
|
|
|
return es.repo.Health(ctx)
|
|
|
|
}
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
func (es *eventstore) V2() *es_v2.Eventstore {
|
|
|
|
return es.esV2
|
|
|
|
}
|