19
19
import com .google .cloud .firestore .pipeline .expressions .Selectable ;
20
20
import com .google .cloud .firestore .pipeline .stages .AddFields ;
21
21
import com .google .cloud .firestore .pipeline .stages .Aggregate ;
22
- import com .google .cloud .firestore .pipeline .stages .Collection ;
23
- import com .google .cloud .firestore .pipeline .stages .CollectionGroup ;
24
- import com .google .cloud .firestore .pipeline .stages .Database ;
25
22
import com .google .cloud .firestore .pipeline .stages .Distinct ;
26
- import com .google .cloud .firestore .pipeline .stages .Documents ;
27
23
import com .google .cloud .firestore .pipeline .stages .FindNearest ;
28
24
import com .google .cloud .firestore .pipeline .stages .GenericStage ;
29
25
import com .google .cloud .firestore .pipeline .stages .Limit ;
33
29
import com .google .cloud .firestore .pipeline .stages .Stage ;
34
30
import com .google .cloud .firestore .pipeline .stages .StageUtils ;
35
31
import com .google .cloud .firestore .pipeline .stages .Where ;
36
- import com .google .common .collect .ImmutableList ;
32
+ import com .google .common .collect .FluentIterable ;
37
33
import com .google .common .collect .ImmutableMap ;
38
- import com .google .common .collect .Lists ;
39
34
import com .google .firestore .v1 .Document ;
40
35
import com .google .firestore .v1 .ExecutePipelineRequest ;
41
36
import com .google .firestore .v1 .ExecutePipelineResponse ;
48
43
import java .util .List ;
49
44
import java .util .logging .Level ;
50
45
import java .util .logging .Logger ;
51
- import java .util .stream .Collectors ;
52
46
53
47
/**
54
48
* The Pipeline class provides a flexible and expressive framework for building complex data
103
97
@ BetaApi
104
98
public final class Pipeline {
105
99
private static Logger logger = Logger .getLogger (Pipeline .class .getName ());
106
- private final ImmutableList <Stage > stages ;
100
+ private final FluentIterable <Stage > stages ;
107
101
private final Firestore db ;
108
102
109
- private Pipeline (Firestore db , List <Stage > stages ) {
103
+ private Pipeline (Firestore db , FluentIterable <Stage > stages ) {
110
104
this .db = db ;
111
- this .stages = ImmutableList . copyOf ( stages ) ;
105
+ this .stages = stages ;
112
106
}
113
107
114
108
@ InternalApi
115
- Pipeline (Firestore db , Collection collection ) {
116
- this (db , Lists . newArrayList ( collection ));
109
+ Pipeline (Firestore db , Stage stage ) {
110
+ this (db , FluentIterable . of ( stage ));
117
111
}
118
112
119
- @ InternalApi
120
- Pipeline (Firestore db , CollectionGroup group ) {
121
- this (db , Lists .newArrayList (group ));
122
- }
123
-
124
- @ InternalApi
125
- Pipeline (Firestore firestore , Database db ) {
126
- this (firestore , Lists .newArrayList (db ));
127
- }
128
-
129
- @ InternalApi
130
- Pipeline (Firestore db , Documents docs ) {
131
- this (db , Lists .newArrayList (docs ));
113
+ private Pipeline append (Stage stage ) {
114
+ return new Pipeline (this .db , stages .append (stage ));
132
115
}
133
116
134
117
/**
@@ -162,12 +145,7 @@ private Pipeline(Firestore db, List<Stage> stages) {
162
145
*/
163
146
@ BetaApi
164
147
public Pipeline addFields (Selectable ... fields ) {
165
- return new Pipeline (
166
- this .db ,
167
- ImmutableList .<Stage >builder ()
168
- .addAll (stages )
169
- .add (new AddFields (PipelineUtils .selectablesToMap (fields )))
170
- .build ());
148
+ return append (new AddFields (PipelineUtils .selectablesToMap (fields )));
171
149
}
172
150
173
151
/**
@@ -201,12 +179,7 @@ public Pipeline addFields(Selectable... fields) {
201
179
*/
202
180
@ BetaApi
203
181
public Pipeline select (Selectable ... selections ) {
204
- return new Pipeline (
205
- this .db ,
206
- ImmutableList .<Stage >builder ()
207
- .addAll (stages )
208
- .add (new Select (PipelineUtils .selectablesToMap (selections )))
209
- .build ());
182
+ return append (new Select (PipelineUtils .selectablesToMap (selections )));
210
183
}
211
184
212
185
/**
@@ -232,12 +205,7 @@ public Pipeline select(Selectable... selections) {
232
205
*/
233
206
@ BetaApi
234
207
public Pipeline select (String ... fields ) {
235
- return new Pipeline (
236
- this .db ,
237
- ImmutableList .<Stage >builder ()
238
- .addAll (stages )
239
- .add (new Select (PipelineUtils .fieldNamesToMap (fields )))
240
- .build ());
208
+ return append (new Select (PipelineUtils .fieldNamesToMap (fields )));
241
209
}
242
210
243
211
/**
@@ -273,8 +241,7 @@ public Pipeline select(String... fields) {
273
241
*/
274
242
@ BetaApi
275
243
public Pipeline where (FilterCondition condition ) {
276
- return new Pipeline (
277
- this .db , ImmutableList .<Stage >builder ().addAll (stages ).add (new Where (condition )).build ());
244
+ return append (new Where (condition ));
278
245
}
279
246
280
247
/**
@@ -299,8 +266,7 @@ public Pipeline where(FilterCondition condition) {
299
266
*/
300
267
@ BetaApi
301
268
public Pipeline offset (int offset ) {
302
- return new Pipeline (
303
- this .db , ImmutableList .<Stage >builder ().addAll (stages ).add (new Offset (offset )).build ());
269
+ return append (new Offset (offset ));
304
270
}
305
271
306
272
/**
@@ -330,8 +296,7 @@ public Pipeline offset(int offset) {
330
296
*/
331
297
@ BetaApi
332
298
public Pipeline limit (int limit ) {
333
- return new Pipeline (
334
- this .db , ImmutableList .<Stage >builder ().addAll (stages ).add (new Limit (limit )).build ());
299
+ return append (new Limit (limit ));
335
300
}
336
301
337
302
/**
@@ -358,12 +323,7 @@ public Pipeline limit(int limit) {
358
323
*/
359
324
@ BetaApi
360
325
public Pipeline aggregate (ExprWithAlias <Accumulator >... accumulators ) {
361
- return new Pipeline (
362
- this .db ,
363
- ImmutableList .<Stage >builder ()
364
- .addAll (stages )
365
- .add (Aggregate .withAccumulators (accumulators ))
366
- .build ());
326
+ return append (Aggregate .withAccumulators (accumulators ));
367
327
}
368
328
369
329
/**
@@ -400,8 +360,7 @@ public Pipeline aggregate(ExprWithAlias<Accumulator>... accumulators) {
400
360
*/
401
361
@ BetaApi
402
362
public Pipeline aggregate (Aggregate aggregate ) {
403
- return new Pipeline (
404
- this .db , ImmutableList .<Stage >builder ().addAll (stages ).add (aggregate ).build ());
363
+ return append (aggregate );
405
364
}
406
365
407
366
/**
@@ -423,12 +382,7 @@ public Pipeline aggregate(Aggregate aggregate) {
423
382
*/
424
383
@ BetaApi
425
384
public Pipeline distinct (String ... fields ) {
426
- return new Pipeline (
427
- this .db ,
428
- ImmutableList .<Stage >builder ()
429
- .addAll (stages )
430
- .add (new Distinct (PipelineUtils .fieldNamesToMap (fields )))
431
- .build ());
385
+ return append (new Distinct (PipelineUtils .fieldNamesToMap (fields )));
432
386
}
433
387
434
388
/**
@@ -460,12 +414,7 @@ public Pipeline distinct(String... fields) {
460
414
*/
461
415
@ BetaApi
462
416
public Pipeline distinct (Selectable ... selectables ) {
463
- return new Pipeline (
464
- this .db ,
465
- ImmutableList .<Stage >builder ()
466
- .addAll (stages )
467
- .add (new Distinct (PipelineUtils .selectablesToMap (selectables )))
468
- .build ());
417
+ return append (new Distinct (PipelineUtils .selectablesToMap (selectables )));
469
418
}
470
419
471
420
/**
@@ -528,14 +477,8 @@ public Pipeline findNearest(
528
477
public Pipeline findNearest (
529
478
Expr property , double [] vector , FindNearest .FindNearestOptions options ) {
530
479
// Implementation for findNearest (add the FindNearest stage if needed)
531
- return new Pipeline (
532
- this .db ,
533
- ImmutableList .<Stage >builder ()
534
- .addAll (stages )
535
- .add (
536
- new FindNearest (
537
- property , vector , options )) // Assuming FindNearest takes these arguments
538
- .build ());
480
+ return append (
481
+ new FindNearest (property , vector , options )); // Assuming FindNearest takes these arguments
539
482
}
540
483
541
484
/**
@@ -571,12 +514,7 @@ public Pipeline findNearest(
571
514
*/
572
515
@ BetaApi
573
516
public Pipeline sort (List <Ordering > orders , Sort .Density density , Sort .Truncation truncation ) {
574
- return new Pipeline (
575
- this .db ,
576
- ImmutableList .<Stage >builder ()
577
- .addAll (stages )
578
- .add (new Sort (orders , density , truncation ))
579
- .build ());
517
+ return append (new Sort (orders , density , truncation ));
580
518
}
581
519
582
520
/**
@@ -633,12 +571,7 @@ public Pipeline sort(Ordering... orders) {
633
571
@ BetaApi
634
572
public Pipeline genericStage (String name , List <Object > params ) {
635
573
// Implementation for genericStage (add the GenericStage if needed)
636
- return new Pipeline (
637
- this .db ,
638
- ImmutableList .<Stage >builder ()
639
- .addAll (stages )
640
- .add (new GenericStage (name , params )) // Assuming GenericStage takes a list of params
641
- .build ());
574
+ return append (new GenericStage (name , params )); // Assuming GenericStage takes a list of params
642
575
}
643
576
644
577
/**
@@ -805,11 +738,7 @@ private Value toProto() {
805
738
return Value .newBuilder ()
806
739
.setPipelineValue (
807
740
com .google .firestore .v1 .Pipeline .newBuilder ()
808
- .addAllStages (
809
- stages .stream ()
810
- .map (StageUtils ::toStageProto )
811
- .collect (Collectors .toList ())) // Use the static method
812
- )
741
+ .addAllStages (stages .transform (StageUtils ::toStageProto )))
813
742
.build ();
814
743
}
815
744
0 commit comments