6868 * .kind(kind)
6969 * .projection(Projection.property("age"), Projection.first("name"))
7070 * .filter(PropertyFilter.gt("age", 18))
71- * .groupBy ("age")
71+ * .distinct ("age")
7272 * .orderBy(OrderBy.asc("age"))
7373 * .limit(10)
7474 * .build();
@@ -86,9 +86,9 @@ public class StructuredQuery<V> extends Query<V> {
8686 private static final String KEY_PROPERTY_NAME = "__key__" ;
8787
8888 private final transient String kind ;
89- private final ImmutableList <Projection > projection ;
89+ private final ImmutableList <String > projection ;
9090 private final transient Filter filter ;
91- private final ImmutableList <String > groupBy ;
91+ private final ImmutableList <String > distinctOn ;
9292 private final transient ImmutableList <OrderBy > orderBy ;
9393 private final transient Cursor startCursor ;
9494 private final transient Cursor endCursor ;
@@ -108,10 +108,8 @@ static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) {
108108 switch (filterPb .getFilterTypeCase ()) {
109109 case COMPOSITE_FILTER :
110110 return CompositeFilter .fromPb (filterPb .getCompositeFilter ());
111- case PROPERTY_FILTER :
112- return PropertyFilter .fromPb (filterPb .getPropertyFilter ());
113111 default :
114- return null ;
112+ return PropertyFilter . fromPb ( filterPb . getPropertyFilter ()) ;
115113 }
116114 }
117115 }
@@ -525,65 +523,15 @@ static OrderBy fromPb(com.google.datastore.v1beta3.PropertyOrder propertyOrderPb
525523 }
526524 }
527525
528- public static final class Projection implements Serializable {
529-
530- private static final long serialVersionUID = 3083707957256279470L ;
531-
532- private final String property ;
533-
534- private Projection (String property ) {
535- this .property = property ;
536- }
537-
538- @ Override
539- public int hashCode () {
540- return Objects .hash (property );
541- }
542-
543- @ Override
544- public boolean equals (Object obj ) {
545- if (obj == this ) {
546- return true ;
547- }
548- if (!(obj instanceof Projection )) {
549- return false ;
550- }
551- return Objects .equals (property , ((Projection ) obj ).property );
552- }
553-
554- @ Override
555- public String toString () {
556- ToStringHelper toStringHelper = MoreObjects .toStringHelper (this );
557- toStringHelper .add ("property" , property );
558- return toStringHelper .toString ();
559- }
560-
561- com .google .datastore .v1beta3 .Projection toPb () {
562- com .google .datastore .v1beta3 .Projection .Builder expressionPb =
563- com .google .datastore .v1beta3 .Projection .newBuilder ();
564- expressionPb .setProperty (
565- com .google .datastore .v1beta3 .PropertyReference .newBuilder ().setName (property ).build ());
566- return expressionPb .build ();
567- }
568-
569- public static Projection fromPb (
570- com .google .datastore .v1beta3 .Projection projectionPb ) {
571- return new Projection (projectionPb .getProperty ().getName ());
572- }
573-
574- public static Projection property (String property ) {
575- return new Projection (property );
576- }
577- }
578-
579526 static class BaseBuilder <V , B extends BaseBuilder <V , B >> {
580527
581528 private final ResultType <V > resultType ;
582529 private String namespace ;
583530 private String kind ;
584- private final List <Projection > projection = new LinkedList <>();
531+ private final List <String > projection = new LinkedList <>();
585532 private Filter filter ;
586- private final List <String > groupBy = new LinkedList <>();
533+ private boolean distinctOnAll = false ;
534+ private final List <String > distinctOn = new LinkedList <>();
587535 private final List <OrderBy > orderBy = new LinkedList <>();
588536 private Cursor startCursor ;
589537 private Cursor endCursor ;
@@ -658,32 +606,39 @@ B clearProjection() {
658606 return self ();
659607 }
660608
661- B projection (Projection projection , Projection ... others ) {
609+ B projection (String projection , String ... others ) {
662610 clearProjection ();
663611 addProjection (projection , others );
664612 return self ();
665613 }
666614
667- B addProjection (Projection projection , Projection ... others ) {
615+ B addProjection (String projection , String ... others ) {
668616 this .projection .add (projection );
669617 Collections .addAll (this .projection , others );
670618 return self ();
671619 }
672620
673- B clearGroupBy () {
674- groupBy .clear ();
621+ B clearDistinct () {
622+ distinctOn .clear ();
623+ distinctOnAll = false ;
675624 return self ();
676625 }
677626
678- B groupBy (String property , String ... others ) {
679- clearGroupBy ();
680- addGroupBy (property , others );
627+ B distinct (String ... properties ) {
628+ clearDistinct ();
629+ if (properties .length == 0 ) {
630+ this .distinctOnAll = true ;
631+ } else if (properties .length == 1 ) {
632+ addDistinct (properties [0 ]);
633+ } else {
634+ addDistinct (properties [0 ], Arrays .copyOfRange (properties , 1 , properties .length ));
635+ }
681636 return self ();
682637 }
683638
684- B addGroupBy (String property , String ... others ) {
685- this .groupBy .add (property );
686- Collections .addAll (this .groupBy , others );
639+ B addDistinct (String property , String ... others ) {
640+ this .distinctOn .add (property );
641+ Collections .addAll (this .distinctOn , others );
687642 return self ();
688643 }
689644
@@ -712,15 +667,20 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) {
712667 }
713668 for (com .google .datastore .v1beta3 .Projection projectionPb
714669 : queryPb .getProjectionList ()) {
715- addProjection (Projection . fromPb ( projectionPb ));
670+ addProjection (projectionPb . getProperty (). getName ( ));
716671 }
717- for (com .google .datastore .v1beta3 .PropertyReference groupByPb : queryPb .getDistinctOnList ()) {
718- addGroupBy ( groupByPb .getName ());
672+ for (com .google .datastore .v1beta3 .PropertyReference distinctOnPb : queryPb .getDistinctOnList ()) {
673+ addDistinct ( distinctOnPb .getName ());
719674 }
675+ distinctOnAll = false ;
720676 return self ();
721677 }
722678
723679 public StructuredQuery <V > build () {
680+ if (distinctOnAll ) {
681+ clearDistinct ();
682+ this .distinctOn .addAll (this .projection );
683+ }
724684 return new StructuredQuery <>(this );
725685 }
726686 }
@@ -748,14 +708,14 @@ public static final class KeyQueryBuilder extends BaseBuilder<Key, KeyQueryBuild
748708
749709 KeyQueryBuilder () {
750710 super (ResultType .KEY );
751- projection (Projection . property ( KEY_PROPERTY_NAME ) );
711+ projection (KEY_PROPERTY_NAME );
752712 }
753713
754714 @ Override
755715 protected KeyQueryBuilder mergeFrom (com .google .datastore .v1beta3 .Query queryPb ) {
756716 super .mergeFrom (queryPb );
757- projection (Projection . property ( KEY_PROPERTY_NAME ) );
758- clearGroupBy ();
717+ projection (KEY_PROPERTY_NAME );
718+ clearDistinct ();
759719 return this ;
760720 }
761721
@@ -783,28 +743,28 @@ public ProjectionEntityQueryBuilder clearProjection() {
783743 }
784744
785745 @ Override
786- public ProjectionEntityQueryBuilder projection (Projection projection , Projection ... others ) {
746+ public ProjectionEntityQueryBuilder projection (String projection , String ... others ) {
787747 return super .projection (projection , others );
788748 }
789749
790750 @ Override
791- public ProjectionEntityQueryBuilder addProjection (Projection projection , Projection ... others ) {
751+ public ProjectionEntityQueryBuilder addProjection (String projection , String ... others ) {
792752 return super .addProjection (projection , others );
793753 }
794754
795755 @ Override
796- public ProjectionEntityQueryBuilder clearGroupBy () {
797- return super .clearGroupBy ();
756+ public ProjectionEntityQueryBuilder clearDistinct () {
757+ return super .clearDistinct ();
798758 }
799759
800760 @ Override
801- public ProjectionEntityQueryBuilder groupBy (String property , String ... others ) {
802- return super .groupBy ( property , others );
761+ public ProjectionEntityQueryBuilder distinct (String ... properties ) {
762+ return super .distinct ( properties );
803763 }
804764
805765 @ Override
806- public ProjectionEntityQueryBuilder addGroupBy (String property , String ... others ) {
807- return super .addGroupBy (property , others );
766+ public ProjectionEntityQueryBuilder addDistinct (String property , String ... others ) {
767+ return super .addDistinct (property , others );
808768 }
809769 }
810770
@@ -813,7 +773,7 @@ public ProjectionEntityQueryBuilder addGroupBy(String property, String... others
813773 kind = builder .kind ;
814774 projection = ImmutableList .copyOf (builder .projection );
815775 filter = builder .filter ;
816- groupBy = ImmutableList .copyOf (builder .groupBy );
776+ distinctOn = ImmutableList .copyOf (builder .distinctOn );
817777 orderBy = ImmutableList .copyOf (builder .orderBy );
818778 startCursor = builder .startCursor ;
819779 endCursor = builder .endCursor ;
@@ -824,7 +784,7 @@ public ProjectionEntityQueryBuilder addGroupBy(String property, String... others
824784 @ Override
825785 public int hashCode () {
826786 return Objects .hash (namespace (), kind , startCursor , endCursor , offset , limit , filter , orderBy ,
827- projection (), groupBy ());
787+ groupBy ());
828788 }
829789
830790 @ Override
@@ -845,7 +805,7 @@ public boolean equals(Object obj) {
845805 && Objects .equals (filter , other .filter )
846806 && Objects .equals (orderBy , other .orderBy )
847807 && Objects .equals (projection , other .projection )
848- && Objects .equals (groupBy , other .groupBy );
808+ && Objects .equals (distinctOn , other .distinctOn );
849809
850810 }
851811
@@ -854,19 +814,19 @@ public String kind() {
854814 }
855815
856816 boolean keyOnly () {
857- return projection .size () == 1 && KEY_PROPERTY_NAME .equals (projection .get (0 ). property );
817+ return projection .size () == 1 && KEY_PROPERTY_NAME .equals (projection .get (0 ));
858818 }
859819
860- public List <Projection > projection () {
820+ public List <String > projection () {
861821 return projection ;
862822 }
863823
864824 public Filter filter () {
865825 return filter ;
866826 }
867827
868- public List <String > groupBy () {
869- return groupBy ;
828+ public List <String > distinct () {
829+ return distinctOn ;
870830 }
871831
872832 public ImmutableList <OrderBy > orderBy () {
@@ -935,12 +895,16 @@ protected com.google.datastore.v1beta3.Query toPb() {
935895 for (OrderBy value : orderBy ) {
936896 queryPb .addOrder (value .toPb ());
937897 }
938- for (String value : groupBy ) {
898+ for (String value : distinctOn ) {
939899 queryPb .addDistinctOn (com .google .datastore .v1beta3 .PropertyReference .newBuilder ()
940900 .setName (value ).build ());
941901 }
942- for (Projection value : projection ) {
943- queryPb .addProjection (value .toPb ());
902+ for (String value : projection ) {
903+ com .google .datastore .v1beta3 .Projection .Builder expressionPb =
904+ com .google .datastore .v1beta3 .Projection .newBuilder ();
905+ expressionPb .setProperty (
906+ com .google .datastore .v1beta3 .PropertyReference .newBuilder ().setName (value ).build ());
907+ queryPb .addProjection (expressionPb .build ());
944908 }
945909 return queryPb .build ();
946910 }
0 commit comments