Search in sources :

Example 11 with DocumentSnapshot

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);
    }
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 12 with DocumentSnapshot

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;
    }
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 13 with DocumentSnapshot

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);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 14 with DocumentSnapshot

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);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) NotImplementedException(bio.terra.common.exception.NotImplementedException)

Example 15 with DocumentSnapshot

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);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) FileSystemExecutionException(bio.terra.service.filedata.exception.FileSystemExecutionException) ExecutionException(java.util.concurrent.ExecutionException) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)103 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)73 Test (org.junit.Test)52 DocumentReference (com.google.cloud.firestore.DocumentReference)49 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)23 ExecutionException (java.util.concurrent.ExecutionException)13 CollectionReference (com.google.cloud.firestore.CollectionReference)12 Query (com.google.cloud.firestore.Query)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)10 SingleField (com.google.cloud.firestore.LocalFirestoreHelper.SingleField)9 WriteResult (com.google.cloud.firestore.WriteResult)9 ArrayList (java.util.ArrayList)8 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 HashMap (java.util.HashMap)6 BulkWriter (com.google.cloud.firestore.BulkWriter)5 City (com.example.firestore.snippets.model.City)4 Timestamp (com.google.cloud.Timestamp)4 FirestoreBundleTest.verifyNamedQuery (com.google.cloud.firestore.FirestoreBundleTest.verifyNamedQuery)4 AllSupportedTypes (com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes)4 WriteBatch (com.google.cloud.firestore.WriteBatch)4