Skip to content

Commit

Permalink
Merge pull request IQSS#9684 from ErykKul/9683_get_dataset_api_in_sin…
Browse files Browse the repository at this point in the history
…gle_query

Faster combined query for retrieving datasets via API
  • Loading branch information
sekmiller authored May 24, 2024
2 parents 2bf05c1 + fb4eb8b commit 1f9a682
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public Dataset findDeep(Object pk) {
.setHint("eclipselink.left-join-fetch", "o.files.dataFileTags")
.setHint("eclipselink.left-join-fetch", "o.files.fileMetadatas")
.setHint("eclipselink.left-join-fetch", "o.files.fileMetadatas.fileCategories")
//.setHint("eclipselink.left-join-fetch", "o.files.guestbookResponses")
.setHint("eclipselink.left-join-fetch", "o.files.fileMetadatas.varGroups")
//.setHint("eclipselink.left-join-fetch", "o.files.guestbookResponses
.setHint("eclipselink.left-join-fetch", "o.files.embargo")
.setHint("eclipselink.left-join-fetch", "o.files.retention")
.setHint("eclipselink.left-join-fetch", "o.files.fileAccessRequests")
Expand Down
40 changes: 30 additions & 10 deletions src/main/java/edu/harvard/iq/dataverse/api/AbstractApiBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import edu.harvard.iq.dataverse.engine.command.exception.RateLimitCommandException;
import edu.harvard.iq.dataverse.externaltools.ExternalToolServiceBean;
import edu.harvard.iq.dataverse.license.LicenseServiceBean;
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
import edu.harvard.iq.dataverse.locality.StorageSiteServiceBean;
import edu.harvard.iq.dataverse.metrics.MetricsServiceBean;
import edu.harvard.iq.dataverse.search.savedsearch.SavedSearchServiceBean;
Expand Down Expand Up @@ -371,31 +372,50 @@ protected DataverseLinkingDataverse findDataverseLinkingDataverseOrDie(String da
}

protected Dataset findDatasetOrDie(String id) throws WrappedResponse {
return findDatasetOrDie(id, false);
}

protected Dataset findDatasetOrDie(String id, boolean deep) throws WrappedResponse {
Long datasetId;
Dataset dataset;
if (id.equals(PERSISTENT_ID_KEY)) {
String persistentId = getRequestParameter(PERSISTENT_ID_KEY.substring(1));
if (persistentId == null) {
throw new WrappedResponse(
badRequest(BundleUtil.getStringFromBundle("find.dataset.error.dataset_id_is_null", Collections.singletonList(PERSISTENT_ID_KEY.substring(1)))));
}
dataset = datasetSvc.findByGlobalId(persistentId);
if (dataset == null) {
throw new WrappedResponse(notFound(BundleUtil.getStringFromBundle("find.dataset.error.dataset.not.found.persistentId", Collections.singletonList(persistentId))));
GlobalId globalId;
try {
globalId = PidUtil.parseAsGlobalID(persistentId);
} catch (IllegalArgumentException e) {
throw new WrappedResponse(
badRequest(BundleUtil.getStringFromBundle("find.dataset.error.dataset.not.found.bad.id", Collections.singletonList(persistentId))));
}
datasetId = dvObjSvc.findIdByGlobalId(globalId, DvObject.DType.Dataset);
if (datasetId == null) {
datasetId = dvObjSvc.findIdByAltGlobalId(globalId, DvObject.DType.Dataset);
}
if (datasetId == null) {
throw new WrappedResponse(
notFound(BundleUtil.getStringFromBundle("find.dataset.error.dataset_id_is_null", Collections.singletonList(PERSISTENT_ID_KEY.substring(1)))));
}
return dataset;

} else {
try {
dataset = datasetSvc.find(Long.parseLong(id));
if (dataset == null) {
throw new WrappedResponse(notFound(BundleUtil.getStringFromBundle("find.dataset.error.dataset.not.found.id", Collections.singletonList(id))));
}
return dataset;
datasetId = Long.parseLong(id);
} catch (NumberFormatException nfe) {
throw new WrappedResponse(
badRequest(BundleUtil.getStringFromBundle("find.dataset.error.dataset.not.found.bad.id", Collections.singletonList(id))));
}
}
if (deep) {
dataset = datasetSvc.findDeep(datasetId);
} else {
dataset = datasetSvc.find(datasetId);
}
if (dataset == null) {
throw new WrappedResponse(notFound(BundleUtil.getStringFromBundle("find.dataset.error.dataset.not.found.id", Collections.singletonList(id))));
}
return dataset;
}

protected DatasetVersion findDatasetVersionOrDie(final DataverseRequest req, String versionNumber, final Dataset ds, boolean includeDeaccessioned, boolean checkPermsWhenDeaccessioned) throws WrappedResponse {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public interface DsVersionHandler<T> {
@Path("{id}")
public Response getDataset(@Context ContainerRequestContext crc, @PathParam("id") String id, @Context UriInfo uriInfo, @Context HttpHeaders headers, @Context HttpServletResponse response, @QueryParam("returnOwners") boolean returnOwners) {
return response( req -> {
final Dataset retrieved = execCommand(new GetDatasetCommand(req, findDatasetOrDie(id)));
final Dataset retrieved = execCommand(new GetDatasetCommand(req, findDatasetOrDie(id, true)));
final DatasetVersion latest = execCommand(new GetLatestAccessibleDatasetVersionCommand(req, retrieved));
final JsonObjectBuilder jsonbuilder = json(retrieved, returnOwners);
//Report MDC if this is a released version (could be draft if user has access, or user may not have access at all and is not getting metadata beyond the minimum)
Expand Down Expand Up @@ -480,7 +480,7 @@ public Response getVersionFiles(@Context ContainerRequestContext crc,
@Context UriInfo uriInfo,
@Context HttpHeaders headers) {
return response(req -> {
DatasetVersion datasetVersion = getDatasetVersionOrDie(req, versionId, findDatasetOrDie(datasetId), uriInfo, headers, includeDeaccessioned);
DatasetVersion datasetVersion = getDatasetVersionOrDie(req, versionId, findDatasetOrDie(datasetId, false), uriInfo, headers, includeDeaccessioned);
DatasetVersionFilesServiceBean.FileOrderCriteria fileOrderCriteria;
try {
fileOrderCriteria = orderCriteria != null ? DatasetVersionFilesServiceBean.FileOrderCriteria.valueOf(orderCriteria) : DatasetVersionFilesServiceBean.FileOrderCriteria.NameAZ;
Expand Down

0 comments on commit 1f9a682

Please sign in to comment.