Skip to content

Commit ac4f762

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 1333 b: refs/heads/update-datastore c: 0c2f9e9 h: refs/heads/master i: 1331: 0aafd31 v: v3
1 parent 21feb94 commit ac4f762

4 files changed

Lines changed: 39 additions & 50 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ refs/heads/gh-pages: d1b373c30c176edc08692348167bec3a244bb823
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3
66
refs/heads/pubsub-alpha: dea33f5d017ff85c14573b4a06c6b1f2c4bbe2b0
77
refs/heads/resource-manager: ebf4adc5ee835cd2086c4ac5b4e78d01a5a005a7
8-
refs/heads/update-datastore: acf2c4207f384d9e4d8f7ce958a1d7ac2263ff7a
8+
refs/heads/update-datastore: 0c2f9e93ffb78aadb55c3abf558e47c616336c18

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
* .kind(kind)
6969
* .projection(Projection.property("age"), Projection.first("name"))
7070
* .filter(PropertyFilter.gt("age", 18))
71-
* .distinct("age")
71+
* .distinctOn("age")
7272
* .orderBy(OrderBy.asc("age"))
7373
* .limit(10)
7474
* .build();
@@ -532,7 +532,6 @@ static class BaseBuilder<V, B extends BaseBuilder<V, B>> {
532532
private String kind;
533533
private final List<String> projection = new LinkedList<>();
534534
private Filter filter;
535-
private boolean distinct = false;
536535
private final List<String> distinctOn = new LinkedList<>();
537536
private final List<OrderBy> orderBy = new LinkedList<>();
538537
private Cursor startCursor;
@@ -620,30 +619,18 @@ B addProjection(String projection, String... others) {
620619
return self();
621620
}
622621

623-
B clearDistinct() {
622+
B clearDistinctOn() {
624623
distinctOn.clear();
625-
distinct = false;
626624
return self();
627625
}
628626

629-
B distinct(String... properties) {
630-
clearDistinct();
631-
if (properties.length == 0) {
632-
clearDistinct();
633-
this.distinct = true;
634-
this.distinctOn.addAll(this.projection);
635-
} else if (properties.length == 1) {
636-
addDistinct(properties[0]);
637-
} else {
638-
addDistinct(properties[0], Arrays.copyOfRange(properties, 1, properties.length));
639-
}
627+
B distinctOn(String property, String... others) {
628+
clearDistinctOn();
629+
addDistinctOn(property, others);
640630
return self();
641631
}
642632

643-
B addDistinct(String property, String... others) {
644-
if (this.distinct) {
645-
throw new IllegalStateException("\"distinct()\" is currently set.");
646-
}
633+
B addDistinctOn(String property, String... others) {
647634
this.distinctOn.add(property);
648635
Collections.addAll(this.distinctOn, others);
649636
return self();
@@ -677,7 +664,7 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) {
677664
addProjection(projectionPb.getProperty().getName());
678665
}
679666
for (com.google.datastore.v1beta3.PropertyReference distinctOnPb : queryPb.getDistinctOnList()) {
680-
addDistinct(distinctOnPb.getName());
667+
addDistinctOn(distinctOnPb.getName());
681668
}
682669
return self();
683670
}
@@ -717,7 +704,7 @@ public static final class KeyQueryBuilder extends BaseBuilder<Key, KeyQueryBuild
717704
protected KeyQueryBuilder mergeFrom(com.google.datastore.v1beta3.Query queryPb) {
718705
super.mergeFrom(queryPb);
719706
projection(KEY_PROPERTY_NAME);
720-
clearDistinct();
707+
clearDistinctOn();
721708
return this;
722709
}
723710

@@ -755,18 +742,18 @@ public ProjectionEntityQueryBuilder addProjection(String projection, String... o
755742
}
756743

757744
@Override
758-
public ProjectionEntityQueryBuilder clearDistinct() {
759-
return super.clearDistinct();
745+
public ProjectionEntityQueryBuilder clearDistinctOn() {
746+
return super.clearDistinctOn();
760747
}
761748

762749
@Override
763-
public ProjectionEntityQueryBuilder distinct(String... properties) {
764-
return super.distinct(properties);
750+
public ProjectionEntityQueryBuilder distinctOn(String property, String... others) {
751+
return super.distinctOn(property, others);
765752
}
766753

767754
@Override
768-
public ProjectionEntityQueryBuilder addDistinct(String property, String... others) {
769-
return super.addDistinct(property, others);
755+
public ProjectionEntityQueryBuilder addDistinctOn(String property, String... others) {
756+
return super.addDistinctOn(property, others);
770757
}
771758
}
772759

@@ -786,7 +773,7 @@ public ProjectionEntityQueryBuilder addDistinct(String property, String... other
786773
@Override
787774
public int hashCode() {
788775
return Objects.hash(namespace(), kind, startCursor, endCursor, offset, limit, filter, orderBy,
789-
distinct());
776+
distinctOn());
790777
}
791778

792779
@Override
@@ -827,7 +814,7 @@ public Filter filter() {
827814
return filter;
828815
}
829816

830-
public List<String> distinct() {
817+
public List<String> distinctOn() {
831818
return distinctOn;
832819
}
833820

branches/update-datastore/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,15 @@ public void testRunStructuredQuery() {
428428
assertTrue(projectionEntity.names().isEmpty());
429429
assertFalse(results2.hasNext());
430430

431-
StructuredQuery<ProjectionEntity> projectionQuery = Query.projectionEntityQueryBuilder()
432-
.kind(KIND2)
433-
.projection("age")
434-
.filter(PropertyFilter.gt("age", 18))
435-
.distinct("age")
436-
.orderBy(OrderBy.asc("age"))
437-
.limit(10)
438-
.build();
431+
StructuredQuery<ProjectionEntity> projectionQuery =
432+
Query.projectionEntityQueryBuilder()
433+
.kind(KIND2)
434+
.projection("age")
435+
.filter(PropertyFilter.gt("age", 18))
436+
.distinctOn("age")
437+
.orderBy(OrderBy.asc("age"))
438+
.limit(10)
439+
.build();
439440

440441
QueryResults<ProjectionEntity> results4 = datastore.run(projectionQuery);
441442
assertTrue(results4.hasNext());

branches/update-datastore/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,19 @@ public class SerializationTest {
6767
.kind("k")
6868
.filter(PropertyFilter.eq("p1", "hello"))
6969
.build();
70-
private static final Query<ProjectionEntity> QUERY3 = Query.projectionEntityQueryBuilder()
71-
.kind("k")
72-
.namespace("ns1")
73-
.projection("p")
74-
.limit(100)
75-
.offset(5)
76-
.startCursor(CURSOR1)
77-
.endCursor(CURSOR2)
78-
.filter(CompositeFilter.and(PropertyFilter.gt("p1", 10), PropertyFilter.eq("a", "v")))
79-
.addDistinct("p")
80-
.addOrderBy(OrderBy.asc("p"))
81-
.build();
70+
private static final Query<ProjectionEntity> QUERY3 =
71+
Query.projectionEntityQueryBuilder()
72+
.kind("k")
73+
.namespace("ns1")
74+
.projection("p")
75+
.limit(100)
76+
.offset(5)
77+
.startCursor(CURSOR1)
78+
.endCursor(CURSOR2)
79+
.filter(CompositeFilter.and(PropertyFilter.gt("p1", 10), PropertyFilter.eq("a", "v")))
80+
.addDistinctOn("p")
81+
.addOrderBy(OrderBy.asc("p"))
82+
.build();
8283
private static final KeyValue KEY_VALUE = KeyValue.of(KEY1);
8384
private static final NullValue NULL_VALUE = NullValue.builder().excludeFromIndexes(true).build();
8485
private static final StringValue STRING_VALUE = StringValue.of("hello");

0 commit comments

Comments
 (0)