Table of Contents

Class SqlServerCursorMapStore

Namespace
Excalibur.EventSourcing.SqlServer
Assembly
Excalibur.EventSourcing.SqlServer.dll

SQL Server implementation of ICursorMapStore using a key-value table.

public sealed class SqlServerCursorMapStore : ICursorMapStore
Inheritance
SqlServerCursorMapStore
Implements
Inherited Members

Remarks

Uses a ProjectionCursorMaps table with columns (ProjectionName, StreamId, Position). Saves are atomic via MERGE.

This store issues no DDL at run time. Provision the table from the 008_CreateCursorMapSchema.sql script shipped in this package's scripts folder before the first checkpoint; without it every save fails with Msg 208, Invalid object name 'ProjectionCursorMaps'.

Table DDL, abridged — run the script rather than transcribing this. The script carries the binary collation on the three key columns and the non-clustered primary key, both of which are load-bearing and neither of which is visible here: the natural key is 1152 bytes, over SQL Server's 900-byte clustered limit, so a plain PRIMARY KEY creates a table that accepts the definition and then refuses long rows at run time.

CREATE TABLE ProjectionCursorMaps (
    TenantId NVARCHAR(64) NOT NULL,
    ProjectionName NVARCHAR(256) NOT NULL,
    StreamId NVARCHAR(256) NOT NULL,
    Position BIGINT NOT NULL,
    CONSTRAINT PK_ProjectionCursorMaps PRIMARY KEY NONCLUSTERED (TenantId, ProjectionName, StreamId)
);

Constructors

SqlServerCursorMapStore(Func<SqlConnection>, ILogger<SqlServerCursorMapStore>, ITenantContext)

Initializes a new instance with a connection factory and an ambient tenant context.

public SqlServerCursorMapStore(Func<SqlConnection> connectionFactory, ILogger<SqlServerCursorMapStore> logger, ITenantContext tenantContext)

Parameters

connectionFactory Func<SqlConnection>

A factory that creates SqlConnection instances.

logger ILogger<SqlServerCursorMapStore>

The logger instance.

tenantContext ITenantContext

The ambient tenant context. Required: this store partitions rows by tenant, and it resolves that partition from here, so there is no state in which the partition is undecided. A single-tenant host receives the framework default context and operates as the one canonical tenant.

SqlServerCursorMapStore(string, ILogger<SqlServerCursorMapStore>, ITenantContext)

Initializes a new instance with a connection string and an ambient tenant context.

public SqlServerCursorMapStore(string connectionString, ILogger<SqlServerCursorMapStore> logger, ITenantContext tenantContext)

Parameters

connectionString string

The SQL Server connection string.

logger ILogger<SqlServerCursorMapStore>

The logger instance.

tenantContext ITenantContext

The ambient tenant context. Required: this store partitions rows by tenant, and it resolves that partition from here, so there is no state in which the partition is undecided. A single-tenant host receives the framework default context and operates as the one canonical tenant.

Methods

GetCursorMapAsync(string, CancellationToken)

Gets the cursor map for a named projection.

public Task<IReadOnlyDictionary<string, long>> GetCursorMapAsync(string projectionName, CancellationToken cancellationToken)

Parameters

projectionName string

The unique projection identifier.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<IReadOnlyDictionary<string, long>>

A dictionary mapping stream identifiers to their last-processed positions. Returns an empty dictionary if no cursor map exists.

ResetCursorMapAsync(string, CancellationToken)

Resets the cursor map for a named projection, removing all stored positions. Used during projection rebuild to restart from the beginning.

public Task ResetCursorMapAsync(string projectionName, CancellationToken cancellationToken)

Parameters

projectionName string

The unique projection identifier.

cancellationToken CancellationToken

Cancellation token.

Returns

Task

SaveCursorMapAsync(string, IReadOnlyDictionary<string, long>, CancellationToken)

Saves the cursor map for a named projection atomically. All stream positions are updated together or not at all.

public Task SaveCursorMapAsync(string projectionName, IReadOnlyDictionary<string, long> cursorMap, CancellationToken cancellationToken)

Parameters

projectionName string

The unique projection identifier.

cursorMap IReadOnlyDictionary<string, long>

The stream positions to persist.

cancellationToken CancellationToken

Cancellation token.

Returns

Task