forked from icatproject/topcat
-
Notifications
You must be signed in to change notification settings - Fork 2
Limit number of files that can be submitted, other bugfixes #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package org.icatproject.topcat; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.ListIterator; | ||
| import java.util.ArrayList; | ||
|
|
@@ -21,6 +23,12 @@ | |
|
|
||
| public class IcatClient { | ||
|
|
||
| public class DatafilesResponse { | ||
| public final List<Long> ids = new ArrayList<>(); | ||
| public final Set<String> missing = new HashSet<>(); | ||
| public long totalSize = 0L; | ||
| } | ||
|
|
||
| private Logger logger = LoggerFactory.getLogger(IcatClient.class); | ||
|
|
||
| private HttpClient httpClient; | ||
|
|
@@ -139,7 +147,7 @@ public String getFullName() throws TopcatException { | |
| * @throws TopcatException | ||
| */ | ||
| public JsonArray getDatasets(String visitId) throws TopcatException { | ||
| String query = "SELECT dataset.id, dataset.fileCount from Dataset dataset"; | ||
| String query = "SELECT dataset.id, dataset.fileCount, dataset.fileSize from Dataset dataset"; | ||
| query += " WHERE dataset.investigation.visitId = '" + visitId + "' ORDER BY dataset.id"; | ||
| return submitQuery(query); | ||
| } | ||
|
|
@@ -153,45 +161,56 @@ public JsonArray getDatasets(String visitId) throws TopcatException { | |
| * @throws TopcatException | ||
| * @throws UnsupportedEncodingException | ||
| */ | ||
| public List<Long> getDatafiles(List<String> files) throws TopcatException, UnsupportedEncodingException { | ||
| List<Long> datafileIds = new ArrayList<>(); | ||
| public DatafilesResponse getDatafiles(List<String> files) throws TopcatException, UnsupportedEncodingException { | ||
| DatafilesResponse response = new DatafilesResponse(); | ||
| if (files.size() == 0) { | ||
| // Ensure that we don't error when calling .next() below by returning early | ||
| return datafileIds; | ||
| return response; | ||
| } | ||
|
|
||
| // Total limit - "entityManager?sessionId=" - `sessionId` - "?query=" - `queryPrefix` - `querySuffix | ||
| // Limit is 1024 - 24 - 36 - 7 - 51 - 17 | ||
| // Limit is 1024 - 24 - 36 - 7 - 48 - 17 | ||
| int getUrlLimit = Integer.parseInt(Properties.getInstance().getProperty("getUrlLimit", "1024")); | ||
| int chunkLimit = getUrlLimit - 135; | ||
| String queryPrefix = "SELECT d.id from Datafile d WHERE d.location in ("; | ||
| int chunkLimit = getUrlLimit - 132; | ||
| String queryPrefix = "SELECT d from Datafile d WHERE d.location in ("; | ||
| String querySuffix = ") ORDER BY d.id"; | ||
| ListIterator<String> iterator = files.listIterator(); | ||
|
|
||
| String chunkedFiles = "'" + iterator.next() + "'"; | ||
| String file = iterator.next(); | ||
| String chunkedFiles = "'" + file + "'"; | ||
| response.missing.add(file); | ||
| int chunkSize = URLEncoder.encode(chunkedFiles, "UTF8").length(); | ||
| while (iterator.hasNext()) { | ||
| String file = "'" + iterator.next() + "'"; | ||
| int encodedFileLength = URLEncoder.encode(file, "UTF8").length(); | ||
| file = iterator.next(); | ||
| String quotedFile = "'" + file + "'"; | ||
| int encodedFileLength = URLEncoder.encode(quotedFile, "UTF8").length(); | ||
| if (chunkSize + 3 + encodedFileLength > chunkLimit) { | ||
| JsonArray jsonArray = submitQuery(queryPrefix + chunkedFiles + querySuffix); | ||
| for (JsonNumber datafileIdJsonNumber : jsonArray.getValuesAs(JsonNumber.class)) { | ||
| datafileIds.add(datafileIdJsonNumber.longValueExact()); | ||
| for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) { | ||
| JsonObject datafile = jsonObject.getJsonObject("Datafile"); | ||
| response.ids.add(datafile.getJsonNumber("id").longValueExact()); | ||
| response.missing.remove(datafile.getString("location")); | ||
| response.totalSize += datafile.getJsonNumber("fileSize").longValueExact(); | ||
| } | ||
|
|
||
| chunkedFiles = file; | ||
| chunkedFiles = quotedFile; | ||
| chunkSize = encodedFileLength; | ||
| response.missing.add(file); | ||
| } else { | ||
| chunkedFiles += "," + file; | ||
| chunkedFiles += "," + quotedFile; | ||
| chunkSize += 3 + encodedFileLength; // 3 is size of , when encoded as %2C | ||
| response.missing.add(file); | ||
| } | ||
| } | ||
| JsonArray jsonArray = submitQuery(queryPrefix + chunkedFiles + querySuffix); | ||
| for (JsonNumber datafileIdJsonNumber : jsonArray.getValuesAs(JsonNumber.class)) { | ||
| datafileIds.add(datafileIdJsonNumber.longValueExact()); | ||
| for (JsonObject jsonObject : jsonArray.getValuesAs(JsonObject.class)) { | ||
| JsonObject datafile = jsonObject.getJsonObject("Datafile"); | ||
| response.ids.add(datafile.getJsonNumber("id").longValueExact()); | ||
| response.missing.remove(datafile.getString("location")); | ||
| response.totalSize += datafile.getJsonNumber("fileSize").longValueExact(); | ||
| } | ||
|
|
||
| return datafileIds; | ||
| return response; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -209,6 +228,25 @@ public long getDatasetFileCount(long datasetId) throws TopcatException { | |
| return jsonArray.getJsonNumber(0).longValueExact(); | ||
| } | ||
|
|
||
| /** | ||
| * Utility method to get the fileSize (not size) of a Dataset by SELECTing its | ||
| * child Datafiles. Ideally the fileSize field should be used, this is a | ||
| * fallback option if that field is not set. | ||
| * | ||
| * @param datasetId ICAT Dataset.id | ||
| * @return The total size of Datafiles in the specified Dataset | ||
| * @throws TopcatException | ||
| */ | ||
| public long getDatasetFileSize(long datasetId) throws TopcatException { | ||
| String query = "SELECT datafile.fileSize FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId; | ||
|
||
| JsonArray jsonArray = submitQuery(query); | ||
| long size = 0L; | ||
| for (JsonNumber number : jsonArray.getValuesAs(JsonNumber.class)) { | ||
| size += number.longValueExact(); | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| /** | ||
| * Utility method for submitting an unencoded query to the entityManager | ||
| * endpoint, and returning the resultant JsonArray. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These 7 lines are a copy of those just above, so should go in a separate method to avoid duplication. There was some duplication previously but it is now worse with the use of the new response object and the additional values being populated.