public override async Task InitializeAsync(CancellationToken cancellationToken = default)
        {
            if (_rows != null)
            {
                throw new InvalidOperationException("Cannot initialize more than once.");
            }

            if (ResponseStream != null)
            {
                var body = await _deserializer.DeserializeAsync <ViewResultData>(ResponseStream, cancellationToken).ConfigureAwait(false);

                MetaData = new ViewMetaData
                {
                    TotalRows = body.total_rows
                };

                _rows = body.rows?.Select(p => new ViewRow <TKey, TValue>
                {
                    Id    = p.id,
                    Key   = p.key,
                    Value = p.value
                }) ?? Enumerable.Empty <IViewRow <TKey, TValue> >();
            }
            else
            {
                _rows = Enumerable.Empty <IViewRow <TKey, TValue> >();
            }

            DecodeSpan?.Finish();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// An enumerator that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator <T> GetEnumerator()
        {
            if (_forcedRead)
            {
                // Return the cached results from the previous call to ForceRead
                foreach (var row in Rows)
                {
                    yield return(row);
                }

                yield break;
            }

            if (_hasReadResults)
            {
                // Don't allow enumeration more than once, unless stream was force read into memory

                throw new StreamAlreadyReadException();
            }

            // Ensure that reading has begun and we're ready to deserialize result rows
            CheckRead();

            if (!_hasFinishedReading)
            {
                // Read isn't complete, so the stream is currently waiting to deserialize the results

                while (_reader.Read())
                {
                    if (_reader.Depth == 2)
                    {
                        yield return(ReadItem <T>(_reader));
                    }
                    if (_reader.Path == "results" && _reader.TokenType == JsonToken.EndArray)
                    {
                        break;
                    }
                }

                // Read any remaining attributes after the results
                ReadResponseAttributes();
            }

            if (QueryTimer != null)
            {
                QueryTimer.ClusterElapsedTime = Metrics.ElaspedTime;
            }
            if (DecodeSpan != null)
            {
                DecodeSpan.SetPeerLatencyTag(Metrics.ElaspedTime);
                DecodeSpan.Finish();
            }

            _hasReadResults = true;
        }
        public override async IAsyncEnumerator <IViewRow <TKey, TValue> > GetAsyncEnumerator(CancellationToken cancellationToken = default)
#pragma warning restore 8425
        {
            if (_hasReadRows)
            {
                // Don't allow enumeration more than once

                throw new StreamAlreadyReadException();
            }
            if (_hasFinishedReading)
            {
                // empty collection
                _hasReadRows = true;
                yield break;
            }
            if (!_hasReadToRows)
            {
                throw new InvalidOperationException(
                          $"{nameof(StreamingViewResult<TKey, TValue>)} has not been initialized, call InitializeAsync first");
            }

            if (_reader == null)
            {
                // Should not be possible
                throw new InvalidOperationException("_reader is null");
            }

            await foreach (var row in _reader.ReadObjectsAsync <ViewRowData>(cancellationToken).ConfigureAwait(false))
            {
                yield return(new ViewRow <TKey, TValue>
                {
                    Id = row.id,
                    Key = row.key,
                    Value = row.value
                });
            }

            // we've reached the end of the stream, so mark it as finished reading
            _hasReadRows = true;

            // Read any remaining attributes after the results
            await ReadResponseAttributes(cancellationToken).ConfigureAwait(false);

            // if we have a decode span, finish it
            DecodeSpan?.Dispose();
        }
 public virtual void Dispose()
 {
     ResponseStream?.Dispose();
     DecodeSpan?.Dispose();
 }