Search in sources :

Example 1 with QuerySpec

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);
}
Also used : Policy(org.eclipse.dataspaceconnector.policy.model.Policy) QuerySpec(org.eclipse.dataspaceconnector.spi.query.QuerySpec) Test(org.junit.jupiter.api.Test)

Example 2 with QuerySpec

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);
        }
    });
}
Also used : TransactionContext(org.eclipse.dataspaceconnector.spi.transaction.TransactionContext) Connection(java.sql.Connection) DataAddressResolver(org.eclipse.dataspaceconnector.spi.asset.DataAddressResolver) Result(org.eclipse.dataspaceconnector.spi.result.Result) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) AssetLoader(org.eclipse.dataspaceconnector.dataloading.AssetLoader) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) SqlQueryExecutor.executeQuery(org.eclipse.dataspaceconnector.sql.SqlQueryExecutor.executeQuery) Asset(org.eclipse.dataspaceconnector.spi.types.domain.asset.Asset) AssetIndex(org.eclipse.dataspaceconnector.spi.asset.AssetIndex) DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) QuerySpec(org.eclipse.dataspaceconnector.spi.query.QuerySpec) String.format(java.lang.String.format) DataAddress(org.eclipse.dataspaceconnector.spi.types.domain.DataAddress) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) AssetSelectorExpression(org.eclipse.dataspaceconnector.spi.asset.AssetSelectorExpression) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException) Criterion(org.eclipse.dataspaceconnector.spi.query.Criterion) AssetEntry(org.eclipse.dataspaceconnector.dataloading.AssetEntry) SQLException(java.sql.SQLException) EdcPersistenceException(org.eclipse.dataspaceconnector.spi.persistence.EdcPersistenceException) Result(org.eclipse.dataspaceconnector.spi.result.Result)

Aggregations

QuerySpec (org.eclipse.dataspaceconnector.spi.query.QuerySpec)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 String.format (java.lang.String.format)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 DataSource (javax.sql.DataSource)1 AssetEntry (org.eclipse.dataspaceconnector.dataloading.AssetEntry)1 AssetLoader (org.eclipse.dataspaceconnector.dataloading.AssetLoader)1 Policy (org.eclipse.dataspaceconnector.policy.model.Policy)1 AssetIndex (org.eclipse.dataspaceconnector.spi.asset.AssetIndex)1 AssetSelectorExpression (org.eclipse.dataspaceconnector.spi.asset.AssetSelectorExpression)1 DataAddressResolver (org.eclipse.dataspaceconnector.spi.asset.DataAddressResolver)1