Skip to content

Commit 77decb5

Browse files
committed
Simplify Pipeline
1 parent edc1cc9 commit 77decb5

File tree

1 file changed

+23
-94
lines changed
  • google-cloud-firestore/src/main/java/com/google/cloud/firestore

1 file changed

+23
-94
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Pipeline.java

Lines changed: 23 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import com.google.cloud.firestore.pipeline.expressions.Selectable;
2020
import com.google.cloud.firestore.pipeline.stages.AddFields;
2121
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;
2522
import com.google.cloud.firestore.pipeline.stages.Distinct;
26-
import com.google.cloud.firestore.pipeline.stages.Documents;
2723
import com.google.cloud.firestore.pipeline.stages.FindNearest;
2824
import com.google.cloud.firestore.pipeline.stages.GenericStage;
2925
import com.google.cloud.firestore.pipeline.stages.Limit;
@@ -33,9 +29,8 @@
3329
import com.google.cloud.firestore.pipeline.stages.Stage;
3430
import com.google.cloud.firestore.pipeline.stages.StageUtils;
3531
import com.google.cloud.firestore.pipeline.stages.Where;
36-
import com.google.common.collect.ImmutableList;
32+
import com.google.common.collect.FluentIterable;
3733
import com.google.common.collect.ImmutableMap;
38-
import com.google.common.collect.Lists;
3934
import com.google.firestore.v1.Document;
4035
import com.google.firestore.v1.ExecutePipelineRequest;
4136
import com.google.firestore.v1.ExecutePipelineResponse;
@@ -48,7 +43,6 @@
4843
import java.util.List;
4944
import java.util.logging.Level;
5045
import java.util.logging.Logger;
51-
import java.util.stream.Collectors;
5246

5347
/**
5448
* The Pipeline class provides a flexible and expressive framework for building complex data
@@ -103,32 +97,21 @@
10397
@BetaApi
10498
public final class Pipeline {
10599
private static Logger logger = Logger.getLogger(Pipeline.class.getName());
106-
private final ImmutableList<Stage> stages;
100+
private final FluentIterable<Stage> stages;
107101
private final Firestore db;
108102

109-
private Pipeline(Firestore db, List<Stage> stages) {
103+
private Pipeline(Firestore db, FluentIterable<Stage> stages) {
110104
this.db = db;
111-
this.stages = ImmutableList.copyOf(stages);
105+
this.stages = stages;
112106
}
113107

114108
@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));
117111
}
118112

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));
132115
}
133116

134117
/**
@@ -162,12 +145,7 @@ private Pipeline(Firestore db, List<Stage> stages) {
162145
*/
163146
@BetaApi
164147
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)));
171149
}
172150

173151
/**
@@ -201,12 +179,7 @@ public Pipeline addFields(Selectable... fields) {
201179
*/
202180
@BetaApi
203181
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)));
210183
}
211184

212185
/**
@@ -232,12 +205,7 @@ public Pipeline select(Selectable... selections) {
232205
*/
233206
@BetaApi
234207
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)));
241209
}
242210

243211
/**
@@ -273,8 +241,7 @@ public Pipeline select(String... fields) {
273241
*/
274242
@BetaApi
275243
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));
278245
}
279246

280247
/**
@@ -299,8 +266,7 @@ public Pipeline where(FilterCondition condition) {
299266
*/
300267
@BetaApi
301268
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));
304270
}
305271

306272
/**
@@ -330,8 +296,7 @@ public Pipeline offset(int offset) {
330296
*/
331297
@BetaApi
332298
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));
335300
}
336301

337302
/**
@@ -358,12 +323,7 @@ public Pipeline limit(int limit) {
358323
*/
359324
@BetaApi
360325
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));
367327
}
368328

369329
/**
@@ -400,8 +360,7 @@ public Pipeline aggregate(ExprWithAlias<Accumulator>... accumulators) {
400360
*/
401361
@BetaApi
402362
public Pipeline aggregate(Aggregate aggregate) {
403-
return new Pipeline(
404-
this.db, ImmutableList.<Stage>builder().addAll(stages).add(aggregate).build());
363+
return append(aggregate);
405364
}
406365

407366
/**
@@ -423,12 +382,7 @@ public Pipeline aggregate(Aggregate aggregate) {
423382
*/
424383
@BetaApi
425384
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)));
432386
}
433387

434388
/**
@@ -460,12 +414,7 @@ public Pipeline distinct(String... fields) {
460414
*/
461415
@BetaApi
462416
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)));
469418
}
470419

471420
/**
@@ -528,14 +477,8 @@ public Pipeline findNearest(
528477
public Pipeline findNearest(
529478
Expr property, double[] vector, FindNearest.FindNearestOptions options) {
530479
// 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
539482
}
540483

541484
/**
@@ -571,12 +514,7 @@ public Pipeline findNearest(
571514
*/
572515
@BetaApi
573516
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));
580518
}
581519

582520
/**
@@ -633,12 +571,7 @@ public Pipeline sort(Ordering... orders) {
633571
@BetaApi
634572
public Pipeline genericStage(String name, List<Object> params) {
635573
// 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
642575
}
643576

644577
/**
@@ -805,11 +738,7 @@ private Value toProto() {
805738
return Value.newBuilder()
806739
.setPipelineValue(
807740
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)))
813742
.build();
814743
}
815744

0 commit comments

Comments
 (0)