-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: move version downloads to version queries
- Loading branch information
1 parent
9448e2b
commit b0c1b1b
Showing
10 changed files
with
121 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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
56 changes: 56 additions & 0 deletions
56
backend/src/main/java/io/papermc/hangar/db/mappers/VersionDownloadsMapper.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.papermc.hangar.db.mappers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.papermc.hangar.model.api.project.version.FileInfo; | ||
import io.papermc.hangar.model.api.project.version.PlatformVersionDownload; | ||
import io.papermc.hangar.model.common.Platform; | ||
import io.papermc.hangar.service.internal.file.FileService; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import org.jdbi.v3.core.mapper.ColumnMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class VersionDownloadsMapper implements ColumnMapper<Map<Platform, PlatformVersionDownload>> { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final FileService fileService; | ||
|
||
public VersionDownloadsMapper(final ObjectMapper objectMapper, final FileService fileService) { | ||
this.objectMapper = objectMapper; | ||
this.fileService = fileService; | ||
} | ||
|
||
@Override | ||
public Map<Platform, PlatformVersionDownload> map(final ResultSet r, final int columnNumber, final StatementContext ctx) throws SQLException { | ||
final String raw = r.getString(columnNumber); | ||
final Map<Platform, PlatformVersionDownload> result = new EnumMap<>(Platform.class); | ||
final String[] projectNamespace = (String[]) r.getArray("project_namespace").getArray(); | ||
final String version = r.getString("version_string"); | ||
try { | ||
final JsonNode parsedDownloads = this.objectMapper.readTree(raw); | ||
for (final JsonNode download : parsedDownloads) { | ||
final JsonNode platforms = download.get("platforms"); | ||
final String fileName = download.get("file_name").asText(); | ||
final long fileSize = download.get("file_size").asLong(); | ||
final String hash = download.get("hash").asText(); | ||
final String externalUrl = download.get("external_url").asText(); | ||
final FileInfo fileInfo = "null".equals(fileName) ? null : new FileInfo(fileName, fileSize, hash); | ||
final Platform downloadPlatform = Platform.values()[download.get("download_platform").asInt()]; | ||
final String downloadUrl = fileInfo != null ? this.fileService.getVersionDownloadUrl(projectNamespace[0], projectNamespace[1], version, downloadPlatform, fileInfo.getName()) : null; | ||
final PlatformVersionDownload pvd = new PlatformVersionDownload(fileInfo, "null".equals(externalUrl) ? null : externalUrl, downloadUrl); | ||
for (final JsonNode platformId : platforms) { | ||
result.put(Platform.values()[platformId.asInt()], pvd); | ||
} | ||
} | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return result; | ||
} | ||
} |
This file contains 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 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 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 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 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.