zitadel/internal/eventstore/event.go
Silvan 1d84635836
feat(eventstore): add search table (#8191)
# Which Problems Are Solved

To improve performance a new table and method is implemented on
eventstore. The goal of this table is to index searchable fields on
command side to use it on command and query side.

The table allows to store one primitive value (numeric, text) per row.

The eventstore framework is extended by the `Search`-method which allows
to search for objects.
The `Command`-interface is extended by the `SearchOperations()`-method
which does manipulate the the `search`-table.

# How the Problems Are Solved

This PR adds the capability of improving performance for command and
query side by using the `Search`-method of the eventstore instead of
using one of the `Filter`-methods.

# Open Tasks

- [x] Add feature flag
- [x] Unit tests
- [ ] ~~Benchmarks if needed~~
- [x] Ensure no behavior change
- [x] Add setup step to fill table with current data
- [x] Add projection which ensures data added between setup and start of
the new version are also added to the table

# Additional Changes

The `Search`-method is currently used by `ProjectGrant`-command side.

# Additional Context

- Closes https://github.com/zitadel/zitadel/issues/8094
2024-07-03 15:00:56 +00:00

110 lines
2.7 KiB
Go

package eventstore
import (
"encoding/json"
"reflect"
"time"
"github.com/zitadel/zitadel/internal/zerrors"
)
type action interface {
Aggregate() *Aggregate
// Creator is the userid of the user which created the action
Creator() string
// Type describes the action
Type() EventType
// Revision of the action
Revision() uint16
}
// Command is the intend to store an event into the eventstore
type Command interface {
action
// Payload returns the payload of the event. It represent the changed fields by the event
// valid types are:
// * nil: no payload
// * struct: which can be marshalled to json
// * pointer: to struct which can be marshalled to json
// * []byte: json marshalled data
Payload() any
// UniqueConstraints should be added for unique attributes of an event, if nil constraints will not be checked
UniqueConstraints() []*UniqueConstraint
// Fields should be added for fields which should be indexed for lookup, if nil fields will not be indexed
Fields() []*FieldOperation
}
// Event is a stored activity
type Event interface {
action
// Sequence of the event in the aggregate
Sequence() uint64
// CreatedAt is the time the event was created at
CreatedAt() time.Time
// Position is the global position of the event
Position() float64
// Unmarshal parses the payload and stores the result
// in the value pointed to by ptr. If ptr is nil or not a pointer,
// Unmarshal returns an error
Unmarshal(ptr any) error
// Deprecated: only use for migration
DataAsBytes() []byte
}
type EventType string
func EventData(event Command) ([]byte, error) {
switch data := event.Payload().(type) {
case nil:
return nil, nil
case []byte:
if json.Valid(data) {
return data, nil
}
return nil, zerrors.ThrowInvalidArgument(nil, "V2-6SbbS", "data bytes are not json")
}
dataType := reflect.TypeOf(event.Payload())
if dataType.Kind() == reflect.Ptr {
dataType = dataType.Elem()
}
if dataType.Kind() == reflect.Struct {
dataBytes, err := json.Marshal(event.Payload())
if err != nil {
return nil, zerrors.ThrowInvalidArgument(err, "V2-xG87M", "could not marshal data")
}
return dataBytes, nil
}
return nil, zerrors.ThrowInvalidArgument(nil, "V2-91NRm", "wrong type of event data")
}
type BaseEventSetter[T any] interface {
Event
SetBaseEvent(*BaseEvent)
*T
}
func GenericEventMapper[T any, PT BaseEventSetter[T]](event Event) (Event, error) {
e := PT(new(T))
e.SetBaseEvent(BaseEventFromRepo(event))
err := event.Unmarshal(e)
if err != nil {
return nil, zerrors.ThrowInternal(err, "ES-Thai6", "unable to unmarshal event")
}
return e, nil
}
func isEventTypes(command Command, types ...EventType) bool {
for _, typ := range types {
if command.Type() == typ {
return true
}
}
return false
}