Skip to content

Commit 2e3562f

Browse files
Updated the QueryDsl implementation to build more expressive vault query expressions.
1 parent 590dc12 commit 2e3562f

File tree

8 files changed

+506
-323
lines changed

8 files changed

+506
-323
lines changed

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/Extensions.QueryDsl.kt

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,42 @@
1717
package io.onixlabs.corda.core.services
1818

1919
import net.corda.core.contracts.ContractState
20-
import net.corda.core.node.services.Vault
21-
import net.corda.core.node.services.vault.PageSpecification
2220
import net.corda.core.node.services.vault.QueryCriteria
23-
import net.corda.core.node.services.vault.Sort
21+
import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria
2422

2523
/**
2624
* Creates a [QueryCriteria] using the Query DSL.
2725
*
2826
* @param T The underlying [ContractState] which will be included in the query.
2927
* @param contractStateType The [ContractState] class which will be included in the query.
30-
* @param stateStatus The state status which will be applied to the query.
31-
* @param relevancyStatus The relevancy status which will be applied to the query.
32-
* @param page The page specification which will be applied to the query.
33-
* @param sort The sorting which will be applied to the query.
3428
* @return Returns the [QueryCriteria] that was created using the Query DSL.
3529
*/
3630
@QueryDslContext
37-
fun <T : ContractState> vaultQuery(
38-
contractStateType: Class<T>,
39-
stateStatus: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
40-
relevancyStatus: Vault.RelevancyStatus = Vault.RelevancyStatus.RELEVANT,
41-
page: PageSpecification = MAXIMUM_PAGE_SPECIFICATION,
42-
sort: Sort = DEFAULT_SORTING,
43-
action: QueryDsl<T>.() -> Unit
44-
): QueryCriteria {
45-
val criteria = QueryCriteria.VaultQueryCriteria(
46-
contractStateTypes = setOf(contractStateType),
47-
status = stateStatus,
48-
relevancyStatus = relevancyStatus
49-
)
50-
51-
return vaultQuery(criteria, page, sort, action)
31+
fun <T : ContractState> vaultQuery(contractStateType: Class<T>, action: QueryDsl<T>.() -> Unit): QueryCriteria {
32+
val criteria = VaultQueryCriteria(contractStateTypes = setOf(contractStateType))
33+
return vaultQuery(criteria, action)
5234
}
5335

5436
/**
5537
* Creates a [QueryCriteria] using the Query DSL.
5638
*
5739
* @param T The underlying [ContractState] which will be included in the query.
58-
* @param stateStatus The state status which will be applied to the query.
59-
* @param relevancyStatus The relevancy status which will be applied to the query.
60-
* @param page The page specification which will be applied to the query.
61-
* @param sort The sorting which will be applied to the query.
6240
* @return Returns the [QueryCriteria] that was created using the Query DSL.
6341
*/
6442
@QueryDslContext
65-
inline fun <reified T : ContractState> vaultQuery(
66-
stateStatus: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
67-
relevancyStatus: Vault.RelevancyStatus = Vault.RelevancyStatus.RELEVANT,
68-
page: PageSpecification = MAXIMUM_PAGE_SPECIFICATION,
69-
sort: Sort = DEFAULT_SORTING,
70-
noinline action: QueryDsl<T>.() -> Unit
71-
): QueryCriteria {
72-
return vaultQuery(T::class.java, stateStatus, relevancyStatus, page, sort, action)
43+
inline fun <reified T : ContractState> vaultQuery(noinline action: QueryDsl<T>.() -> Unit): QueryCriteria {
44+
return vaultQuery(T::class.java, action)
7345
}
7446

7547
/**
7648
* Creates a [QueryCriteria] using the Query DSL.
7749
*
7850
* @param T The underlying [ContractState] which will be included in the query.
7951
* @param criteria The base criteria upon which the Query DSL will build.
80-
* @param page The page specification which will be applied to the query.
81-
* @param sort The sorting which will be applied to the query.
8252
* @return Returns the [QueryCriteria] that was created using the Query DSL.
8353
*/
84-
private fun <T : ContractState> vaultQuery(
85-
criteria: QueryCriteria,
86-
page: PageSpecification = MAXIMUM_PAGE_SPECIFICATION,
87-
sort: Sort = DEFAULT_SORTING,
88-
action: QueryDsl<T>.() -> Unit
89-
): QueryCriteria {
90-
val queryDsl = QueryDsl<T>(criteria, page, sort)
54+
private fun <T : ContractState> vaultQuery(criteria: QueryCriteria, action: QueryDsl<T>.() -> Unit): QueryCriteria {
55+
val queryDsl = QueryDsl<T>(criteria)
9156
action(queryDsl)
92-
return queryDsl.criteria
57+
return queryDsl.getQueryCriteria()
9358
}

onixlabs-corda-core-workflow/src/main/kotlin/io/onixlabs/corda/core/services/Extensions.VaultService.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import java.util.*
3131
* @param criteria The query criteria that will be used to obtain the sequence.
3232
* @return Returns true if the sequence contains any elements; otherwise, false.
3333
*/
34-
fun <T : ContractState> VaultService<T>.any(criteria: QueryCriteria = defaultQueryCriteria): Boolean {
35-
return VaultSequence(this, criteria).any()
36-
}
34+
fun <T : ContractState> VaultService<T>.any(
35+
criteria: QueryCriteria = defaultQueryCriteria,
36+
paging: PageSpecification = MAXIMUM_PAGE_SPECIFICATION,
37+
sorting: Sort = DEFAULT_SORTING
38+
): Boolean = VaultSequence(this, criteria, paging, sorting).any()
3739

3840
/**
3941
* Determines whether the sequence contains any elements.
@@ -43,7 +45,7 @@ fun <T : ContractState> VaultService<T>.any(criteria: QueryCriteria = defaultQue
4345
* @return Returns true if the sequence contains any elements; otherwise, false.
4446
*/
4547
fun <T : ContractState> VaultService<T>.any(action: QueryDsl<T>.() -> Unit): Boolean {
46-
val queryDsl = QueryDsl<T>()
48+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
4749
action(queryDsl)
4850
return VaultSequence(this, queryDsl).any()
4951
}
@@ -55,9 +57,11 @@ fun <T : ContractState> VaultService<T>.any(action: QueryDsl<T>.() -> Unit): Boo
5557
* @param criteria The query criteria that will be used to obtain the sequence.
5658
* @return Returns a count of the number of elements in a sequence.
5759
*/
58-
fun <T : ContractState> VaultService<T>.count(criteria: QueryCriteria = defaultQueryCriteria): Int {
59-
return VaultSequence(this, criteria).count()
60-
}
60+
fun <T : ContractState> VaultService<T>.count(
61+
criteria: QueryCriteria = defaultQueryCriteria,
62+
paging: PageSpecification = MAXIMUM_PAGE_SPECIFICATION,
63+
sorting: Sort = DEFAULT_SORTING
64+
): Int = VaultSequence(this, criteria, paging, sorting).count()
6165

6266
/**
6367
* Counts the number of elements in a sequence.
@@ -67,7 +71,7 @@ fun <T : ContractState> VaultService<T>.count(criteria: QueryCriteria = defaultQ
6771
* @return Returns a count of the number of elements in a sequence.
6872
*/
6973
fun <T : ContractState> VaultService<T>.count(action: QueryDsl<T>.() -> Unit): Int {
70-
val queryDsl = QueryDsl<T>()
74+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
7175
action(queryDsl)
7276
return VaultSequence(this, queryDsl).count()
7377
}
@@ -95,7 +99,7 @@ fun <T : ContractState> VaultService<T>.filter(
9599
* @return Returns a filtered sequence of elements.
96100
*/
97101
fun <T : ContractState> VaultService<T>.filter(action: QueryDsl<T>.() -> Unit): VaultSequence<T> {
98-
val queryDsl = QueryDsl<T>()
102+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
99103
action(queryDsl)
100104
return VaultSequence(this, queryDsl)
101105
}
@@ -123,7 +127,7 @@ fun <T : ContractState> VaultService<T>.first(
123127
* @return Returns the first element in the sequence.
124128
*/
125129
fun <T : ContractState> VaultService<T>.first(action: QueryDsl<T>.() -> Unit): StateAndRef<T> {
126-
val queryDsl = QueryDsl<T>()
130+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
127131
action(queryDsl)
128132
return VaultSequence(this, queryDsl).first()
129133
}
@@ -151,7 +155,7 @@ fun <T : ContractState> VaultService<T>.firstOrNull(
151155
* @return Returns the first element in the sequence, or null if no element is found.
152156
*/
153157
fun <T : ContractState> VaultService<T>.firstOrNull(action: QueryDsl<T>.() -> Unit): StateAndRef<T>? {
154-
val queryDsl = QueryDsl<T>()
158+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
155159
action(queryDsl)
156160
return VaultSequence(this, queryDsl).firstOrNull()
157161
}
@@ -179,7 +183,7 @@ fun <T : ContractState> VaultService<T>.last(
179183
* @return Returns the last element in the sequence.
180184
*/
181185
fun <T : ContractState> VaultService<T>.last(action: QueryDsl<T>.() -> Unit): StateAndRef<T> {
182-
val queryDsl = QueryDsl<T>()
186+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
183187
action(queryDsl)
184188
return VaultSequence(this, queryDsl).last()
185189
}
@@ -207,7 +211,7 @@ fun <T : ContractState> VaultService<T>.lastOrNull(
207211
* @return Returns the last element in the sequence, or null if no element is found.
208212
*/
209213
fun <T : ContractState> VaultService<T>.lastOrNull(action: QueryDsl<T>.() -> Unit): StateAndRef<T>? {
210-
val queryDsl = QueryDsl<T>()
214+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
211215
action(queryDsl)
212216
return VaultSequence(this, queryDsl).lastOrNull()
213217
}
@@ -235,7 +239,7 @@ fun <T : ContractState> VaultService<T>.single(
235239
* @return Returns the last element in the sequence.
236240
*/
237241
fun <T : ContractState> VaultService<T>.single(action: QueryDsl<T>.() -> Unit): StateAndRef<T> {
238-
val queryDsl = QueryDsl<T>()
242+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
239243
action(queryDsl)
240244
return VaultSequence(this, queryDsl).single()
241245
}
@@ -263,7 +267,7 @@ fun <T : ContractState> VaultService<T>.singleOrNull(
263267
* @return Returns the last element in the sequence, or null if no element is found.
264268
*/
265269
fun <T : ContractState> VaultService<T>.singleOrNull(action: QueryDsl<T>.() -> Unit): StateAndRef<T>? {
266-
val queryDsl = QueryDsl<T>()
270+
val queryDsl = QueryDsl<T>(defaultQueryCriteria)
267271
action(queryDsl)
268272
return VaultSequence(this, queryDsl).singleOrNull()
269273
}

0 commit comments

Comments
 (0)