Azure / azure-cosmos-dotnet-v3

.NET SDK for Azure Cosmos DB for the core SQL API
MIT License
724 stars 481 forks source link

Feed iterator documentation issues #4392

Open Arcalise08 opened 3 months ago

Arcalise08 commented 3 months ago

In all documentation relating to feed iterators it mentions using it in similar to the following way

var feedIterator = container.GetItemLinqQueryable<User>(
        false,
        null,
        new QueryRequestOptions
        {
            MaxItemCount = 150
        }
    )
    .ToFeedIterator();

var list5 = new List<User>();
string continuation = "";
while (feedIterator.HasMoreResults)
{
    var response = await feedIterator.ReadNextAsync();
    list5.AddRange(response);

    if (response.Count > 0)
    {
        continuation = response.ContinuationToken;
        break;
    }
}

Specifically the while loop here has led to some confusion. What's the purpose of a while loop that breaks when there are responses? Why wouldn't we use it in the following fashion for paged results?

var feedIterator = container.GetItemLinqQueryable<User>(
        false,
        null,
        new QueryRequestOptions
        {
            MaxItemCount = 150
        }
    )
    .ToFeedIterator();

var list5 = new List<User>();
string continuation = "";
if (feedIterator.HasMoreResults)
{
    var response = await feedIterator.ReadNextAsync();
    list5.AddRange(response);

    if (response.Count > 0)
    {
        continuation = response.ContinuationToken;
    }
}

Is there some reason specifically to use a while loop when it breaks when there's a response? I didn't know if this had anything to do with error retrying or something similar. And the code here is prevalent in most documentation and code comments.

ealsur commented 3 months ago

Can you please share where did you see this example? Which is the documentation?

In

if (feedIterator.HasMoreResults)
{
    var response = await feedIterator.ReadNextAsync();
    list5.AddRange(response);

    if (response.Count > 0)
    {
        continuation = response.ContinuationToken;
    }
}

Capturing the continuation makes no sense because the query is executing completely.

In:

var response = await feedIterator.ReadNextAsync();
    list5.AddRange(response);

    if (response.Count > 0)
    {
        continuation = response.ContinuationToken;
        break;
    }

This is simply an example of how to yield a query. Given some condition (in this case Count > 0 but it could be anything that makes sense to you), capture the continuation to continue later at some other time and stop the query.

Arcalise08 commented 3 months ago

Please check the .NET usage examples for pagination

https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/Queries/Program.cs#L230

ealsur commented 3 months ago

Thanks. So the issue is that the example is unclear on why it is capturing the token when it does?

Arcalise08 commented 3 months ago

Its unclear why its in a while loop. If the entire point is to iterate once, then capture the continuation token (if there is one) and return the results. What is the point of putting it in a while loop in the pagination example?

Hopefully I'm being clear here. The example will always only iterate once.