storage

package
v0.0.0-...-89e24ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetId

func GetId() (uuid.UUID, error)

func GetTx

func GetTx[T any](ctx context.Context) T

Types

type ActorRepository

type ActorRepository[T spry.Actor[T]] struct {
	Repository[T]
}

func GetActorRepositoryFor

func GetActorRepositoryFor[T spry.Actor[T]](storage Storage) ActorRepository[T]

func (ActorRepository[T]) Fetch

func (repository ActorRepository[T]) Fetch(ids spry.Identifiers) (T, error)

func (ActorRepository[T]) Handle

func (repository ActorRepository[T]) Handle(command spry.Command) spry.Results[T]

type AggregateIdMap

type AggregateIdMap struct {
	ActorName  string
	ActorId    uuid.UUID
	Aggregated AggregatedIds
}

func CreateAggregateIdMap

func CreateAggregateIdMap(actorName string, actorId uuid.UUID) AggregateIdMap

func EmptyAggregateIdMap

func EmptyAggregateIdMap() AggregateIdMap

func (*AggregateIdMap) AddIdsFor

func (idMap *AggregateIdMap) AddIdsFor(child string, id ...uuid.UUID)

type AggregateRepository

type AggregateRepository[T spry.Aggregate[T]] struct {
	Repository[T]
}

func GetAggregateRepositoryFor

func GetAggregateRepositoryFor[T spry.Aggregate[T]](storage Storage) AggregateRepository[T]

func (AggregateRepository[T]) Fetch

func (repository AggregateRepository[T]) Fetch(ids spry.Identifiers) (T, error)

func (AggregateRepository[T]) Handle

func (repository AggregateRepository[T]) Handle(command spry.Command) spry.Results[T]

type AggregatedIds

type AggregatedIds = map[string][]uuid.UUID

type Caster

type Caster = func(any) (any, error)

type CommandRecord

type CommandRecord struct {
	// a generated uuid for this event
	Id uuid.UUID `json:"id"`
	// the type name of the command
	Type string `json:"type"`
	// namespace for the command
	Namespace string `json:"namespace"`
	// the time the command was handled
	CreatedOn time.Time `json:"createdOn"`
	// the time the command was handled
	ReceivedOn time.Time `json:"receivedOn"`
	// the time the command was handled
	HandledOn time.Time `json:"handledOn"`
	// the id of the recipient actor
	HandledBy uuid.UUID `json:"handledBy"`
	// the version of the actor that handled the command
	HandledVersion uint64
	// the contents of the command
	Data any `json:"data"`
}

func NewCommandRecord

func NewCommandRecord(command spry.Command) (CommandRecord, error)

func (CommandRecord) IsValid

func (command CommandRecord) IsValid() bool

type CommandStore

type CommandStore interface {
	Add(context.Context, string, CommandRecord) error
}

type EventRecord

type EventRecord struct {
	// a generated uuid for this event
	Id uuid.UUID `json:"id"`
	// the type name of the event
	Type string `json:"type"`
	// inferred from the actor emitting the event
	ActorNamespace string `json:"namespace"`
	// this is the addressable identity of the owning model
	ActorId uuid.UUID `json:"actorId"`
	// the type of the model the event was generated for
	ActorName string `json:"actor"`
	// UTC ISO date time string when event was created
	CreatedOn time.Time `json:"createdOn"`
	// the type of the actor instantiating the event
	CreatedBy string `json:"createdBy"`
	// the id of the snapshot instantiating the event
	CreatedById uuid.UUID `json:"createdById"`
	// the vector of the snapshot instantiating the event
	CreatedByVector string `json:"createdByVector"`
	// the version of the snapshot instantiating the event
	CreatedByVersion uint64 `json:"createdByVersion"`
	// the command type/topic that triggered the event
	InitiatedBy string `json:"initiatedBy"`
	// the id of the message that triggered the event
	InitiatedById uuid.UUID `json:"initiatedById"`
	// the contents of the event
	Data any `json:"data"`
}

func NewEventRecord

func NewEventRecord(event spry.Event) (EventRecord, error)

func (EventRecord) IsValid

func (event EventRecord) IsValid() bool

type EventStore

type EventStore interface {
	Add(context.Context, []EventRecord) error
	FetchAggregatedSince(context.Context, string, uuid.UUID, uuid.UUID, LastEventMap, TypeMap) ([]EventRecord, error)
	FetchSince(context.Context, string, uuid.UUID, uuid.UUID, TypeMap) ([]EventRecord, error)
}

type IdAssignment

type IdAssignment struct {
	ActorName   string
	AssignedId  uuid.UUID
	Identifiers spry.Identifiers
	Json        string
}

func NewAssignment

func NewAssignment(name string, ids spry.Identifiers, id uuid.UUID) IdAssignment

type IdAssignments

type IdAssignments struct {
	// contains filtered or unexported fields
}

func NewAssignments

func NewAssignments(aggregateName string) IdAssignments

func (*IdAssignments) AddAssignment

func (a *IdAssignments) AddAssignment(name string, ids spry.Identifiers, id uuid.UUID)

func (*IdAssignments) CreateAssignment

func (a *IdAssignments) CreateAssignment(name string, ids spry.Identifiers) uuid.UUID

func (*IdAssignments) GetAggregateId

func (a *IdAssignments) GetAggregateId() uuid.UUID

func (*IdAssignments) GetIdFor

func (a *IdAssignments) GetIdFor(name string, ids spry.Identifiers) uuid.UUID

type LastEventMap

type LastEventMap struct {
	LastEvents map[string]map[uuid.UUID]uuid.UUID
}

func CreateLastEvents

func CreateLastEvents() LastEventMap

func (*LastEventMap) AddLastEventFor

func (last *LastEventMap) AddLastEventFor(child string, childId uuid.UUID, lastEventId uuid.UUID)

func (*LastEventMap) UpdateFromMap

func (last *LastEventMap) UpdateFromMap(idMap AggregateIdMap)

type NoOpTx

type NoOpTx struct{}

func (NoOpTx) Commit

func (tx NoOpTx) Commit() error

func (NoOpTx) Rollback

func (tx NoOpTx) Rollback() error

type Repository

type Repository[T any] struct {
	ActorType reflect.Type
	ActorName string
	Storage   Storage
	Mapping   TypeMap
}

func (Repository[T]) Apply

func (repository Repository[T]) Apply(events []spry.Event, actor T) T

A side-effect free way of applying events to an actor instance

type Snapshot

type Snapshot struct {
	// aggregates track records and last events from each
	// for each child type, track the last event per Identifier
	LastEventMap
	// a generated uuid (system id) for the snapshot instance
	Id uuid.UUID `json:"id"`
	// this is the addressable identity of the owning model
	ActorId uuid.UUID `json:"actorId"`
	// the type name of the actor
	Type string `json:"type"`
	// a serialized causal tracker
	Vector string `json:"vector"`
	// a numeric version of the model
	Version uint64 `json:"version"`
	// the causal tracker of the preceding snapshot
	Ancestor string `json:"ancestor"`
	// UTC ISO date time string when event was created
	CreatedOn time.Time `json:"createdOn"`
	// the number of events applied to reach the present state
	EventsApplied uint64 `json:"eventsApplied"`
	// the number of events since the last snapshot was created
	EventSinceSnapshot int
	// the UUID of the last event played against the instance
	LastEventId uuid.UUID `json:"lastEventId"`
	// the UUID of the last command handled
	LastCommandId uuid.UUID `json:"lastCommandId"`
	// the wall clock at the time of the last command
	LastCommandOn time.Time `json:"lastCommandOn"`
	// the wall clock at the time of the last event
	LastEventOn time.Time `json:"lastEventOn"`
	// the contents of the snapshot
	Data any `json:"data"`
}

func NewSnapshot

func NewSnapshot(actor any) (Snapshot, error)

func (Snapshot) IsValid

func (snapshot Snapshot) IsValid() bool

type SnapshotStore

type SnapshotStore interface {
	Add(context.Context, string, Snapshot, bool) error
	Fetch(context.Context, string, uuid.UUID) (Snapshot, error)
}

type Storage

type Storage interface {
	AddCommand(context.Context, string, CommandRecord) error
	AddEvents(context.Context, []EventRecord) error
	AddMap(context.Context, string, spry.Identifiers, uuid.UUID) error
	AddSnapshot(context.Context, string, Snapshot, bool) error
	AddLink(context.Context, string, uuid.UUID, string, uuid.UUID) error
	Commit(context.Context) error
	FetchAggregatedEventsSince(context.Context, string, uuid.UUID, uuid.UUID, LastEventMap) ([]EventRecord, error)
	FetchEventsSince(context.Context, string, uuid.UUID, uuid.UUID) ([]EventRecord, error)
	FetchId(context.Context, string, spry.Identifiers) (uuid.UUID, error)
	FetchIdMap(context.Context, string, uuid.UUID) (AggregateIdMap, error)
	FetchLatestSnapshot(context.Context, string, uuid.UUID) (Snapshot, error)
	GetContext(context.Context) (context.Context, error)
	RegisterPrimitives(...any)
	Rollback(context.Context) error
}

func NewStorage

func NewStorage[Tx any](
	commands CommandStore,
	events EventStore,
	maps MapStore,
	snapshots SnapshotStore,
	txs TxProvider[Tx]) Storage

type Stores

type Stores[Tx any] struct {
	Commands     CommandStore
	Events       EventStore
	Maps         MapStore
	Primitives   TypeMap
	Snapshots    SnapshotStore
	Transactions TxProvider[Tx]
}

func (Stores[Tx]) AddCommand

func (storage Stores[Tx]) AddCommand(ctx context.Context, actorName string, command CommandRecord) error

func (Stores[Tx]) AddEvents

func (storage Stores[Tx]) AddEvents(ctx context.Context, events []EventRecord) error
func (storage Stores[Tx]) AddLink(
	ctx context.Context,
	parentName string,
	parentId uuid.UUID,
	childName string,
	childId uuid.UUID) error

func (Stores[Tx]) AddMap

func (storage Stores[Tx]) AddMap(ctx context.Context, actorName string, identifiers spry.Identifiers, uid uuid.UUID) error

func (Stores[Tx]) AddSnapshot

func (storage Stores[Tx]) AddSnapshot(ctx context.Context, actorName string, snapshot Snapshot, allowPartition bool) error

func (Stores[Tx]) Commit

func (storage Stores[Tx]) Commit(ctx context.Context) error

func (Stores[Tx]) FetchAggregatedEventsSince

func (storage Stores[Tx]) FetchAggregatedEventsSince(ctx context.Context, actorName string, actorId uuid.UUID, eventId uuid.UUID, idMap LastEventMap) ([]EventRecord, error)

func (Stores[Tx]) FetchEventsSince

func (storage Stores[Tx]) FetchEventsSince(ctx context.Context, actorName string, actorId uuid.UUID, eventId uuid.UUID) ([]EventRecord, error)

func (Stores[Tx]) FetchId

func (storage Stores[Tx]) FetchId(ctx context.Context, actorName string, identifiers spry.Identifiers) (uuid.UUID, error)

func (Stores[Tx]) FetchIdMap

func (storage Stores[Tx]) FetchIdMap(ctx context.Context, actorName string, actorId uuid.UUID) (AggregateIdMap, error)

func (Stores[Tx]) FetchLatestSnapshot

func (storage Stores[Tx]) FetchLatestSnapshot(ctx context.Context, actorName string, actorId uuid.UUID) (Snapshot, error)

func (Stores[Tx]) GetContext

func (storage Stores[Tx]) GetContext(ctx context.Context) (context.Context, error)

func (Stores[Tx]) RegisterPrimitives

func (storage Stores[Tx]) RegisterPrimitives(types ...any)

func (Stores[Tx]) Rollback

func (storage Stores[Tx]) Rollback(ctx context.Context) error

type StringTemplate

type StringTemplate struct {
	// contains filtered or unexported fields
}

func CreateTemplateFromFS

func CreateTemplateFromFS(files fs.FS, paths ...string) (*StringTemplate, error)

func (StringTemplate) Execute

func (st StringTemplate) Execute(name string, data any) (string, error)

type TxProvider

type TxProvider[T any] interface {
	GetTransaction(ctx context.Context) (T, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type TypeMap

type TypeMap struct {
	Events   map[string]Caster
	Commands map[string]Caster
}

func CreateTypeMap

func CreateTypeMap() TypeMap

func (TypeMap) AddTypes

func (m TypeMap) AddTypes(types ...any)

func (TypeMap) AsCommand

func (m TypeMap) AsCommand(commandType string, v any) (spry.Command, error)

func (TypeMap) AsEvent

func (m TypeMap) AsEvent(eventType string, v any) (spry.Event, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL