Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit a612f0b

Browse files
committed
Delete upsert() from Pipeline.java and tests
1 parent e162660 commit a612f0b

7 files changed

Lines changed: 228 additions & 171 deletions

File tree

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

Lines changed: 162 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import com.google.cloud.firestore.pipeline.stages.Unnest;
6262
import com.google.cloud.firestore.pipeline.stages.UnnestOptions;
6363
import com.google.cloud.firestore.pipeline.stages.Update;
64-
import com.google.cloud.firestore.pipeline.stages.Upsert;
6564
import com.google.cloud.firestore.pipeline.stages.Where;
6665
import com.google.cloud.firestore.telemetry.MetricsUtil.MetricsContext;
6766
import com.google.cloud.firestore.telemetry.TelemetryConstants;
@@ -1002,105 +1001,224 @@ public Pipeline unnest(Selectable field, UnnestOptions options) {
10021001
/**
10031002
* Performs a delete operation on documents from previous stages.
10041003
*
1005-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1004+
* <p>
1005+
* Example:
1006+
*
1007+
* <pre>{@code
1008+
* // Delete all documents in the "logs" collection where "status" is "archived"
1009+
* firestore.pipeline()
1010+
* .collection("logs")
1011+
* .where(field("status").equal("archived"))
1012+
* .delete()
1013+
* .execute()
1014+
* .get();
1015+
* }</pre>
1016+
*
1017+
* @return A new {@code Pipeline} object with this stage appended to the stage
1018+
* list.
10061019
*/
10071020
@BetaApi
10081021
public Pipeline delete() {
10091022
return append(new Delete());
10101023
}
10111024

10121025
/**
1013-
* Performs an upsert operation using documents from previous stages.
1026+
* Performs an update operation using documents from previous stages.
10141027
*
1015-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1016-
*/
1017-
@BetaApi
1018-
public Pipeline upsert() {
1019-
return append(new Upsert());
1020-
}
1021-
1022-
/**
1023-
* Performs an upsert operation using documents from previous stages.
1028+
* <p>
1029+
* This method updates the documents in place based on the data flowing through
1030+
* the pipeline.
1031+
* To specify transformations, use {@link #update(Selectable...)}.
10241032
*
1025-
* @param target The collection to upsert to.
1026-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1027-
*/
1028-
@BetaApi
1029-
public Pipeline upsert(CollectionReference target) {
1030-
return append(new Upsert().withCollection(target));
1031-
}
1032-
1033-
/**
1034-
* Performs an upsert operation using documents from previous stages.
1033+
* <p>
1034+
* Example 1: Update a collection's schema by adding a new field and removing
1035+
* an old one.
10351036
*
1036-
* @param upsertStage The {@code Upsert} stage to append.
1037-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1038-
*/
1039-
@InternalApi
1040-
public Pipeline upsert(Upsert upsertStage) {
1041-
return append(upsertStage);
1042-
}
1043-
1044-
/**
1045-
* Performs an update operation using documents from previous stages.
1037+
* <pre>{@code
1038+
* firestore.pipeline()
1039+
* .collection("books")
1040+
* .addFields(constant("Fiction").as("genre"))
1041+
* .removeFields("old_genre")
1042+
* .update()
1043+
* .execute()
1044+
* .get();
1045+
* }</pre>
10461046
*
1047-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1047+
* <p>
1048+
* Example 2: Update documents in place with data from literals.
1049+
*
1050+
* <pre>{@code
1051+
* Map<String, Object> updateData = new HashMap<>();
1052+
* updateData.put("__name__", firestore.collection("books").document("book1"));
1053+
* updateData.put("status", "Updated");
1054+
*
1055+
* firestore.pipeline()
1056+
* .literals(updateData)
1057+
* .update()
1058+
* .execute()
1059+
* .get();
1060+
* }</pre>
1061+
*
1062+
* @return A new {@code Pipeline} object with this stage appended to the stage
1063+
* list.
10481064
*/
10491065
@BetaApi
10501066
public Pipeline update() {
10511067
return append(new Update());
10521068
}
10531069

10541070
/**
1055-
* Performs an update operation using documents from previous stages.
1071+
* Performs an update operation using documents from previous stages with
1072+
* specified
1073+
* transformations.
1074+
*
1075+
* <p>
1076+
* Example:
1077+
*
1078+
* <pre>{@code
1079+
* // Update the "status" field to "Discounted" for all books where price > 50
1080+
* firestore.pipeline()
1081+
* .collection("books")
1082+
* .where(field("price").greaterThan(50))
1083+
* .update(constant("Discounted").as("status"))
1084+
* .execute()
1085+
* .get();
1086+
* }</pre>
10561087
*
10571088
* @param transformations The transformations to apply.
1058-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1089+
* @return A new {@code Pipeline} object with this stage appended to the stage
1090+
* list.
10591091
*/
10601092
@BetaApi
10611093
public Pipeline update(Selectable... transformations) {
10621094
return append(new Update().withTransformations(transformations));
10631095
}
10641096

10651097
/**
1066-
* Performs an update operation using documents from previous stages.
1098+
* Performs an update operation using an {@link Update} stage.
1099+
*
1100+
* <p>
1101+
* This method allows you to use a pre-configured {@link Update} stage.
1102+
*
1103+
* <p>
1104+
* Example:
1105+
*
1106+
* <pre>{@code
1107+
* Update updateStage = new Update().withTransformations(constant("Updated").as("status"));
1108+
*
1109+
* firestore.pipeline()
1110+
* .collection("books")
1111+
* .where(field("title").equal("The Hitchhiker's Guide to the Galaxy"))
1112+
* .update(updateStage)
1113+
* .execute()
1114+
* .get();
1115+
* }</pre>
10671116
*
10681117
* @param update The {@code Update} stage to append.
1069-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1118+
* @return A new {@code Pipeline} object with this stage appended to the stage
1119+
* list.
10701120
*/
1071-
@InternalApi
1121+
@BetaApi
10721122
public Pipeline update(Update update) {
10731123
return append(update);
10741124
}
10751125

10761126
/**
10771127
* Performs an insert operation using documents from previous stages.
10781128
*
1079-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1129+
* <p>
1130+
* The documents must include a valid {@code __name__} field specifying the
1131+
* document reference to insert. If the document already exists, the operation
1132+
* will fail.
1133+
*
1134+
* <p>
1135+
* Example:
1136+
*
1137+
* <pre>{@code
1138+
* Map<String, Object> book = new HashMap<>();
1139+
* book.put("__name__", firestore.collection("books").document("newBook"));
1140+
* book.put("title", "New Book");
1141+
*
1142+
* firestore.pipeline()
1143+
* .literals(book)
1144+
* .insert()
1145+
* .execute()
1146+
* .get();
1147+
* }</pre>
1148+
*
1149+
* @return A new {@code Pipeline} object with this stage appended to the stage
1150+
* list.
10801151
*/
10811152
@BetaApi
10821153
public Pipeline insert() {
10831154
return append(new Insert());
10841155
}
10851156

10861157
/**
1087-
* Performs an insert operation using documents from previous stages.
1158+
* Performs an insert operation using documents from previous stages into a
1159+
* specified target
1160+
* collection.
1161+
*
1162+
* <p>
1163+
* If documents have an ID (or expression evaluation for ID), they will use it.
1164+
* Otherwise,
1165+
* auto-generated IDs will be used if applicable (depending on the source).
1166+
*
1167+
* <p>
1168+
* Example:
1169+
*
1170+
* <pre>{@code
1171+
* CollectionReference backupCol = firestore.collection("books_backup");
1172+
*
1173+
* Map<String, Object> book = new HashMap<>();
1174+
* book.put("title", "New Book");
1175+
*
1176+
* firestore.pipeline()
1177+
* .literals(book)
1178+
* .insert(backupCol)
1179+
* .execute()
1180+
* .get();
1181+
* }</pre>
10881182
*
10891183
* @param target The collection to insert to.
1090-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1184+
* @return A new {@code Pipeline} object with this stage appended to the stage
1185+
* list.
10911186
*/
10921187
@BetaApi
10931188
public Pipeline insert(CollectionReference target) {
10941189
return append(new Insert().withCollection(target));
10951190
}
10961191

10971192
/**
1098-
* Performs an insert operation using documents from previous stages.
1193+
* Performs an insert operation using an {@link Insert} stage.
1194+
*
1195+
* <p>
1196+
* This method allows you to use a pre-configured {@link Insert} stage.
1197+
*
1198+
* <p>
1199+
* Example: Use a pre-configured {@link Insert} stage with a target collection
1200+
* and ID expression, reading from literals.
1201+
*
1202+
* <pre>{@code
1203+
* CollectionReference targetCol = firestore.collection("books_backup");
1204+
* Insert insertStage = new Insert().withCollection(targetCol).withIdExpression(field("custom_id"));
1205+
*
1206+
* Map<String, Object> book = new HashMap<>();
1207+
* book.put("custom_id", "book1");
1208+
* book.put("title", "Book 1");
1209+
*
1210+
* firestore.pipeline()
1211+
* .literals(book)
1212+
* .insert(insertStage)
1213+
* .execute()
1214+
* .get();
1215+
* }</pre>
10991216
*
11001217
* @param insertStage The {@code Insert} stage to append.
1101-
* @return A new {@code Pipeline} object with this stage appended to the stage list.
1218+
* @return A new {@code Pipeline} object with this stage appended to the stage
1219+
* list.
11021220
*/
1103-
@InternalApi
1221+
@BetaApi
11041222
public Pipeline insert(Insert insertStage) {
11051223
return append(insertStage);
11061224
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,21 @@ public Pipeline documents(String... docs) {
159159
}
160160

161161
/**
162-
* Creates a new {@link Pipeline} that operates on a static set of documents
163-
* represented as Maps.
162+
* Creates a new {@link Pipeline} that operates on a static set of documents represented as Maps.
163+
*
164+
* <p>Example:
165+
*
166+
* <pre>{@code
167+
* Map<String, Object> doc1 = new HashMap<>();
168+
* doc1.put("title", "Book 1");
169+
* Map<String, Object> doc2 = new HashMap<>();
170+
* doc2.put("title", "Book 2");
171+
*
172+
* Snapshot snapshot = firestore.pipeline()
173+
* .literals(doc1, doc2)
174+
* .execute()
175+
* .get();
176+
* }</pre>
164177
*
165178
* @param data The Maps representing documents to include in the pipeline.
166179
* @return A new {@code Pipeline} instance with a literals source.

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@
1818

1919
import com.google.api.core.BetaApi;
2020
import com.google.api.core.InternalApi;
21-
import com.google.cloud.firestore.CollectionReference;
22-
import com.google.cloud.firestore.PipelineUtils;
2321
import com.google.firestore.v1.Value;
2422
import java.util.ArrayList;
25-
import java.util.List;
26-
import javax.annotation.Nullable;
2723

2824
@InternalApi
2925
public final class Delete extends Stage {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
package com.google.cloud.firestore.pipeline.stages;
1818

1919
import com.google.api.core.BetaApi;
20-
import com.google.api.core.InternalApi;
2120
import com.google.cloud.firestore.CollectionReference;
2221
import com.google.cloud.firestore.PipelineUtils;
2322
import com.google.cloud.firestore.pipeline.expressions.Expression;
24-
import com.google.cloud.firestore.pipeline.expressions.Selectable;
2523
import com.google.firestore.v1.Value;
26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import javax.annotation.Nullable;
2924

3025
@BetaApi
3126
public final class Insert extends Stage {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Iterable<Value> toStageArgs() {
4646
}
4747

4848
private Value encodeLiteralMap(Map<?, ?> map) {
49-
com.google.firestore.v1.MapValue.Builder mapValue = com.google.firestore.v1.MapValue.newBuilder();
49+
com.google.firestore.v1.MapValue.Builder mapValue =
50+
com.google.firestore.v1.MapValue.newBuilder();
5051
for (Map.Entry<?, ?> entry : map.entrySet()) {
5152
String key = String.valueOf(entry.getKey());
5253
Object v = entry.getValue();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.cloud.firestore.pipeline.stages;
1818

1919
import com.google.api.core.BetaApi;
20-
import com.google.api.core.InternalApi;
2120
import com.google.cloud.firestore.CollectionReference;
2221
import com.google.cloud.firestore.PipelineUtils;
2322
import com.google.cloud.firestore.pipeline.expressions.Expression;
@@ -56,7 +55,8 @@ public Upsert withCollection(CollectionReference target) {
5655

5756
@BetaApi
5857
public Upsert withIdExpression(Expression idExpr) {
59-
return new Upsert(this.transformations, this.options.with("document_id", PipelineUtils.encodeValue(idExpr)));
58+
return new Upsert(
59+
this.transformations, this.options.with("document_id", PipelineUtils.encodeValue(idExpr)));
6060
}
6161

6262
@BetaApi

0 commit comments

Comments
 (0)