using static MFiles.Grpc.Samples.MFilesValues;
namespace MFiles.Grpc.Samples.Recipes;
///
/// Recipe "paging": read every document, a page at a time, in object ID order.
/// Arguments: [page size, default 100]
///
public static class Paging
{
/// Sort key for ascending object ID order.
private const int SortByObjectId = -102;
public static async Task RunAsync(MFilesSettings settings, string[] args)
{
int pageSize = RecipeHelpers.IntArg(args, 0, 100);
await using var connection = await MFilesConnection.ConnectAsync(settings, RecipeHelpers.ClientName);
var search = new IRPCSearch.IRPCSearchClient(connection.CallInvoker);
// [START paging]
// Keyset paging: sort by object ID and ask each page for IDs above the last one seen.
int lastId = 0, page = 0, total = 0;
while (true)
{
var request = new SearchObjectsRequest
{
Conditions =
{
new SearchConditionArray
{
Value =
{
IsObjectType(0),
IsNotDeleted(),
StatusCondition(StatusType.ObjectId, ConditionType.GreaterThan, Integer(lastId)),
},
},
},
Sorting = new SearchResultsSorting
{
Rules = new SortRuleValueArray { Values = { new SortRule { Key = SortByObjectId } } },
},
Limit = (uint)pageSize,
};
var response = await search.SearchObjectsAsync(request);
if (response.Results.Count == 0)
break;
page++;
total += response.Results.Count;
int firstId = response.Results[0].Object.ObjectInfo.ObjId.ItemId.InternalId;
lastId = response.Results[^1].Object.ObjectInfo.ObjId.ItemId.InternalId;
Console.WriteLine($"Page {page}: {response.Results.Count} documents, IDs {firstId}-{lastId}");
if (!response.HasMoreResults)
break;
}
// [END paging]
Console.WriteLine($"Total: {total} documents in {page} pages");
}
}