Ejemplo n.º 1
0
        /// <summary>
        /// Fetches document snapshots from the server, based on an optional transaction ID.
        /// </summary>
        /// <param name="documents">The document references to fetch. Must not be null, or contain null references.</param>
        /// <param name="transactionId">A transaction ID, or null to not include any transaction ID.</param>
        /// <param name="cancellationToken">A cancellation token for the operation.</param>
        /// <returns>The document snapshots, in the order they are provided in the response. (This may not be the order of <paramref name="documents"/>.)</returns>
        internal async Task <IList <DocumentSnapshot> > GetDocumentSnapshotsAsync(IEnumerable <DocumentReference> documents, ByteString transactionId, CancellationToken cancellationToken)
        {
            GaxPreconditions.CheckNotNull(documents, nameof(documents));
            var request = new BatchGetDocumentsRequest {
                Database = RootPath, Documents = { documents.Select(ExtractPath) }
            };

            if (transactionId != null)
            {
                request.Transaction = transactionId;
            }
            var stream = Client.BatchGetDocuments(request, CallSettings.FromCancellationToken(cancellationToken));

            using (var responseStream = stream.ResponseStream)
            {
                List <DocumentSnapshot> snapshots = new List <DocumentSnapshot>();

                // Note: no need to worry about passing the cancellation token in here, as we've passed it into the overall call.
                // If the token is cancelled, the call will be aborted.
                while (await responseStream.MoveNext().ConfigureAwait(false))
                {
                    var response = responseStream.Current;
                    var readTime = Timestamp.FromProto(response.ReadTime);
                    switch (response.ResultCase)
                    {
                    case BatchGetDocumentsResponse.ResultOneofCase.Found:
                        snapshots.Add(DocumentSnapshot.ForDocument(this, response.Found, readTime));
                        break;

                    case BatchGetDocumentsResponse.ResultOneofCase.Missing:
                        snapshots.Add(DocumentSnapshot.ForMissingDocument(this, response.Missing, readTime));
                        break;

                    default:
                        throw new InvalidOperationException($"Unknown response type: {response.ResultCase}");
                    }
                }
                return(snapshots);
            }

            string ExtractPath(DocumentReference documentReference)
            {
                GaxPreconditions.CheckArgument(documentReference != null, nameof(documents), "DocumentReference sequence must not contain null elements.");
                return(documentReference.Path);
            }
        }
Ejemplo n.º 2
0
        internal async Task <QuerySnapshot> SnapshotAsync(ByteString transactionId, CancellationToken cancellationToken)
        {
            var       responses = StreamResponsesAsync(transactionId, cancellationToken);
            Timestamp?readTime  = null;
            List <DocumentSnapshot> snapshots = new List <DocumentSnapshot>();
            await responses.ForEachAsync(response =>
            {
                if (response.Document != null)
                {
                    snapshots.Add(DocumentSnapshot.ForDocument(Database, response.Document, Timestamp.FromProto(response.ReadTime)));
                }
                if (readTime == null && response.ReadTime != null)
                {
                    readTime = Timestamp.FromProto(response.ReadTime);
                }
            }, cancellationToken).ConfigureAwait(false);

            GaxPreconditions.CheckState(readTime != null, "The stream returned from RunQuery did not provide a read timestamp.");

            return(new QuerySnapshot(this, snapshots.AsReadOnly(), readTime.Value));
        }
Ejemplo n.º 3
0
 internal IAsyncEnumerable <DocumentSnapshot> StreamAsync(ByteString transactionId, CancellationToken cancellationToken) =>
 StreamResponsesAsync(transactionId, cancellationToken)
 .Where(resp => resp.Document != null)
 .Select(resp => DocumentSnapshot.ForDocument(Database, resp.Document, Timestamp.FromProto(resp.ReadTime)));
Ejemplo n.º 4
0
 internal static WriteResult FromProto(V1Beta1.WriteResult result, wkt::Timestamp commitTime)
 {
     GaxPreconditions.CheckNotNull(result, nameof(result));
     GaxPreconditions.CheckNotNull(commitTime, nameof(commitTime));
     return(new WriteResult(Timestamp.FromProto(result.UpdateTime ?? commitTime)));
 }