diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/CacheDB.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/CacheDB.kt index 445bdada..e960a99b 100644 --- a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/CacheDB.kt +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/CacheDB.kt @@ -73,6 +73,18 @@ object CacheDB { return connection.use { it.selectUploadFiles(datasetID) } } + fun selectInstallFileCount(datasetID: DatasetID) = + connection.use { it.selectInstallFileCount(datasetID) } + + fun selectInstallFileSummaries(datasetIDs: List) = + connection.use { it.selectInstallFileSummaries(datasetIDs) } + + fun selectUploadFileCount(datasetID: DatasetID) = + connection.use { it.selectUploadFileCount(datasetID) } + + fun selectUploadFileSummaries(datasetIDs: List) = + connection.use { it.selectUploadFileSummaries(datasetIDs) } + fun selectDatasetList(query: DatasetListQuery): List { log.debug("selecting dataset list for user {}", query.userID) return connection.use { it.selectDatasetList(query) } diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/model/DatasetFileSummary.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/model/DatasetFileSummary.kt new file mode 100644 index 00000000..d9dd12e9 --- /dev/null +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/model/DatasetFileSummary.kt @@ -0,0 +1,6 @@ +package org.veupathdb.vdi.lib.db.cache.model + +data class DatasetFileSummary( + val count: UInt, + val size: ULong, +) diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-count.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-count.kt new file mode 100644 index 00000000..1d5c3d41 --- /dev/null +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-count.kt @@ -0,0 +1,23 @@ +package org.veupathdb.vdi.lib.db.cache.sql.select + +import org.veupathdb.vdi.lib.common.field.DatasetID +import org.veupathdb.vdi.lib.db.cache.util.setDatasetID +import org.veupathdb.vdi.lib.db.cache.util.withPreparedStatement +import org.veupathdb.vdi.lib.db.cache.util.withResults +import java.sql.Connection + +// language=postgresql +private const val SQL = """ +SELECT + count(1) +FROM + vdi.install_files +WHERE + dataset_id = ? +""" + +internal fun Connection.selectInstallFileCount(datasetID: DatasetID) = + withPreparedStatement(SQL) { + setDatasetID(1, datasetID) + withResults { next(); getInt(1) } + } \ No newline at end of file diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-summaries.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-summaries.kt new file mode 100644 index 00000000..f4e653c3 --- /dev/null +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-summaries.kt @@ -0,0 +1,35 @@ +package org.veupathdb.vdi.lib.db.cache.sql.select + +import org.veupathdb.vdi.lib.common.field.DatasetID +import org.veupathdb.vdi.lib.db.cache.model.DatasetFileSummary +import org.veupathdb.vdi.lib.db.cache.util.* +import java.sql.Connection + +// language=postgresql +private const val SQL = """ +SELECT + dataset_id +, count(1) AS count +, sum(file_size) AS size +FROM + vdi.install_files AS v + INNER JOIN unnest(?::VARCHAR[]) AS dataset_ids(dataset_id) + USING(dataset_id) +GROUP BY + dataset_id +""" + +internal fun Connection.selectInstallFileSummaries(datasetIDs: List): Map = + withPreparedStatement(SQL) { + val idArray = Array(datasetIDs.size) { i -> datasetIDs[i].toString() } + setArray(1, createArrayOf("VARCHAR", idArray)) + withResults { + val out = HashMap(datasetIDs.size) + + while (next()) { + out[getDatasetID("dataset_id")] = DatasetFileSummary(getInt("count").toUInt(), getLong("size").toULong()) + } + + out + } + } \ No newline at end of file diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-count.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-count.kt new file mode 100644 index 00000000..1dbc64b8 --- /dev/null +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-count.kt @@ -0,0 +1,23 @@ +package org.veupathdb.vdi.lib.db.cache.sql.select + +import org.veupathdb.vdi.lib.common.field.DatasetID +import org.veupathdb.vdi.lib.db.cache.util.setDatasetID +import org.veupathdb.vdi.lib.db.cache.util.withPreparedStatement +import org.veupathdb.vdi.lib.db.cache.util.withResults +import java.sql.Connection + +// language=postgresql +private const val SQL = """ +SELECT + count(1) +FROM + vdi.upload_files +WHERE + dataset_id = ? +""" + +internal fun Connection.selectUploadFileCount(datasetID: DatasetID) = + withPreparedStatement(SQL) { + setDatasetID(1, datasetID) + withResults { next(); getInt(1) } + } \ No newline at end of file diff --git a/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-summaries.kt b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-summaries.kt new file mode 100644 index 00000000..f972954c --- /dev/null +++ b/components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-summaries.kt @@ -0,0 +1,35 @@ +package org.veupathdb.vdi.lib.db.cache.sql.select + +import org.veupathdb.vdi.lib.common.field.DatasetID +import org.veupathdb.vdi.lib.db.cache.model.DatasetFileSummary +import org.veupathdb.vdi.lib.db.cache.util.* +import java.sql.Connection + +// language=postgresql +private const val SQL = """ +SELECT + dataset_id +, count(1) AS count +, sum(v.file_size) AS size +FROM + vdi.upload_files AS v + INNER JOIN unnest(?::VARCHAR[]) AS dataset_ids(dataset_id) + USING(dataset_id) +GROUP BY + dataset_id +""" + +internal fun Connection.selectUploadFileSummaries(datasetIDs: List): Map = + withPreparedStatement(SQL) { + val idArray = Array(datasetIDs.size) { i -> datasetIDs[i].toString() } + setArray(1, createArrayOf("VARCHAR", idArray)) + withResults { + val out = HashMap(datasetIDs.size) + + while (next()) { + out[getDatasetID("dataset_id")] = DatasetFileSummary(getInt("count").toUInt(), getLong("size").toULong()) + } + + out + } + } \ No newline at end of file