Skip to content

Commit

Permalink
add pagination for getAllArtifactStores
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Modi <[email protected]>
  • Loading branch information
hjmodi committed Apr 5, 2024
1 parent a44620b commit 89e0a00
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.commonjava.indy.service.repository.model.StoreType;
import org.commonjava.indy.service.repository.model.dto.EndpointView;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.model.dto.ListArtifactStoreDTO;
import org.commonjava.indy.service.repository.util.JaxRsUriFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -257,13 +258,16 @@ public Boolean isStoreDataEmpty()
}

public EndpointViewListing getEndpointsListing( final String pkgType, final String baseUri,
final JaxRsUriFormatter uriFormatter )
final JaxRsUriFormatter uriFormatter, final String page )
throws IndyWorkflowException
{
List<ArtifactStore> stores;
String nextPage = "";
try
{
stores = new ArrayList<>( storeManager.getAllArtifactStores() );
ListArtifactStoreDTO result = storeManager.getAllArtifactStores(page);
nextPage = result.getNextPage();
stores = new ArrayList<>( result.getItems() );
if ( StringUtils.isNotBlank( pkgType ) && !"all".equals( pkgType ) && isValidPackageType( pkgType ) )
{
stores = stores.stream()
Expand Down Expand Up @@ -292,7 +296,21 @@ public EndpointViewListing getEndpointsListing( final String pkgType, final Stri
}
}

return new EndpointViewListing( points );
EndpointViewListing result = new EndpointViewListing( points );
result.setCurrentPage( page );

if (!nextPage.isEmpty()) {
result.setNextPage( nextPage );
}

return result;
}

public EndpointViewListing getEndpointsListing( final String pkgType, final String baseUri,
final JaxRsUriFormatter uriFormatter )
throws IndyWorkflowException
{
return getEndpointsListing( pkgType, baseUri, uriFormatter, "" );
}

public Map<String, List<String>> getStoreKeysByPackageType( final String pkgType )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.commonjava.indy.service.repository.model.StoreKey;
import org.commonjava.indy.service.repository.model.dto.EndpointView;
import org.commonjava.indy.service.repository.model.dto.EndpointViewListing;
import org.commonjava.indy.service.repository.model.dto.ListArtifactStoreDTO;
import org.commonjava.indy.service.repository.model.version.Versioning;

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -57,13 +58,14 @@ public Versioning getVersionInfo()
return versioning;
}

public EndpointViewListing getEndpointsListing( final String baseUri, final JaxRsUriFormatter uriFormatter )
throws IndyWorkflowException
public EndpointViewListing getEndpointsListing( final String baseUri, final JaxRsUriFormatter uriFormatter, final String page )
throws IndyWorkflowException
{
final List<ArtifactStore> stores;
try
{
stores = new ArrayList<>( dataManager.getAllArtifactStores() );
ListArtifactStoreDTO result = dataManager.getAllArtifactStores(page);
stores = new ArrayList<>( result.getItems() );
}
catch ( final IndyDataException e )
{
Expand All @@ -89,6 +91,12 @@ public EndpointViewListing getEndpointsListing( final String baseUri, final JaxR
return new EndpointViewListing( points );
}

public EndpointViewListing getEndpointsListing( final String baseUri, final JaxRsUriFormatter uriFormatter )
throws IndyWorkflowException
{
return getEndpointsListing( baseUri, uriFormatter, "" );
}

public Map<String, List<String>> getAllStoreKeys()
throws IndyWorkflowException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ Optional<ArtifactStore> getArtifactStore( StoreKey key )
Set<ArtifactStore> getAllArtifactStores()
throws IndyDataException;

/**
* Return the full list of {@link ArtifactStore} instances of a given {@link StoreType} (hosted, remote, or group) available on the system.
*/
ListArtifactStoreDTO getAllArtifactStores(String page)
throws IndyDataException;

/**
* Return the {@link ArtifactStore} instances as a {@link Stream}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,19 @@ public void clear( ChangeSummary summary )
// @WithSpan
public Set<ArtifactStore> getAllArtifactStores()
{
Set<DtxArtifactStore> dtxArtifactStoreSet = storeQuery.getAllArtifactStores();
ListArtifactStoreDTO result = getAllArtifactStores("");
return result.getItems();
}

@Override
// @WithSpan
public ListArtifactStoreDTO getAllArtifactStores(String page)
{
ListDtxArtifactStoreDTO result = storeQuery.getAllArtifactStores(page);
Set<DtxArtifactStore> dtxArtifactStoreSet = result.getItems();
Set<ArtifactStore> artifactStoreSet = new HashSet<>();
dtxArtifactStoreSet.forEach( dtxArtifactStore -> artifactStoreSet.add( toArtifactStore( dtxArtifactStore ) ) );
return artifactStoreSet;
return new ListArtifactStoreDTO(artifactStoreSet, page, result.getNextPage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,40 @@ public Set<DtxArtifactStore> getArtifactStoresByPkgAndType( String packageType,
return result.getItems();
}

public Set<DtxArtifactStore> getAllArtifactStores()
public ListDtxArtifactStoreDTO getAllArtifactStores( String page )
{

BoundStatement bound = preparedArtifactStoresQuery.bind();

if (!page.isEmpty()) {
PagingState currentPage = PagingState.fromString( page );
bound.setPagingState( currentPage );
}

ResultSet result = session.execute( bound );

PagingState nextPage = result.getExecutionInfo().getPagingState();
int remaining = result.getAvailableWithoutFetching();

if (!page.isEmpty()) {
remaining = Math.min( remaining, config.cassandraPageSize );
}

Set<DtxArtifactStore> dtxArtifactStoreSet = new HashSet<>();
result.forEach( row -> dtxArtifactStoreSet.add( toDtxArtifactStore( row ) ) );
for (Row row: result)
{
dtxArtifactStoreSet.add( toDtxArtifactStore( row ) );
if (--remaining == 0) {
break;
}
}

return new ListDtxArtifactStoreDTO( dtxArtifactStoreSet, page, nextPage.toString() );
}

return dtxArtifactStoreSet;
public Set<DtxArtifactStore> getAllArtifactStores()
{
ListDtxArtifactStoreDTO result = getAllArtifactStores("");
return result.getItems();
}

public Boolean isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public Set<ArtifactStore> getAllArtifactStores()
return new HashSet<>( stores.values() );
}

@Override
public ListArtifactStoreDTO getAllArtifactStores(String page)
{
return new ListArtifactStoreDTO(new HashSet<>( stores.values() ), page, "");
}

@Override
public Map<StoreKey, ArtifactStore> getArtifactStoresByKey()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,15 @@ public Response getStoreEmpty()
@Path( "/endpoints/{packageType}" )
@GET
@Produces( APPLICATION_JSON )
public Response getEndpoints( @PathParam( "packageType" ) final String pkgType, @Context final UriInfo uriInfo )
public Response getEndpoints( @PathParam( "packageType" ) final String pkgType, @QueryParam( "page") String page,
@Context final UriInfo uriInfo )
{
Response response;
try
{
final String baseUri = uriInfo.getBaseUriBuilder().path( API_PREFIX ).build().toString();

final EndpointViewListing listing = queryController.getEndpointsListing( pkgType, baseUri, uriFormatter );
final EndpointViewListing listing = queryController.getEndpointsListing( pkgType, baseUri, uriFormatter, page );
response = responseHelper.formatOkResponseWithJsonEntity( listing );

logger.info( "\n\n\n\n\n\n{} Sent all-endpoints:\n\n{}\n\n\n\n\n\n\n", new Date(), listing );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.commonjava.indy.service.repository.jaxrs.version;

import jakarta.ws.rs.QueryParam;
import org.commonjava.indy.service.repository.controller.StatsController;
import org.commonjava.indy.service.repository.exception.IndyWorkflowException;
import org.commonjava.indy.service.repository.jaxrs.ResponseHelper;
Expand Down Expand Up @@ -114,14 +115,14 @@ public Response getPackageTypeNames()
@Path( "/all-endpoints" )
@GET
@Produces( APPLICATION_JSON )
public Response getAllEndpoints( @Context final UriInfo uriInfo )
public Response getAllEndpoints( @QueryParam( "page") String page, @Context final UriInfo uriInfo )
{
Response response;
try
{
final String baseUri = uriInfo.getBaseUriBuilder().path( Constants.API_PREFIX ).build().toString();

final EndpointViewListing listing = statsController.getEndpointsListing( baseUri, uriFormatter );
final EndpointViewListing listing = statsController.getEndpointsListing( baseUri, uriFormatter, page );
response = responseHelper.formatOkResponseWithJsonEntity( listing );

logger.info( "\n\n\n\n\n\n{} Sent all-endpoints:\n\n{}\n\n\n\n\n\n\n", new Date(), listing );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class EndpointViewListing

private List<EndpointView> items;

private String currentPage;

private String nextPage;

public EndpointViewListing()
{
}
Expand All @@ -44,6 +48,14 @@ public EndpointViewListing( final List<EndpointView> items )
this.items = items;
}

public EndpointViewListing( final List<EndpointView> items, final String currentPage, final String nextPage)
{
Collections.sort( items );
this.items = items;
this.currentPage = currentPage;
this.nextPage = nextPage;
}

public List<EndpointView> getItems()
{
return items;
Expand All @@ -60,4 +72,23 @@ public void setItems( final List<EndpointView> items )
this.items = items;
}

public String getCurrentPage()
{
return currentPage;
}

public void setCurrentPage( String currentPage )
{
this.currentPage = currentPage;
}

public String getNextPage()
{
return nextPage;
}

public void setNextPage( String nextPage )
{
this.nextPage = nextPage;
}
}

0 comments on commit 89e0a00

Please sign in to comment.