Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(sample): normalize firestore sample's region tags #453

Merged
merged 2 commits into from Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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