CORDA-3191: When querying for attachment ids now only retrieve the attachment id columm not whole object. (#6199)

This commit is contained in:
Adel El-Beik 2020-04-30 11:52:47 +01:00 committed by GitHub
parent ecf74053c0
commit 9bcb9b2f54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -458,17 +458,38 @@ class NodeAttachmentService @JvmOverloads constructor(
}
}
// TODO do not retrieve whole attachments only to return ids - https://r3-cev.atlassian.net/browse/CORDA-3191 raised to address this
override fun queryAttachments(criteria: AttachmentQueryCriteria, sorting: AttachmentSort?): List<AttachmentId> {
log.info("Attachment query criteria: $criteria, sorting: $sorting")
return database.transaction {
createAttachmentsQuery(
createAttachmentsIdsQuery(
criteria,
sorting
).resultList.map { AttachmentId.parse(it.attId) }
).resultList.map { AttachmentId.parse(it) }
}
}
private fun createAttachmentsIdsQuery(
criteria: AttachmentQueryCriteria,
sorting: AttachmentSort?
): Query<String> {
val session = currentDBSession()
val criteriaBuilder = session.criteriaBuilder
val criteriaQuery = criteriaBuilder.createQuery(String::class.java)
val root = criteriaQuery.from(DBAttachment::class.java)
criteriaQuery.select(root.get("${DBAttachment::attId.name}"))
val criteriaParser = HibernateAttachmentQueryCriteriaParser<DBAttachment,String>(
criteriaBuilder,
criteriaQuery,
root
)
// parse criteria and build where predicates
criteriaParser.parse(criteria, sorting)
// prepare query for execution
return session.createQuery(criteriaQuery)
}
private fun createAttachmentsQuery(
criteria: AttachmentQueryCriteria,
sorting: AttachmentSort?
@ -477,11 +498,13 @@ class NodeAttachmentService @JvmOverloads constructor(
val criteriaBuilder = session.criteriaBuilder
val criteriaQuery = criteriaBuilder.createQuery(DBAttachment::class.java)
val root = criteriaQuery.from(DBAttachment::class.java)
criteriaQuery.select(root)
val criteriaParser = HibernateAttachmentQueryCriteriaParser(
val criteriaParser = HibernateAttachmentQueryCriteriaParser<DBAttachment,DBAttachment>(
criteriaBuilder,
criteriaQuery,
criteriaQuery.from(DBAttachment::class.java)
root
)
// parse criteria and build where predicates

View File

@ -155,18 +155,14 @@ abstract class AbstractQueryCriteriaParser<Q : GenericQueryCriteria<Q,P>, in P:
}
}
class HibernateAttachmentQueryCriteriaParser(override val criteriaBuilder: CriteriaBuilder,
private val criteriaQuery: CriteriaQuery<NodeAttachmentService.DBAttachment>, val root: Root<NodeAttachmentService.DBAttachment>) :
class HibernateAttachmentQueryCriteriaParser<T,R>(override val criteriaBuilder: CriteriaBuilder,
private val criteriaQuery: CriteriaQuery<R>, val root: Root<T>) :
AbstractQueryCriteriaParser<AttachmentQueryCriteria, AttachmentsQueryCriteriaParser, AttachmentSort>(), AttachmentsQueryCriteriaParser {
private companion object {
private val log = contextLogger()
}
init {
criteriaQuery.select(root)
}
override fun parse(criteria: AttachmentQueryCriteria, sorting: AttachmentSort?): Collection<Predicate> {
val predicateSet = criteria.visit(this)