10
10
import com .google .cloud .Timestamp ;
11
11
import com .google .cloud .firestore .pipeline .PaginatingPipeline ;
12
12
import com .google .cloud .firestore .pipeline .expressions .AccumulatorTarget ;
13
- import com .google .cloud .firestore .pipeline .expressions .Expr ;
14
- import com .google .cloud .firestore .pipeline .expressions .ExprWithAlias ;
15
13
import com .google .cloud .firestore .pipeline .expressions .Field ;
16
- import com .google .cloud .firestore .pipeline .expressions .Fields ;
17
14
import com .google .cloud .firestore .pipeline .expressions .FilterCondition ;
18
15
import com .google .cloud .firestore .pipeline .expressions .Ordering ;
19
16
import com .google .cloud .firestore .pipeline .expressions .Selectable ;
44
41
import io .opencensus .trace .Tracing ;
45
42
import java .util .ArrayList ;
46
43
import java .util .Arrays ;
47
- import java .util .HashMap ;
48
44
import java .util .List ;
49
45
import java .util .Map ;
50
46
import java .util .logging .Level ;
86
82
@ BetaApi
87
83
public final class Pipeline {
88
84
private final ImmutableList <Stage > stages ;
85
+ private final Firestore db ;
89
86
90
- private Pipeline (List <Stage > stages ) {
87
+ private Pipeline (Firestore db , List <Stage > stages ) {
88
+ this .db = db ;
91
89
this .stages = ImmutableList .copyOf (stages );
92
90
}
93
91
94
- Pipeline (Collection collection ) {
95
- this (Lists .newArrayList (collection ));
92
+ Pipeline (Firestore db , Collection collection ) {
93
+ this (db , Lists .newArrayList (collection ));
96
94
}
97
95
98
- Pipeline (CollectionGroup group ) {
99
- this (Lists .newArrayList (group ));
96
+ Pipeline (Firestore db , CollectionGroup group ) {
97
+ this (db , Lists .newArrayList (group ));
100
98
}
101
99
102
- Pipeline (Database db ) {
103
- this (Lists .newArrayList (db ));
100
+ Pipeline (Firestore firestore , Database db ) {
101
+ this (firestore , Lists .newArrayList (db ));
104
102
}
105
103
106
- Pipeline (Documents docs ) {
107
- this (Lists .newArrayList (docs ));
108
- }
109
-
110
- private Map <String , Expr > projectablesToMap (Selectable ... selectables ) {
111
- Map <String , Expr > projMap = new HashMap <>();
112
- for (Selectable proj : selectables ) {
113
- if (proj instanceof Field ) {
114
- Field fieldProj = (Field ) proj ;
115
- projMap .put (fieldProj .getPath ().getEncodedPath (), fieldProj );
116
- } else if (proj instanceof AccumulatorTarget ) {
117
- AccumulatorTarget aggregatorProj = (AccumulatorTarget ) proj ;
118
- projMap .put (aggregatorProj .getFieldName (), aggregatorProj .getAccumulator ());
119
- } else if (proj instanceof Fields ) {
120
- Fields fieldsProj = (Fields ) proj ;
121
- if (fieldsProj .getFields () != null ) {
122
- fieldsProj .getFields ().forEach (f -> projMap .put (f .getPath ().getEncodedPath (), f ));
123
- }
124
- } else if (proj instanceof ExprWithAlias ) {
125
- ExprWithAlias exprWithAlias = (ExprWithAlias ) proj ;
126
- projMap .put (exprWithAlias .getAlias (), exprWithAlias .getExpr ());
127
- }
128
- }
129
- return projMap ;
130
- }
131
-
132
- private Map <String , Expr > fieldNamesToMap (String ... fields ) {
133
- Map <String , Expr > projMap = new HashMap <>();
134
- for (String field : fields ) {
135
- projMap .put (field , Field .of (field ));
136
- }
137
- return projMap ;
104
+ Pipeline (Firestore db , Documents docs ) {
105
+ this (db , Lists .newArrayList (docs ));
138
106
}
139
107
140
108
@ BetaApi
141
109
public Pipeline addFields (Selectable ... fields ) {
142
110
return new Pipeline (
111
+ this .db ,
143
112
ImmutableList .<Stage >builder ()
144
113
.addAll (stages )
145
- .add (new AddFields (projectablesToMap (fields )))
114
+ .add (new AddFields (PipelineUtils . selectablesToMap (fields )))
146
115
.build ());
147
116
}
148
117
149
118
@ BetaApi
150
119
public Pipeline select (Selectable ... projections ) {
151
120
return new Pipeline (
121
+ this .db ,
152
122
ImmutableList .<Stage >builder ()
153
123
.addAll (stages )
154
- .add (new Select (projectablesToMap (projections )))
124
+ .add (new Select (PipelineUtils . selectablesToMap (projections )))
155
125
.build ());
156
126
}
157
127
158
128
@ BetaApi
159
129
public Pipeline select (String ... fields ) {
160
130
return new Pipeline (
131
+ this .db ,
161
132
ImmutableList .<Stage >builder ()
162
133
.addAll (stages )
163
- .add (new Select (fieldNamesToMap (fields )))
134
+ .add (new Select (PipelineUtils . fieldNamesToMap (fields )))
164
135
.build ());
165
136
}
166
137
167
138
@ BetaApi
168
139
public Pipeline where (FilterCondition condition ) {
169
140
return new Pipeline (
141
+ this .db ,
170
142
ImmutableList .<Stage >builder ().addAll (stages ).add (new Where (condition )).build ());
171
143
}
172
144
173
145
@ BetaApi
174
146
public Pipeline offset (int offset ) {
175
147
return new Pipeline (
148
+ this .db ,
176
149
ImmutableList .<Stage >builder ().addAll (stages ).add (new Offset (offset )).build ());
177
150
}
178
151
179
152
@ BetaApi
180
153
public Pipeline limit (int limit ) {
181
154
return new Pipeline (
155
+ this .db ,
182
156
ImmutableList .<Stage >builder ().addAll (stages ).add (new Limit (limit )).build ());
183
157
}
184
158
185
159
@ BetaApi
186
160
public Pipeline aggregate (AccumulatorTarget ... aggregators ) {
187
161
return new Pipeline (
188
- ImmutableList .<Stage >builder ().addAll (stages ).add (new Aggregate (aggregators )).build ());
162
+ this .db ,
163
+ ImmutableList .<Stage >builder ().addAll (stages ).add (Aggregate .newInstance ().withAccumulators (aggregators )).build ());
164
+ }
165
+
166
+ @ BetaApi
167
+ public Pipeline aggregate (Aggregate aggregate ) {
168
+ return new Pipeline (
169
+ this .db ,
170
+ ImmutableList .<Stage >builder ().addAll (stages )
171
+ .add (aggregate ).build ());
189
172
}
190
173
191
174
@ BetaApi
@@ -199,6 +182,7 @@ public Pipeline findNearest(
199
182
Field property , double [] vector , FindNearest .FindNearestOptions options ) {
200
183
// Implementation for findNearest (add the FindNearest stage if needed)
201
184
return new Pipeline (
185
+ this .db ,
202
186
ImmutableList .<Stage >builder ()
203
187
.addAll (stages )
204
188
.add (
@@ -210,6 +194,7 @@ public Pipeline findNearest(
210
194
@ BetaApi
211
195
public Pipeline sort (List <Ordering > orders , Sort .Density density , Sort .Truncation truncation ) {
212
196
return new Pipeline (
197
+ this .db ,
213
198
ImmutableList .<Stage >builder ()
214
199
.addAll (stages )
215
200
.add (new Sort (orders , density , truncation ))
@@ -231,6 +216,7 @@ public PaginatingPipeline paginate(int pageSize, Ordering... orders) {
231
216
public Pipeline genericStage (String name , Map <String , Object > params ) {
232
217
// Implementation for genericStage (add the GenericStage if needed)
233
218
return new Pipeline (
219
+ this .db ,
234
220
ImmutableList .<Stage >builder ()
235
221
.addAll (stages )
236
222
.add (
@@ -242,7 +228,7 @@ public Pipeline genericStage(String name, Map<String, Object> params) {
242
228
}
243
229
244
230
@ BetaApi
245
- public ApiFuture <List <PipelineResult >> execute (Firestore db ) {
231
+ public ApiFuture <List <PipelineResult >> execute () {
246
232
if (db instanceof FirestoreImpl ) {
247
233
FirestoreImpl firestoreImpl = (FirestoreImpl ) db ;
248
234
Value pipelineValue = toProto ();
@@ -287,7 +273,7 @@ public void onError(Throwable t) {
287
273
}
288
274
289
275
@ BetaApi
290
- public void execute (Firestore db , ApiStreamObserver <PipelineResult > observer ) {
276
+ public void execute (ApiStreamObserver <PipelineResult > observer ) {
291
277
if (db instanceof FirestoreImpl ) {
292
278
FirestoreImpl firestoreImpl = (FirestoreImpl ) db ;
293
279
Value pipelineValue = toProto ();
0 commit comments