use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippetsIT method testMultipleCursorConditions.
@Test
public void testMultipleCursorConditions() throws Exception {
// populate us_cities collection
Map<String, String> city1 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Massachusetts").build();
Map<String, String> city2 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Missouri").build();
Map<String, String> city3 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Wisconsin").build();
db.collection("us_cities").document("Massachusetts").set(city1).get();
db.collection("us_cities").document("Missouri").set(city2).get();
db.collection("us_cities").document("Wisconsin").set(city3).get();
Query query1 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield");
// all documents are retrieved
QuerySnapshot querySnapshot = query1.get().get();
List<QueryDocumentSnapshot> docs = querySnapshot.getDocuments();
assertEquals(3, docs.size());
// Will return "Springfield, Missouri" and "Springfield, Wisconsin"
Query query2 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield", "Missouri");
// only Missouri and Wisconsin are retrieved
List<String> expectedResults = Arrays.asList("Missouri", "Wisconsin");
List<String> result = getResults(query2);
assertTrue(Objects.equals(result, expectedResults));
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippetsIT method getResults.
private List<String> getResults(Query query) throws Exception {
// asynchronously retrieve query results
ApiFuture<QuerySnapshot> future = query.get();
// block on response
QuerySnapshot querySnapshot = future.get();
List<String> docIds = new ArrayList<>();
for (DocumentSnapshot document : querySnapshot.getDocuments()) {
docIds.add(document.getId());
}
return docIds;
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippetsIT method deleteAllDocuments.
private static void deleteAllDocuments() throws Exception {
ApiFuture<QuerySnapshot> future = db.collection("cities").get();
QuerySnapshot querySnapshot = future.get();
for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
// block on delete operation
db.collection("cities").document(doc.getId()).delete().get();
}
}
use of com.google.cloud.firestore.QuerySnapshot in project getting-started-java by GoogleCloudPlatform.
the class TranslateServlet method doGet.
// [START getting_started_background_app_list]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
CollectionReference translations = firestore.collection("translations");
QuerySnapshot snapshot;
try {
snapshot = translations.limit(10).get().get();
} catch (InterruptedException | ExecutionException e) {
throw new ServletException("Exception retrieving documents from Firestore.", e);
}
List<TranslateMessage> translateMessages = Lists.newArrayList();
List<QueryDocumentSnapshot> documents = Lists.newArrayList(snapshot.getDocuments());
documents.sort(Comparator.comparing(DocumentSnapshot::getCreateTime));
for (DocumentSnapshot document : Lists.reverse(documents)) {
String encoded = gson.toJson(document.getData());
TranslateMessage message = gson.fromJson(encoded, TranslateMessage.class);
message.setData(decode(message.getData()));
translateMessages.add(message);
}
req.setAttribute("messages", translateMessages);
req.setAttribute("page", "list");
req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
use of com.google.cloud.firestore.QuerySnapshot in project getting-started-java by GoogleCloudPlatform.
the class FirestoreDao method listBooksByUser.
// [END bookshelf_firestore_list_books]
// [START bookshelf_firestore_list_by_user]
@Override
public Result<Book> listBooksByUser(String userId, String startTitle) {
Query booksQuery = booksCollection.orderBy("title").whereEqualTo(Book.CREATED_BY_ID, userId).limit(10);
if (startTitle != null) {
booksQuery = booksQuery.startAfter(startTitle);
}
try {
QuerySnapshot snapshot = booksQuery.get().get();
List<Book> results = documentsToBooks(snapshot.getDocuments());
String newCursor = null;
if (results.size() > 0) {
newCursor = results.get(results.size() - 1).getTitle();
}
return new Result<>(results, newCursor);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return new Result<>(Lists.newArrayList(), null);
}
Aggregations