use of org.eclipse.dataspaceconnector.spi.query.QuerySpec in project DataSpaceConnector by eclipse-dataspaceconnector.
the class InMemoryPolicyStoreTest method findAll_allFilters.
@Test
void findAll_allFilters() {
Policy policy1 = Policy.Builder.newInstance().target("target1").id("1C").build();
Policy policy2 = Policy.Builder.newInstance().target("target1").id("1A").build();
Policy policy3 = Policy.Builder.newInstance().target("target1").id("1B").build();
Policy policyX = Policy.Builder.newInstance().target("target2").id("2X").build();
Policy policyY = Policy.Builder.newInstance().target("target2").id("2Y").build();
store.save(policy1);
store.save(policy2);
store.save(policy3);
store.save(policyX);
store.save(policyY);
QuerySpec uid = QuerySpec.Builder.newInstance().filter("target=target1").sortField("uid").sortOrder(SortOrder.DESC).offset(1).limit(1).build();
assertThat(store.findAll(uid)).containsExactly(policy3);
}
use of org.eclipse.dataspaceconnector.spi.query.QuerySpec in project DataSpaceConnector by eclipse-dataspaceconnector.
the class SqlAssetIndex method queryAssets.
@Override
public Stream<Asset> queryAssets(QuerySpec querySpec) {
Objects.requireNonNull(querySpec);
var conditions = querySpec.getFilterExpression().stream().map(SqlConditionExpression::new).collect(Collectors.toList());
var results = conditions.stream().map(SqlConditionExpression::isValidExpression).collect(Collectors.toList());
if (results.stream().anyMatch(Result::failed)) {
var message = results.stream().flatMap(r -> r.getFailureMessages().stream()).collect(Collectors.joining(", "));
throw new IllegalArgumentException(message);
}
var subSelects = conditions.stream().map(this::toSubSelect).collect(Collectors.toList());
var template = "%s %s LIMIT %s OFFSET %s";
var query = format(template, sqlAssetQueries.getSqlAssetListClause(), concatSubSelects(subSelects), querySpec.getLimit(), querySpec.getOffset());
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
var params = conditions.stream().flatMap(SqlConditionExpression::toStatementParameter).collect(Collectors.toList());
List<String> ids = executeQuery(connection, this::mapAssetIds, query, params.toArray(Object[]::new));
return ids.stream().map(this::findById);
} catch (SQLException e) {
throw new EdcPersistenceException(e);
}
});
}
Aggregations