コード例 #1
0
 public async Task <long> GetCountAsync(string key,
                                        CancellationToken cancellationToken)
 {
     using (_tracer.StartSpan(nameof(GetCountAsync)))
     {
         var callSettings = CallSettings.FromCancellationToken(
             cancellationToken);
         var query = new Query(_options.Value.Kind)
         {
             Filter = Filter.GreaterThan("__key__", _keyFactory.CreateKey(key)),
             Order  = { { "__key__", PropertyOrder.Types.Direction.Ascending } }
         };
         long count       = 0;
         var  lazyResults = _datastore.RunQueryLazilyAsync(query,
                                                           callSettings: callSettings).GetAsyncEnumerator(cancellationToken);
         while (await lazyResults.MoveNextAsync())
         {
             Entity entity = lazyResults.Current;
             if (!entity.Key.Path.First().Name.StartsWith(key))
             {
                 break;
             }
             count += (long)entity[COUNT];
         }
         return(count);
     }
 }
コード例 #2
0
        public async Task LazyGqlQueryAsync()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: RunQueryLazilyAsync(GqlQuery,*,*)
            DatastoreDb db       = DatastoreDb.Create(projectId, namespaceId);
            GqlQuery    gqlQuery = new GqlQuery
            {
                QueryString   = "SELECT * FROM book WHERE author = @author",
                NamedBindings = { { "author", new GqlQueryParameter {
                                        Value = "Jane Austen"
                                    } } },
            };
            AsyncLazyDatastoreQuery results = db.RunQueryLazilyAsync(gqlQuery);
            // AsyncLazyDatastoreQuery implements IAsyncEnumerable<Entity>, but you can
            // call AsResponses() to see the raw RPC responses, or
            // GetAllResultsAsync() to get all the results into memory, complete with
            // the end cursor and the reason for the query finishing.
            await results.ForEachAsync(entity =>
            {
                Console.WriteLine(entity);
            });

            // End snippet

            // This will run the query again, admittedly...
            List <Entity> entities = await results.ToList();

            Assert.Equal(1, entities.Count);
            Entity book = entities[0];

            Assert.Equal("Jane Austen", (string)book["author"]);
            Assert.Equal("Pride and Prejudice", (string)book["title"]);
        }
コード例 #3
0
        public async Task LazyStructuredQueryAsync()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: RunQueryLazilyAsync(Query,*,*)
            DatastoreDb db    = DatastoreDb.Create(projectId, namespaceId);
            Query       query = new Query("book")
            {
                Filter = Filter.Equal("author", "Jane Austen")
            };
            AsyncLazyDatastoreQuery results = db.RunQueryLazilyAsync(query);
            // AsyncLazyDatastoreQuery implements IAsyncEnumerable<Entity>, but you can
            // call AsResponses() to see the raw RPC responses, or
            // GetAllResultsAsync() to get all the results into memory, complete with
            // the end cursor and the reason for the query finishing.
            await results.ForEachAsync(entity =>
            {
                Console.WriteLine(entity);
            });

            // End snippet

            // This will run the query again, admittedly...
            List <Entity> entities = await results.ToListAsync();

            Assert.Equal(1, entities.Count);
            Entity book = entities[0];

            Assert.Equal("Jane Austen", (string)book["author"]);
            Assert.Equal("Pride and Prejudice", (string)book["title"]);
        }