Exemple #1
0
        public bool PutBlock(string blockId, byte[] data)
        {
            bool     flag;
            bool     flag1    = false;
            DateTime?nullable = this._lastModifiedTime;
            bool     flag2    = false;
            long     num      = (long)0;
            string   file     = null;

            this._currentLength += data.LongLength;
            this._blockIds.Add(blockId);
            try
            {
                for (int i = 0; i < this._maxNumberOfRetries; i++)
                {
                    this._lastModifiedTime = nullable;
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
                        {
                            using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext())
                            {
                                this.GetContainer(dbContext);
                                BlockBlob blob = this.GetBlob(dbContext, true) as BlockBlob;
                                if (!flag2)
                                {
                                    file  = BlockBlobDataManager.WriteBytesToFile(new BlockBlobMetaInfo(blob.AccountName, blob.ContainerName, blob.BlobName, blob.DirectoryPath), data, out num);
                                    flag2 = true;
                                }
                                BlockData blockDatum = new BlockData()
                                {
                                    AccountName      = blob.AccountName,
                                    ContainerName    = blob.ContainerName,
                                    BlobName         = blob.BlobName,
                                    VersionTimestamp = blob.VersionTimestamp,
                                    IsCommitted      = false,
                                    BlockId          = blockId,
                                    Length           = new long?(data.LongLength),
                                    FilePath         = file,
                                    StartOffset      = new long?(num)
                                };
                                dbContext.BlocksData.InsertOnSubmit(blockDatum);
                                this.SetCopyStatus(blob);
                                dbContext.SubmitChanges();
                                dbContext.Refresh(RefreshMode.OverwriteCurrentValues, blob);
                                transactionScope.Complete();
                                this._lastModifiedTime = blob.LastModificationTime;
                            }
                        }
                        Logger <IRestProtocolHeadLogger> .Instance.Info.Log("Uploaded Block: {0}", new object[] { blockId });

                        break;
                    }
                    catch (SqlException sqlException)
                    {
                        if (this.ShouldRethrowException(i, sqlException, out flag1))
                        {
                            throw;
                        }
                    }
                }
                if (this._currentLength < this._contentLength)
                {
                    return(true);
                }
                return(this.PutBlockList());
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                Logger <IRestProtocolHeadLogger> .Instance.Error.Log("CopyBlob: PutBlock exception {0}", new object[] { exception });

                this.SetCopyFailed(exception, flag1, null);
                flag = false;
            }
            return(flag);
        }
Exemple #2
0
        private IEnumerator <IAsyncResult> PutBlobImpl(string contentType, long contentLength, byte[] serviceMetadata, byte[] applicationMetadata, Stream inputStream, byte[] contentMD5, bool invokeGeneratePutBlobServiceMetadata, GeneratePutBlobServiceMetadata generatePutBlobServiceMetadata, bool isLargeBlockBlobRequest, ISequenceNumberUpdate sequenceNumberUpdate, OverwriteOption overwriteOption, IBlobObjectCondition condition, AsyncIteratorContext <NoResults> context)
        {
            IAsyncResult asyncResult = this._storageManager.AsyncProcessor.BeginExecute((TimeSpan param0) => {
                long num;
                long num1;
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext())
                    {
                        base.LoadContainer(dbContext);
                        string str          = null;
                        BlockBlob blockBlob = base.TryLoadBlockBlob(dbContext, out str);
                        DateTime utcNow     = DateTime.UtcNow;
                        bool flag           = false;
                        if (blockBlob != null)
                        {
                            if (blockBlob.IsCommitted.Value && overwriteOption == OverwriteOption.CreateNewOnly)
                            {
                                throw new BlobAlreadyExistsException();
                            }
                            DbBlobObject.CheckCopyState(blockBlob);
                            flag = DbBlobObject.CheckConditionsAndReturnResetRequired(blockBlob, new BlobLeaseInfo(blockBlob, utcNow), condition, null, true);
                            dbContext.ClearUncommittedBlocks(this._blob.AccountName, this._blob.ContainerName, this._blob.BlobName);
                            dbContext.Refresh(RefreshMode.KeepChanges, blockBlob);
                        }
                        else
                        {
                            if (overwriteOption == OverwriteOption.UpdateExistingOnly)
                            {
                                if (condition == null || !condition.LeaseId.HasValue)
                                {
                                    throw new ConditionNotMetException();
                                }
                                throw new LeaseNotPresentException();
                            }
                            StorageStampHelpers.CheckBlobName(this._blob.BlobName, this._blob.ContainerName);
                            if (string.IsNullOrEmpty(str))
                            {
                                str = BlockBlobDataManager.CreateUniqueDirectoryLoadBalanced();
                            }
                            blockBlob = new BlockBlob()
                            {
                                AccountName      = this._blob.AccountName,
                                ContainerName    = this._blob.ContainerName,
                                BlobName         = this._blob.BlobName,
                                VersionTimestamp = DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc),
                                DirectoryPath    = str
                            };
                            dbContext.Blobs.InsertOnSubmit(blockBlob);
                        }
                        StorageStampHelpers.ValidatePutBlobArguments(this, contentLength, null, applicationMetadata, contentMD5, sequenceNumberUpdate, overwriteOption, condition, true, false);
                        byte[] byteArrayFromStream = DbStorageHelper.GetByteArrayFromStream(inputStream, out num, false, isLargeBlockBlobRequest);
                        string file          = BlockBlobDataManager.WriteBytesToFile(new BlockBlobMetaInfo(this._blob.AccountName, this._blob.ContainerName, this._blob.BlobName, str), byteArrayFromStream, out num1);
                        Guid guid            = Guid.NewGuid();
                        BlockData blockDatum = new BlockData()
                        {
                            AccountName      = this._blob.AccountName,
                            ContainerName    = this._blob.ContainerName,
                            BlobName         = this._blob.BlobName,
                            VersionTimestamp = this._blob.VersionTimestamp,
                            IsCommitted      = false,
                            BlockId          = DbListBlobObject.ToHexString(guid.ToByteArray()),
                            FilePath         = file,
                            StartOffset      = new long?(num1)
                        };
                        dbContext.BlocksData.InsertOnSubmit(blockDatum);
                        if (invokeGeneratePutBlobServiceMetadata && generatePutBlobServiceMetadata != null)
                        {
                            serviceMetadata = generatePutBlobServiceMetadata();
                        }
                        blockBlob.ContentType              = contentType ?? "application/octet-stream";
                        blockBlob.ContentMD5               = contentMD5;
                        blockBlob.ServiceMetadata          = serviceMetadata;
                        blockBlob.Metadata                 = applicationMetadata;
                        blockBlob.IsCommitted              = new bool?(false);
                        blockBlob.HasBlock                 = new bool?(false);
                        blockBlob.UncommittedBlockIdLength = null;
                        blockBlob.GenerationId             = Guid.NewGuid().ToString();
                        blockBlob.SnapshotCount            = 0;
                        blockDatum.Length       = new long?(num);
                        blockBlob.ContentLength = num;
                        base.ResetBlobLeaseToAvailable(blockBlob, flag);
                        dbContext.SubmitChanges();
                        StringBuilder stringBuilder = new StringBuilder();
                        DbListBlobObject.SerializeCommitBlockListEntry(stringBuilder, guid.ToByteArray(), BlockSource.Uncommitted);
                        DateTime?nullable = null;
                        dbContext.CommitBlockList(this._blob.AccountName, this._blob.ContainerName, this._blob.BlobName, stringBuilder.ToString(), ref nullable);
                        transactionScope.Complete();
                        blockBlob.LastModificationTime = nullable;
                        this._blob     = blockBlob;
                        this.LeaseInfo = new BlobLeaseInfo(blockBlob, utcNow);
                    }
                }
            }, base.Timeout, context.GetResumeCallback(), context.GetResumeState("DbListBlobObject.PutBlobImpl"));

            yield return(asyncResult);

            this._storageManager.AsyncProcessor.EndExecute(asyncResult);
        }
Exemple #3
0
        public bool PutPage(long startIndex, byte[] data)
        {
            bool flag;
            bool flag1 = false;

            this._currentLength += data.LongLength;
            bool     flag2    = false;
            DateTime?nullable = this._lastModifiedTime;

            try
            {
                for (int i = 0; i < this._maxNumberOfRetries; i++)
                {
                    this._lastModifiedTime = nullable;
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            using (DevelopmentStorageDbDataContext dbContext = DevelopmentStorageDbDataContext.GetDbContext())
                            {
                                this.GetContainer(dbContext);
                                PageBlob    blob        = this.GetBlob(dbContext, true) as PageBlob;
                                CurrentPage currentPage = new CurrentPage()
                                {
                                    AccountName      = blob.AccountName,
                                    ContainerName    = blob.ContainerName,
                                    BlobName         = blob.BlobName,
                                    VersionTimestamp = blob.VersionTimestamp,
                                    StartOffset      = startIndex,
                                    EndOffset        = startIndex + data.LongLength,
                                    SnapshotCount    = blob.SnapshotCount
                                };
                                dbContext.CurrentPages.InsertOnSubmit(currentPage);
                                lock (DbPageBlobObject.m_writeFileLock)
                                {
                                    if (!flag2)
                                    {
                                        using (FileStream fileStream = new FileStream(DbPageBlobObject.GetFilePath(blob.FileName), FileMode.OpenOrCreate, FileAccess.Write))
                                        {
                                            fileStream.Seek(startIndex, SeekOrigin.Begin);
                                            fileStream.Write(data, 0, (int)data.Length);
                                        }
                                        flag2 = true;
                                    }
                                    dbContext.SubmitChanges();
                                    dbContext.Refresh(RefreshMode.OverwriteCurrentValues, blob);
                                    this.SetCopyStatus(blob);
                                    dbContext.SubmitChanges();
                                    dbContext.Refresh(RefreshMode.OverwriteCurrentValues, blob);
                                    transactionScope.Complete();
                                    this._lastModifiedTime = blob.LastModificationTime;
                                }
                                IStringDataEventStream info = Logger <IRestProtocolHeadLogger> .Instance.Info;
                                object[] objArray           = new object[] { startIndex, startIndex + data.LongLength - (long)1 };
                                info.Log("Uploaded Page: {0}-{1}", objArray);
                            }
                        }
                        break;
                    }
                    catch (SqlException sqlException)
                    {
                        if (this.ShouldRethrowException(i, sqlException, out flag1))
                        {
                            throw;
                        }
                    }
                }
                return(true);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                Logger <IRestProtocolHeadLogger> .Instance.Error.Log("CopyBlob: PutPage exception {0}", new object[] { exception });

                this.SetCopyFailed(exception, flag1, null);
                flag = false;
            }
            return(flag);
        }