Search in sources :

Example 1 with DatasetDataProject

use of bio.terra.service.dataset.DatasetDataProject in project jade-data-repo by DataBiosphere.

the class CreateDatasetPrimaryDataStep method undoStep.

@Override
public StepResult undoStep(FlightContext context) throws InterruptedException {
    Dataset dataset = getDataset(context);
    // get the cloud project for the dataset if it exists
    Optional<DatasetDataProject> optDataProject = dataLocationService.getProject(dataset);
    if (optDataProject.isPresent()) {
        // there can only be primary data to delete if a cloud project exists for the dataset
        pdao.deleteDataset(dataset);
    }
    return StepResult.getStepResultSuccess();
}
Also used : Dataset(bio.terra.service.dataset.Dataset) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject)

Example 2 with DatasetDataProject

use of bio.terra.service.dataset.DatasetDataProject in project jade-data-repo by DataBiosphere.

the class FireStoreDao method lookupFile.

public FireStoreFile lookupFile(Dataset dataset, String fileId) {
    DatasetDataProject dataProject = dataLocationService.getProjectOrThrow(dataset);
    Firestore firestore = FireStoreProject.get(dataProject.getGoogleProjectId()).getFirestore();
    String datasetId = dataset.getId().toString();
    return fileDao.retrieveFileMetadata(firestore, datasetId, fileId);
}
Also used : Firestore(com.google.cloud.firestore.Firestore) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject)

Example 3 with DatasetDataProject

use of bio.terra.service.dataset.DatasetDataProject in project jade-data-repo by DataBiosphere.

the class FireStoreDependencyDao method storeSnapshotFileDependency.

public void storeSnapshotFileDependency(Dataset dataset, String snapshotId, String fileId) {
    DatasetDataProject dataProject = dataLocationService.getProjectOrThrow(dataset);
    FireStoreProject fireStoreProject = FireStoreProject.get(dataProject.getGoogleProjectId());
    String dependencyCollectionName = getDatasetDependencyId(dataset.getId().toString());
    CollectionReference depColl = fireStoreProject.getFirestore().collection(dependencyCollectionName);
    ApiFuture<Void> transaction = fireStoreProject.getFirestore().runTransaction(xn -> {
        Query query = depColl.whereEqualTo("fileId", fileId).whereEqualTo("snapshotId", snapshotId);
        ApiFuture<QuerySnapshot> querySnapshot = query.get();
        // There should be zero or one documents. Since we keep track of greater than one reference using the
        // reference count.
        List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
        switch(documents.size()) {
            case 0:
                {
                    // no dependency yet. Let's make one
                    FireStoreDependency fireStoreDependency = new FireStoreDependency().snapshotId(snapshotId).fileId(fileId).refCount(1L);
                    DocumentReference docRef = depColl.document();
                    xn.set(docRef, fireStoreDependency);
                    break;
                }
            case 1:
                {
                    // existing dependency; increment the reference count
                    QueryDocumentSnapshot docSnap = documents.get(0);
                    FireStoreDependency fireStoreDependency = docSnap.toObject(FireStoreDependency.class);
                    fireStoreDependency.refCount(fireStoreDependency.getRefCount() + 1);
                    xn.set(docSnap.getReference(), fireStoreDependency);
                    break;
                }
            default:
                throw new FileSystemCorruptException("Found more than one document for a file dependency");
        }
        return null;
    });
    fireStoreUtils.transactionGet("store dependency", transaction);
}
Also used : Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) FileSystemCorruptException(bio.terra.service.filedata.exception.FileSystemCorruptException) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 4 with DatasetDataProject

use of bio.terra.service.dataset.DatasetDataProject 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 5 with DatasetDataProject

use of bio.terra.service.dataset.DatasetDataProject in project jade-data-repo by DataBiosphere.

the class FireStoreDependencyDao method removeSnapshotFileDependency.

public void removeSnapshotFileDependency(Dataset dataset, String snapshotId, String fileId) {
    DatasetDataProject dataProject = dataLocationService.getProjectOrThrow(dataset);
    FireStoreProject fireStoreProject = FireStoreProject.get(dataProject.getGoogleProjectId());
    String dependencyCollectionName = getDatasetDependencyId(dataset.getId().toString());
    CollectionReference depColl = fireStoreProject.getFirestore().collection(dependencyCollectionName);
    ApiFuture<Void> transaction = fireStoreProject.getFirestore().runTransaction(xn -> {
        Query query = depColl.whereEqualTo("fileId", fileId).whereEqualTo("snapshotId", snapshotId);
        ApiFuture<QuerySnapshot> querySnapshot = query.get();
        List<QueryDocumentSnapshot> documents = querySnapshot.get().getDocuments();
        if (documents.size() == 0) {
            // No file - nothing to delete
            return null;
        }
        if (documents.size() > 1) {
            throw new FileSystemCorruptException("Found more than one document for a file dependency");
        }
        QueryDocumentSnapshot docSnap = documents.get(0);
        FireStoreDependency fireStoreDependency = docSnap.toObject(FireStoreDependency.class);
        if (fireStoreDependency.getRefCount() <= 1) {
            xn.delete(docSnap.getReference());
        } else {
            fireStoreDependency.refCount(fireStoreDependency.getRefCount() - 1);
            xn.set(docSnap.getReference(), fireStoreDependency);
        }
        return null;
    });
    fireStoreUtils.transactionGet("delete dependency", transaction);
}
Also used : Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DatasetDataProject(bio.terra.service.dataset.DatasetDataProject) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) FileSystemCorruptException(bio.terra.service.filedata.exception.FileSystemCorruptException)

Aggregations

DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)20 Firestore (com.google.cloud.firestore.Firestore)10 CollectionReference (com.google.cloud.firestore.CollectionReference)6 Query (com.google.cloud.firestore.Query)5 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)4 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)4 Dataset (bio.terra.service.dataset.Dataset)2 DatasetDataProjectSummary (bio.terra.service.dataset.DatasetDataProjectSummary)2 FileSystemCorruptException (bio.terra.service.filedata.exception.FileSystemCorruptException)2 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)2 GoogleProjectResource (bio.terra.service.resourcemanagement.google.GoogleProjectResource)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 ExecutionException (java.util.concurrent.ExecutionException)2 DataProjectNotFoundException (bio.terra.service.resourcemanagement.exception.DataProjectNotFoundException)1 GoogleResourceNotFoundException (bio.terra.service.resourcemanagement.exception.GoogleResourceNotFoundException)1 GoogleProjectRequest (bio.terra.service.resourcemanagement.google.GoogleProjectRequest)1 SnapshotDataProject (bio.terra.service.snapshot.SnapshotDataProject)1 DocumentReference (com.google.cloud.firestore.DocumentReference)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1