private void sendRequest() { com.google.datastore.v1.RunQueryRequest.Builder requestPb = com.google.datastore.v1.RunQueryRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); runQueryResponsePb = datastore.runQuery(requestPb.build()); mostRecentQueryPb = runQueryResponsePb.getQuery(); if (mostRecentQueryPb == null) { mostRecentQueryPb = requestPb.getQuery(); } lastBatch = runQueryResponsePb.getBatch().getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = runQueryResponsePb.getBatch().getEntityResultsList().iterator(); actualResultType = ResultType.fromPb(runQueryResponsePb.getBatch().getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results actualResultType = ResultType.PROJECTION_ENTITY; } Preconditions.checkState( queryResultType.isAssignableFrom(actualResultType), "Unexpected result type " + actualResultType + " vs " + queryResultType); }
@Test public void testEventualConsistencyQuery() { ReadOptions readOption = ReadOptions.newBuilder().setReadConsistencyValue(ReadConsistency.EVENTUAL_VALUE).build(); com.google.datastore.v1.GqlQuery query = com.google.datastore.v1.GqlQuery.newBuilder().setQueryString("FROM * SELECT *").build(); RunQueryRequest.Builder expectedRequest = RunQueryRequest.newBuilder() .setReadOptions(readOption) .setGqlQuery(query) .setPartitionId(PartitionId.newBuilder().setProjectId(PROJECT_ID).build()); EasyMock.expect(rpcMock.runQuery(expectedRequest.build())) .andReturn(RunQueryResponse.newBuilder().build()); EasyMock.replay(rpcFactoryMock, rpcMock); Datastore datastore = rpcMockOptions.getService(); datastore.run( Query.newGqlQueryBuilder("FROM * SELECT *").build(), ReadOption.eventualConsistency()); EasyMock.verify(rpcFactoryMock, rpcMock); }
private void sendRequest() { com.google.datastore.v1.RunQueryRequest.Builder requestPb = com.google.datastore.v1.RunQueryRequest.newBuilder(); if (readOptionsPb != null) { requestPb.setReadOptions(readOptionsPb); } requestPb.setPartitionId(partitionIdPb); query.populatePb(requestPb); runQueryResponsePb = datastore.runQuery(requestPb.build()); mostRecentQueryPb = runQueryResponsePb.getQuery(); if (mostRecentQueryPb == null) { mostRecentQueryPb = requestPb.getQuery(); } lastBatch = runQueryResponsePb.getBatch().getMoreResults() != MoreResultsType.NOT_FINISHED; entityResultPbIter = runQueryResponsePb.getBatch().getEntityResultsList().iterator(); actualResultType = ResultType.fromPb(runQueryResponsePb.getBatch().getEntityResultType()); if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) { // projection entity can represent all type of results actualResultType = ResultType.PROJECTION_ENTITY; } Preconditions.checkState( queryResultType.isAssignableFrom(actualResultType), "Unexpected result type " + actualResultType + " vs " + queryResultType); }
@Override public ListenableFuture<QueryResult> executeAsync(final Query statement, final ListenableFuture<TransactionResult> txn) { final ListenableFuture<Response> httpResponse = Futures.transformAsync(txn, result -> { final String namespace = config.getNamespace(); final RunQueryRequest.Builder request = RunQueryRequest.newBuilder() .setQuery(statement.getPb(namespace != null ? namespace : "")); if (namespace != null) { request.setPartitionId(PartitionId.newBuilder().setNamespaceId(namespace)); } final ByteString transaction = result.getTransaction(); if (transaction != null) { request.setReadOptions(ReadOptions.newBuilder().setTransaction(transaction)); } final ProtoHttpContent payload = new ProtoHttpContent(request.build()); return ListenableFutureAdapter.asGuavaFuture(prepareRequest("runQuery", payload).execute()); }, MoreExecutors.directExecutor()); return Futures.transformAsync(httpResponse, response -> { if (!isSuccessful(response.getStatusCode())) { throw new DatastoreException(response.getStatusCode(), response.getResponseBody()); } final RunQueryResponse query = RunQueryResponse.parseFrom(streamResponse(response)); return Futures.immediateFuture(QueryResult.build(query)); }, MoreExecutors.directExecutor()); } }