Table of Contents

Method CommitAsync

Namespace
MailFathom.Infrastructure.Persistence.Users
Assembly
MailFathom.Infrastructure.dll

CommitAsync(UserId, long, Func<GoverningSettingsPolicies, UserRecordCandidate?>, CancellationToken)

Replaces one user's record, if it still stands at the expected version and the policies in force admit what the caller composes under them.

Task<long?> CommitAsync(UserId user, long expectedVersion, Func<GoverningSettingsPolicies, UserRecordCandidate?> compose, CancellationToken cancellationToken)

Parameters

user UserId

The user whose record is written.

expectedVersion long

The version the change was composed over.

compose Func<GoverningSettingsPolicies, UserRecordCandidate>

Composes the record to commit under the policies that govern the user, or answers null to commit nothing. It is called at most once, inside the write's transaction, and performs no I/O of its own.

cancellationToken CancellationToken

Cancels the commit.

Returns

Task<long?>

The version the commit produced, or null when the deployment holds no such user, their record had already moved past expectedVersion, or compose answered nothing.

Remarks

A user the deployment does not hold and a record somebody else moved are one answer, because the statement distinguishes neither and a caller acts on both the same way: it re-reads the user's record, which either reports the version now in force or reports that there is nobody to write for.

The policies are handed to the caller rather than read by it beforehand, because what the record is judged by and what its row is served on have to be the policies in force when the write commits: a replica that has not yet adopted a policy change would otherwise judge and project a record against a policy that no longer holds. They are read inside the write's transaction, under the lock a policy write excludes record writes with, so no policy moves between that read and the commit.

The switches the candidate carries are the ones the record is served on once resolved through those policies, which the same statement writes onto the user's row — where a request reads them without reading any record.

Exceptions

ArgumentException

Thrown when user names nobody, or when the composed record is empty, white space, not a JSON object, or past MaximumOctets as the database would store it.

ArgumentNullException

Thrown when compose is null.

ArgumentOutOfRangeException

Thrown when expectedVersion is negative.

UserSettingsUnwritableException

Thrown when the write did not commit. Every failure but two leaves the user's record exactly as it was; the exceptions are a command timeout and a connection lost while the commit was in flight, because it had been sent by then, so whether it applied is settled by reading the version now in force rather than assumed here.