memoryprojection

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package memoryprojection provides utilities for building in-memory projections.

Memory projections do not persist any state, and therefore may only be useful for testing or with an event-sourcing engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Query

func Query[T, R any, H MessageHandler[T]](p *Projection[T, H], q func(T) R) R

Query queries a value of type T to produce a result of type R.

q is called with the current value, which may be read within the lifetime of the call to fn. fn MUST NOT retain a reference to the value after the call returns. fn MUST NOT modify the value.

Types

type MessageHandler

type MessageHandler[T any] interface {
	// Configure produces a configuration for this handler by calling methods on
	// the configurer c.
	//
	// The implementation MUST allow for multiple calls to Configure(). Each
	// call SHOULD produce the same configuration.
	//
	// The engine MUST call Configure() before calling HandleEvent(). It is
	// RECOMMENDED that the engine only call Configure() once per handler.
	Configure(c dogma.ProjectionConfigurer)

	// HandleEvent updates the projection to reflect the occurrence of an event.
	// It may do so by modifying v in-place then returning it, or by returning
	// an entirely new value.
	//
	// The engine MAY provide guarantees about the order in which event messages
	// will be passed to HandleEvent(), however in the interest of engine
	// portability the implementation SHOULD NOT assume that HandleEvent() will
	// be called with events in the same order that they were recorded.
	//
	// The engine MUST NOT call HandleEvent() with any message of a type that
	// has not been configured for consumption by a prior call to Configure().
	// If any such message is passed, the implementation MUST panic with the
	// UnexpectedMessage value.
	//
	// The engine MAY call HandleEvent() from multiple goroutines concurrently.
	HandleEvent(v T, s dogma.ProjectionEventScope, m dogma.Message) (T, error)

	// Compact reduces the size of the projection's data. It may do so by
	// modifying v in-place then returning it, or by returning an entirely new
	// value.
	//
	// The implementation SHOULD attempt to decrease the size of the
	// projection's data by whatever means available. For example, it may delete
	// any unused data, or collapse multiple data sets into one.
	//
	// The engine SHOULD call Compact() repeatedly throughout the lifetime of
	// the projection. The precise scheduling of calls to Compact() are
	// engine-defined. It MAY be called concurrently with any other method.
	Compact(v T, s dogma.ProjectionCompactScope) T
}

MessageHandler is a specialization of dogma.ProjectionMessageHandler that builds an in-memory projection represented by a value of type T.

type NoCompactBehavior

type NoCompactBehavior[T any] struct{}

NoCompactBehavior can be embedded in MessageHandler implementations to indicate that the projection does not require its data to be compacted.

It provides an implementation of MessageHandler.Compact() that does nothing.

func (NoCompactBehavior[T]) Compact

func (NoCompactBehavior[T]) Compact(v T, _ dogma.ProjectionCompactScope) T

Compact does nothing.

type Projection

type Projection[T any, H MessageHandler[T]] struct {
	Handler H
	// contains filtered or unexported fields
}

Projection is an in-memory projection that builds a value of type T.

func (*Projection[T, H]) CloseResource

func (p *Projection[T, H]) CloseResource(ctx context.Context, r []byte) error

CloseResource informs the projection that the resource r will not be used in any future calls to HandleEvent().

func (*Projection[T, H]) Compact

Compact reduces the size of the projection's data.

func (*Projection[T, H]) Configure

func (p *Projection[T, H]) Configure(c dogma.ProjectionConfigurer)

Configure produces a configuration for this handler by calling methods on the configurer c.

func (*Projection[T, H]) DeleteResource

func (p *Projection[T, H]) DeleteResource(_ context.Context, r []byte) error

DeleteResource removes all information about the resource r.

func (*Projection[T, H]) HandleEvent

func (p *Projection[T, H]) HandleEvent(
	_ context.Context,
	r, c, n []byte,
	s dogma.ProjectionEventScope,
	m dogma.Message,
) (bool, error)

HandleEvent updates the projection to reflect the occurrence of an event.

func (*Projection[T, H]) ResourceRepository

func (p *Projection[T, H]) ResourceRepository(context.Context) (resource.Repository, error)

ResourceRepository returns a repository that can be used to manipulate the handler's resource versions.

func (*Projection[T, H]) ResourceVersion

func (p *Projection[T, H]) ResourceVersion(_ context.Context, r []byte) ([]byte, error)

ResourceVersion returns the version of the resource r.

func (*Projection[T, H]) StoreResourceVersion

func (p *Projection[T, H]) StoreResourceVersion(_ context.Context, r, v []byte) error

StoreResourceVersion sets the version of the resource r to v without checking the current version.

func (*Projection[T, H]) TimeoutHint

func (p *Projection[T, H]) TimeoutHint(dogma.Message) time.Duration

TimeoutHint returns a duration that is suitable for computing a deadline for the handling of the given message by this handler.

func (*Projection[T, H]) UpdateResourceVersion

func (p *Projection[T, H]) UpdateResourceVersion(_ context.Context, r, c, n []byte) (bool, error)

UpdateResourceVersion updates the version of the resource r to n.

If c is not the current version of r, it returns false and no update occurs.

Directories

Path Synopsis
Package fixtures is a set of test fixtures and mocks for in-memory projections.
Package fixtures is a set of test fixtures and mocks for in-memory projections.

Jump to

Keyboard shortcuts

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