Skip to content

Commit

Permalink
add file summary queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxcapades committed Oct 9, 2023
1 parent 1847a2a commit 371d16f
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DatasetID>) =
connection.use { it.selectInstallFileSummaries(datasetIDs) }

fun selectUploadFileCount(datasetID: DatasetID) =
connection.use { it.selectUploadFileCount(datasetID) }

fun selectUploadFileSummaries(datasetIDs: List<DatasetID>) =
connection.use { it.selectUploadFileSummaries(datasetIDs) }

fun selectDatasetList(query: DatasetListQuery): List<DatasetRecord> {
log.debug("selecting dataset list for user {}", query.userID)
return connection.use { it.selectDatasetList(query) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.veupathdb.vdi.lib.db.cache.model

data class DatasetFileSummary(
val count: UInt,
val size: ULong,
)
Original file line number Diff line number Diff line change
@@ -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) }
}
Original file line number Diff line number Diff line change
@@ -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<DatasetID>): Map<DatasetID, DatasetFileSummary> =
withPreparedStatement(SQL) {
val idArray = Array<Any>(datasetIDs.size) { i -> datasetIDs[i].toString() }
setArray(1, createArrayOf("VARCHAR", idArray))
withResults {
val out = HashMap<DatasetID, DatasetFileSummary>(datasetIDs.size)

while (next()) {
out[getDatasetID("dataset_id")] = DatasetFileSummary(getInt("count").toUInt(), getLong("size").toULong())
}

out
}
}
Original file line number Diff line number Diff line change
@@ -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) }
}
Original file line number Diff line number Diff line change
@@ -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<DatasetID>): Map<DatasetID, DatasetFileSummary> =
withPreparedStatement(SQL) {
val idArray = Array<Any>(datasetIDs.size) { i -> datasetIDs[i].toString() }
setArray(1, createArrayOf("VARCHAR", idArray))
withResults {
val out = HashMap<DatasetID, DatasetFileSummary>(datasetIDs.size)

while (next()) {
out[getDatasetID("dataset_id")] = DatasetFileSummary(getInt("count").toUInt(), getLong("size").toULong())
}

out
}
}

0 comments on commit 371d16f

Please sign in to comment.