Skip to content

Class CassandraEventStore

Namespace: Cephalon.EventSourcing.Cassandra
Assembly: Cephalon.EventSourcing.Cassandra.dll

Cassandra-backed implementation of using a wide-column table with a composite primary key of (stream_id, stream_version) and optimistic concurrency detection via Lightweight Transaction (LWT) INSERT IF NOT EXISTS.

public sealed class CassandraEventStore : IEventStore, IDisposable, IAsyncDisposable

objectCassandraEventStore

IEventStore, IDisposable, IAsyncDisposable

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Known limitation: The version-read followed by LWT INSERT is not fully atomic. Two concurrent writers that both observe the same expectedVersion will both proceed to issue their LWT INSERT statements. The second writer's LWT will return [applied]=false (because the clustering key is already occupied by the first writer's event), which is caught and rethrown as . The optimistic-concurrency guard therefore holds, but it is enforced at the LWT layer rather than through a single atomic compare-and-swap on the version itself.

CassandraEventStore(ICluster, string, string)

Section titled “ CassandraEventStore(ICluster, string, string)”

Initializes a new instance of the class.

public CassandraEventStore(ICluster cluster, string keyspace, string tableName)

cluster ICluster

The Cassandra cluster. Session is opened lazily on first operation.

keyspace string

The Cassandra keyspace that contains the event-streams table.

tableName string

The Cassandra table name used to persist event stream rows.

AppendAsync(string, IReadOnlyCollection<IDomainEvent>, long, CancellationToken)

Section titled “ AppendAsync(string, IReadOnlyCollection<IDomainEvent>, long, CancellationToken)”

Appends one or more events to the requested stream after checking the expected version.

public Task AppendAsync(string streamId, IReadOnlyCollection<IDomainEvent> events, long expectedVersion, CancellationToken cancellationToken = default)

streamId string

The stable stream identifier.

events IReadOnlyCollection<IDomainEvent>

The events to append.

expectedVersion long

The current stream version expected by the caller. Use -1 to require a brand-new stream.

cancellationToken CancellationToken

The token that cancels the operation.

Task

A task that completes when the append finishes.

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

ValueTask

A task that represents the asynchronous dispose operation.

GetVersionAsync(string, CancellationToken)

Section titled “ GetVersionAsync(string, CancellationToken)”

Gets the latest version known for the requested stream.

public Task<long> GetVersionAsync(string streamId, CancellationToken cancellationToken = default)

streamId string

The stable stream identifier.

cancellationToken CancellationToken

The token that cancels the operation.

Task<long>

A task that returns the current stream version, or -1 when the stream does not exist.

ReadStreamAsync(string, long, CancellationToken)

Section titled “ ReadStreamAsync(string, long, CancellationToken)”

Reads the requested stream from the supplied version onward.

public IAsyncEnumerable<IDomainEvent> ReadStreamAsync(string streamId, long fromVersion = 0, CancellationToken cancellationToken = default)

streamId string

The stable stream identifier.

fromVersion long

The first stream version to include. The default is 0.

cancellationToken CancellationToken

The token that cancels the operation.

IAsyncEnumerable<IDomainEvent>

An async sequence of domain events in ascending stream-version order.