HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

"Good" Blob search

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
blobsearchgood

Problem

I written this code to search 'blob' items (text files) on the basis of their content. For example, if I search for "Good", then the names of files that contain "Good" or "good" should appear in the search result.

My code is working but I want to optimize it:

```
class BlobSearch
{
public static int num = 1;
static void Main(string[] args)
{
string accountName = "accountName";
string accessKey = "accesskey";
string azureConString = "DefaultEndpointsProtocol=https;AccountName=" + accountName + ";AccountKey=" + accessKey;
string blob = "MyBlobContainer";
string searchText = string.Empty;

Console.WriteLine("Type and enter to search : ");
searchText = Console.ReadLine();

CloudStorageAccount account = CloudStorageAccount.Parse(azureConString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(blob);

blobContainer.FetchAttributes();
var blobItemList = blobContainer.ListBlobs();
GetBlobList(searchText, blobContainer, blobItemList);

Console.ReadLine();
}

private static async void GetBlobList(string searchText, CloudBlobContainer blobContainer, IEnumerable blobItemList)
{
foreach (var item in blobItemList)
{
string line = string.Empty;
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(item.Uri.ToString());
if (blockBlob.Name.Contains(".txt"))
{
await Search(searchText, blockBlob);
}
}
}

private async static Task Search(string searchText, CloudBlockBlob blockBlob)
{
string text = await blockBlob.DownloadTextAsync();
if (text.ToLower().IndexOf(searchText.ToLower()) != -1)
{
Console.WriteLine("Result : " + num + " => " + blockBlob.Name.Substring(blockBlob.Name.LastIndexOf('/') + 1));
num+

Solution

If I understand correctly:

  • blobContainer.ListBlobs() is what is taking a long time



  • ListBlobs is not code that you control or can improve



  • There isn't a way to perform the filtering you want without calling ListBlobs



It follows that there isn't any way to appreciably improve your program without fundamentally altering your approach. As the comments suggest, indexing is one common approach, either a simple solution you implement yourself or an integration with an existing software package (depending on your needs and requirements).

Context

StackExchange Code Review Q#54009, answer score: 2

Revisions (0)

No revisions yet.