Home > OS >  How does FeedIterator and ReadNextAsync work?
How does FeedIterator and ReadNextAsync work?

Time:12-29

Can someone explain me the behind the scenes of `

// Query multiple items from container
using FeedIterator<Product> feed = container.GetItemQueryIterator<Product>(
    queryText: "SELECT * FROM products"
);

// Iterate query result pages
while (feed.HasMoreResults)
{
    FeedResponse<Product> response = await feed.ReadNextAsync();

    // Iterate query results
    foreach (Product item in response)
    {
        Console.WriteLine($"Found item:\t{item.name}");
    }
}

`

From https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-dotnet-query-items here what i understand is GetItemQuery fetches the data from the db in pages. feed.HasMoreResults checks if there is any page left, and then ReadNextAsync loads the next page. Response contains all the items in the page loaded and in the foreach loop we go through each item. I would still want an elaborate description of how GetItemQueryIterator fetches data. Does it fetch the entire data that satisfies the query? When GetItemQueryIterator is called, are the pages loaded on some buffer or something or does ReadNextAsync fetches each page directly from the database? Like for a large database are all the pages fetched at once, or a list of pages is being sent by GetItemQueryIterator and ReadNextAsync fetches each page from the list. I dont know how any of this thing is working.

CodePudding user response:

When you obtain the FeedIterator<T> using GetItemQueryIterator<T> no data is retrieved from the database apart from some metadata.

Once you use the ReadNextAsync method the query is send for the first time to the Cosmos database and you'll get the first set of documents back. Each subsequent ReadNextAsync will get the next set of documents of documents until the last response indicates you've obtained everything and HasMoreResults is set to false.

  • Related