mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
1d84635836
# 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
311 lines
7.6 KiB
Go
311 lines
7.6 KiB
Go
package project
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
var (
|
|
UniqueRoleType = "project_role"
|
|
roleEventTypePrefix = projectEventTypePrefix + "role."
|
|
RoleAddedType = roleEventTypePrefix + "added"
|
|
RoleChangedType = roleEventTypePrefix + "changed"
|
|
RoleRemovedType = roleEventTypePrefix + "removed"
|
|
|
|
ProjectRoleSearchType = "project_role"
|
|
ProjectRoleRevision = uint8(1)
|
|
ProjectRoleKeySearchField = "key"
|
|
ProjectRoleDisplayNameSearchField = "display_name"
|
|
ProjectRoleGroupSearchField = "group"
|
|
)
|
|
|
|
func NewAddProjectRoleUniqueConstraint(roleKey, projectID string) *eventstore.UniqueConstraint {
|
|
return eventstore.NewAddEventUniqueConstraint(
|
|
UniqueRoleType,
|
|
fmt.Sprintf("%s:%s", roleKey, projectID),
|
|
"Errors.Project.Role.AlreadyExists")
|
|
}
|
|
|
|
func NewRemoveProjectRoleUniqueConstraint(roleKey, projectID string) *eventstore.UniqueConstraint {
|
|
return eventstore.NewRemoveUniqueConstraint(
|
|
UniqueRoleType,
|
|
fmt.Sprintf("%s:%s", roleKey, projectID))
|
|
}
|
|
|
|
type RoleAddedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Key string `json:"key,omitempty"`
|
|
DisplayName string `json:"displayName,omitempty"`
|
|
Group string `json:"group,omitempty"`
|
|
}
|
|
|
|
func (e *RoleAddedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *RoleAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return []*eventstore.UniqueConstraint{NewAddProjectRoleUniqueConstraint(e.Key, e.Aggregate().ID)}
|
|
}
|
|
|
|
func (e *RoleAddedEvent) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
ProjectRoleKeySearchField,
|
|
&eventstore.Value{
|
|
Value: e.Key,
|
|
ShouldIndex: true,
|
|
},
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
ProjectRoleDisplayNameSearchField,
|
|
&eventstore.Value{
|
|
Value: e.DisplayName,
|
|
ShouldIndex: true,
|
|
},
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
ProjectRoleGroupSearchField,
|
|
&eventstore.Value{
|
|
Value: e.Group,
|
|
ShouldIndex: true,
|
|
},
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
}
|
|
}
|
|
|
|
func NewRoleAddedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
key,
|
|
displayName,
|
|
group string,
|
|
) *RoleAddedEvent {
|
|
return &RoleAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
RoleAddedType,
|
|
),
|
|
Key: key,
|
|
DisplayName: displayName,
|
|
Group: group,
|
|
}
|
|
}
|
|
|
|
func RoleAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
e := &RoleAddedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
|
|
err := event.Unmarshal(e)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-2M0xy", "unable to unmarshal project role")
|
|
}
|
|
|
|
return e, nil
|
|
}
|
|
|
|
type RoleChangedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Key string `json:"key,omitempty"`
|
|
DisplayName *string `json:"displayName,omitempty"`
|
|
Group *string `json:"group,omitempty"`
|
|
}
|
|
|
|
func (e *RoleChangedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *RoleChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func (e *RoleChangedEvent) Fields() []*eventstore.FieldOperation {
|
|
operations := make([]*eventstore.FieldOperation, 0, 2)
|
|
if e.DisplayName != nil {
|
|
operations = append(operations, eventstore.SetField(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
ProjectRoleDisplayNameSearchField,
|
|
&eventstore.Value{
|
|
Value: *e.DisplayName,
|
|
ShouldIndex: true,
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
))
|
|
}
|
|
if e.Group != nil {
|
|
operations = append(operations, eventstore.SetField(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
ProjectRoleGroupSearchField,
|
|
&eventstore.Value{
|
|
Value: *e.Group,
|
|
ShouldIndex: true,
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
))
|
|
}
|
|
|
|
return operations
|
|
}
|
|
|
|
func NewRoleChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
key string,
|
|
changes []RoleChanges,
|
|
) (*RoleChangedEvent, error) {
|
|
if len(changes) == 0 {
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "PROJECT-eR9vx", "Errors.NoChangesFound")
|
|
}
|
|
changeEvent := &RoleChangedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
RoleChangedType,
|
|
),
|
|
Key: key,
|
|
}
|
|
for _, change := range changes {
|
|
change(changeEvent)
|
|
}
|
|
return changeEvent, nil
|
|
}
|
|
|
|
type RoleChanges func(event *RoleChangedEvent)
|
|
|
|
func ChangeKey(key string) func(event *RoleChangedEvent) {
|
|
return func(e *RoleChangedEvent) {
|
|
e.Key = key
|
|
}
|
|
}
|
|
|
|
func ChangeDisplayName(displayName string) func(event *RoleChangedEvent) {
|
|
return func(e *RoleChangedEvent) {
|
|
e.DisplayName = &displayName
|
|
}
|
|
}
|
|
|
|
func ChangeGroup(group string) func(event *RoleChangedEvent) {
|
|
return func(e *RoleChangedEvent) {
|
|
e.Group = &group
|
|
}
|
|
}
|
|
func RoleChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
e := &RoleChangedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
|
|
err := event.Unmarshal(e)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-3M0vx", "unable to unmarshal project role")
|
|
}
|
|
|
|
return e, nil
|
|
}
|
|
|
|
type RoleRemovedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Key string `json:"key,omitempty"`
|
|
}
|
|
|
|
func (e *RoleRemovedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *RoleRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return []*eventstore.UniqueConstraint{NewRemoveProjectRoleUniqueConstraint(e.Key, e.Aggregate().ID)}
|
|
}
|
|
|
|
func (e *RoleRemovedEvent) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.RemoveSearchFieldsByAggregateAndObject(
|
|
e.Aggregate(),
|
|
projectRoleSearchObject(e.Key),
|
|
),
|
|
}
|
|
}
|
|
|
|
func NewRoleRemovedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
key string) *RoleRemovedEvent {
|
|
return &RoleRemovedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
RoleRemovedType,
|
|
),
|
|
Key: key,
|
|
}
|
|
}
|
|
|
|
func RoleRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
e := &RoleRemovedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
|
|
err := event.Unmarshal(e)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-1M0xs", "unable to unmarshal project role")
|
|
}
|
|
|
|
return e, nil
|
|
}
|
|
|
|
func projectRoleSearchObject(id string) eventstore.Object {
|
|
return eventstore.Object{
|
|
Type: ProjectRoleSearchType,
|
|
Revision: ProjectRoleRevision,
|
|
ID: id,
|
|
}
|
|
}
|