Esempio n. 1
0
        /// <summary>
        /// Adds a commit to a stream.
        /// </summary>
        /// <param name="attempt">The commit attempt to be added.</param>
        /// <returns>An Commit if successful.</returns>
        public ICommit Commit(CommitAttempt attempt)
        {
            var pageBlob = WrappedPageBlob.CreateNewIfNotExists(_primaryContainer, attempt.BucketId + "/" + attempt.StreamId, _options.BlobNumPages);
            HeaderDefinitionMetadata headerDefinitionMetadata = null;
            var header = GetHeaderWithRetry(pageBlob, out headerDefinitionMetadata);

            // we must commit at a page offset, we will just track how many pages in we must start writing at
            var startPage = 0;

            foreach (var commit in header.PageBlobCommitDefinitions)
            {
                if (commit.CommitId == attempt.CommitId)
                {
                    throw new DuplicateCommitException("Duplicate Commit Attempt");
                }

                startPage += commit.TotalPagesUsed;
            }

            if (attempt.CommitSequence <= header.LastCommitSequence)
            {
                throw new ConcurrencyException("Concurrency exception in Commit");
            }

            var blobCommit =
                new AzureBlobCommit
            {
                BucketId       = attempt.BucketId,
                CommitId       = attempt.CommitId,
                CommitSequence = attempt.CommitSequence,
                CommitStampUtc = attempt.CommitStamp,
                Events         = attempt.Events.ToList(),
                Headers        = attempt.Headers,
                StreamId       = attempt.StreamId,
                StreamRevision = attempt.StreamRevision,
                Checkpoint     = GetNextCheckpoint()
            };
            var serializedBlobCommit = _serializer.Serialize(blobCommit);

            header.AppendPageBlobCommitDefinition(new PageBlobCommitDefinition(serializedBlobCommit.Length, attempt.CommitId, attempt.StreamRevision,
                                                                               attempt.CommitStamp, header.PageBlobCommitDefinitions.Count, startPage, blobCommit.Checkpoint));
            ++header.UndispatchedCommitCount;
            header.LastCommitSequence = attempt.CommitSequence;

            CommitNewMessage(pageBlob, serializedBlobCommit, header, headerDefinitionMetadata, startPage * BlobPageSize);
            return(CreateCommitFromAzureBlobCommit(blobCommit));
        }
Esempio n. 2
0
 /// <summary>
 /// Creates a Commit object from an AzureBlobEntry.
 /// </summary>
 /// <param name="blobEntry">The source AzureBlobEntry.</param>
 /// <returns>The populated Commit.</returns>
 private ICommit CreateCommitFromAzureBlobCommit(AzureBlobCommit blobEntry)
 {
     return(new Commit(blobEntry.BucketId, blobEntry.StreamId, blobEntry.StreamRevision, blobEntry.CommitId,
                       blobEntry.CommitSequence, blobEntry.CommitStampUtc, blobEntry.Checkpoint.ToString(),
                       blobEntry.Headers, blobEntry.Events));
 }