开发者

CloudBlobContainer.ListBlobs() - using ToList() to reduce transaction cost

I have two code examples :

var container = new CloudBlobContainer("address");
va开发者_开发问答r blobs = container.ListBlobs();
foreach (var blob in blobs)
{
    //Do somthing
}

and this :

var container = new CloudBlobContainer("address");
var blobs = container.ListBlobs().ToList();
foreach (var blob in blobs)
{
     //Do somthing
}

Will the second example give any advantage "transaction-wise" ?

How many transaction are made to the blob storage in each example ?


Edit: It appears this question is in reference to a blog post by Martin Ingvar Kofoed Jensen. The difference between this question and that question is the call to .Where(...).SingleOrDefault(). Because LINQ is lazy-evaluated, at the stage of Container.ListBlobs() it is still an IEnumerable and hasn't yet called the REST API (no data has been retrieved). As soon as a result operation occurs (such as ToList() or SingleOrDefault()), the data is downloaded. Since a non-lazy operation is called on a lazy list inside the loop in the following code, it incurs a transaction for every loop iteration:

foreach (string filePath in allFilesInStartFolder)
{
    string fileHash = GetFileHashFromCache(filePath, lastSync);

    /* Checking for added files */
    var blob = blobs.Where(b => b.LocalPath == filePath).SingleOrDefault();
    // ^^ This is a non-lazy op on a lazy evalution, so it causes a REST call.
    ....
}

Regarding the question as it's actually written: Both code snippets will incur a single transaction (up to 5,000 blobs). I tested both code snippets in LinqPad/Fiddler, and I only see a single API call to generate the list of blobs (replaced our storage name with {mystore}):

https://{mystore}.blob.core.windows.net/
    {mystore}?restype=container&comp=list&delimiter=%2F&timeout=90

According to the documentation for List Blobs REST API, up to 5,000 results can be returned in a single call. So if you want to list all blobs in the entire container, it will take at most (# blobs / 5000) transactions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜