68
68
* .kind(kind)
69
69
* .projection(Projection.property("age"), Projection.first("name"))
70
70
* .filter(PropertyFilter.gt("age", 18))
71
- * .groupBy ("age")
71
+ * .distinct ("age")
72
72
* .orderBy(OrderBy.asc("age"))
73
73
* .limit(10)
74
74
* .build();
@@ -86,9 +86,9 @@ public class StructuredQuery<V> extends Query<V> {
86
86
private static final String KEY_PROPERTY_NAME = "__key__" ;
87
87
88
88
private final transient String kind ;
89
- private final ImmutableList <Projection > projection ;
89
+ private final ImmutableList <String > projection ;
90
90
private final transient Filter filter ;
91
- private final ImmutableList <String > groupBy ;
91
+ private final ImmutableList <String > distinctOn ;
92
92
private final transient ImmutableList <OrderBy > orderBy ;
93
93
private final transient Cursor startCursor ;
94
94
private final transient Cursor endCursor ;
@@ -108,10 +108,8 @@ static Filter fromPb(com.google.datastore.v1beta3.Filter filterPb) {
108
108
switch (filterPb .getFilterTypeCase ()) {
109
109
case COMPOSITE_FILTER :
110
110
return CompositeFilter .fromPb (filterPb .getCompositeFilter ());
111
- case PROPERTY_FILTER :
112
- return PropertyFilter .fromPb (filterPb .getPropertyFilter ());
113
111
default :
114
- return null ;
112
+ return PropertyFilter . fromPb ( filterPb . getPropertyFilter ()) ;
115
113
}
116
114
}
117
115
}
@@ -525,65 +523,15 @@ static OrderBy fromPb(com.google.datastore.v1beta3.PropertyOrder propertyOrderPb
525
523
}
526
524
}
527
525
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
-
579
526
static class BaseBuilder <V , B extends BaseBuilder <V , B >> {
580
527
581
528
private final ResultType <V > resultType ;
582
529
private String namespace ;
583
530
private String kind ;
584
- private final List <Projection > projection = new LinkedList <>();
531
+ private final List <String > projection = new LinkedList <>();
585
532
private Filter filter ;
586
- private final List <String > groupBy = new LinkedList <>();
533
+ private boolean distinctOnAll = false ;
534
+ private final List <String > distinctOn = new LinkedList <>();
587
535
private final List <OrderBy > orderBy = new LinkedList <>();
588
536
private Cursor startCursor ;
589
537
private Cursor endCursor ;
@@ -658,32 +606,39 @@ B clearProjection() {
658
606
return self ();
659
607
}
660
608
661
- B projection (Projection projection , Projection ... others ) {
609
+ B projection (String projection , String ... others ) {
662
610
clearProjection ();
663
611
addProjection (projection , others );
664
612
return self ();
665
613
}
666
614
667
- B addProjection (Projection projection , Projection ... others ) {
615
+ B addProjection (String projection , String ... others ) {
668
616
this .projection .add (projection );
669
617
Collections .addAll (this .projection , others );
670
618
return self ();
671
619
}
672
620
673
- B clearGroupBy () {
674
- groupBy .clear ();
621
+ B clearDistinct () {
622
+ distinctOn .clear ();
623
+ distinctOnAll = false ;
675
624
return self ();
676
625
}
677
626
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
+ }
681
636
return self ();
682
637
}
683
638
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 );
687
642
return self ();
688
643
}
689
644
@@ -712,15 +667,20 @@ B mergeFrom(com.google.datastore.v1beta3.Query queryPb) {
712
667
}
713
668
for (com .google .datastore .v1beta3 .Projection projectionPb
714
669
: queryPb .getProjectionList ()) {
715
- addProjection (Projection . fromPb ( projectionPb ));
670
+ addProjection (projectionPb . getProperty (). getName ( ));
716
671
}
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 ());
719
674
}
675
+ distinctOnAll = false ;
720
676
return self ();
721
677
}
722
678
723
679
public StructuredQuery <V > build () {
680
+ if (distinctOnAll ) {
681
+ clearDistinct ();
682
+ this .distinctOn .addAll (this .projection );
683
+ }
724
684
return new StructuredQuery <>(this );
725
685
}
726
686
}
@@ -748,14 +708,14 @@ public static final class KeyQueryBuilder extends BaseBuilder<Key, KeyQueryBuild
748
708
749
709
KeyQueryBuilder () {
750
710
super (ResultType .KEY );
751
- projection (Projection . property ( KEY_PROPERTY_NAME ) );
711
+ projection (KEY_PROPERTY_NAME );
752
712
}
753
713
754
714
@ Override
755
715
protected KeyQueryBuilder mergeFrom (com .google .datastore .v1beta3 .Query queryPb ) {
756
716
super .mergeFrom (queryPb );
757
- projection (Projection . property ( KEY_PROPERTY_NAME ) );
758
- clearGroupBy ();
717
+ projection (KEY_PROPERTY_NAME );
718
+ clearDistinct ();
759
719
return this ;
760
720
}
761
721
@@ -783,28 +743,28 @@ public ProjectionEntityQueryBuilder clearProjection() {
783
743
}
784
744
785
745
@ Override
786
- public ProjectionEntityQueryBuilder projection (Projection projection , Projection ... others ) {
746
+ public ProjectionEntityQueryBuilder projection (String projection , String ... others ) {
787
747
return super .projection (projection , others );
788
748
}
789
749
790
750
@ Override
791
- public ProjectionEntityQueryBuilder addProjection (Projection projection , Projection ... others ) {
751
+ public ProjectionEntityQueryBuilder addProjection (String projection , String ... others ) {
792
752
return super .addProjection (projection , others );
793
753
}
794
754
795
755
@ Override
796
- public ProjectionEntityQueryBuilder clearGroupBy () {
797
- return super .clearGroupBy ();
756
+ public ProjectionEntityQueryBuilder clearDistinct () {
757
+ return super .clearDistinct ();
798
758
}
799
759
800
760
@ 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 );
803
763
}
804
764
805
765
@ 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 );
808
768
}
809
769
}
810
770
@@ -813,7 +773,7 @@ public ProjectionEntityQueryBuilder addGroupBy(String property, String... others
813
773
kind = builder .kind ;
814
774
projection = ImmutableList .copyOf (builder .projection );
815
775
filter = builder .filter ;
816
- groupBy = ImmutableList .copyOf (builder .groupBy );
776
+ distinctOn = ImmutableList .copyOf (builder .distinctOn );
817
777
orderBy = ImmutableList .copyOf (builder .orderBy );
818
778
startCursor = builder .startCursor ;
819
779
endCursor = builder .endCursor ;
@@ -824,7 +784,7 @@ public ProjectionEntityQueryBuilder addGroupBy(String property, String... others
824
784
@ Override
825
785
public int hashCode () {
826
786
return Objects .hash (namespace (), kind , startCursor , endCursor , offset , limit , filter , orderBy ,
827
- projection (), groupBy ());
787
+ groupBy ());
828
788
}
829
789
830
790
@ Override
@@ -845,7 +805,7 @@ public boolean equals(Object obj) {
845
805
&& Objects .equals (filter , other .filter )
846
806
&& Objects .equals (orderBy , other .orderBy )
847
807
&& Objects .equals (projection , other .projection )
848
- && Objects .equals (groupBy , other .groupBy );
808
+ && Objects .equals (distinctOn , other .distinctOn );
849
809
850
810
}
851
811
@@ -854,19 +814,19 @@ public String kind() {
854
814
}
855
815
856
816
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 ));
858
818
}
859
819
860
- public List <Projection > projection () {
820
+ public List <String > projection () {
861
821
return projection ;
862
822
}
863
823
864
824
public Filter filter () {
865
825
return filter ;
866
826
}
867
827
868
- public List <String > groupBy () {
869
- return groupBy ;
828
+ public List <String > distinct () {
829
+ return distinctOn ;
870
830
}
871
831
872
832
public ImmutableList <OrderBy > orderBy () {
@@ -935,12 +895,16 @@ protected com.google.datastore.v1beta3.Query toPb() {
935
895
for (OrderBy value : orderBy ) {
936
896
queryPb .addOrder (value .toPb ());
937
897
}
938
- for (String value : groupBy ) {
898
+ for (String value : distinctOn ) {
939
899
queryPb .addDistinctOn (com .google .datastore .v1beta3 .PropertyReference .newBuilder ()
940
900
.setName (value ).build ());
941
901
}
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 ());
944
908
}
945
909
return queryPb .build ();
946
910
}
0 commit comments