Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Delay initialization of the Firestore client #109

Merged
merged 3 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example;

import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.FieldPath;
import com.google.cloud.firestore.Firestore;
Expand Down Expand Up @@ -45,6 +46,7 @@ public static void main(String[] args) throws Exception {

deleteCollection(db);
createUserDocument(db);
createUserDocumentPojo(db);
readDocuments(db);
runSampleQueries(db);
}
Expand All @@ -54,12 +56,12 @@ private static void deleteCollection(Firestore db) throws Exception {
for (DocumentReference doc : documents) {
doc.delete().get();
}

}

private static void createUserDocument(Firestore db) throws Exception {
DocumentReference docRef = db.collection(USERS_COLLECTION).document("alovelace");
Map<String, Object> data = new HashMap<>();
data.put("id", "10");
data.put("first", "Ada");
data.put("last", "Lovelace");
data.put("born", 1815);
Expand All @@ -68,6 +70,16 @@ private static void createUserDocument(Firestore db) throws Exception {
System.out.println("Created user " + docRef.getId() + ". Timestamp: " + result.getUpdateTime());
}

private static void createUserDocumentPojo(Firestore db) throws Exception {
CollectionReference collectionReference = db.collection(USERS_COLLECTION);
WriteResult result =
collectionReference.document()
.set(new Person("Alan", "Turing", 1912))
.get();

System.out.println("Created user by POJO. Timestamp: " + result.getUpdateTime());
}

private static void readDocuments(Firestore db) throws Exception {
ApiFuture<QuerySnapshot> query = db.collection(USERS_COLLECTION).get();
QuerySnapshot querySnapshot = query.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020 Google LLC
*
* 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
*
* https://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;

import com.google.cloud.firestore.annotation.DocumentId;

/**
* Simple document object to load into Firestore.
*/
public class Person {

@DocumentId
public String id;

public String first;

public String last;

public int born;

Person(String first, String last, int born) {
this.first = first;
this.last = last;
this.born = born;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name" : "com.example.Person",
"allDeclaredConstructors" : true,
"allDeclaredMethods" : true,
"allDeclaredFields" : true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Args = -H:+AllowIncompleteClasspath --report-unsupported-elements-at-runtime \
io.grpc.netty.shaded.io.grpc.netty,\
io.grpc.netty.shaded.io.netty.channel.epoll,\
io.grpc.netty.shaded.io.netty.channel.unix,\
com.google.api.client.googleapis.services.AbstractGoogleClientRequest$ApiClientVersion
com.google.api.client.googleapis.services.AbstractGoogleClientRequest$ApiClientVersion,\
com.google.cloud.firestore.FirestoreImpl