Skip to content

Commit 64622b1

Browse files
committed
startsWith and endsWith
1 parent a1a6076 commit 64622b1

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
*/
8282
@BetaApi
8383
public final class Pipeline {
84+
private static Logger logger = Logger.getLogger(Pipeline.class.getName());
8485
private final ImmutableList<Stage> stages;
8586
private final Firestore db;
8687

@@ -388,7 +389,7 @@ public void onComplete() {
388389
}
389390
};
390391

391-
Logger.getLogger("Pipeline").log(Level.WARNING, "Sending request: " + request);
392+
logger.log(Level.INFO, "Sending pipeline request: " + request.getStructuredPipeline());
392393

393394
rpcContext.streamRequest(request, observer, rpcContext.getClient().executePipelineCallable());
394395
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.google.cloud.firestore.pipeline.expressions;
2+
3+
import com.google.api.core.BetaApi;
4+
import com.google.api.core.InternalApi;
5+
import com.google.common.collect.Lists;
6+
7+
@BetaApi
8+
public final class EndsWith extends Function implements FilterCondition {
9+
@InternalApi
10+
EndsWith(Expr expr, Expr postfix) {
11+
super("ends_with", Lists.newArrayList(expr, postfix));
12+
}
13+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,26 @@ default RegexMatch regexMatches(String regex) {
228228
return new RegexMatch(this, Constant.of(regex));
229229
}
230230

231+
@BetaApi
232+
default StartsWith startsWith(String prefix) {
233+
return new StartsWith(this, Constant.of(prefix));
234+
}
235+
236+
@BetaApi
237+
default StartsWith startsWith(Expr prefix) {
238+
return new StartsWith(this, prefix);
239+
}
240+
241+
@BetaApi
242+
default EndsWith endsWith(String postfix) {
243+
return new EndsWith(this, Constant.of(postfix));
244+
}
245+
246+
@BetaApi
247+
default EndsWith endsWith(Expr postfix) {
248+
return new EndsWith(this, postfix);
249+
}
250+
231251
@BetaApi
232252
default StrConcat strConcat(List<Expr> elements) {
233253
return new StrConcat(this, elements);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,36 @@ public static RegexMatch regexMatch(String field, String pattern) {
485485
return new RegexMatch(Field.of(field), Constant.of(pattern));
486486
}
487487

488+
@BetaApi
489+
public static StartsWith startsWith(String field, String prefix) {
490+
return new StartsWith(Field.of(field), Constant.of(prefix));
491+
}
492+
493+
@BetaApi
494+
public static StartsWith startsWith(String field, Expr prefix) {
495+
return new StartsWith(Field.of(field), prefix);
496+
}
497+
498+
@BetaApi
499+
public static StartsWith startsWith(Expr expr, Expr prefix) {
500+
return new StartsWith(expr, prefix);
501+
}
502+
503+
@BetaApi
504+
public static EndsWith endsWith(String field, String prefix) {
505+
return new EndsWith(Field.of(field), Constant.of(prefix));
506+
}
507+
508+
@BetaApi
509+
public static EndsWith endsWith(String field, Expr prefix) {
510+
return new EndsWith(Field.of(field), prefix);
511+
}
512+
513+
@BetaApi
514+
public static EndsWith endsWith(Expr expr, Expr prefix) {
515+
return new EndsWith(expr, prefix);
516+
}
517+
488518
@BetaApi
489519
public static StrConcat strConcat(Expr expr, Expr... elements) {
490520
return new StrConcat(expr, Arrays.asList(elements));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.google.cloud.firestore.pipeline.expressions;
2+
3+
import com.google.api.core.BetaApi;
4+
import com.google.api.core.InternalApi;
5+
import com.google.common.collect.Lists;
6+
7+
@BetaApi
8+
public final class StartsWith extends Function implements FilterCondition {
9+
@InternalApi
10+
StartsWith(Expr expr, Expr prefix) {
11+
super("starts_with", Lists.newArrayList(expr, prefix));
12+
}
13+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.google.cloud.firestore.pipeline.expressions.Function.cosineDistance;
3131
import static com.google.cloud.firestore.pipeline.expressions.Function.countAll;
3232
import static com.google.cloud.firestore.pipeline.expressions.Function.dotProductDistance;
33+
import static com.google.cloud.firestore.pipeline.expressions.Function.endsWith;
3334
import static com.google.cloud.firestore.pipeline.expressions.Function.eq;
3435
import static com.google.cloud.firestore.pipeline.expressions.Function.euclideanDistance;
3536
import static com.google.cloud.firestore.pipeline.expressions.Function.gt;
@@ -39,6 +40,7 @@
3940
import static com.google.cloud.firestore.pipeline.expressions.Function.not;
4041
import static com.google.cloud.firestore.pipeline.expressions.Function.or;
4142
import static com.google.cloud.firestore.pipeline.expressions.Function.parent;
43+
import static com.google.cloud.firestore.pipeline.expressions.Function.startsWith;
4244
import static com.google.cloud.firestore.pipeline.expressions.Function.strConcat;
4345
import static com.google.cloud.firestore.pipeline.expressions.Function.subtract;
4446
import static com.google.common.truth.Truth.assertThat;
@@ -526,6 +528,43 @@ public void testStrConcat() throws Exception {
526528
map("bookInfo", "Douglas Adams - The Hitchhiker's Guide to the Galaxy")));
527529
}
528530

531+
@Test
532+
public void testStartsWith() throws Exception {
533+
List<PipelineResult> results =
534+
collection.pipeline()
535+
.where(startsWith("title", "The"))
536+
.select("title")
537+
.sort(Field.of("title").ascending())
538+
.execute().get();
539+
540+
assertThat(data(results))
541+
.isEqualTo(
542+
Lists.newArrayList(
543+
map("title", "The Great Gatsby"),
544+
map("title", "The Handmaid's Tale"),
545+
map("title", "The Hitchhiker's Guide to the Galaxy"),
546+
map("title", "The Lord of the Rings")
547+
));
548+
}
549+
550+
@Test
551+
public void testEndsWith() throws Exception {
552+
List<PipelineResult> results =
553+
collection
554+
.pipeline()
555+
.where(endsWith(Field.of("title"), Constant.of("y")))
556+
.select("title")
557+
.sort(Field.of("title").descending())
558+
.execute()
559+
.get();
560+
561+
assertThat(data(results))
562+
.isEqualTo(
563+
Lists.newArrayList(
564+
map("title", "The Hitchhiker's Guide to the Galaxy"),
565+
map("title", "The Great Gatsby")));
566+
}
567+
529568
@Test
530569
public void testLength() throws Exception {
531570
List<PipelineResult> results =

0 commit comments

Comments
 (0)