Skip to content

Commit acf2c42

Browse files
author
Ajay Kannan
committed
Include cursor workaround, fix distinct in StructuredQuery, and remove extra checks
1 parent 16afb0d commit acf2c42

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/GqlQuery.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.google.gcloud.datastore.Validator.validateNamespace;
2121

2222
import com.google.common.base.MoreObjects;
23-
import com.google.common.base.Preconditions;
2423
import com.google.common.collect.ImmutableList;
2524
import com.google.common.collect.ImmutableMap;
2625
import com.google.common.collect.ImmutableSortedMap;
@@ -138,8 +137,10 @@ static Binding fromPb(com.google.datastore.v1beta3.GqlQueryParameter argPb) {
138137
switch (argPb.getParameterTypeCase()) {
139138
case CURSOR:
140139
return new Binding(new Cursor(argPb.getCursor()));
141-
default:
140+
case VALUE:
142141
return new Binding(Value.fromPb(argPb.getValue()));
142+
default:
143+
throw new AssertionError("Unexpected enum value " + argPb.getParameterTypeCase());
143144
}
144145
}
145146
}
@@ -397,13 +398,11 @@ private static <V> GqlQuery<V> fromPb(
397398
for (Map.Entry<String, com.google.datastore.v1beta3.GqlQueryParameter> nameArg
398399
: queryPb.getNamedBindings().entrySet()) {
399400
Binding currBinding = Binding.fromPb(nameArg.getValue());
400-
Preconditions.checkState(currBinding != null);
401401
builder.namedBindings.put(nameArg.getKey(), currBinding);
402402
}
403403
for (com.google.datastore.v1beta3.GqlQueryParameter numberArg
404404
: queryPb.getPositionalBindingsList()) {
405405
Binding currBinding = Binding.fromPb(numberArg);
406-
Preconditions.checkState(currBinding != null);
407406
builder.positionalBindings.add(currBinding);
408407
}
409408
return builder.build();

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/QueryResultsImpl.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.gcloud.datastore;
1818

19-
import com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType;
2019
import com.google.common.base.Preconditions;
2120
import com.google.common.collect.AbstractIterator;
21+
import com.google.datastore.v1beta3.QueryResultBatch.MoreResultsType;
2222
import com.google.gcloud.datastore.Query.ResultType;
2323
import com.google.protobuf.ByteString;
2424

@@ -55,6 +55,11 @@ class QueryResultsImpl<T> extends AbstractIterator<T> implements QueryResults<T>
5555
}
5656
partitionIdPb = pbBuilder.build();
5757
sendRequest();
58+
if (queryResultBatchPb.getSkippedResults() > 0) {
59+
cursor = queryResultBatchPb.getSkippedCursor();
60+
} else {
61+
cursor = ByteString.EMPTY;
62+
}
5863
}
5964

6065
private void sendRequest() {
@@ -68,13 +73,6 @@ private void sendRequest() {
6873
queryResultBatchPb = datastore.runQuery(requestPb.build()).getBatch();
6974
lastBatch = queryResultBatchPb.getMoreResults() != MoreResultsType.NOT_FINISHED;
7075
entityResultPbIter = queryResultBatchPb.getEntityResultsList().iterator();
71-
if (queryResultBatchPb.getSkippedResults() > 0) {
72-
cursor = queryResultBatchPb.getSkippedCursor();
73-
} else if (entityResultPbIter.hasNext()) {
74-
cursor = queryResultBatchPb.getEntityResults(0).getCursor();
75-
} else {
76-
cursor = queryResultBatchPb.getEndCursor();
77-
}
7876
actualResultType = ResultType.fromPb(queryResultBatchPb.getEntityResultType());
7977
if (Objects.equals(queryResultType, ResultType.PROJECTION_ENTITY)) {
8078
// projection entity can represent all type of results

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/StructuredQuery.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) {
108108
switch (filterPb.getFilterTypeCase()) {
109109
case COMPOSITE_FILTER:
110110
return CompositeFilter.fromPb(filterPb.getCompositeFilter());
111-
default:
111+
case PROPERTY_FILTER:
112112
return PropertyFilter.fromPb(filterPb.getPropertyFilter());
113+
default:
114+
throw new AssertionError("Unexpected enum value " + filterPb.getFilterTypeCase());
113115
}
114116
}
115117
}
@@ -530,7 +532,7 @@ static class BaseBuilder<V, B extends BaseBuilder<V, B>> {
530532
private String kind;
531533
private final List<String> projection = new LinkedList<>();
532534
private Filter filter;
533-
private boolean distinctOnAll = false;
535+
private boolean distinct = false;
534536
private final List<String> distinctOn = new LinkedList<>();
535537
private final List<OrderBy> orderBy = new LinkedList<>();
536538
private Cursor startCursor;
@@ -620,14 +622,16 @@ B addProjection(String projection, String... others) {
620622

621623
B clearDistinct() {
622624
distinctOn.clear();
623-
distinctOnAll = false;
625+
distinct = false;
624626
return self();
625627
}
626628

627629
B distinct(String... properties) {
628630
clearDistinct();
629631
if (properties.length == 0) {
630-
this.distinctOnAll = true;
632+
clearDistinct();
633+
this.distinct = true;
634+
this.distinctOn.addAll(this.projection);
631635
} else if (properties.length == 1) {
632636
addDistinct(properties[0]);
633637
} else {
@@ -637,6 +641,9 @@ B distinct(String... properties) {
637641
}
638642

639643
B addDistinct(String property, String... others) {
644+
if (this.distinct) {
645+
throw new IllegalStateException("\"distinct()\" is currently set.");
646+
}
640647
this.distinctOn.add(property);
641648
Collections.addAll(this.distinctOn, others);
642649
return self();
@@ -672,15 +679,10 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) {
672679
for (com.google.datastore.v1beta3.PropertyReference distinctOnPb : queryPb.getDistinctOnList()) {
673680
addDistinct(distinctOnPb.getName());
674681
}
675-
distinctOnAll = false;
676682
return self();
677683
}
678684

679685
public StructuredQuery<V> build() {
680-
if (distinctOnAll) {
681-
clearDistinct();
682-
this.distinctOn.addAll(this.projection);
683-
}
684686
return new StructuredQuery<>(this);
685687
}
686688
}

0 commit comments

Comments
 (0)