use of com.google.cloud.firestore.DocumentSnapshot in project cubanews by snava10.
the class MetadataController method countDocuments.
@GetMapping("/api/metadata/countDocuments")
public Mono<Long> countDocuments() throws ExecutionException, InterruptedException {
DocumentReference docRef = db.collection(countersCollectionName).document("pages-total-counter");
ApiFuture<DocumentSnapshot> future = docRef.get();
DocumentSnapshot document = future.get();
if (document.exists()) {
return Mono.just(Objects.requireNonNull(document.getLong("totalDocs")));
} else {
System.out.println("No such document!");
return Mono.just(0L);
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project cubanews by snava10.
the class HtmlCrawler method increaseCounter.
private ApiFuture<WriteResult> increaseCounter(Firestore db, long counterIncrease) throws ExecutionException, InterruptedException {
if (counterIncrease == 0) {
System.out.println("Counter increase 0");
return null;
}
DocumentReference docRef = db.collection("counters").document("pages-total-counter");
ApiFuture<DocumentSnapshot> future = docRef.get();
DocumentSnapshot document = future.get();
if (document.exists()) {
long current = document.getLong("totalDocs");
return docRef.update(new HashMap<String, Object>() {
{
put("totalDocs", current + counterIncrease);
put("lastDelta", counterIncrease);
put("lastUpdated", LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
}
});
} else {
return null;
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.
the class FireStoreDependencyDao method deleteSnapshotFileDependencies.
public void deleteSnapshotFileDependencies(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();
try {
List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
for (DocumentSnapshot docSnap : documents) {
logger.info("deleting: " + docSnap.toString());
docSnap.getReference().delete();
}
} catch (ExecutionException ex) {
throw new FileSystemExecutionException("delete dependencies - execution exception", ex);
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.
the class FireStoreDirectoryDao method addEntryToSnapshot.
// -- Snapshot filesystem methods --
/**
* Given an file id from a dataset directory, create a similar entry in the snapshot directory.
* The snapshot version of the entry differs because it has a the dataset name added to its path.
*/
public void addEntryToSnapshot(Firestore datasetFirestore, String datasetId, String datasetDirName, Firestore snapshotFirestore, String snapshotId, String fileId) {
FireStoreDirectoryEntry datasetEntry = retrieveById(datasetFirestore, datasetId, fileId);
if (!datasetEntry.getIsFileRef()) {
throw new NotImplementedException("Directories are not yet supported as references");
// TODO: Add directory support. Here is a sketch of a brute force implementation:
// Given the directory, walk its entire subtree collecting the ids of all of the files.
// Then loop through that id set calling this method on each id. It is simple, but wasteful
// because as we walk the subtree, we can build the directory structure, so we can skip the
// algorithm below that creates all of the parent directories. A better way would be to
// insert the directory and its parents and then, as we walk the subtree, clone the directory
// entries as we go. More efficient, but an entirely separate code path...
}
// Create the top directory structure (/_dr_/<datasetDirName>)
storeTopDirectory(snapshotFirestore, snapshotId, datasetDirName);
// Store the base entry under the datasetDir
FireStoreDirectoryEntry snapEntry = datasetEntry.copyEntryUnderNewPath(datasetDirName);
storeDirectoryEntry(snapshotFirestore, snapshotId, snapEntry);
// Now we walk up the *dataset* directory path, retrieving existing directories.
// For each directory, we make a new entry under the datasetDir path and store it.
// That keeps the directory file ids consistent
String lookupDirPath = makeLookupPath(datasetEntry.getPath());
for (String testPath = lookupDirPath; !testPath.isEmpty(); testPath = fireStoreUtils.getDirectoryPath(testPath)) {
DocumentSnapshot docSnap = lookupByPathNoXn(datasetFirestore, datasetId, testPath);
FireStoreDirectoryEntry datasetDir = docSnap.toObject(FireStoreDirectoryEntry.class);
FireStoreDirectoryEntry snapshotDir = datasetDir.copyEntryUnderNewPath(datasetDirName);
storeDirectoryEntry(snapshotFirestore, snapshotId, snapshotDir);
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.
the class FireStoreFileDao method lookupByFileId.
private DocumentSnapshot lookupByFileId(Firestore firestore, String collectionId, String fileId, Transaction xn) {
DocumentReference docRef = getFileDocRef(firestore, collectionId, fileId);
ApiFuture<DocumentSnapshot> docSnapFuture = xn.get(docRef);
try {
return docSnapFuture.get();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new FileSystemExecutionException("lookupByFileId - execution interrupted", ex);
} catch (ExecutionException ex) {
throw new FileSystemExecutionException("lookupByFileId - execution exception", ex);
}
}
Aggregations