Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ public <T> QueryResults<T> run(Query<T> query, ReadOption... options) {
return run(toReadOptionsPb(options), query);
}

@SuppressWarnings("unchecked")
<T> QueryResults<T> run(com.google.datastore.v1.ReadOptions readOptionsPb, Query<T> query) {
return new QueryResultsImpl<>(this, readOptionsPb, query);
return new QueryResultsImpl<>(
this, readOptionsPb, (RecordQuery<T>) query, query.getNamespace());
}

com.google.datastore.v1.RunQueryResponse runQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.cloud.datastore.Validator.validateNamespace;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.InternalApi;
import com.google.cloud.Timestamp;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -71,7 +72,7 @@
* @param <V> the type of the result values this query will produce
* @see <a href="https://cloud.google.com/datastore/docs/apis/gql/gql_reference">GQL Reference</a>
*/
public final class GqlQuery<V> extends Query<V> {
public final class GqlQuery<V> extends Query<V> implements RecordQuery<V> {

private static final long serialVersionUID = -5514894742849230793L;

Expand All @@ -80,6 +81,8 @@ public final class GqlQuery<V> extends Query<V> {
private final ImmutableMap<String, Binding> namedBindings;
private final ImmutableList<Binding> positionalBindings;

private final ResultType<V> resultType;

static final class Binding implements Serializable {

private static final long serialVersionUID = 2344746877591371548L;
Expand Down Expand Up @@ -423,7 +426,8 @@ private static <V> Binding toBinding(
}

private GqlQuery(Builder<V> builder) {
super(builder.resultType, builder.namespace);
super(builder.namespace);
resultType = checkNotNull(builder.resultType);
queryString = builder.queryString;
allowLiteral = builder.allowLiteral;
namedBindings = ImmutableMap.copyOf(builder.namedBindings);
Expand Down Expand Up @@ -461,9 +465,15 @@ public List<Object> getNumberArgs() {
return builder.build();
}

@Override
public ResultType<V> getType() {
return resultType;
}

@Override
public String toString() {
return super.toStringHelper()
return toStringHelper()
.add("type", getType())
.add("queryString", queryString)
.add("allowLiteral", allowLiteral)
.add("namedBindings", namedBindings)
Expand Down Expand Up @@ -507,13 +517,15 @@ com.google.datastore.v1.GqlQuery toPb() {
return queryPb.build();
}

@InternalApi
@Override
void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb) {
public void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb) {
requestPb.setGqlQuery(toPb());
}

@InternalApi
@Override
Query<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb) {
public RecordQuery<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb) {
return StructuredQuery.<V>fromPb(getType(), getNamespace(), responsePb.getQuery())
.nextQuery(responsePb);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.cloud.datastore;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.Maps;
Expand All @@ -39,7 +37,6 @@ public abstract class Query<V> implements Serializable {

private static final long serialVersionUID = 7967659059395653941L;

private final ResultType<V> resultType;
private final String namespace;

/**
Expand Down Expand Up @@ -156,27 +153,18 @@ static ResultType<?> fromPb(com.google.datastore.v1.EntityResult.ResultType type
}
}

Query(ResultType<V> resultType, String namespace) {
this.resultType = checkNotNull(resultType);
Query(String namespace) {
this.namespace = namespace;
}

ResultType<V> getType() {
return resultType;
}

public String getNamespace() {
return namespace;
}

ToStringHelper toStringHelper() {
return MoreObjects.toStringHelper(this).add("type", resultType).add("namespace", namespace);
return MoreObjects.toStringHelper(this).add("namespace", namespace);
}

abstract void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb);

abstract Query<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb);

/**
* Returns a new {@link GqlQuery} builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class QueryResultsImpl<T> extends AbstractIterator<T> implements QueryResults<T>
private final com.google.datastore.v1.ReadOptions readOptionsPb;
private final com.google.datastore.v1.PartitionId partitionIdPb;
private final ResultType<T> queryResultType;
private Query<T> query;
private RecordQuery<T> query;
private ResultType<?> actualResultType;
private com.google.datastore.v1.RunQueryResponse runQueryResponsePb;
private com.google.datastore.v1.Query mostRecentQueryPb;
Expand All @@ -40,16 +40,19 @@ class QueryResultsImpl<T> extends AbstractIterator<T> implements QueryResults<T>
private MoreResultsType moreResults;

QueryResultsImpl(
DatastoreImpl datastore, com.google.datastore.v1.ReadOptions readOptionsPb, Query<T> query) {
DatastoreImpl datastore,
com.google.datastore.v1.ReadOptions readOptionsPb,
RecordQuery<T> query,
String namespace) {
this.datastore = datastore;
this.readOptionsPb = readOptionsPb;
this.query = query;
queryResultType = query.getType();
com.google.datastore.v1.PartitionId.Builder pbBuilder =
com.google.datastore.v1.PartitionId.newBuilder();
pbBuilder.setProjectId(datastore.getOptions().getProjectId());
if (query.getNamespace() != null) {
pbBuilder.setNamespaceId(query.getNamespace());
if (namespace != null) {
pbBuilder.setNamespaceId(namespace);
} else if (datastore.getOptions().getNamespace() != null) {
pbBuilder.setNamespaceId(datastore.getOptions().getNamespace());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.datastore;

import com.google.api.core.InternalApi;
import com.google.cloud.datastore.Query.ResultType;

/** An internal marker interface to represent {@link Query} that returns the entity records. */
@InternalApi
public interface RecordQuery<V> {

@InternalApi
ResultType<V> getType();

@InternalApi
void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb);

@InternalApi
RecordQuery<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.core.ApiFunction;
import com.google.api.core.InternalApi;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.cloud.Timestamp;
Expand Down Expand Up @@ -85,7 +86,7 @@
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
* queries</a>
*/
public abstract class StructuredQuery<V> extends Query<V> {
public abstract class StructuredQuery<V> extends Query<V> implements RecordQuery<V> {

private static final long serialVersionUID = 546838955624019594L;
static final String KEY_PROPERTY_NAME = "__key__";
Expand All @@ -100,6 +101,8 @@ public abstract class StructuredQuery<V> extends Query<V> {
private final int offset;
private final Integer limit;

private final ResultType<V> resultType;

public abstract static class Filter implements Serializable {

private static final long serialVersionUID = -6443285436239990860L;
Expand Down Expand Up @@ -899,7 +902,8 @@ B mergeFrom(com.google.datastore.v1.Query queryPb) {
}

StructuredQuery(BuilderImpl<V, ?> builder) {
super(builder.resultType, builder.namespace);
super(builder.namespace);
resultType = checkNotNull(builder.resultType);
kind = builder.kind;
projection = ImmutableList.copyOf(builder.projection);
filter = builder.filter;
Expand All @@ -914,6 +918,7 @@ B mergeFrom(com.google.datastore.v1.Query queryPb) {
@Override
public String toString() {
return toStringHelper()
.add("type", getType())
.add("kind", kind)
.add("startCursor", startCursor)
.add("endCursor", endCursor)
Expand Down Expand Up @@ -1013,13 +1018,19 @@ public Integer getLimit() {

public abstract Builder<V> toBuilder();

public ResultType<V> getType() {
return resultType;
}

@InternalApi
@Override
void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb) {
public void populatePb(com.google.datastore.v1.RunQueryRequest.Builder requestPb) {
requestPb.setQuery(toPb());
}

@InternalApi
@Override
StructuredQuery<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb) {
public StructuredQuery<V> nextQuery(com.google.datastore.v1.RunQueryResponse responsePb) {
Builder<V> builder = toBuilder();
builder.setStartCursor(new Cursor(responsePb.getBatch().getEndCursor()));
if (offset > 0 && responsePb.getBatch().getSkippedResults() < offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ private List<RunQueryResponse> buildResponsesForQueryPagination() {
Entity entity5 = Entity.newBuilder(KEY5).set("value", "value").build();
datastore.add(ENTITY3, entity4, entity5);
List<RunQueryResponse> responses = new ArrayList<>();
Query<Key> query = Query.newKeyQueryBuilder().build();
RecordQuery<Key> query = Query.newKeyQueryBuilder().build();
RunQueryRequest.Builder requestPb = RunQueryRequest.newBuilder();
query.populatePb(requestPb);
QueryResultBatch queryResultBatchPb =
Expand Down Expand Up @@ -722,7 +722,7 @@ private List<RunQueryResponse> buildResponsesForQueryPaginationWithLimit() {
datastore.add(ENTITY3, entity4, entity5);
DatastoreRpc datastoreRpc = datastore.getOptions().getDatastoreRpcV1();
List<RunQueryResponse> responses = new ArrayList<>();
Query<Entity> query = Query.newEntityQueryBuilder().build();
RecordQuery<Entity> query = Query.newEntityQueryBuilder().build();
RunQueryRequest.Builder requestPb = RunQueryRequest.newBuilder();
query.populatePb(requestPb);
QueryResultBatch queryResultBatchPb =
Expand Down