From 5cdbec9ddf8236d4f381fc5b654e487da31a4d94 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Mon, 21 Aug 2023 10:30:42 +0100 Subject: [PATCH] ENT-6876: Optimised vault query to not query for total state count if the first page isn't full (#7449) --- .../net/corda/node/services/vault/NodeVaultService.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt index ac0913604c..9c90c36dd3 100644 --- a/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt +++ b/node/src/main/kotlin/net/corda/node/services/vault/NodeVaultService.kt @@ -706,9 +706,6 @@ class NodeVaultService( paging: PageSpecification, sorting: Sort, contractStateType: Class): Vault.Page { - // calculate total results where a page specification has been defined - val totalStatesAvailable = if (paging.isDefault) -1 else queryTotalStateCount(criteria, contractStateType) - val (query, stateTypes) = createQuery(criteria, contractStateType, sorting) query.setResultWindow(paging) @@ -732,6 +729,13 @@ class NodeVaultService( ArrayList() ) + val totalStatesAvailable = when { + paging.isDefault -> -1L + // If the first page isn't full then we know that's all the states that are available + paging.pageNumber == DEFAULT_PAGE_NUM && states.size < paging.pageSize -> states.size.toLong() + else -> queryTotalStateCount(criteria, contractStateType) + } + return Vault.Page(states, statesMetadata, totalStatesAvailable, stateTypes, otherResults) }