Revert linearId (UniqueIdentifier) Vault Query search attribute (#1325)

* Revert `linearId` (UniqueIdentifier) search attribute for developer convenience.

* Introduced secondary LinearStateQueryCriteria constructor as per MN review comments.

* Fix broken test.
This commit is contained in:
josecoll 2017-08-29 10:27:43 +01:00 committed by GitHub
parent 041af053e3
commit a027e51ca3
4 changed files with 41 additions and 18 deletions

View File

@ -4,12 +4,12 @@ package net.corda.core.node.services.vault
import net.corda.core.contracts.ContractState import net.corda.core.contracts.ContractState
import net.corda.core.contracts.StateRef import net.corda.core.contracts.StateRef
import net.corda.core.contracts.UniqueIdentifier
import net.corda.core.identity.AbstractParty import net.corda.core.identity.AbstractParty
import net.corda.core.node.services.Vault import net.corda.core.node.services.Vault
import net.corda.core.schemas.PersistentState import net.corda.core.schemas.PersistentState
import net.corda.core.serialization.CordaSerializable import net.corda.core.serialization.CordaSerializable
import net.corda.core.utilities.OpaqueBytes import net.corda.core.utilities.OpaqueBytes
import org.bouncycastle.asn1.x500.X500Name
import java.time.Instant import java.time.Instant
import java.util.* import java.util.*
import javax.persistence.criteria.Predicate import javax.persistence.criteria.Predicate
@ -69,6 +69,11 @@ sealed class QueryCriteria {
val externalId: List<String>? = null, val externalId: List<String>? = null,
override val status: Vault.StateStatus = Vault.StateStatus.UNCONSUMED, override val status: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
override val contractStateTypes: Set<Class<out ContractState>>? = null) : CommonQueryCriteria() { override val contractStateTypes: Set<Class<out ContractState>>? = null) : CommonQueryCriteria() {
constructor(participants: List<AbstractParty>? = null,
linearId: List<UniqueIdentifier>? = null,
status: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
contractStateTypes: Set<Class<out ContractState>>? = null) : this(participants, linearId?.map { it.id }, linearId?.mapNotNull { it.externalId }, status, contractStateTypes)
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> { override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
super.visit(parser) super.visit(parser)
return parser.parseCriteria(this) return parser.parseCriteria(this)

View File

@ -287,6 +287,7 @@ class HibernateQueryCriteriaParser(val contractType: Class<out ContractState>,
// linear ids externalId // linear ids externalId
criteria.externalId?.let { criteria.externalId?.let {
val externalIds = criteria.externalId as List<String> val externalIds = criteria.externalId as List<String>
if (externalIds.isNotEmpty())
predicateSet.add(criteriaBuilder.and(vaultLinearStates.get<String>("externalId").`in`(externalIds))) predicateSet.add(criteriaBuilder.and(vaultLinearStates.get<String>("externalId").`in`(externalIds)))
} }

View File

@ -191,12 +191,12 @@ public class VaultQueryJavaTests extends TestDependencyInjectionBase {
QueryCriteria vaultCriteria = new VaultQueryCriteria(status, contractStateTypes); QueryCriteria vaultCriteria = new VaultQueryCriteria(status, contractStateTypes);
List<UUID> linearIds = Collections.singletonList(ids.getSecond().getId()); List<UniqueIdentifier> linearIds = Collections.singletonList(ids.getSecond());
QueryCriteria linearCriteriaAll = new LinearStateQueryCriteria(null, linearIds, null, status); QueryCriteria linearCriteriaAll = new LinearStateQueryCriteria(null, linearIds, Vault.StateStatus.UNCONSUMED, null);
QueryCriteria dealCriteriaAll = new LinearStateQueryCriteria(null, null, dealIds, status); QueryCriteria dealCriteriaAll = new LinearStateQueryCriteria(null, null, dealIds);
QueryCriteria compositeCriteria1 = dealCriteriaAll.or(linearCriteriaAll); QueryCriteria compositeCriteria1 = dealCriteriaAll.or(linearCriteriaAll);
QueryCriteria compositeCriteria2 = vaultCriteria.and(compositeCriteria1); QueryCriteria compositeCriteria2 = compositeCriteria1.and(vaultCriteria);
PageSpecification pageSpec = new PageSpecification(DEFAULT_PAGE_NUM, MAX_PAGE_SIZE); PageSpecification pageSpec = new PageSpecification(DEFAULT_PAGE_NUM, MAX_PAGE_SIZE);
Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC); Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC);
@ -307,10 +307,10 @@ public class VaultQueryJavaTests extends TestDependencyInjectionBase {
Set<Class<ContractState>> contractStateTypes = new HashSet(Arrays.asList(DealState.class, LinearState.class)); Set<Class<ContractState>> contractStateTypes = new HashSet(Arrays.asList(DealState.class, LinearState.class));
QueryCriteria vaultCriteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, contractStateTypes); QueryCriteria vaultCriteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, contractStateTypes);
List<UUID> linearIds = Collections.singletonList(uid.getId()); List<UniqueIdentifier> linearIds = Collections.singletonList(uid);
List<AbstractParty> dealParty = Collections.singletonList(getMEGA_CORP()); List<AbstractParty> dealParty = Collections.singletonList(getMEGA_CORP());
QueryCriteria dealCriteria = new LinearStateQueryCriteria(dealParty, null, dealIds); QueryCriteria dealCriteria = new LinearStateQueryCriteria(dealParty, null, dealIds);
QueryCriteria linearCriteria = new LinearStateQueryCriteria(dealParty, linearIds, null); QueryCriteria linearCriteria = new LinearStateQueryCriteria(dealParty, linearIds, Vault.StateStatus.UNCONSUMED, null);
QueryCriteria dealOrLinearIdCriteria = dealCriteria.or(linearCriteria); QueryCriteria dealOrLinearIdCriteria = dealCriteria.or(linearCriteria);
QueryCriteria compositeCriteria = dealOrLinearIdCriteria.and(vaultCriteria); QueryCriteria compositeCriteria = dealOrLinearIdCriteria.and(vaultCriteria);

View File

@ -1234,8 +1234,8 @@ class VaultQueryTests : TestDependencyInjectionBase() {
} }
database.transaction { database.transaction {
// DOCSTART VaultQueryExample8 // DOCSTART VaultQueryExample8
val linearIds = issuedStates.states.map { it.state.data.linearId.id }.toList() val linearIds = issuedStates.states.map { it.state.data.linearId }.toList()
val criteria = LinearStateQueryCriteria(uuid = listOf(linearIds.first(), linearIds.last())) val criteria = LinearStateQueryCriteria(linearId = listOf(linearIds.first(), linearIds.last()))
val results = vaultQuerySvc.queryBy<LinearState>(criteria) val results = vaultQuerySvc.queryBy<LinearState>(criteria)
// DOCEND VaultQueryExample8 // DOCEND VaultQueryExample8
assertThat(results.states).hasSize(2) assertThat(results.states).hasSize(2)
@ -1243,7 +1243,7 @@ class VaultQueryTests : TestDependencyInjectionBase() {
} }
@Test @Test
fun `unconsumed linear heads for linearId with external Id`() { fun `unconsumed linear heads by linearId`() {
val (linearState1, linearState3) = val (linearState1, linearState3) =
database.transaction { database.transaction {
val linearState1 = services.fillWithSomeTestLinearStates(1, "ID1") val linearState1 = services.fillWithSomeTestLinearStates(1, "ID1")
@ -1252,15 +1252,32 @@ class VaultQueryTests : TestDependencyInjectionBase() {
Pair(linearState1, linearState3) Pair(linearState1, linearState3)
} }
database.transaction { database.transaction {
val linearIds = listOf(linearState1.states.first().state.data.linearId.id, linearState3.states.first().state.data.linearId.id) val linearIds = listOf(linearState1.states.first().state.data.linearId, linearState3.states.first().state.data.linearId)
val criteria = LinearStateQueryCriteria(uuid = linearIds) val criteria = LinearStateQueryCriteria(linearId = linearIds)
val results = vaultQuerySvc.queryBy<LinearState>(criteria) val results = vaultQuerySvc.queryBy<LinearState>(criteria)
assertThat(results.states).hasSize(2) assertThat(results.states).hasSize(2)
} }
} }
@Test @Test
fun `all linear states for a given id`() { fun `unconsumed linear heads for linearId by external Id`() {
val (linearState1, linearState3) =
database.transaction {
val linearState1 = services.fillWithSomeTestLinearStates(1, "ID1")
services.fillWithSomeTestLinearStates(1, "ID2")
val linearState3 = services.fillWithSomeTestLinearStates(1, "ID3")
Pair(linearState1, linearState3)
}
database.transaction {
val externalIds = listOf(linearState1.states.first().state.data.linearId.externalId!!, linearState3.states.first().state.data.linearId.externalId!!)
val criteria = LinearStateQueryCriteria(externalId = externalIds)
val results = vaultQuerySvc.queryBy<LinearState>(criteria)
assertThat(results.states).hasSize(2)
}
}
@Test
fun `all linear states for a given linear id`() {
val linearId = val linearId =
database.transaction { database.transaction {
val txns = services.fillWithSomeTestLinearStates(1, "TEST") val txns = services.fillWithSomeTestLinearStates(1, "TEST")
@ -1273,7 +1290,7 @@ class VaultQueryTests : TestDependencyInjectionBase() {
database.transaction { database.transaction {
// should now have 1 UNCONSUMED & 3 CONSUMED state refs for Linear State with "TEST" // should now have 1 UNCONSUMED & 3 CONSUMED state refs for Linear State with "TEST"
// DOCSTART VaultQueryExample9 // DOCSTART VaultQueryExample9
val linearStateCriteria = LinearStateQueryCriteria(uuid = listOf(linearId.id), status = Vault.StateStatus.ALL) val linearStateCriteria = LinearStateQueryCriteria(linearId = listOf(linearId), status = Vault.StateStatus.ALL)
val vaultCriteria = VaultQueryCriteria(status = Vault.StateStatus.ALL) val vaultCriteria = VaultQueryCriteria(status = Vault.StateStatus.ALL)
val results = vaultQuerySvc.queryBy<LinearState>(linearStateCriteria and vaultCriteria) val results = vaultQuerySvc.queryBy<LinearState>(linearStateCriteria and vaultCriteria)
// DOCEND VaultQueryExample9 // DOCEND VaultQueryExample9
@ -1305,7 +1322,7 @@ class VaultQueryTests : TestDependencyInjectionBase() {
} }
@Test @Test
fun `unconsumed linear states sorted by linear state attribute`() { fun `unconsumed linear states sorted by external id`() {
database.transaction { database.transaction {
services.fillWithSomeTestLinearStates(1, externalId = "111") services.fillWithSomeTestLinearStates(1, externalId = "111")
services.fillWithSomeTestLinearStates(2, externalId = "222") services.fillWithSomeTestLinearStates(2, externalId = "222")
@ -1360,7 +1377,7 @@ class VaultQueryTests : TestDependencyInjectionBase() {
} }
@Test @Test
fun `return consumed linear states for a given id`() { fun `return consumed linear states for a given linear id`() {
val txns = val txns =
database.transaction { database.transaction {
val txns = services.fillWithSomeTestLinearStates(1, "TEST") val txns = services.fillWithSomeTestLinearStates(1, "TEST")
@ -1372,7 +1389,7 @@ class VaultQueryTests : TestDependencyInjectionBase() {
} }
database.transaction { database.transaction {
// should now have 1 UNCONSUMED & 3 CONSUMED state refs for Linear State with "TEST" // should now have 1 UNCONSUMED & 3 CONSUMED state refs for Linear State with "TEST"
val linearStateCriteria = LinearStateQueryCriteria(uuid = txns.states.map { it.state.data.linearId.id }, status = Vault.StateStatus.CONSUMED) val linearStateCriteria = LinearStateQueryCriteria(linearId = txns.states.map { it.state.data.linearId }, status = Vault.StateStatus.CONSUMED)
val vaultCriteria = VaultQueryCriteria(status = Vault.StateStatus.CONSUMED) val vaultCriteria = VaultQueryCriteria(status = Vault.StateStatus.CONSUMED)
val sorting = Sort(setOf(Sort.SortColumn(SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC))) val sorting = Sort(setOf(Sort.SortColumn(SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC)))
val results = vaultQuerySvc.queryBy<LinearState>(linearStateCriteria.and(vaultCriteria), sorting = sorting) val results = vaultQuerySvc.queryBy<LinearState>(linearStateCriteria.and(vaultCriteria), sorting = sorting)