BlobSync.CommonOps.CreateSignatureForLocalFile C# (CSharp) Method

CreateSignatureForLocalFile() public static method

public static CreateSignatureForLocalFile ( string localFilePath ) : SizeBasedCompleteSignature
localFilePath string
return BlobSync.Datatypes.SizeBasedCompleteSignature
        public static SizeBasedCompleteSignature CreateSignatureForLocalFile(string localFilePath)
        {
            var sig = new SizeBasedCompleteSignature();

            var buffer = new byte[ConfigHelper.SignatureSize];
            var sigDict = new Dictionary<int, List<BlockSignature>>();

            using (var fs = new FileStream(localFilePath, FileMode.Open))
            {
                long offset = 0;
                uint idCount = 0;
                int bytesRead = 0;

                while ((bytesRead = fs.Read(buffer, 0, ConfigHelper.SignatureSize)) > 0)
                {
                    var blockSig = GenerateBlockSig(buffer, offset,bytesRead, idCount);
                    List<BlockSignature> sigList;
                    if (!sigDict.TryGetValue(bytesRead, out sigList))
                    {
                        sigList = new List<BlockSignature>();
                        sigDict[bytesRead] = sigList;
                    }

                    sigList.Add(blockSig);

                    offset += bytesRead;
                    idCount++;
                }

            }

            var sizedBaseSignature = new SizeBasedCompleteSignature();
            sizedBaseSignature.Signatures = new Dictionary<int, CompleteSignature>();

            foreach (var key in sigDict.Keys)
            {
                var compSig = new CompleteSignature() {SignatureList = sigDict[key].ToArray()};
                sizedBaseSignature.Signatures[key] = compSig;

            }

            return sizedBaseSignature;
        }

Usage Example

Example #1
0
        // updates blob if possible.
        // if blob doesn't already exist OR does not have a signature file
        // then we just upload as usual.
        public long UploadFile(string containerName, string blobName, string localFilePath)
        {
            var fileLength = CommonOps.GetFileSize(localFilePath);

            // not used here but is cached for later.
            // WORK IN PROGRESS DONT ERASE THIS LINE.
            //ConfigHelper.GetSignatureSize(fileLength, true);

            // 1) Does remote blob exist?
            // 2) if so, download existing signature for blob.
            if (AzureHelper.DoesBlobExist(containerName, blobName) && AzureHelper.DoesBlobSignatureExist(containerName, blobName))
            {
                // 3) If blob exists and have signature, then let the magic begin.
                // 3.1) Download existing blob signature from Azure.
                // 3.2) Search through local file for matches in existing blob signature.
                // 3.3) Upload differences to Azure
                // 3.4) Upload new signature.s

                var blobSig       = DownloadSignatureForBlob(containerName, blobName);
                var searchResults = CommonOps.SearchLocalFileForSignatures(localFilePath, blobSig);
                var allBlocks     = UploadDelta(localFilePath, searchResults, containerName, blobName);
                var sig           = CommonOps.CreateSignatureFromNewAndReusedBlocks(allBlocks);

                UploadSignatureForBlob(blobName, containerName, sig);

                long bytesUploaded = allBlocks.Where(b => b.IsNew).Select(b => b.Size).Sum();

                return(bytesUploaded);
            }
            else
            {
                // 4) If blob or signature does NOT exist, just upload as normal. No tricky stuff to do here.
                // 4.1) Generate signature and upload it.

                var remainingBytes = new RemainingBytes()
                {
                    BeginOffset = 0,
                    EndOffset   = fileLength - 1
                };

                var allUploadedBlocks = UploadBytesParallel(remainingBytes, localFilePath, containerName, blobName);
                // var allUploadedBlocks = UploadBytes(remainingBytes, localFilePath, containerName, blobName);

                var res = (from b in allUploadedBlocks orderby b.Offset ascending select b.BlockId);
                PutBlockList(res.ToArray(), containerName, blobName);

                var sig = CommonOps.CreateSignatureForLocalFile(localFilePath);
                UploadSignatureForBlob(blobName, containerName, sig);

                return(fileLength);
            }
        }
All Usage Examples Of BlobSync.CommonOps::CreateSignatureForLocalFile