Silvan b5564572bc
feat(eventstore): increase parallel write capabilities (#5940)
This implementation increases parallel write capabilities of the eventstore.
Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and  [06](https://zitadel.com/docs/support/advisory/a10006).
The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`.
If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
2023-10-19 12:19:10 +02:00

106 lines
2.2 KiB
Go

package database
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/jackc/pgtype"
)
type TextArray[t ~string] []t
// Scan implements the [database/sql.Scanner] interface.
func (s *TextArray[t]) Scan(src any) error {
array := new(pgtype.TextArray)
if err := array.Scan(src); err != nil {
return err
}
return array.AssignTo(s)
}
// Value implements the [database/sql/driver.Valuer] interface.
func (s TextArray[t]) Value() (driver.Value, error) {
if len(s) == 0 {
return nil, nil
}
array := pgtype.TextArray{}
if err := array.Set(s); err != nil {
return nil, err
}
return array.Value()
}
type arrayField interface {
~int8 | ~uint8 | ~int16 | ~uint16 | ~int32 | ~uint32
}
type Array[F arrayField] []F
// Scan implements the [database/sql.Scanner] interface.
func (a *Array[F]) Scan(src any) error {
array := new(pgtype.Int8Array)
if err := array.Scan(src); err != nil {
return err
}
elements := make([]int64, len(array.Elements))
if err := array.AssignTo(&elements); err != nil {
return err
}
*a = make([]F, len(elements))
for i, element := range elements {
(*a)[i] = F(element)
}
return nil
}
// Value implements the [database/sql/driver.Valuer] interface.
func (a Array[F]) Value() (driver.Value, error) {
if len(a) == 0 {
return nil, nil
}
array := pgtype.Int8Array{}
if err := array.Set(a); err != nil {
return nil, err
}
return array.Value()
}
type Map[V any] map[string]V
// Scan implements the [database/sql.Scanner] interface.
func (m *Map[V]) Scan(src any) error {
bytea := new(pgtype.Bytea)
if err := bytea.Scan(src); err != nil {
return err
}
if len(bytea.Bytes) == 0 {
return nil
}
return json.Unmarshal(bytea.Bytes, &m)
}
// Value implements the [database/sql/driver.Valuer] interface.
func (m Map[V]) Value() (driver.Value, error) {
if len(m) == 0 {
return nil, nil
}
return json.Marshal(m)
}
type Duration time.Duration
// Scan implements the [database/sql.Scanner] interface.
func (d *Duration) Scan(src any) error {
interval := new(pgtype.Interval)
if err := interval.Scan(src); err != nil {
return err
}
*d = Duration(time.Duration(interval.Microseconds*1000) + time.Duration(interval.Days)*24*time.Hour + time.Duration(interval.Months)*30*24*time.Hour)
return nil
}