From 371d16f1b9fc5b583b7a0c593bc5c6cc44049c3c Mon Sep 17 00:00:00 2001 From: Elizabeth Paige Harper Date: Mon, 9 Oct 2023 18:55:13 -0400 Subject: [PATCH] add file summary queries --- .../org/veupathdb/vdi/lib/db/cache/CacheDB.kt | 12 +++++++ .../lib/db/cache/model/DatasetFileSummary.kt | 6 ++++ .../sql/select/select-install-file-count.kt | 23 ++++++++++++ .../select/select-install-file-summaries.kt | 35 +++++++++++++++++++ .../sql/select/select-upload-file-count.kt | 23 ++++++++++++ .../select/select-upload-file-summaries.kt | 35 +++++++++++++++++++ 6 files changed, 134 insertions(+) create mode 100644 components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/model/DatasetFileSummary.kt create mode 100644 components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-count.kt create mode 100644 components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-install-file-summaries.kt create mode 100644 components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-count.kt create mode 100644 components/cache-db/src/main/kotlin/org/veupathdb/vdi/lib/db/cache/sql/select/select-upload-file-summaries.kt 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