use of com.google.cloud.firestore.QuerySnapshot in project getting-started-java by GoogleCloudPlatform.
the class FirestoreDao method listBooks.
// [END bookshelf_firestore_documents_to_books]
// [START bookshelf_firestore_list_books]
@Override
public Result<Book> listBooks(String startTitle) {
Query booksQuery = booksCollection.orderBy("title").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);
}
use of com.google.cloud.firestore.QuerySnapshot in project beam by apache.
the class FirestoreV1IT method batchWrite_partialFailureOutputsToDeadLetterQueue.
@Test
public void batchWrite_partialFailureOutputsToDeadLetterQueue() throws InterruptedException, ExecutionException, TimeoutException {
String collectionId = "a";
String docId = helper.docId();
Write validWrite = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).build();
long millis = System.currentTimeMillis();
Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000).setNanos((int) ((millis % 1000) * 1000000)).build();
String docId2 = helper.docId();
helper.getBaseDocument().collection(collectionId).document(docId2).create(ImmutableMap.of("foo", "baz")).get(10, TimeUnit.SECONDS);
// this will fail because we're setting a updateTime precondition to before it was created
Write conditionalUpdate = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId2)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).setCurrentDocument(Precondition.newBuilder().setUpdateTime(timestamp)).build();
List<Write> writes = newArrayList(validWrite, conditionalUpdate);
RpcQosOptions options = BaseFirestoreIT.rpcQosOptions.toBuilder().withBatchMaxCount(2).build();
PCollection<WriteFailure> writeFailurePCollection = testPipeline.apply(Create.of(writes)).apply(FirestoreIO.v1().write().batchWrite().withDeadLetterQueue().withRpcQosOptions(options).build());
PAssert.that(writeFailurePCollection).satisfies((writeFailures) -> {
Iterator<WriteFailure> iterator = writeFailures.iterator();
assertTrue(iterator.hasNext());
WriteFailure failure = iterator.next();
assertEquals(Code.FAILED_PRECONDITION, Code.forNumber(failure.getStatus().getCode()));
assertNotNull(failure.getWriteResult());
assertFalse(failure.getWriteResult().hasUpdateTime());
assertEquals(conditionalUpdate, failure.getWrite());
assertFalse(iterator.hasNext());
return null;
});
testPipeline.run(this.options);
ApiFuture<QuerySnapshot> actualDocsQuery = helper.getBaseDocument().collection(collectionId).orderBy("__name__").get();
QuerySnapshot querySnapshot = actualDocsQuery.get(10, TimeUnit.SECONDS);
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
List<KV<String, String>> actualDocumentIds = documents.stream().map(doc -> KV.of(doc.getId(), doc.getString("foo"))).collect(Collectors.toList());
List<KV<String, String>> expected = newArrayList(KV.of(docId, docId), KV.of(docId2, "baz"));
assertEquals(expected, actualDocumentIds);
}
Aggregations