Skip to content

Commit

Permalink
docs(sample): normalize firestore sample's region tags (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
athakor committed Nov 5, 2020
1 parent 22f98a1 commit b529245
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
Expand Up @@ -20,8 +20,10 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.DocumentReference;
// [START fs_include_dependencies]
// [START firestore_setup_dependencies]
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
// [END firestore_setup_dependencies]
// [END fs_include_dependencies]
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
Expand All @@ -44,19 +46,23 @@ public class Quickstart {
*/
public Quickstart() {
// [START fs_initialize]
// [START firestore_setup_client_create]
Firestore db = FirestoreOptions.getDefaultInstance().getService();
// [END firestore_setup_client_create]
// [END fs_initialize]
this.db = db;
}

public Quickstart(String projectId) throws Exception {
// [START fs_initialize_project_id]
// [START firestore_setup_client_create_with_project_id]
FirestoreOptions firestoreOptions =
FirestoreOptions.getDefaultInstance().toBuilder()
.setProjectId(projectId)
.setCredentials(GoogleCredentials.getApplicationDefault())
.build();
Firestore db = firestoreOptions.getService();
// [END firestore_setup_client_create_with_project_id]
// [END fs_initialize_project_id]
this.db = db;
}
Expand All @@ -74,6 +80,7 @@ void addDocument(String docName) throws Exception {
switch (docName) {
case "alovelace": {
// [START fs_add_data_1]
// [START firestore_setup_dataset_pt1]
DocumentReference docRef = db.collection("users").document("alovelace");
// Add document data with id "alovelace" using a hashmap
Map<String, Object> data = new HashMap<>();
Expand All @@ -85,11 +92,13 @@ void addDocument(String docName) throws Exception {
// ...
// result.get() blocks on response
System.out.println("Update time : " + result.get().getUpdateTime());
// [END firestore_setup_dataset_pt1]
// [END fs_add_data_1]
break;
}
case "aturing": {
// [START fs_add_data_2]
// [START firestore_setup_dataset_pt2]
DocumentReference docRef = db.collection("users").document("aturing");
// Add document data with an additional field ("middle")
Map<String, Object> data = new HashMap<>();
Expand All @@ -100,6 +109,7 @@ void addDocument(String docName) throws Exception {

ApiFuture<WriteResult> result = docRef.set(data);
System.out.println("Update time : " + result.get().getUpdateTime());
// [END firestore_setup_dataset_pt2]
// [END fs_add_data_2]
break;
}
Expand Down
Expand Up @@ -54,6 +54,7 @@ Map<String, Object> listenToDocument() throws Exception {
final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create();

// [START listen_to_document]
// [START firestore_listen_document]
DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
Expand All @@ -76,6 +77,7 @@ public void onEvent(@Nullable DocumentSnapshot snapshot,
// [END_EXCLUDE]
}
});
// [END firestore_listen_document]
// [END listen_to_document]

return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
Expand All @@ -88,6 +90,7 @@ List<String> listenForMultiple() throws Exception {
final SettableApiFuture<List<String>> future = SettableApiFuture.create();

// [START listen_to_multiple]
// [START firestore_listen_query_snapshots]
db.collection("cities")
.whereEqualTo("state", "CA")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
Expand All @@ -113,6 +116,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
// [END_EXCLUDE]
}
});
// [END firestore_listen_query_snapshots]
// [END listen_to_multiple]

return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
Expand All @@ -125,6 +129,7 @@ List<DocumentChange> listenForChanges() throws Exception {
SettableApiFuture<List<DocumentChange>> future = SettableApiFuture.create();

// [START listen_for_changes]
// [START firestore_listen_query_changes]
db.collection("cities")
.whereEqualTo("state", "CA")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
Expand Down Expand Up @@ -158,6 +163,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
// [END_EXCLUDE]
}
});
// [END firestore_listen_query_changes]
// [END listen_for_changes]

return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
Expand All @@ -168,6 +174,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
*/
void detachListener() {
// [START detach_errors]
// [START firestore_listen_detach]
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(
new EventListener<QuerySnapshot>() {
Expand All @@ -184,6 +191,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,

// Stop listening to changes
registration.remove();
// [END firestore_listen_detach]
// [END detach_errors]
}

Expand All @@ -192,6 +200,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
*/
void listenErrors() {
// [START listen_errors]
// [START firestore_listen_handle_error]
db.collection("cities")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
Expand All @@ -209,6 +218,7 @@ public void onEvent(@Nullable QuerySnapshot snapshots,
}
}
});
// [END firestore_listen_handle_error]
// [END listen_errors]
}

Expand Down

0 comments on commit b529245

Please sign in to comment.