Skip to content

Commit

Permalink
pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Modi <[email protected]>
  • Loading branch information
hjmodi committed Apr 3, 2024
1 parent de1148f commit 54a0800
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ public List<ArtifactStore> getAllOfType( final StoreType type )
}
}

public List<ArtifactStore> getAllOfType( final String packageType, final StoreType type )
throws IndyWorkflowException

public List<ArtifactStore> getAllOfType( final String packageType, final StoreType type, String page )
throws IndyWorkflowException
{
try
{
ArtifactStoreQuery<ArtifactStore> query = storeManager.query().storeTypes( type );
if ( !ALL_PACKAGE_TYPES.equals( packageType ) )
{
return new ArrayList<>( storeManager.getArtifactStoresByPkgAndType( packageType, type ) );
return new ArrayList<>( storeManager.getArtifactStoresByPkgAndType( packageType, type, page ) );
}
else
{
Expand All @@ -135,6 +136,13 @@ public List<ArtifactStore> getAllOfType( final String packageType, final StoreTy
}
}


public List<ArtifactStore> getAllOfType( final String packageType, final StoreType type )
throws IndyWorkflowException
{
return getAllOfType( packageType, type, "" );
}

public List<ArtifactStore> getAllStores()
throws IndyWorkflowException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,6 @@ Set<Group> affectedBy( Collection<StoreKey> keys )

Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType storeType );

Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType storeType, String page );

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public class CassandraConfiguration
@ConfigProperty( name = "cassandra.enabled", defaultValue = "false" )
Boolean enabled;

@Inject
@ConfigProperty( name = "cassandra.page_size", defaultValue = "5000" )
int cassandraPageSize;

@Inject
@ConfigProperty( name = "cassandra.host", defaultValue = "localhost" )
String cassandraHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,24 @@ public Set<StoreKey> getStoreKeysByPkgAndType( final String pkg, final StoreType
}

@Override
public Set<ArtifactStore> getArtifactStoresByPkgAndType( final String pkg, final StoreType type )
public Set<ArtifactStore> getArtifactStoresByPkgAndType( final String pkg, final StoreType type, String page )
{

logger.trace( "Get stores: {}/{}", pkg, type );

Set<DtxArtifactStore> dtxArtifactStoreSet = storeQuery.getArtifactStoresByPkgAndType( pkg, type );
Set<DtxArtifactStore> dtxArtifactStoreSet = storeQuery.getArtifactStoresByPkgAndType( pkg, type, page );
Set<ArtifactStore> storeSet = new HashSet<>();
dtxArtifactStoreSet.forEach( dtxArtifactStore -> storeSet.add( toArtifactStore( dtxArtifactStore ) ) );
return storeSet;
}

@Override
public Set<ArtifactStore> getArtifactStoresByPkgAndType( final String pkg, final StoreType type )
{

return getArtifactStoresByPkgAndType( pkg, type, "" );
}

@Override
public boolean hasArtifactStore( StoreKey key )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.commonjava.indy.service.repository.data.cassandra;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PagingState;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
Expand Down Expand Up @@ -142,19 +143,42 @@ public DtxArtifactStore getArtifactStore( String packageType, StoreType type, St
return toDtxArtifactStore( result.one() );
}

public Set<DtxArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType type )
public Set<DtxArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType type, String page )
{

BoundStatement bound =
preparedArtifactStoresQueryByKeys.bind( CassandraStoreUtil.getTypeKey( packageType, type.name() ) );
preparedArtifactStoresQueryByKeys.bind( CassandraStoreUtil.getTypeKey( packageType, type.name() ) );

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 dtxArtifactStoreSet;
}

public Set<DtxArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType type )
{
return getArtifactStoresByPkgAndType( packageType, type, "" );
}

public Set<DtxArtifactStore> getAllArtifactStores()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Stream<StoreKey> streamArtifactStoreKeys()
}

@Override
public Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType storeType )
public Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType storeType, String page )
{
return stores.values()
.stream()
Expand All @@ -165,6 +165,12 @@ public Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, Sto
.collect( Collectors.toSet() );
}

@Override
public Set<ArtifactStore> getArtifactStoresByPkgAndType( String packageType, StoreType storeType )
{
return getArtifactStoresByPkgAndType( packageType, storeType, "" );
}

@Override
protected ArtifactStore putArtifactStoreInternal( StoreKey storeKey, ArtifactStore store )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,46 @@ public Response getAll( final @Parameter(
@PathParam( "packageType" ) String packageType,
@Parameter( name = "type", in = PATH, description = "The type of the repository.",
content = @Content( schema = @Schema( implementation = StoreType.class ) ),
required = true ) @PathParam( "type" ) String type )
required = true ) @PathParam( "type" ) String type,
@QueryParam( "page") String page
)
{

final StoreType st = StoreType.get( type );

Response response;
try
{
final List<ArtifactStore> stores = adminController.getAllOfType( packageType, st, page );

logger.info( "Returning listing containing stores:\n\t{}", new JoinString( "\n\t", stores ) );

final StoreListingDTO<ArtifactStore> dto = new StoreListingDTO<>( stores );

response = responseHelper.formatOkResponseWithJsonEntity( dto );
}
catch ( final IndyWorkflowException e )
{
logger.error( e.getMessage(), e );
response = responseHelper.formatResponse( e );
}

return response;
}


@Operation( description = "Retrieve the definitions of all artifact stores of a given type on the system" )
@APIResponse( responseCode = "200",
content = @Content( schema = @Schema( implementation = StoreListingDTO.class ) ),
description = "The store definitions" )
@GET
@Produces( APPLICATION_JSON )
public Response getAllPaginated( final @Parameter(
description = "Filter only stores that support the package type (eg. maven, npm). NOTE: '_all' returns all." )
@PathParam( "packageType" ) String packageType,
@Parameter( name = "type", in = PATH, description = "The type of the repository.",
content = @Content( schema = @Schema( implementation = StoreType.class ) ),
required = true ) @PathParam( "type" ) String type )
{

final StoreType st = StoreType.get( type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@

@Schema( type = SchemaType.OBJECT, discriminatorProperty = "type", description = "List of artifact store definitions" )
public class StoreListingDTO<T extends ArtifactStore>
implements Iterable<T>
implements Iterable<T>
{

@JsonProperty
@Schema( implementation = ArtifactStore.class, description = "The store definition list", required = true )
private List<T> items;

@JsonProperty( "current_page" )
private String currentPage;

@JsonProperty( "next_page" )
private String nextPage;

public StoreListingDTO()
{
}
Expand All @@ -42,6 +48,13 @@ public StoreListingDTO( final List<T> items )
this.items = items;
}

public StoreListingDTO( final List<T> items, final String currentPage, final String nextPage )
{
this.items = items;
this.currentPage = currentPage;
this.nextPage = nextPage;
}

public List<T> getItems()
{
return items == null ? Collections.emptyList() : items;
Expand All @@ -57,6 +70,18 @@ public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append( "StoreListingDTO[" );
if ( !currentPage.isEmpty() )
{
sb.append( "\ncurrentPage=" ).append( currentPage );
}
if ( !nextPage.isEmpty() )
{
sb.append( "\nnextPage=" ).append( nextPage );
}
if ( !currentPage.isEmpty() || !nextPage.isEmpty() )
{
sb.append( "\n [" );
}
if ( items == null || items.isEmpty() )
{
sb.append( "NO STORES" );
Expand All @@ -68,7 +93,10 @@ public String toString()
sb.append( "\n " ).append( item );
}
}

if ( !currentPage.isEmpty() || !nextPage.isEmpty() )
{
sb.append( "\n ]" );
}
sb.append( "\n]" );
return sb.toString();
}
Expand All @@ -79,4 +107,23 @@ public Iterator<T> iterator()
return getItems().iterator();
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public List<ArtifactStore> getAllOfType( final String packageType, final StoreTy
throw new IndyWorkflowException( Response.Status.NOT_FOUND.getStatusCode(), "Not found" );
}

@Override
public List<ArtifactStore> getAllOfType( final String packageType, final StoreType type, String page )
throws IndyWorkflowException
{
if ( MAVEN_PKG_KEY.equals( packageType ) )
{
if ( type == StoreType.remote )
{
RemoteRepository repo1 = new RemoteRepository( MAVEN_PKG_KEY, "test1", "http://repo.test1" );
RemoteRepository repo2 = new RemoteRepository( MAVEN_PKG_KEY, "test2", "http://repo.test2" );
return Arrays.asList( repo1, repo2 );
}
if ( type == StoreType.hosted )
{
return Collections.emptyList();
}
}
throw new IndyWorkflowException( Response.Status.NOT_FOUND.getStatusCode(), "Not found" );
}

@Override
public ArtifactStore get( final StoreKey key )
{
Expand Down

0 comments on commit 54a0800

Please sign in to comment.