Search in sources :

Example 61 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project spring-cloud-gcp by spring-cloud.

the class FirestoreDefaultClassMapper method entityToDocument.

public <T> Document entityToDocument(T entity, String documentResourceName) {
    DocumentSnapshot documentSnapshot = INTERNAL.snapshotFromObject(NOT_USED_PATH, entity);
    Map<String, Value> valuesMap = INTERNAL.protoFromSnapshot(documentSnapshot);
    return Document.newBuilder().putAllFields(valuesMap).setName(documentResourceName).build();
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Value(com.google.firestore.v1.Value)

Example 62 with DocumentSnapshot

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

Example 63 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.

the class FireStoreDirectoryDao method lookupByFilePath.

private DocumentSnapshot lookupByFilePath(Firestore firestore, String collectionId, String lookupPath, Transaction xn) {
    try {
        DocumentReference docRef = firestore.collection(collectionId).document(encodePathAsFirestoreDocumentName(lookupPath));
        ApiFuture<DocumentSnapshot> docSnapFuture = xn.get(docRef);
        return docSnapFuture.get();
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new FileSystemExecutionException("lookupByEntryPath - execution interrupted", ex);
    } catch (AbortedException | ExecutionException ex) {
        throw handleExecutionException("lookupByEntryPath", ex);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) AbortedException(com.google.api.gax.rpc.AbortedException) 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)

Example 64 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.

the class FireStoreDirectoryDao method createDirectoryEntry.

// Note that this does not test for duplicates. If invoked on an existing path it will overwrite
// the entry. Existence checking is handled at upper layers.
public void createDirectoryEntry(Firestore firestore, String collectionId, FireStoreDirectoryEntry createEntry) {
    ApiFuture<Void> transaction = firestore.runTransaction(xn -> {
        List<FireStoreDirectoryEntry> createList = new ArrayList<>();
        // Walk up the lookup directory path, finding missing directories we get to an existing one
        // We will create the ROOT_DIR_NAME directory here if it does not exist.
        String lookupDirPath = makeLookupPath(createEntry.getPath());
        for (String testPath = lookupDirPath; !testPath.isEmpty(); testPath = fireStoreUtils.getDirectoryPath(testPath)) {
            // !!! In this case we are using a lookup path
            DocumentSnapshot docSnap = lookupByFilePath(firestore, collectionId, testPath, xn);
            if (docSnap.exists()) {
                break;
            }
            FireStoreDirectoryEntry dirToCreate = makeDirectoryEntry(testPath);
            createList.add(dirToCreate);
        }
        for (FireStoreDirectoryEntry dirToCreate : createList) {
            xn.set(getDocRef(firestore, collectionId, dirToCreate), dirToCreate);
        }
        xn.set(getDocRef(firestore, collectionId, createEntry), createEntry);
        return null;
    });
    fireStoreUtils.transactionGet("createFileRef", transaction);
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) ArrayList(java.util.ArrayList)

Example 65 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project jade-data-repo by DataBiosphere.

the class FireStoreDirectoryDao method storeDirectoryEntry.

// Non-transactional store of a directory entry
private void storeDirectoryEntry(Firestore firestore, String collectionId, FireStoreDirectoryEntry entry) {
    try {
        DocumentReference newRef = getDocRef(firestore, collectionId, entry);
        ApiFuture<DocumentSnapshot> newSnapFuture = newRef.get();
        DocumentSnapshot newSnap = newSnapFuture.get();
        if (!newSnap.exists()) {
            ApiFuture<WriteResult> writeFuture = newRef.set(entry);
            writeFuture.get();
        }
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new FileSystemExecutionException("storeDirectoryEntry - execution interrupted", ex);
    } catch (AbortedException | ExecutionException ex) {
        throw handleExecutionException("storeDirectoryEntry", ex);
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) AbortedException(com.google.api.gax.rpc.AbortedException) 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