gpt4 book ai didi

c# - 我的扩展方法可以变得更好吗? BlockBlobClient GetBlockById dotnet

转载 作者:行者123 更新时间:2023-12-02 23:34:31 24 4
gpt4 key购买 nike

我为 BlockBlobClient 制作了一个扩展方法,用于按 block ID 获取特定 block ,并且我想知道是否可以调整此代码片段以提高性能/其他内容

public static async Task<T> GetBlockByIdAsync<T>(this BlockBlobClient blockBlobClient, string blockId, CancellationToken cancellationToken)
{
var blockListResponse = await blockBlobClient.GetBlockListAsync(cancellationToken: cancellationToken);
var blockList = blockListResponse.Value.CommittedBlocks.ToList();

var currentBlock = blockList.FirstOrDefault(a => a.Name == blockId);

if (currentBlock.Name == null)
{
throw new InvalidOperationException($"Could not find BlockId {blockId}");
}

var length = currentBlock.SizeLong;
var index = blockList.FindIndex(a => a.Name == blockId);

var offset = 0L;
for (var i = 0; i < index; i++)
{
offset += blockList[i].SizeLong;
}

var options = new BlobDownloadOptions()
{
Range = new HttpRange(offset, length)
};

var blockInfo = await blockBlobClient.DownloadStreamingAsync(options, cancellationToken);

return JsonSerializer.Deserialize<T>(blockInfo.Value.Content);
}

最佳答案

唯一真正突出的是 blockList 的多次迭代(3 次) .

  1. blockList.FirstOrDefault(a => a.Name == blockId)
  2. blockList.FindIndex(a => a.Name == blockId)
  3. for (var i = 0; i < index; i++)

您可以通过一次迭代来完成此操作,如下所示。

public static async Task<T> GetBlockByIdAsync<T>(this BlockBlobClient blockBlobClient, string blockId, CancellationToken cancellationToken)
{
var blockListResponse = await blockBlobClient.GetBlockListAsync(cancellationToken: cancellationToken);
var blockList = blockListResponse.Value.CommittedBlocks.ToList();

var length = 0L;
var offset = 0L;

// iterate over all blocks until we find the block we want.
foreach (var block in blockList){
if (block.Name == blockId){
length = block.SizeLong;
break;
}

// We haven't found the block we want yet so update the offset.
offset += block.SizeLong;
}

// Check if we found the block we were looking for.
if (length == 0)
{
throw new InvalidOperationException($"Could not find BlockId {blockId}");
}

var options = new BlobDownloadOptions()
{
Range = new HttpRange(offset, length)
};

var blockInfo = await blockBlobClient.DownloadStreamingAsync(options, cancellationToken);

return JsonSerializer.Deserialize<T>(blockInfo.Value.Content);
}

警告我没有在本地运行此代码,甚至没有尝试编译它,因此可能会出现错误,但这个想法是合理的。

更新有一个我错过的迭代:blockListResponse.Value.CommittedBlocks.ToList();

当然,调用列表可以防止重复调用 CommittedBlocks但这可能会更有效。

假设blockListResponse.Value.CommittedBlocks返回 IEnumerable` 你甚至可以删除这个...

public static async Task<T> GetBlockByIdAsync<T>(this BlockBlobClient blockBlobClient, string blockId, CancellationToken cancellationToken)
{
var blockListResponse = await blockBlobClient.GetBlockListAsync(cancellationToken: cancellationToken);
var blockList = blockListResponse.Value.CommittedBlocks;

var length = 0L;
var offset = 0L;

// iterate over all blocks until we find the block we want.
var blockListEnumerator = blockList.GetEnumerator();
while (blockListEnumerator.MoveNext())
{
var block = blockListEnumerator.Current as BlobBlock;

if (block.Name == blockId){
length = block.SizeLong;
break;
}

// We haven't found the block we want yet so update the offset.
offset += block.SizeLong;
}

// Check if we found the block we were looking for.
if (length == 0)
{
throw new InvalidOperationException($"Could not find BlockId {blockId}");
}

var options = new BlobDownloadOptions()
{
Range = new HttpRange(offset, length)
};

var blockInfo = await blockBlobClient.DownloadStreamingAsync(options, cancellationToken);

return JsonSerializer.Deserialize<T>(blockInfo.Value.Content);
}

关于c# - 我的扩展方法可以变得更好吗? BlockBlobClient GetBlockById dotnet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75661095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com