Skip to content

Commit a1a6076

Browse files
committed
Fixes
1 parent 593d39e commit a1a6076

File tree

11 files changed

+61
-83
lines changed

11 files changed

+61
-83
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,37 +138,35 @@ public Pipeline select(String... fields) {
138138
@BetaApi
139139
public Pipeline where(FilterCondition condition) {
140140
return new Pipeline(
141-
this.db,
142-
ImmutableList.<Stage>builder().addAll(stages).add(new Where(condition)).build());
141+
this.db, ImmutableList.<Stage>builder().addAll(stages).add(new Where(condition)).build());
143142
}
144143

145144
@BetaApi
146145
public Pipeline offset(int offset) {
147146
return new Pipeline(
148-
this.db,
149-
ImmutableList.<Stage>builder().addAll(stages).add(new Offset(offset)).build());
147+
this.db, ImmutableList.<Stage>builder().addAll(stages).add(new Offset(offset)).build());
150148
}
151149

152150
@BetaApi
153151
public Pipeline limit(int limit) {
154152
return new Pipeline(
155-
this.db,
156-
ImmutableList.<Stage>builder().addAll(stages).add(new Limit(limit)).build());
153+
this.db, ImmutableList.<Stage>builder().addAll(stages).add(new Limit(limit)).build());
157154
}
158155

159156
@BetaApi
160157
public Pipeline aggregate(AccumulatorTarget... aggregators) {
161158
return new Pipeline(
162159
this.db,
163-
ImmutableList.<Stage>builder().addAll(stages).add(Aggregate.newInstance().withAccumulators(aggregators)).build());
160+
ImmutableList.<Stage>builder()
161+
.addAll(stages)
162+
.add(Aggregate.newInstance().withAccumulators(aggregators))
163+
.build());
164164
}
165165

166166
@BetaApi
167167
public Pipeline aggregate(Aggregate aggregate) {
168168
return new Pipeline(
169-
this.db,
170-
ImmutableList.<Stage>builder().addAll(stages)
171-
.add(aggregate).build());
169+
this.db, ImmutableList.<Stage>builder().addAll(stages).add(aggregate).build());
172170
}
173171

174172
@BetaApi

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public class PipelineSource {
1414
private final Firestore db;
1515

1616
@InternalApi
17-
PipelineSource(Firestore db){
17+
PipelineSource(Firestore db) {
1818
this.db = db;
1919
}
2020

2121
@Nonnull
2222
@BetaApi
2323
public Pipeline collection(@Nonnull String path) {
24-
return new Pipeline(this.db,new Collection(path));
24+
return new Pipeline(this.db, new Collection(path));
2525
}
2626

2727
@Nonnull

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,8 @@ public Pipeline pipeline() {
21282128
// From
21292129
Pipeline ppl =
21302130
this.options.getAllDescendants()
2131-
? new PipelineSource(this.getFirestore()).collectionGroup(this.options.getCollectionId())
2131+
? new PipelineSource(this.getFirestore())
2132+
.collectionGroup(this.options.getCollectionId())
21322133
: new PipelineSource(this.getFirestore())
21332134
.collection(
21342135
this.options.getParentPath().append(this.options.getCollectionId()).getPath());

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/ArrayConcat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
public final class ArrayConcat extends Function {
1010
@InternalApi
1111
ArrayConcat(Expr array, List<Expr> rest) {
12-
super("array_concat", Lists.newArrayList(array, new ListOfExprs(rest)));
12+
super("array_concat", Lists.asList(array, rest.toArray(new Expr[] {})));
1313
}
1414
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expr.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.google.cloud.firestore.pipeline.expressions;
22

3+
import static com.google.cloud.firestore.pipeline.expressions.FunctionUtils.toExprList;
4+
35
import com.google.api.core.BetaApi;
46
import java.util.Arrays;
57
import java.util.List;
6-
import java.util.stream.Collectors;
78

89
@BetaApi
910
public interface Expr {
@@ -109,11 +110,7 @@ default Lte lte(Object other) {
109110

110111
@BetaApi
111112
default In inAny(Object... other) {
112-
List<Expr> othersAsExpr =
113-
Arrays.stream(other)
114-
.map(obj -> (obj instanceof Expr) ? (Expr) obj : Constant.of(obj))
115-
.collect(Collectors.toList());
116-
return new In(this, othersAsExpr);
113+
return new In(this, toExprList(other));
117114
}
118115

119116
@BetaApi
@@ -128,8 +125,7 @@ default ArrayConcat arrayConcat(Expr... elements) {
128125

129126
@BetaApi
130127
default ArrayConcat arrayConcat(Object... elements) {
131-
return new ArrayConcat(
132-
this, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
128+
return new ArrayConcat(this, toExprList(elements));
133129
}
134130

135131
@BetaApi
@@ -149,8 +145,7 @@ default ArrayContainsAll arrayContainsAll(Expr... elements) {
149145

150146
@BetaApi
151147
default ArrayContainsAll arrayContainsAll(Object... elements) {
152-
return new ArrayContainsAll(
153-
this, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
148+
return new ArrayContainsAll(this, toExprList(elements));
154149
}
155150

156151
@BetaApi
@@ -160,8 +155,7 @@ default ArrayContainsAny arrayContainsAny(Expr... elements) {
160155

161156
@BetaApi
162157
default ArrayContainsAny arrayContainsAny(Object... elements) {
163-
return new ArrayContainsAny(
164-
this, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
158+
return new ArrayContainsAny(this, toExprList(elements));
165159
}
166160

167161
@BetaApi

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Function.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.google.cloud.firestore.pipeline.expressions;
22

3+
import static com.google.cloud.firestore.pipeline.expressions.FunctionUtils.toExprList;
4+
35
import com.google.api.core.BetaApi;
46
import com.google.api.core.InternalApi;
57
import com.google.cloud.firestore.DocumentReference;
@@ -340,8 +342,7 @@ public static ArrayConcat arrayConcat(Expr expr, Expr... elements) {
340342

341343
@BetaApi
342344
public static ArrayConcat arrayConcat(Expr expr, Object... elements) {
343-
return new ArrayConcat(
344-
expr, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
345+
return new ArrayConcat(expr, toExprList(elements));
345346
}
346347

347348
@BetaApi
@@ -351,8 +352,7 @@ public static ArrayConcat arrayConcat(String field, Expr... elements) {
351352

352353
@BetaApi
353354
public static ArrayConcat arrayConcat(String field, Object... elements) {
354-
return new ArrayConcat(
355-
Field.of(field), Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
355+
return new ArrayConcat(Field.of(field), toExprList(elements));
356356
}
357357

358358
@BetaApi
@@ -382,8 +382,7 @@ public static ArrayContainsAll arrayContainsAll(Expr expr, Expr... elements) {
382382

383383
@BetaApi
384384
public static ArrayContainsAll arrayContainsAll(Expr expr, Object... elements) {
385-
return new ArrayContainsAll(
386-
expr, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
385+
return new ArrayContainsAll(expr, toExprList(elements));
387386
}
388387

389388
@BetaApi
@@ -393,8 +392,7 @@ public static ArrayContainsAll arrayContainsAll(String field, Expr... elements)
393392

394393
@BetaApi
395394
public static ArrayContainsAll arrayContainsAll(String field, Object... elements) {
396-
return new ArrayContainsAll(
397-
Field.of(field), Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
395+
return new ArrayContainsAll(Field.of(field), toExprList(elements));
398396
}
399397

400398
@BetaApi
@@ -404,8 +402,7 @@ public static ArrayContainsAny arrayContainsAny(Expr expr, Expr... elements) {
404402

405403
@BetaApi
406404
public static ArrayContainsAny arrayContainsAny(Expr expr, Object... elements) {
407-
return new ArrayContainsAny(
408-
expr, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
405+
return new ArrayContainsAny(expr, toExprList(elements));
409406
}
410407

411408
@BetaApi
@@ -415,8 +412,7 @@ public static ArrayContainsAny arrayContainsAny(String field, Expr... elements)
415412

416413
@BetaApi
417414
public static ArrayContainsAny arrayContainsAny(String field, Object... elements) {
418-
return new ArrayContainsAny(
419-
Field.of(field), Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
415+
return new ArrayContainsAny(Field.of(field), toExprList(elements));
420416
}
421417

422418
@BetaApi
@@ -496,8 +492,7 @@ public static StrConcat strConcat(Expr expr, Expr... elements) {
496492

497493
@BetaApi
498494
public static StrConcat strConcat(Expr expr, Object... elements) {
499-
return new StrConcat(
500-
expr, Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
495+
return new StrConcat(expr, toExprList(elements));
501496
}
502497

503498
@BetaApi
@@ -507,8 +502,7 @@ public static StrConcat strConcat(String field, Expr... elements) {
507502

508503
@BetaApi
509504
public static StrConcat strConcat(String field, Object... elements) {
510-
return new StrConcat(
511-
Field.of(field), Arrays.stream(elements).map(Constant::of).collect(Collectors.toList()));
505+
return new StrConcat(Field.of(field), toExprList(elements));
512506
}
513507

514508
@BetaApi

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/FunctionUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.api.core.InternalApi;
44
import com.google.firestore.v1.ArrayValue;
55
import com.google.firestore.v1.Value;
6+
import java.util.Arrays;
7+
import java.util.List;
68
import java.util.stream.Collectors;
79

810
@InternalApi
@@ -29,4 +31,11 @@ public static Value exprToValue(Expr expr) {
2931
throw new IllegalArgumentException("Unsupported expression type: " + expr.getClass());
3032
}
3133
}
34+
35+
@InternalApi
36+
static List<Expr> toExprList(Object[] other) {
37+
return Arrays.stream(other)
38+
.map(obj -> (obj instanceof Expr) ? (Expr) obj : Constant.of(obj))
39+
.collect(Collectors.toList());
40+
}
3241
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/StrConcat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
public final class StrConcat extends Function {
1010
@InternalApi
1111
StrConcat(Expr first, List<Expr> exprs) {
12-
super("str_concat", Lists.newArrayList(first, new ListOfExprs(exprs)));
12+
super("str_concat", Lists.asList(first, exprs.toArray(new Expr[] {})));
1313
}
1414
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/stages/Aggregate.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,28 @@ public final class Aggregate implements Stage {
2020
private final Map<String, Accumulator> accumulators;
2121

2222
@BetaApi
23-
public static Aggregate newInstance(){
23+
public static Aggregate newInstance() {
2424
return new Aggregate(Collections.emptyMap(), Collections.emptyMap());
2525
}
2626

2727
@BetaApi
28-
public Aggregate withGroups(String... fields){
29-
return new Aggregate(
30-
PipelineUtils.fieldNamesToMap(fields),
31-
this.accumulators);
28+
public Aggregate withGroups(String... fields) {
29+
return new Aggregate(PipelineUtils.fieldNamesToMap(fields), this.accumulators);
3230
}
3331

3432
@BetaApi
35-
public Aggregate withGroups(Selectable... selectables){
36-
return new Aggregate(
37-
PipelineUtils.selectablesToMap(selectables), this.accumulators);
33+
public Aggregate withGroups(Selectable... selectables) {
34+
return new Aggregate(PipelineUtils.selectablesToMap(selectables), this.accumulators);
3835
}
3936

4037
@BetaApi
41-
public Aggregate withAccumulators(AccumulatorTarget... aggregators){
42-
return new Aggregate(this.groups, Arrays.stream(aggregators)
43-
.collect(
44-
Collectors.toMap(
45-
AccumulatorTarget::getFieldName, AccumulatorTarget::getAccumulator)));
38+
public Aggregate withAccumulators(AccumulatorTarget... aggregators) {
39+
return new Aggregate(
40+
this.groups,
41+
Arrays.stream(aggregators)
42+
.collect(
43+
Collectors.toMap(
44+
AccumulatorTarget::getFieldName, AccumulatorTarget::getAccumulator)));
4645
}
4746

4847
private Aggregate(Map<String, Expr> groups, Map<String, Accumulator> accumulators) {

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,11 @@ public void testGroupBys() throws Exception {
281281
collection
282282
.pipeline()
283283
.where(lt("published", 1900))
284-
.aggregate(
285-
Aggregate
286-
.newInstance()
287-
.withGroups("genre"))
284+
.aggregate(Aggregate.newInstance().withGroups("genre"))
288285
.execute()
289286
.get();
290287
assertThat(data(results))
291-
.containsExactly(
292-
map("genre", "Romance"),
293-
map("genre", "Psychological Thriller")
294-
);
288+
.containsExactly(map("genre", "Romance"), map("genre", "Psychological Thriller"));
295289
}
296290

297291
@Test
@@ -301,20 +295,17 @@ public void testGroupBysAndAggregate() throws Exception {
301295
.pipeline()
302296
.where(lt("published", 1984))
303297
.aggregate(
304-
Aggregate
305-
.newInstance()
298+
Aggregate.newInstance()
306299
.withGroups("genre")
307-
.withAccumulators(avg("rating").as("avg_rating"))
308-
)
300+
.withAccumulators(avg("rating").as("avg_rating")))
309301
.where(gt("avg_rating", 4.3))
310302
.execute()
311303
.get();
312304
assertThat(data(results))
313305
.containsExactly(
314306
map("avg_rating", 4.7, "genre", "Fantasy"),
315307
map("avg_rating", 4.5, "genre", "Romance"),
316-
map("avg_rating", 4.4, "genre", "Science Fiction")
317-
);
308+
map("avg_rating", 4.4, "genre", "Science Fiction"));
318309
}
319310

320311
@Test
@@ -444,11 +435,7 @@ public void testArrayContainsAny() throws Exception {
444435
@Test
445436
public void testArrayContainsAll() throws Exception {
446437
List<PipelineResult> results =
447-
collection
448-
.pipeline()
449-
.where(arrayContainsAll("tags", "adventure", "magic"))
450-
.execute()
451-
.get();
438+
collection.pipeline().where(arrayContainsAll("tags", "adventure", "magic")).execute().get();
452439

453440
assertThat(data(results)).isEqualTo(Lists.newArrayList(map("title", "The Lord of the Rings")));
454441
}
@@ -491,8 +478,7 @@ public void testArrayFilter() throws Exception {
491478
collection
492479
.pipeline()
493480
.select(
494-
arrayFilter(Field.of("tags"), Function.eq(arrayElement(), ""))
495-
.as("filteredTags"))
481+
arrayFilter(Field.of("tags"), Function.eq(arrayElement(), "")).as("filteredTags"))
496482
.limit(1)
497483
.execute()
498484
.get();
@@ -589,8 +575,7 @@ public void testTrim() throws Exception {
589575
collection
590576
.pipeline()
591577
.addFields(
592-
strConcat(Constant.of(" "), Field.of("title"), Constant.of(" "))
593-
.as("spacedTitle"))
578+
strConcat(Constant.of(" "), Field.of("title"), Constant.of(" ")).as("spacedTitle"))
594579
.select(Field.of("spacedTitle").trim().as("trimmedTitle"), Field.of("spacedTitle"))
595580
.limit(1)
596581
.execute()
@@ -787,8 +772,7 @@ public void testDistanceFunctions() throws Exception {
787772
collection
788773
.pipeline()
789774
.select(
790-
cosineDistance(Constant.ofVector(sourceVector), targetVector)
791-
.as("cosineDistance"),
775+
cosineDistance(Constant.ofVector(sourceVector), targetVector).as("cosineDistance"),
792776
dotProductDistance(Constant.ofVector(sourceVector), targetVector)
793777
.as("dotProductDistance"),
794778
euclideanDistance(Constant.ofVector(sourceVector), targetVector)

0 commit comments

Comments
 (0)