Beispiel #1
0
 private static Commit BuildCommit(CommitAttempt attempt)
 {
     return(new Commit(attempt.BucketId,
                       attempt.StreamId,
                       //attempt.ConcurrencyToken,
                       attempt.StreamRevision,
                       attempt.CommitStamp,
                       attempt.Headers,
                       attempt.Body,
                       attempt.Events,
                       isDispatched: false));
 }
Beispiel #2
0
        public async Task <ICommit> CommitAsync(CommitAttempt attempt, CancellationToken cancellation)
        {
            StreamHead streamHead;

            if (attempt.StreamRevision == 1)
            {
                streamHead = await AddStreamHeadAsync(attempt.BucketId,
                                                      attempt.StreamId,
                                                      headRevision : 0,
                                                      snapshotRevision : 0,
                                                      dispatchedRevision : 0,
                                                      cancellation);
            }
            else
            {
                streamHead = await _database.GetOneAsync <StreamHead>(p => p.BucketId.Equals(attempt.BucketId) &&
                                                                      p.StreamId.Equals(attempt.StreamId), cancellation);
            }

            if (streamHead.HeadRevision >= attempt.StreamRevision)
            {
                return(null);
            }

            var commit = BuildCommit(attempt);

            try
            {
                if (!await _database.AddAsync(commit, cancellation))
                {
                    return(null);
                }
            }
            catch (OperationCanceledException) when(cancellation.IsCancellationRequested && attempt.StreamRevision == 1)
            {
                // TODO: Are we allowed to delete the stream head here? Another one may add the first commit to the stream concurrently.
                //await TryWriteOperation(() => _streamHeads.DeleteOneAsync(head => head.BucketId.Equals(attempt.BucketId) && head.StreamId.Equals(attempt.StreamId)));

                throw;
            }

            await UpdateStreamHeadRevisionAsync(streamHead, commit.StreamRevision, cancellation);

            return(commit);
        }