Skip to content

Commit

Permalink
switch to smaller, simpler download size method #6118
Browse files Browse the repository at this point in the history
Also, revert changes to GetDatasetStorageSizeCommand
and DatasetServiceBean since we won't be using them.
  • Loading branch information
pdurbin committed Jul 16, 2020
1 parent 7e79f1b commit c13a5cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 77 deletions.
24 changes: 4 additions & 20 deletions src/main/java/edu/harvard/iq/dataverse/DatasetPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2923,29 +2923,13 @@ public void setSelectedNonDownloadallableFiles(List<FileMetadata> selectedNonDow
}

public String getSizeOfDataset() {
boolean countCachedFiles = false;
boolean useOrigFileSize = false;
boolean dbOnly = true;
GetDatasetStorageSizeCommand cmd = new GetDatasetStorageSizeCommand(dvRequestService.getDataverseRequest(), dataset, countCachedFiles, dbOnly, useOrigFileSize, GetDatasetStorageSizeCommand.Mode.DOWNLOAD, workingVersion);
try {
long bytes = commandEngine.submit(cmd);
return FileSizeChecker.bytesToHumanReadable(bytes);
} catch (CommandException ex) {
return "";
}
boolean original = false;
return DatasetUtil.getDownloadSize(workingVersion, original);
}

public String getSizeOfDatasetOrig() {
boolean countCachedFiles = false;
boolean useOrigFileSize = true;
boolean dbOnly = true;
GetDatasetStorageSizeCommand cmd = new GetDatasetStorageSizeCommand(dvRequestService.getDataverseRequest(), dataset, countCachedFiles, useOrigFileSize, dbOnly, GetDatasetStorageSizeCommand.Mode.DOWNLOAD, workingVersion);
try {
long bytes = commandEngine.submit(cmd);
return FileSizeChecker.bytesToHumanReadable(bytes);
} catch (CommandException ex) {
return "";
}
boolean original = true;
return DatasetUtil.getDownloadSize(workingVersion, original);
}

public void validateAllFilesForDownloadArchival() {
Expand Down
56 changes: 16 additions & 40 deletions src/main/java/edu/harvard/iq/dataverse/DatasetServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -880,27 +880,20 @@ public long findStorageSize(Dataset dataset, boolean countCachedExtras) throws I
return findStorageSize(dataset, countCachedExtras, GetDatasetStorageSizeCommand.Mode.STORAGE, null);
}

public long findStorageSize(Dataset dataset, boolean countCachedExtras, GetDatasetStorageSizeCommand.Mode mode, DatasetVersion version) throws IOException {
boolean useOrigFileSize = false;
boolean dbOnly = false;
return findStorageSize(dataset, countCachedExtras, useOrigFileSize, dbOnly, mode, version);
}
/**
* Returns the total byte size of the files in this dataset
*
* @param dataset
* @param countCachedExtras boolean indicating if the cached disposable extras should also be counted
* @param useOrigFileSize allows original tabular file size to be used instead of derived archival file
* @param mode String indicating whether we are getting the result for storage (entire dataset) or download version based
* @param version optional param for dataset version
* @param dbOnly only get bytes from database, no countCachedExtras processing
* @return total size
* @throws IOException if it can't access the objects via StorageIO
* (in practice, this can only happen when called with countCachedExtras=true; when run in the
* default mode, the method doesn't need to access the storage system, as the
* sizes of the main files are recorded in the database)
*/
public long findStorageSize(Dataset dataset, boolean countCachedExtras, boolean useOrigFileSize, boolean dbOnly, GetDatasetStorageSizeCommand.Mode mode, DatasetVersion version) throws IOException {
public long findStorageSize(Dataset dataset, boolean countCachedExtras, GetDatasetStorageSizeCommand.Mode mode, DatasetVersion version) throws IOException {
long total = 0L;

if (dataset.isHarvested()) {
Expand All @@ -920,44 +913,27 @@ public long findStorageSize(Dataset dataset, boolean countCachedExtras, boolean


//CACHED EXTRAS FOR DOWNLOAD?


for (DataFile datafile : filesToTally) {
if (datafile.isTabularData()) {
if (useOrigFileSize) {
// count the size of the stored original, rather than the main tab-delimited file
Long originalFileSize = datafile.getDataTable().getOriginalFileSize();
if (originalFileSize != null) {
total += originalFileSize;
}
} else {
total += datafile.getFilesize();
}
} else {
total += datafile.getFilesize();
}

if (dbOnly) {
// Skip the rest. Don't add any more to the total. No cached extras.
continue;
}

if (!countCachedExtras) {
if (datafile.isTabularData()) {
// count the size of the stored original, in addition to the main tab-delimited file:
Long originalFileSize = datafile.getDataTable().getOriginalFileSize();
if (originalFileSize != null) {
total += originalFileSize;
if (!countCachedExtras) {
if (datafile.isTabularData()) {
// count the size of the stored original, in addition to the main tab-delimited file:
Long originalFileSize = datafile.getDataTable().getOriginalFileSize();
if (originalFileSize != null) {
total += originalFileSize;
}
}
} else {
StorageIO<DataFile> storageIO = datafile.getStorageIO();
for (String cachedFileTag : storageIO.listAuxObjects()) {
total += storageIO.getAuxObjectSize(cachedFileTag);
}
}
} else {
StorageIO<DataFile> storageIO = datafile.getStorageIO();
for (String cachedFileTag : storageIO.listAuxObjects()) {
total += storageIO.getAuxObjectSize(cachedFileTag);
}
}
}


// and finally,
if (countCachedExtras) {
// count the sizes of the files cached for the dataset itself
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/dataset/DatasetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO;
import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO;
import static edu.harvard.iq.dataverse.dataaccess.DataAccess.getStorageIO;
import edu.harvard.iq.dataverse.datasetutility.FileSizeChecker;

public class DatasetUtil {

Expand Down Expand Up @@ -440,4 +441,24 @@ public static List<DatasetField> getDatasetSummaryFields(DatasetVersion datasetV
return datasetFields;
}

/**
* Given a dataset version, return it's size in human readable units such as
* 42.9 MB.There is a GetDatasetStorageSizeCommand but it's overly complex
* for the use case.
*
* @param original Use the original file size rather than the archival file
* size for tabular files.
*/
public static String getDownloadSize(DatasetVersion dsv, boolean original) {
long bytes = 0l;
for (FileMetadata fileMetadata : dsv.getFileMetadatas()) {
DataFile dataFile = fileMetadata.getDataFile();
if (original && dataFile.isTabularData()) {
bytes += dataFile.getOriginalFileSize();
} else {
bytes += dataFile.getFilesize();
}
}
return FileSizeChecker.bytesToHumanReadable(bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class GetDatasetStorageSizeCommand extends AbstractCommand<Long> {

private final Dataset dataset;
private final Boolean countCachedFiles;
private final boolean useOrigFileSize;
private final boolean dbOnly;
private final Mode mode;
private final DatasetVersion version;

Expand All @@ -46,8 +44,6 @@ public GetDatasetStorageSizeCommand(DataverseRequest aRequest, Dataset target) {
super(aRequest, target);
dataset = target;
countCachedFiles = false;
this.useOrigFileSize = false;
this.dbOnly = false;
mode = Mode.DOWNLOAD;
version = null;
}
Expand All @@ -56,18 +52,6 @@ public GetDatasetStorageSizeCommand(DataverseRequest aRequest, Dataset target, b
super(aRequest, target);
dataset = target;
this.countCachedFiles = countCachedFiles;
this.useOrigFileSize = false;
this.dbOnly = false;
this.mode = mode;
this.version = version;
}

public GetDatasetStorageSizeCommand(DataverseRequest aRequest, Dataset target, boolean countCachedFiles, boolean useOrigFileSize, boolean dbOnly, Mode mode, DatasetVersion version) {
super(aRequest, target);
dataset = target;
this.countCachedFiles = countCachedFiles;
this.useOrigFileSize = useOrigFileSize;
this.dbOnly = dbOnly;
this.mode = mode;
this.version = version;
}
Expand All @@ -82,7 +66,7 @@ public Long execute(CommandContext ctxt) throws CommandException {
}

try {
return ctxt.datasets().findStorageSize(dataset, countCachedFiles, useOrigFileSize, dbOnly, mode, version);
return ctxt.datasets().findStorageSize(dataset, countCachedFiles, mode, version);
} catch (IOException ex) {
throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.datasize.ioerror"), this);
}
Expand Down

0 comments on commit c13a5cc

Please sign in to comment.