AggregateModel

class AggregateModel<S : Any> @InternalWowApi constructor(val type: AggregateType, val stateType: Class<S>, initial: (AggregateRef) -> S, deciders: Map<Class<*>, DecideFunction<S>>, evolvers: Map<Class<*>, (S, Any) -> S>, eventTypes: Collection<EventType>, commandTypes: Collection<CommandType>, afterFunctions: List<AfterFunction<S>> = emptyList(), lifecycle: Set<Class<*>> = Lifecycle.COMMANDS.values.toSet(), mountedCommandTypes: Collection<CommandType> = emptyList(), val ownership: Ownership = Ownership.OWNER, val stateRevision: Int = 1, retiredEvents: Collection<String> = emptyList(), val idSource: AggregateIdSource = AggregateIdSource.ASSIGNED, aggregateIds: Map<Class<*>, (Any) -> String?> = emptyMap(), commitEvolvers: Map<Class<*>, (S, Any, EventStream) -> S> = emptyMap(), access: Map<AccessAttribute<*, *>, (S) -> Any?> = emptyMap(), occurredAts: Map<Class<*>, (Any) -> Long?> = emptyMap(), val accessRevision: Int = 1)

An aggregate type compiled into tables: what each command type runs and how it is named, how each event type evolves the state, and how each event type is named in the log. Built once, read on every command; the hot path only looks things up.

State is mutable by default: evolve may change S in place and return it. The kernel never lets a changed instance outlive a failure: it evolves a state freshly loaded for the attempt and drops it if the attempt does not commit.

stateType is the class of S: snapshots are read back as it, and its schema fingerprint tells whether a snapshot still fits it (EventCodec.stateSchemaFingerprint).

Every model handles the lifecycle commands of lifecycle (all of them unless it says otherwise) without user code: the kernel decides them, under their system names (Lifecycle). A model that declares a decide function for one overrides the kernel's, to add domain rules to it: it decides that command's own lifecycle event beside the aggregate's own events, NoOp or a rejection, and another lifecycle event fails the command (LifecycleOverride). The compiler never generates one, since an annotated model's decide function takes no lifecycle command: its own command produces the lifecycle event beside its events instead, which the kernel evolves as it evolves the lifecycle commands' events. The lifecycle events are always known, so a log written while a command was enabled still reads; they leave the state as it is unless the model declares an evolve function for them. Only the lifecycle types may take a name in the system namespace.

ownership is who the aggregate belongs to besides its tenant: an Ownership.TENANT aggregate records no owner and checks no command's owner (the kernel keeps only Ownership.scopeOf of a command's scope), so it has no owner to transfer: it never handles TransferOwner.

idSource is where the aggregate's id comes from: an AggregateIdSource.OWNER aggregate's id is its owner's, so the kernel records its id as its owner, refuses a command whose caller has another owner, and never transfers its owner. It belongs to its owner (Ownership.OWNER).

An aggregate whose owner is fixed, of either kind, never commits OwnerTransferred: TransferOwner is left out of its lifecycle, a decide function for it is refused here, and so is a decide function or an after hook whose produces lists the event. A decision that holds one anyway, from a function whose events are unknown, fails its command as an undeclared event does (UndeclaredEventException) and commits nothing, whichever function produced it. A log that holds one still replays.

stateRevision is the revision of the state's structure its snapshots are written at: a snapshot of another revision is not read back, and the service rebuilds the stored ones in place when it is raised.

retiredEvents are the stored names of events the aggregate no longer has (9.x DroppedEvent): the class is gone, but the log still holds records under the name. Such a record is skipped, never decoded: replaying the stream applies nothing for it, though the stream still counts as a version, and a subscription passes over it. A name that is neither an event's nor retired still fails to read. A retired name is not blank, is listed once, and is none of the model's event or command names, current or former. Every model also retires the lifecycle events Wow no longer has (KernelLifecycle.RETIRED_EVENTS, such as wow.lifecycle.TagsApplied), which retiredEvents does not list: a log a released version wrote still loads.

aggregateIds reads, by command class, the id of the aggregate a command acts on from its body (@AggregateId): such a command's request need not name its aggregate (aggregateIdOf). A command decided here takes one, not an aggregate whose id is its owner's.

access are the aggregate's access attributes (@Access), each with the function that reads it from the state: the facts its access policies compare with a caller's claims, derived from the state alone (accessOf) by pure, cheap functions, since they run on each commit. Their names differ even ignoring case, and none is a built-in attribute's (AccessAttribute.BUILT_IN). accessRevision is the revision of how they are derived (@AggregateRoot(accessRevision)): raised, the history is stamped again with what the state derives now.

Constructors

Link copied to clipboard
constructor(type: AggregateType, stateType: Class<S>, initial: (AggregateRef) -> S, deciders: Map<Class<*>, DecideFunction<S>>, evolvers: Map<Class<*>, (S, Any) -> S>, eventTypes: Collection<EventType>, commandTypes: Collection<CommandType>, afterFunctions: List<AfterFunction<S>> = emptyList(), lifecycle: Set<Class<*>> = Lifecycle.COMMANDS.values.toSet(), mountedCommandTypes: Collection<CommandType> = emptyList(), ownership: Ownership = Ownership.OWNER, stateRevision: Int = 1, retiredEvents: Collection<String> = emptyList(), idSource: AggregateIdSource = AggregateIdSource.ASSIGNED, aggregateIds: Map<Class<*>, (Any) -> String?> = emptyMap(), commitEvolvers: Map<Class<*>, (S, Any, EventStream) -> S> = emptyMap(), access: Map<AccessAttribute<*, *>, (S) -> Any?> = emptyMap(), occurredAts: Map<Class<*>, (Any) -> Long?> = emptyMap(), accessRevision: Int = 1)

Properties

Link copied to clipboard

The aggregate's access attributes (@Access) in declaration order; the built-in ones are not among them.

Link copied to clipboard

The names of accessAttributes: the keys of accessOf.

Link copied to clipboard
Link copied to clipboard

The command types this model decides, each under its name and former names: those of its decide functions, then the lifecycle commands the kernel decides for it (lifecycleCommands) under their system names.

Link copied to clipboard
Link copied to clipboard

Every stored event name this aggregate knows: its events' current and former names, and the retired ones it skips (isRetired). A record named otherwise is one only a newer model reads.

Link copied to clipboard

The lifecycle commands this model handles, by the kernel or by its own decide functions.

Link copied to clipboard

The commands mounted under this aggregate's routes that it does not decide: admission finds them by name, and a transformer turns each into a command that is decided.

Link copied to clipboard
Link copied to clipboard

Whether an evolve function reads the commit its event belongs to: the kernel then gives each event its commit (evolve), as the log records it, whether it evolves after a decision or replays the log.

Link copied to clipboard

The stored names of the events this aggregate retired: their records are skipped.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun accessOf(state: S): Map<String, Any?>

The values of the aggregate's access attributes in state, by name, in declaration order: a single attribute's value or null, a multiple one's values as a set, copied, so a mutable state evolving further does not change them. Empty when it declares none. A value that is not of its attribute's type fails, naming the attribute.

Link copied to clipboard
fun afterFunctions(commandType: Class<*>): List<AfterFunction<S>>

The after hooks that run for commandType, in declaration order.

Link copied to clipboard

The id of the aggregate command acts on, as its body holds it (@AggregateId): null when its class holds none there (idFromBody), or when it leaves it null (a creation, which then gets one).

Link copied to clipboard
fun commandType(commandClass: Class<*>): CommandType

The name of commandClass, a command this aggregate decides.

Link copied to clipboard
fun decider(commandType: Class<*>): DecideFunction<S>?

The decide function for commandType, or null when the aggregate does not handle it.

Link copied to clipboard

Whether the aggregates of this model have attribute, as a policy of them may compare it: the built-in space of every aggregate, the built-in owner of one its owner holds (Ownership.OWNER; a tenant's aggregate records none), and a declared attribute by its name, type and multiplicity. The query side has them alike (AccessPolicies.declares); a comparison of one they do not have holds neither way, isNull() included.

Link copied to clipboard

The stored names, current and former, of this aggregate's events that are a type, the type itself or a subtype of it: what a handler of each event of type takes, as the log names it.

Link copied to clipboard
fun eventType(eventClass: Class<*>): EventType

The log name and revision of eventClass.

The event type named name (or formerly named so) in the log. A retired name (isRetired) has none: its records are skipped, not read.

Link copied to clipboard
fun evolve(state: S, event: Any, commit: EventStream? = null): S

Applies event to state. commit is the commit the event belongs to, which an evolve function may read (readsCommits): the kernel gives it, from the log or from the decision being committed.

Link copied to clipboard
fun findCommandType(commandClass: Class<*>): CommandType?

The name of commandClass, or null when it is no command of this aggregate.

The command type named name (or formerly named so), or null when this aggregate has none: how a command that arrives in its wire form (a forwarded one) finds the class its body is read as.

Link copied to clipboard

The event type named name (or formerly named so) in the log, or null when this aggregate has none.

Link copied to clipboard

The command mounted under this aggregate named name (or formerly named so), or null when none is.

Link copied to clipboard
fun idFromBody(commandType: Class<*>): Boolean

Whether a command of commandType holds the id of its aggregate in its body (@AggregateId).

Link copied to clipboard

The state of an aggregate that has no event yet.

Link copied to clipboard

Whether name is the stored name of an event this aggregate retired (retiredEvents), or of a lifecycle event Wow no longer has (KernelLifecycle.RETIRED_EVENTS).

Link copied to clipboard

When command declares that what it records happened (@OccurredAt), epoch milliseconds: null when its class declares no such property, or when it leaves it null.