Cleaned up the QueryCriteria API to be more Java friendly

This commit is contained in:
Shams Asari 2017-07-06 10:08:17 +01:00
parent 44f57639d2
commit 182c9cceb5
5 changed files with 21 additions and 16 deletions

View File

@ -96,13 +96,13 @@ sealed class QueryCriteria {
}
// enable composition of [QueryCriteria]
data class AndComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
private data class AndComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
return parser.parseAnd(this.a, this.b)
}
}
data class OrComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
private data class OrComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
return parser.parseOr(this.a, this.b)
}
@ -128,4 +128,4 @@ interface IQueryCriteriaParser {
fun parseOr(left: QueryCriteria, right: QueryCriteria): Collection<Predicate>
fun parseAnd(left: QueryCriteria, right: QueryCriteria): Collection<Predicate>
fun parse(criteria: QueryCriteria, sorting: Sort? = null) : Collection<Predicate>
}
}

View File

@ -91,6 +91,7 @@ fun <O, R> resolveEnclosingObjectFromExpression(expression: CriteriaExpression<O
}
}
@Suppress("UNCHECKED_CAST")
fun <O, C> resolveEnclosingObjectFromColumn(column: Column<O, C>): Class<O> {
return when (column) {
is Column.Java -> column.field.declaringClass as Class<O>
@ -118,14 +119,14 @@ fun <O, C> getColumnName(column: Column<O, C>): String {
* paging and sorting capability:
* https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html
*/
val DEFAULT_PAGE_NUM = 0
val DEFAULT_PAGE_SIZE = 200
const val DEFAULT_PAGE_NUM = 0
const val DEFAULT_PAGE_SIZE = 200
/**
* Note: this maximum size will be configurable in future (to allow for large JVM heap sized node configurations)
* Use [PageSpecification] to correctly handle a number of bounded pages of [MAX_PAGE_SIZE].
*/
val MAX_PAGE_SIZE = 512
const val MAX_PAGE_SIZE = 512
/**
* PageSpecification allows specification of a page number (starting from 0 as default) and page size (defaulting to

View File

@ -20,6 +20,9 @@ UNRELEASED
* Mock identity constants used in tests, such as ``ALICE``, ``BOB``, ``DUMMY_NOTARY``, have moved to ``net.corda.testing``
in the ``test-utils`` module.
* In Java, ``QueryCriteriaUtilsKt`` has moved to ``QueryCriteriaUtils``. Also ``and`` and ``or`` are now instance methods
of ``QueryCrtieria``.
Milestone 13
------------

View File

@ -2,6 +2,7 @@ package net.corda.contracts;
import co.paralleluniverse.fibers.Suspendable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import kotlin.Pair;
import kotlin.Unit;
import net.corda.contracts.asset.CashKt;
@ -28,7 +29,6 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static kotlin.collections.CollectionsKt.single;
import static net.corda.core.contracts.ContractsDSL.requireSingleCommand;
import static net.corda.core.contracts.ContractsDSL.requireThat;
@ -175,7 +175,7 @@ public class JavaCommercialPaper implements Contract {
State groupingKey) {
AuthenticatedObject<Commands.Move> cmd = requireSingleCommand(tx.getCommands(), Commands.Move.class);
// There should be only a single input due to aggregation above
State input = single(inputs);
State input = Iterables.getOnlyElement(inputs);
if (!cmd.getSigners().contains(input.getOwner().getOwningKey()))
throw new IllegalStateException("Failed requirement: the transaction is signed by the owner of the CP");
@ -208,7 +208,7 @@ public class JavaCommercialPaper implements Contract {
AuthenticatedObject<Commands.Redeem> cmd = requireSingleCommand(tx.getCommands(), Commands.Redeem.class);
// There should be only a single input due to aggregation above
State input = single(inputs);
State input = Iterables.getOnlyElement(inputs);
if (!cmd.getSigners().contains(input.getOwner().getOwningKey()))
throw new IllegalStateException("Failed requirement: the transaction is signed by the owner of the CP");
@ -249,7 +249,7 @@ public class JavaCommercialPaper implements Contract {
@NotNull List<? extends AuthenticatedObject<? extends Commands>> commands,
State groupingKey) {
AuthenticatedObject<Commands.Issue> cmd = requireSingleCommand(tx.getCommands(), Commands.Issue.class);
State output = single(outputs);
State output = Iterables.getOnlyElement(outputs);
TimeWindow timeWindowCommand = tx.getTimeWindow();
Instant time = null == timeWindowCommand
? null

View File

@ -49,7 +49,7 @@ import static net.corda.testing.CoreTestUtils.getBOC;
import static net.corda.testing.CoreTestUtils.getBOC_KEY;
import static net.corda.testing.CoreTestUtils.getBOC_PUBKEY;
import static net.corda.core.contracts.ContractsDSL.USD;
import static net.corda.core.node.services.vault.QueryCriteriaUtils.getMAX_PAGE_SIZE;
import static net.corda.core.node.services.vault.QueryCriteriaUtils.MAX_PAGE_SIZE;
import static net.corda.node.utilities.DatabaseSupportKt.configureDatabase;
import static net.corda.node.utilities.DatabaseSupportKt.transaction;
import static net.corda.testing.CoreTestUtils.getMEGA_CORP;
@ -60,7 +60,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class VaultQueryJavaTests {
private MockServices services;
VaultService vaultSvc;
private VaultService vaultSvc;
private VaultQueryService vaultQuerySvc;
private Closeable dataSource;
private Database database;
@ -82,6 +82,7 @@ public class VaultQueryJavaTests {
return makeVaultService(dataSourceProps, hibernateConfig);
}
@NotNull
@Override
public VaultQueryService getVaultQueryService() {
return new HibernateVaultQueryImpl(hibernateConfig, getVaultService().getUpdatesPublisher());
@ -192,7 +193,7 @@ public class VaultQueryJavaTests {
QueryCriteria compositeCriteria1 = dealCriteriaAll.or(linearCriteriaAll);
QueryCriteria compositeCriteria2 = vaultCriteria.and(compositeCriteria1);
PageSpecification pageSpec = new PageSpecification(0, getMAX_PAGE_SIZE());
PageSpecification pageSpec = new PageSpecification(0, MAX_PAGE_SIZE);
Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC);
Sort sorting = new Sort(ImmutableSet.of(sortByUid));
Vault.Page<LinearState> results = vaultQuerySvc.queryBy(LinearState.class, compositeCriteria2, pageSpec, sorting);
@ -269,8 +270,8 @@ public class VaultQueryJavaTests {
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, contractStateTypes);
DataFeed<Vault.Page<ContractState>, Vault.Update> results = vaultQuerySvc.trackBy(ContractState.class, criteria);
Vault.Page<ContractState> snapshot = results.getCurrent();
Observable<Vault.Update> updates = results.getFuture();
Vault.Page<ContractState> snapshot = results.getSnapshot();
Observable<Vault.Update> updates = results.getUpdates();
// DOCEND VaultJavaQueryExample4
assertThat(snapshot.getStates()).hasSize(3);
@ -301,7 +302,7 @@ public class VaultQueryJavaTests {
QueryCriteria dealOrLinearIdCriteria = dealCriteria.or(linearCriteria);
QueryCriteria compositeCriteria = dealOrLinearIdCriteria.and(vaultCriteria);
PageSpecification pageSpec = new PageSpecification(0, getMAX_PAGE_SIZE());
PageSpecification pageSpec = new PageSpecification(0, MAX_PAGE_SIZE);
Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC);
Sort sorting = new Sort(ImmutableSet.of(sortByUid));
DataFeed<Vault.Page<ContractState>, Vault.Update> results = vaultQuerySvc.trackBy(ContractState.class, compositeCriteria, pageSpec, sorting);