Daniel Gretzke
Daniel Gretzke

Reputation: 446

Query for state attributes

If I have a state with an attribute, e.g.:

class Test(val timestamp: Double, val linearId = UniqueIdentifier()): LinearState

I can execute a vault query based on the linearId with LinearStateQueryCriteria. Is there a way to define query criteria for the attributes of a State? For example, get every state where the timestamp attribute is equal to x or even in a range between y and z?

Upvotes: 0

Views: 102

Answers (1)

Ashutosh Meher
Ashutosh Meher

Reputation: 1821

This would work if your State is a QueryableState. You could use a VaultCustomQueryCriteria to get the desired result as shown below:

QueryCriteria generalCriteria = new VaultQueryCriteria(Vault.StateStatus.ALL);

FieldInfo attributeCurrency = getField("currency", CashSchemaV1.PersistentCashState.class);
FieldInfo attributeQuantity = getField("pennies", CashSchemaV1.PersistentCashState.class);

CriteriaExpression currencyIndex = Builder.equal(attributeCurrency, "USD");
CriteriaExpression quantityIndex = Builder.greaterThanOrEqual(attributeQuantity, 10L);

QueryCriteria customCriteria2 = new VaultCustomQueryCriteria(quantityIndex);
QueryCriteria customCriteria1 = new VaultCustomQueryCriteria(currencyIndex);


QueryCriteria criteria = generalCriteria.and(customCriteria1).and(customCriteria2);
Vault.Page<ContractState> results = vaultService.queryBy(Cash.State.class, criteria);

Refer here for more details: https://docs.corda.net/api-vault-query.html

Refer here if you want to learn about QueryableState: https://medium.com/corda/persisting-corda-states-in-custom-database-tables-using-queryablestate-dedaa18b7050

Upvotes: 1

Related Questions