예제 #1
0
        public with_snapshots()
        {
            Snapshots = new DefaultSnapshotStore(new InMemoryPersistence());

            Persistence.AppendAsync("Ticket_1", 1, new Changeset(1, new TicketSold())).Wait();
            Persistence.AppendAsync("Ticket_1", 2, new Changeset(2, new TicketRefunded())).Wait();
        }
예제 #2
0
        public virtual async Task <IChunk> AppendAsync(
            object payload,
            string operationId,
            CancellationToken cancellation
            )
        {
            for (var retries = 0; retries < 10; retries++)
            {
                try
                {
                    if (_lastIndex == -1)
                    {
                        var last = await PeekAsync(cancellation).ConfigureAwait(false);

                        _lastIndex = last?.Index ?? 0;
                    }

                    var index = _lastIndex + 1;

                    var chunk = await Persistence.AppendAsync(this.Id, index, payload, operationId, cancellation)
                                .ConfigureAwait(false);

                    _lastIndex = chunk.Index;
                    return(chunk);
                }
                catch (DuplicateStreamIndexException)
                {
                    _lastIndex = -1;
                }
            }

            throw new AppendFailedException(this.Id, "Too many retries");
        }
        public async Task <IChunk> AppendAsync(object payload, string operationId, CancellationToken cancellation)
        {
            IChunk chunk = null;

            if (Version == -1)
            {
                throw new AppendFailedException(this.Id,
                                                $@"Cannot append on stream {this.Id}
Append can be called only after a Read operation.
If you don't need to read use {typeof(Stream).Name} instead of {GetType().Name}.")
                ;
            }
            long desiredVersion = this.Version + 1;

            try
            {
                chunk = await Persistence.AppendAsync(this.Id, desiredVersion, payload, operationId, cancellation).ConfigureAwait(false);
            }
            catch (DuplicateStreamIndexException e)
            {
                throw new ConcurrencyException($"Concurrency exception on StreamId: {this.Id}", e);
            }
            this.Version = desiredVersion;
            return(chunk);
        }
예제 #4
0
 public virtual Task <IChunk> AppendAsync(
     object payload,
     string operationId,
     CancellationToken cancellation
     )
 {
     return(Persistence.AppendAsync(this.Id, -1, payload, operationId, cancellation));
 }
예제 #5
0
        public async Task AppendAsync(object payload, string operationId, CancellationToken cancellation)
        {
            if (Version == -1)
            {
                throw new AppendFailedException(this.Id,
                                                $@"Cannot append on stream {this.Id}
Append can be called only after a Read operation.
If you don't need to read use {typeof(Stream).Name} instead of {GetType().Name}.")
                ;
            }
            long desiredVersion = this.Version + 1;
            await Persistence.AppendAsync(this.Id, desiredVersion, payload, operationId, cancellation).ConfigureAwait(false);

            this.Version = desiredVersion;
        }
예제 #6
0
 public Task <IChunk> PersistAsync(object payload, long index, string operationId, CancellationToken cancellation)
 {
     return(Persistence.AppendAsync(this.Id, index, payload, operationId, cancellation));
 }
예제 #7
0
 public with_populated_stream()
 {
     Persistence.AppendAsync("Ticket_1", 1, new Changeset(1, new TicketSold())).Wait();
     Persistence.AppendAsync("Ticket_1", 2, new Changeset(2, new TicketRefunded())).Wait();
 }