コード例 #1
0
        /// <summary>
        /// Asynchronously but eagerly fetches a set of rows, up to the specified count, providing a page of results with a next page token
        /// if more results are available. This is typically used within web applications, where the next page token
        /// is propagated to the client along with the results, so that the next page can be retrieved in another request.
        /// </summary>
        /// <param name="pageSize">The maximum number of rows to retrieve. Must be positive.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A task representing the asynchronous operation. When complete, the result is
        /// an in-memory result set of at most the given number of rows.</returns>
        public async Task <BigQueryPage> ReadPageAsync(int pageSize, CancellationToken cancellationToken = default)
        {
            GaxPreconditions.CheckArgumentRange(pageSize, nameof(pageSize), 1, int.MaxValue);
            GetQueryResultsOptions clonedOptions = _options?.Clone() ?? new GetQueryResultsOptions();
            List <BigQueryRow>     rows          = new List <BigQueryRow>(pageSize);

            // Work out whether to use the response we've already got, or create a new one.
            GetQueryResultsResponse response = _response;

            if (response.Rows?.Count > pageSize)
            {
                // Oops. Do it again from scratch, with a useful page size.
                clonedOptions.PageSize = pageSize;
                response = await _client.GetRawQueryResultsAsync(JobReference, clonedOptions, timeoutBase : null, cancellationToken).ConfigureAwait(false);
            }
            // First add the rows from the existing response.
            rows.AddRange(ConvertResponseRows(response));
            string pageToken = response.PageToken;

            clonedOptions.StartIndex = null;

            // Now keep going until we've filled the result set or know there's no more data.
            while (rows.Count < pageSize && pageToken != null)
            {
                clonedOptions.PageToken = pageToken;
                clonedOptions.PageSize  = pageSize - rows.Count;
                var nextResponse = await _client.GetRawQueryResultsAsync(JobReference, clonedOptions, timeoutBase : null, cancellationToken).ConfigureAwait(false);

                rows.AddRange(ConvertResponseRows(nextResponse));
                pageToken = nextResponse.PageToken;
            }
            return(new BigQueryPage(rows, Schema, JobReference, TableReference, pageToken));
        }