diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-08-21 10:29:31 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-08-21 10:29:31 +0200 |
commit | 37a92817ef738ea0c0b82aff15605e65de7fc9dd (patch) | |
tree | 67b731110bd6be3abb6b7a66a244843e7e79be87 /archiva-modules/metadata | |
parent | f10055958657b1ebf47e664d8d240afe2b200f0f (diff) | |
download | archiva-37a92817ef738ea0c0b82aff15605e65de7fc9dd.tar.gz archiva-37a92817ef738ea0c0b82aff15605e65de7fc9dd.zip |
Adding query param object for search queries
Diffstat (limited to 'archiva-modules/metadata')
4 files changed, 123 insertions, 15 deletions
diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/QueryParameter.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/QueryParameter.java new file mode 100644 index 000000000..a5a14d506 --- /dev/null +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/QueryParameter.java @@ -0,0 +1,98 @@ +package org.apache.archiva.metadata; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * + * This class is used to provide additional query parameters to search queries. + * These parameters are hints for the metadata repository implementation, some parameters may be ignored. + * + * The defaults are: + * <li> + * <ul>Sort order: ascending</ul> + * <ul>Offset: 0</ul> + * <ul>Limit: Long.MAX_VALUE</ul> + * <ul>Sort fields: empty, which means it depends on the query</ul> + * </li> + * + * @author Martin Stockhammer <martin_s@apache.org> + */ +public class QueryParameter { + + final boolean ascending; + final List<String> sortFields; + final long offset; + final long limit; + + public QueryParameter(boolean isAscending, long offset, long limit, String... sortFields) { + this.ascending = isAscending; + this.offset = offset; + this.limit = limit; + this.sortFields = Arrays.asList(sortFields); + } + + public QueryParameter(long offset, long limit) { + this.offset=offset; + this.limit = limit; + this.ascending = true; + this.sortFields = Collections.emptyList(); + } + + public QueryParameter(boolean isAscending, long offset, long limit) { + this.ascending = isAscending; + this.offset = offset; + this.limit = limit; + this.sortFields = Collections.emptyList(); + } + + public QueryParameter(long limit) { + this.offset=0; + this.ascending=true; + this.limit=limit; + this.sortFields = Collections.emptyList(); + } + + public QueryParameter() { + this.ascending = true; + this.sortFields = Collections.emptyList(); + this.offset = 0; + this.limit = Long.MAX_VALUE; + } + + public boolean isAscending() { + return ascending; + } + + public List<String> getSortFields() { + return sortFields; + } + + public long getOffset() { + return offset; + } + + public long getLimit() { + return limit; + } +} diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java index d3e18c48d..7d7258645 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/AbstractMetadataRepository.java @@ -19,6 +19,7 @@ package org.apache.archiva.metadata.repository; * under the License. */ +import org.apache.archiva.metadata.QueryParameter; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.MetadataFacet; import org.apache.archiva.metadata.model.MetadataFacetFactory; @@ -111,7 +112,7 @@ public abstract class AbstractMetadataRepository } @Override - public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime ) + public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter ) throws MetadataRepositoryException { throw new UnsupportedOperationException(); @@ -306,13 +307,13 @@ public abstract class AbstractMetadataRepository @Override public <T extends MetadataFacet> Stream<T> getMetadataFacetStream( RepositorySession session, String repositoryId, Class<T> facetClazz ) throws MetadataRepositoryException { - return getMetadataFacetStream( session, repositoryId, facetClazz, 0, Long.MAX_VALUE ); + return getMetadataFacetStream( session, repositoryId, facetClazz, new QueryParameter()); } @Override public Stream<ArtifactMetadata> getArtifactsByDateRangeStream( RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime ) throws MetadataRepositoryException { - return getArtifactsByDateRangeStream( session, repositoryId, startTime, endTime, 0, Long.MAX_VALUE ); + return getArtifactsByDateRangeStream( session, repositoryId, startTime, endTime, new QueryParameter()); } @Override @@ -323,7 +324,7 @@ public abstract class AbstractMetadataRepository } @Override - public <T extends MetadataFacet> Stream<T> getMetadataFacetStream( RepositorySession session, String repositoryId, Class<T> facetClazz, long offset, long maxEntries ) throws MetadataRepositoryException + public <T extends MetadataFacet> Stream<T> getMetadataFacetStream(RepositorySession session, String repositoryId, Class<T> facetClazz, QueryParameter queryParameter) throws MetadataRepositoryException { throw new UnsupportedOperationException(); } @@ -337,7 +338,7 @@ public abstract class AbstractMetadataRepository @Override - public Stream<ArtifactMetadata> getArtifactsByDateRangeStream( RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime, long offset, long maxEntries ) throws MetadataRepositoryException + public Stream<ArtifactMetadata> getArtifactsByDateRangeStream(RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter) throws MetadataRepositoryException { throw new UnsupportedOperationException(); } @@ -358,4 +359,13 @@ public abstract class AbstractMetadataRepository protected Class<? extends MetadataFacet> getFactoryClassForId( String facetId ) { return metadataService.getFactoryClassForId( facetId ); } + + + + @Override + public List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repoId, ZonedDateTime startTime, ZonedDateTime endTime) + throws MetadataRepositoryException { + return getArtifactsByDateRange(session, repoId, startTime, endTime, new QueryParameter()); + } + } diff --git a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java index 68ba5e0e7..b0849cd14 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java +++ b/archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/MetadataRepository.java @@ -19,6 +19,7 @@ package org.apache.archiva.metadata.repository; * under the License. */ +import org.apache.archiva.metadata.QueryParameter; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.model.MetadataFacet; import org.apache.archiva.metadata.model.ProjectMetadata; @@ -157,7 +158,7 @@ public interface MetadataRepository <T extends MetadataFacet> Stream<T> getMetadataFacetStream( RepositorySession session, String repositoryId, Class<T> facetClazz) throws MetadataRepositoryException; - <T extends MetadataFacet> Stream<T> getMetadataFacetStream( RepositorySession session, String repositoryId, Class<T> facetClazz, long offset, long maxEntries) + <T extends MetadataFacet> Stream<T> getMetadataFacetStream(RepositorySession session, String repositoryId, Class<T> facetClazz, QueryParameter queryParameter) throws MetadataRepositoryException; /** @@ -251,6 +252,10 @@ public interface MetadataRepository List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime ) throws MetadataRepositoryException; + List<ArtifactMetadata> getArtifactsByDateRange(RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter ) + throws MetadataRepositoryException; + + /** * Returns all the artifacts * @param session @@ -263,8 +268,8 @@ public interface MetadataRepository Stream<ArtifactMetadata> getArtifactsByDateRangeStream( RepositorySession session, String repositoryId, ZonedDateTime startTime, ZonedDateTime endTime ) throws MetadataRepositoryException; - Stream<ArtifactMetadata> getArtifactsByDateRangeStream( RepositorySession session, String repositoryId, - ZonedDateTime startTime, ZonedDateTime endTime, long offset, long maxEntries ) + Stream<ArtifactMetadata> getArtifactsByDateRangeStream(RepositorySession session, String repositoryId, + ZonedDateTime startTime, ZonedDateTime endTime, QueryParameter queryParameter) throws MetadataRepositoryException; Collection<ArtifactMetadata> getArtifactsByChecksum( RepositorySession session, String repositoryId, String checksum ) diff --git a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java index a13abdc55..d34f8ab0c 100644 --- a/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java +++ b/archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java @@ -20,26 +20,21 @@ package org.apache.archiva.metadata.repository; */ import junit.framework.TestCase; +import org.apache.archiva.metadata.QueryParameter; import org.apache.archiva.metadata.generic.GenericMetadataFacet; import org.apache.archiva.metadata.generic.GenericMetadataFacetFactory; import org.apache.archiva.metadata.model.*; -import org.apache.archiva.metadata.model.facets.AbstractMetadataFacetFactory; -import org.apache.archiva.repository.Repository; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.format.annotation.NumberFormat; import org.springframework.test.context.ContextConfiguration; import java.text.SimpleDateFormat; import java.time.ZoneId; import java.time.ZonedDateTime; -import java.time.temporal.TemporalUnit; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -915,7 +910,7 @@ public abstract class AbstractMetadataRepositoryTest try ( RepositorySession session = getSessionFactory( ).createSession( ) ) { tryAssert( ( ) -> { - Stream<TestMetadataFacet> str = getRepository( ).getMetadataFacetStream( session, TEST_REPO_ID, TestMetadataFacet.class, 0, 100 ); + Stream<TestMetadataFacet> str = getRepository( ).getMetadataFacetStream( session, TEST_REPO_ID, TestMetadataFacet.class, new QueryParameter(0, 100)); assertNotNull( str ); List<TestMetadataFacet> result = str.collect( Collectors.toList( ) ); assertEquals( 100, result.size( ) ); |