use of com.google.cloud.firestore.CollectionReference in project getting-started-java by GoogleCloudPlatform.
the class TranslateServlet method doPost.
// [END getting_started_background_app_list]
/**
* Handle a posted message from Pubsub.
*
* @param req The message Pubsub posts to this process.
* @param resp Not used.
*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// Block requests that don't contain the proper verification token.
String pubsubVerificationToken = PUBSUB_VERIFICATION_TOKEN;
if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// [START getting_started_background_translate_string]
String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
PubSubMessage pubsubMessage = gson.fromJson(body, PubSubMessage.class);
TranslateMessage message = pubsubMessage.getMessage();
// Use Translate service client to translate the message.
Translate translate = (Translate) this.getServletContext().getAttribute("translate");
message.setData(decode(message.getData()));
Translation translation = translate.translate(message.getData(), Translate.TranslateOption.sourceLanguage(message.getAttributes().getSourceLang()), Translate.TranslateOption.targetLanguage(message.getAttributes().getTargetLang()));
// [END getting_started_background_translate_string]
message.setTranslatedText(translation.getTranslatedText());
try {
// [START getting_started_background_translate]
// Use Firestore service client to store the translation in Firestore.
Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
CollectionReference translations = firestore.collection("translations");
ApiFuture<WriteResult> setFuture = translations.document().set(message, SetOptions.merge());
setFuture.get();
resp.getWriter().write(translation.getTranslatedText());
// [END getting_started_background_translate]
} catch (InterruptedException | ExecutionException e) {
throw new ServletException("Exception storing data in Firestore.", e);
}
}
use of com.google.cloud.firestore.CollectionReference in project jade-data-repo by DataBiosphere.
the class FireStoreDependencyDao method fileHasSnapshotReference.
public boolean fileHasSnapshotReference(Dataset dataset, String fileId) throws InterruptedException {
DatasetDataProject dataProject = dataLocationService.getProjectOrThrow(dataset);
FireStoreProject fireStoreProject = FireStoreProject.get(dataProject.getGoogleProjectId());
String dependencyCollectionName = getDatasetDependencyId(dataset.getId().toString());
CollectionReference depColl = fireStoreProject.getFirestore().collection(dependencyCollectionName);
Query query = depColl.whereEqualTo("fileId", fileId);
return hasReference(query);
}
use of com.google.cloud.firestore.CollectionReference in project jade-data-repo by DataBiosphere.
the class FireStoreDependencyDao method getDatasetSnapshotFileIds.
public List<String> getDatasetSnapshotFileIds(Dataset dataset, String snapshotId) throws InterruptedException {
DatasetDataProject dataProject = dataLocationService.getProjectOrThrow(dataset);
FireStoreProject fireStoreProject = FireStoreProject.get(dataProject.getGoogleProjectId());
String dependencyCollectionName = getDatasetDependencyId(dataset.getId().toString());
CollectionReference depColl = fireStoreProject.getFirestore().collection(dependencyCollectionName);
Query query = depColl.whereEqualTo("snapshotId", snapshotId);
ApiFuture<QuerySnapshot> querySnapshot = query.get();
List<String> fileIds = new ArrayList<>();
try {
List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
for (DocumentSnapshot docSnap : documents) {
FireStoreDependency fireStoreDependency = docSnap.toObject(FireStoreDependency.class);
fileIds.add(fireStoreDependency.getFileId());
}
return fileIds;
} catch (ExecutionException ex) {
throw new FileSystemExecutionException("get file ids - execution exception", ex);
}
}
use of com.google.cloud.firestore.CollectionReference in project jade-data-repo by DataBiosphere.
the class FireStoreDirectoryDao method deleteDirectoryEntry.
// true - directory entry existed and was deleted; false - directory entry did not exist
public boolean deleteDirectoryEntry(Firestore firestore, String collectionId, String fileId) {
CollectionReference datasetCollection = firestore.collection(collectionId);
ApiFuture<Boolean> transaction = firestore.runTransaction(xn -> {
List<DocumentReference> deleteList = new ArrayList<>();
// Look up the directory entry by id. If it doesn't exist, we're done
QueryDocumentSnapshot leafSnap = lookupByFileId(firestore, collectionId, fileId, xn);
if (leafSnap == null) {
return false;
}
deleteList.add(leafSnap.getReference());
FireStoreDirectoryEntry leafEntry = leafSnap.toObject(FireStoreDirectoryEntry.class);
String lookupPath = makeLookupPath(leafEntry.getPath());
while (!lookupPath.isEmpty()) {
// Count the number of entries with this path as their directory path
// A value of 1 means that the directory will be empty after its child is
// deleted, so we should delete it also.
Query query = datasetCollection.whereEqualTo("path", makePathFromLookupPath(lookupPath));
ApiFuture<QuerySnapshot> querySnapshot = xn.get(query);
List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
if (documents.size() > 1) {
break;
}
DocumentReference docRef = datasetCollection.document(encodePathAsFirestoreDocumentName(lookupPath));
deleteList.add(docRef);
lookupPath = fireStoreUtils.getDirectoryPath(lookupPath);
}
for (DocumentReference docRef : deleteList) {
xn.delete(docRef);
}
return true;
});
return fireStoreUtils.transactionGet("deleteDirectoryEntry", transaction);
}
use of com.google.cloud.firestore.CollectionReference in project jade-data-repo by DataBiosphere.
the class FireStoreDirectoryDao method lookupByFileId.
// Returns null if not found
private QueryDocumentSnapshot lookupByFileId(Firestore firestore, String collectionId, String fileId, Transaction xn) {
try {
CollectionReference datasetCollection = firestore.collection(collectionId);
Query query = datasetCollection.whereEqualTo("fileId", fileId);
ApiFuture<QuerySnapshot> querySnapshot = xn.get(query);
List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
if (documents.size() == 0) {
return null;
}
if (documents.size() != 1) {
// TODO: We have seen duplicate documents as a result of concurrency issues.
// The query.get() does not appear to be reliably transactional. That may
// be a FireStore bug. Regardless, we treat this as a retryable situation.
// It *might* be corruption bug on our side. If so, the retry will consistently
// fail and eventually give up. When debugging that case, one will have to understand
// the purpose of this logic.
logger.warn("Found too many entries: " + documents.size() + "; for file: " + collectionId + "/" + fileId);
throw new FileSystemAbortTransactionException("lookupByFileId found too many entries");
}
return documents.get(0);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new FileSystemExecutionException("lookupByFileId - execution interrupted", ex);
} catch (AbortedException | ExecutionException ex) {
throw handleExecutionException("lookupByFileId", ex);
}
}
Aggregations