-
Notifications
You must be signed in to change notification settings - Fork 66
/
ManageDataSnippets.java
491 lines (428 loc) · 18.4 KB
/
ManageDataSnippets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.firestore.snippets;
import com.example.firestore.snippets.model.City;
import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.FieldValue;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.SetOptions;
import com.google.cloud.firestore.TransactionOptions;
import com.google.cloud.firestore.WriteBatch;
import com.google.cloud.firestore.WriteResult;
import com.google.protobuf.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/** Snippets to demonstrate Firestore add, update and delete operations. */
class ManageDataSnippets {
private final Firestore db;
ManageDataSnippets(Firestore db) {
this.db = db;
}
/**
* Add a document to a collection using a map.
*
* @return document data
*/
Map<String, Object> addSimpleDocumentAsMap() throws Exception {
// [START firestore_data_set_from_map]
// Create a Map to store the data we want to set
Map<String, Object> docData = new HashMap<>();
docData.put("name", "Los Angeles");
docData.put("state", "CA");
docData.put("country", "USA");
docData.put("regions", Arrays.asList("west_coast", "socal"));
// Add a new document (asynchronously) in collection "cities" with id "LA"
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(docData);
// ...
// future.get() blocks on response
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_map]
return docData;
}
/**
* Add a document to a collection using a map with different supported data types.
*
* @return document data
*/
Map<String, Object> addDocumentWithDifferentDataTypes() throws Exception {
// [START firestore_data_set_from_map_nested]
Map<String, Object> docData = new HashMap<>();
docData.put("stringExample", "Hello, World");
docData.put("booleanExample", false);
docData.put("numberExample", 3.14159265);
docData.put("nullExample", null);
ArrayList<Object> arrayExample = new ArrayList<>();
Collections.addAll(arrayExample, 5L, true, "hello");
docData.put("arrayExample", arrayExample);
Map<String, Object> objectExample = new HashMap<>();
objectExample.put("a", 5L);
objectExample.put("b", true);
docData.put("objectExample", objectExample);
ApiFuture<WriteResult> future = db.collection("data").document("one").set(docData);
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_map_nested]
return docData;
}
/**
* Add a document to a collection as a custom object.
*
* @return entity added
*/
City addSimpleDocumentAsEntity() throws Exception {
// [START firestore_data_set_from_custom_type]
City city =
new City("Los Angeles", "CA", "USA", false, 3900000L, Arrays.asList("west_coast", "socal"));
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(city);
// block on response if required
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_custom_type]
return city;
}
/** set() providing a document ID. */
void setRequiresId(Map<String, String> data) {
// [START firestore_data_set_id_specified]
db.collection("cities").document("new-city-id").set(data);
// [END firestore_data_set_id_specified]
}
/**
* Add a document without explicitly providing the document id. The document id gets automatically
* generated.
*
* @return auto generated id
*/
String addDocumentDataWithAutoGeneratedId() throws Exception {
// [START firestore_data_set_id_random_collection]
// Add document data with auto-generated id.
Map<String, Object> data = new HashMap<>();
data.put("name", "Tokyo");
data.put("country", "Japan");
ApiFuture<DocumentReference> addedDocRef = db.collection("cities").add(data);
System.out.println("Added document with ID: " + addedDocRef.get().getId());
// [END firestore_data_set_id_random_collection]
return addedDocRef.get().getId();
}
/**
* Add data to a document after generating the document id.
*
* @return auto generated id
*/
String addDocumentDataAfterAutoGeneratingId() throws Exception {
City data = new City();
// [START firestore_data_set_id_random_document_ref]
// Add document data after generating an id.
DocumentReference addedDocRef = db.collection("cities").document();
System.out.println("Added document with ID: " + addedDocRef.getId());
// later...
ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
// [END firestore_data_set_id_random_document_ref]
// writeResult.get() blocks on operation
System.out.println("Update time : " + writeResult.get().getUpdateTime());
return addedDocRef.getId();
}
/** Partially update a document using the .update(field1, value1..) method. */
void updateSimpleDocument() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START firestore_data_set_field]
// Update an existing document
DocumentReference docRef = db.collection("cities").document("DC");
// (async) Update one field
ApiFuture<WriteResult> future = docRef.update("capital", true);
// ...
WriteResult result = future.get();
System.out.println("Write result: " + result);
// [END firestore_data_set_field]
}
/** Partially update fields of a document using a map (field => value). */
void updateUsingMap() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START firestore_data_set_document_update]
// update multiple fields using a map
DocumentReference docRef = db.collection("cities").document("DC");
Map<String, Object> updates = new HashMap<>();
updates.put("name", "Washington D.C.");
updates.put("country", "USA");
updates.put("capital", true);
// asynchronously update doc
ApiFuture<WriteResult> writeResult = docRef.update(updates);
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_set_document_update]
}
/** Partially update fields of a document using a map (field => value). */
void updateAndCreateIfMissing() throws Exception {
// [START firestore_data_set_doc_upsert]
// asynchronously update doc, create the document if missing
Map<String, Object> update = new HashMap<>();
update.put("capital", true);
ApiFuture<WriteResult> writeResult =
db.collection("cities").document("BJ").set(update, SetOptions.merge());
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_set_doc_upsert]
}
/** Partial update nested fields of a document. */
void updateNestedFields() throws Exception {
// CHECKSTYLE OFF: VariableDeclarationUsageDistance
// [START firestore_data_set_nested_fields]
// Create an initial document to update
DocumentReference frankDocRef = db.collection("users").document("frank");
Map<String, Object> initialData = new HashMap<>();
initialData.put("name", "Frank");
initialData.put("age", 12);
Map<String, Object> favorites = new HashMap<>();
favorites.put("food", "Pizza");
favorites.put("color", "Blue");
favorites.put("subject", "Recess");
initialData.put("favorites", favorites);
ApiFuture<WriteResult> initialResult = frankDocRef.set(initialData);
// Confirm that data has been successfully saved by blocking on the operation
initialResult.get();
// Update age and favorite color
Map<String, Object> updates = new HashMap<>();
updates.put("age", 13);
updates.put("favorites.color", "Red");
// Async update document
ApiFuture<WriteResult> writeResult = frankDocRef.update(updates);
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_set_nested_fields]
// CHECKSTYLE ON: VariableDeclarationUsageDistance
}
/** Update document with server timestamp. */
void updateServerTimestamp() throws Exception {
db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get();
// [START firestore_data_set_server_timestamp]
DocumentReference docRef = db.collection("objects").document("some-id");
// Update the timestamp field with the value from the server
ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp());
System.out.println("Update time : " + writeResult.get());
// [END firestore_data_set_server_timestamp]
}
/** Update array fields in a document. * */
void updateDocumentArray() throws Exception {
// [START firestore_data_set_array_operations]
DocumentReference washingtonRef = db.collection("cities").document("DC");
// Atomically add a new region to the "regions" array field.
ApiFuture<WriteResult> arrayUnion =
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));
System.out.println("Update time : " + arrayUnion.get());
// Atomically remove a region from the "regions" array field.
ApiFuture<WriteResult> arrayRm =
washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"));
System.out.println("Update time : " + arrayRm.get());
// [END firestore_data_set_array_operations]
}
/** Delete specific fields when updating a document. */
void deleteFields() throws Exception {
City city = new City("Beijing");
city.setCapital(true);
db.collection("cities").document("BJ").set(city).get();
// [START firestore_data_delete_field]
DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
updates.put("capital", FieldValue.delete());
// Update and delete the "capital" field in the document
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());
// [END firestore_data_delete_field]
}
/** Delete a document in a collection. */
void deleteDocument() throws Exception {
db.collection("cities").document("DC").set(new City("Washington, D.C.")).get();
// [START firestore_data_delete_doc]
// asynchronously delete a document
ApiFuture<WriteResult> writeResult = db.collection("cities").document("DC").delete();
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_delete_doc]
}
// [START firestore_data_delete_collection]
/**
* Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on
* document size (atmost 1MB) and application requirements.
*/
void deleteCollection(CollectionReference collection, int batchSize) {
try {
// retrieve a small batch of documents to avoid out-of-memory errors
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
int deleted = 0;
// future.get() blocks on document retrieval
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
document.getReference().delete();
++deleted;
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
}
}
// [END firestore_data_delete_collection]
/**
* Run a simple transaction to perform a field value increment.
*
* @return transaction future
*/
ApiFuture<Void> runSimpleTransaction() throws Exception {
// [START firestore_transaction_document_update]
// Initialize doc
final DocumentReference docRef = db.collection("cities").document("SF");
City city = new City("SF");
city.setCountry("USA");
city.setPopulation(860000L);
docRef.set(city).get();
// run an asynchronous transaction
ApiFuture<Void> futureTransaction =
db.runTransaction(
transaction -> {
// retrieve document and increment population field
DocumentSnapshot snapshot = transaction.get(docRef).get();
long oldPopulation = snapshot.getLong("population");
transaction.update(docRef, "population", oldPopulation + 1);
return null;
});
// block on transaction operation using transaction.get()
// [END firestore_transaction_document_update]
return futureTransaction;
}
/**
* Return information from a conditional transaction.
*
* @param population : set initial population.
*/
String returnInfoFromTransaction(long population) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("population", population);
// Block until transaction is complete is using transaction.get()
db.collection("cities").document("SF").set(map).get();
// [START firestore_transaction_document_update_conditional]
final DocumentReference docRef = db.collection("cities").document("SF");
ApiFuture<String> futureTransaction =
db.runTransaction(
transaction -> {
DocumentSnapshot snapshot = transaction.get(docRef).get();
Long newPopulation = snapshot.getLong("population") + 1;
// conditionally update based on current population
if (newPopulation <= 1000000L) {
transaction.update(docRef, "population", newPopulation);
return "Population increased to " + newPopulation;
} else {
throw new Exception("Sorry! Population is too big.");
}
});
// Print information retrieved from transaction
System.out.println(futureTransaction.get());
// [END firestore_transaction_document_update_conditional]
return futureTransaction.get();
}
/** Write documents in a batch. */
void writeBatch() throws Exception {
db.collection("cities").document("SF").set(new City()).get();
db.collection("cities").document("LA").set(new City()).get();
// [START firestore_data_batch_writes]
// Get a new write batch
WriteBatch batch = db.batch();
// Set the value of 'NYC'
DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, new City());
// Update the population of 'SF'
DocumentReference sfRef = db.collection("cities").document("SF");
batch.update(sfRef, "population", 1000000L);
// Delete the city 'LA'
DocumentReference laRef = db.collection("cities").document("LA");
batch.delete(laRef);
// asynchronously commit the batch
ApiFuture<List<WriteResult>> future = batch.commit();
// ...
// future.get() blocks on batch commit operation
for (WriteResult result : future.get()) {
System.out.println("Update time : " + result.getUpdateTime());
}
// [END firestore_data_batch_writes]
}
public void updateDocumentIncrement() throws ExecutionException, InterruptedException {
final City city = new City();
city.setPopulation(100L);
db.collection("cities").document("DC").set(city).get();
// [START firestore_data_set_numeric_increment]
DocumentReference washingtonRef = db.collection("cities").document("DC");
// Atomically increment the population of the city by 50.
final ApiFuture<WriteResult> updateFuture =
washingtonRef.update("population", FieldValue.increment(50));
// [END firestore_data_set_numeric_increment]
updateFuture.get();
}
/**
* Run a snapshot read to read past versions of documents.
*
* @return transaction future
*/
DocumentSnapshot runSnapshotReads() throws Exception {
// [START firestore_snapshot_read]
// Initialize a documnet
final DocumentReference documentReference = db.collection("cities").document("SF");
City city = new City("SF");
city.setCountry("USA");
city.setPopulation(860008L);
// Write doc and save write result
WriteResult writeResult = documentReference.set(city).get();
// Create a reference to the cities collection
CollectionReference cities = db.collection("cities");
// Create a query against the collection.
Query query = cities.whereEqualTo("capital", true);
// In the transaction options, Set read time and and set to read-only
// Snapshot reads require read-only transactions
// As an example, set read time to the update time of the previous write operation
final Timestamp readTime =
com.google.protobuf.Timestamp.newBuilder()
.setSeconds(writeResult.getUpdateTime().getSeconds())
.setNanos(writeResult.getUpdateTime().getNanos())
.build();
TransactionOptions options =
TransactionOptions.createReadOnlyOptionsBuilder().setReadTime(readTime).build();
// run a transaction
ApiFuture<DocumentSnapshot> futureTransaction =
db.runTransaction(
transaction -> {
// Execute a snapshot read document lookup
final DocumentSnapshot documentResult = transaction.get(documentReference).get();
// Execute a snapshot read query with the same transaction options
final QuerySnapshot queryResult = transaction.get(query).get();
return documentResult;
},
options);
// [END firestore_snapshot_read]
return futureTransaction.get();
}
/** Closes the gRPC channels associated with this instance and frees up their resources. */
void close() throws Exception {
db.close();
}
}