Sourcing::Plugin::Memory

In-memory implementation of the Plugin interface. Suitable for testing and development.

Overview

This class combines Sourcing::Plugin::EventStore::Memory and Sourcing::Plugin::StateCache::Memory to implement both the EventStore and StateCache roles.

You can also use the separate implementations:

Attributes

AttributeTypeDescription
$.event-storeSourcing::Plugin::EventStore::MemoryThe event storage backend
$.state-cacheSourcing::Plugin::StateCache::MemoryThe state caching backend

Methods

multi method emit($event)

Simple emit to the supplier without version checking.

multi method emit($event, :$type, :%ids!, :$current-version!)

Emit with optimistic locking. Uses CAS (compare-and-swap) for atomic version checking. Throws X::OptimisticLocked if the expected version doesn't match the stored version.

Also validates that $type is an aggregation — projections cannot emit events.

method get-events(%ids, %map)

Filters events by type map keys and ID field values.

method get-events-after(Int $id, %ids, %map)

Gets events after a specific version ID. Uses .skip($id + 1) on the event array.

method supply

Returns a Supply that emits events as they're stored.

method events

Returns the list of stored events.

method number-of-events

Returns total event count.

multi method store-cached-data($proj where *.HOW.^can("data-to-store"), UInt :$last-id!)

Stores using the projection's custom data-to-store method for selective serialization.

multi method store-cached-data($proj, Int :$last-id!)

Stores by extracting all public attributes from the projection.

multi method store-cached-data(Mu:U $proj, %ids, %data, Int :$last-id!)

Core storage method. Stores in %!store{ProjectionName}{IDs}{data, last-id}.

method get-cached-data(Mu:U $proj, %ids) is rw

Retrieves cached state. Returns a Hash with last-id (atomicint, default -1) and data (Hash of projection attributes).

Usage

use Sourcing::Plugin::Memory;

# Activate the plugin
Sourcing::Plugin::Memory.use;

# Access the plugin instance
my $memory = $*SourcingConfig;

# Inspect events
say "Total events: ", $memory.events.elems;
say "First event: ", $memory.events[0].^name;

See Also