* under the License.
*/
-import java.io.Serializable;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.database.RepositoryProblemDAO;
import org.apache.maven.archiva.database.SimpleConstraint;
+import java.io.Serializable;
+import java.util.List;
+
/**
* Using a stub for faster tests! Not really used for the unit tests, just for dependency injection.
*/
return null;
}
+ public long count( SimpleConstraint constraint )
+ {
+ return 0; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
public RepositoryProblemDAO getRepositoryProblemDAO()
{
// TODO Auto-generated method stub
* under the License.
*/
-import java.io.Serializable;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.database.RepositoryProblemDAO;
import org.apache.maven.archiva.database.SimpleConstraint;
+import java.io.Serializable;
+import java.util.List;
+
/**
* Using a stub for faster tests! Not really used for the unit tests, just for dependency injection.
*/
return null;
}
+ public long count( SimpleConstraint constraint )
+ {
+ return 0; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
public RepositoryProblemDAO getRepositoryProblemDAO()
{
// TODO Auto-generated method stub
RepositoryProblemDAO getRepositoryProblemDAO();
RepositoryContentStatisticsDAO getRepositoryContentStatisticsDAO();
+
+ long count( SimpleConstraint constraint );
}
public void deleteArtifact( ArchivaArtifact artifact )
throws ArchivaDatabaseException;
+
+ long countArtifacts( DeclarativeConstraint constraint );
}
* @return true if result classes are persistable.
*/
public boolean isResultsPersistable();
+
+ String getCountSql();
}
{
return false;
}
+
+ public String getCountSql()
+ {
+ throw new UnsupportedOperationException( "count not supported" );
+ }
}
implements Constraint
{
private StringBuffer sql = new StringBuffer();
-
+
+ private StringBuffer countSql = new StringBuffer();
+
private Class<?> resultClass;
/**
appendWhereSelectedGroupId( sql );
appendGroupBy( sql );
+ countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
+ countSql.append( " WHERE " );
+ SqlBuilder.appendWhereSelectedRepositories( countSql, "repositoryId", selectedRepositoryIds );
+ countSql.append( " && " );
+ appendWhereSelectedGroupId( countSql );
+ appendGroupBy( countSql );
+
super.params = new Object[] { groupId };
}
appendWhereSelectedGroupId( sql );
appendGroupBy( sql );
+ countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
+ countSql.append( " WHERE " );
+ appendWhereSelectedGroupId( countSql );
+ appendGroupBy( countSql );
+
super.params = new Object[] { groupId };
}
{
appendSelect( sql, isUnique );
sql.append( " WHERE repositoryId == \"" + repoId + "\"" );
-
+
+ if( isUnique )
+ {
+ countSql.append( "SELECT count(this) FROM " ).append( ArchivaArtifactModel.class.getName() );
+ countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
+ countSql.append( " GROUP BY groupId, artifactId" );
+ }
+ else
+ {
+ countSql.append( "SELECT count(artifactId) FROM " ).append( ArchivaArtifactModel.class.getName() );
+ countSql.append( " WHERE repositoryId == \"" ).append( repoId ).append( "\"" );
+ }
+
resultClass = Object[].class;
}
return sql.toString();
}
+ @Override
+ public String getCountSql()
+ {
+ return countSql.toString();
+ }
+
private void appendGroupBy( StringBuffer buf )
{
buf.append( " GROUP BY artifactId ORDER BY artifactId ASCENDING" );
public UniqueGroupIdConstraint()
{
/* this assumes search for no groupId prefix */
- appendSelect( sql );
appendGroupBy( sql );
}
public UniqueGroupIdConstraint( List<String> selectedRepositories )
{
- appendSelect( sql );
sql.append( " WHERE " );
SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositories );
appendGroupBy( sql );
public UniqueGroupIdConstraint( List<String> selectedRepositories, String groupIdPrefix )
{
- appendSelect( sql );
sql.append( " WHERE " );
SqlBuilder.appendWhereSelectedRepositories( sql, "repositoryId", selectedRepositories );
sql.append( " && " );
public UniqueGroupIdConstraint( String groupIdPrefix )
{
- appendSelect( sql );
sql.append( " WHERE " );
appendWhereGroupIdStartsWith( sql );
appendGroupBy( sql );
public String getSelectSql()
{
- return sql.toString();
+ StringBuffer buf = new StringBuffer();
+ appendSelect( buf );
+ buf.append( sql );
+ return buf.toString();
+ }
+
+ @Override
+ public String getCountSql()
+ {
+ StringBuffer buf = new StringBuffer();
+ buf.append( "SELECT count(groupId) FROM " ).append( ArchivaArtifactModel.class.getName() );
+ buf.append( sql );
+ return buf.toString();
}
private void appendGroupBy( StringBuffer buf )
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
-
import javax.jdo.Extent;
import javax.jdo.JDOException;
import javax.jdo.JDOHelper;
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
+ List<?> result = null;
+
try
{
tx.begin();
- List<?> result = null;
-
if ( constraint != null )
{
if ( constraint instanceof DeclarativeConstraint )
result = (List<?>) pm.detachCopyAll( result );
tx.commit();
-
- return result;
}
finally
{
rollbackIfActive( tx );
}
+
+ return result;
}
public List<?> queryObjects( SimpleConstraint constraint )
pm.getFetchPlan().addGroup( constraint.getFetchLimits() );
}
+ List<?> objects;
if ( constraint.getParameters() != null )
{
- return processParameterizedQuery( query, constraint.getParameters() );
+ objects = processParameterizedQuery( query, constraint.getParameters() );
}
-
- return (List<?>) query.execute();
+ else
+ {
+ objects = (List<?>) query.execute();
+ }
+ return objects;
}
private List<?> processConstraint( PersistenceManager pm, Class<?> clazz, DeclarativeConstraint constraint )
return jdoFactory;
}
+ public long countObjects( SimpleConstraint constraint )
+ {
+ PersistenceManager pm = getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+
+ try
+ {
+ tx.begin();
+
+ Query query = pm.newQuery( constraint.getCountSql() );
+
+ if ( constraint.getFetchLimits() != null )
+ {
+ pm.getFetchPlan().addGroup( constraint.getFetchLimits() );
+ }
+
+ List<?> objects;
+ if ( constraint.getParameters() != null )
+ {
+ objects = processParameterizedQuery( query, constraint.getParameters() );
+ }
+ else
+ {
+ objects = (List<?>) query.execute();
+ }
+
+ Long result = !objects.isEmpty() ? (Long) objects.get( 0 ) : 0;
+
+ tx.commit();
+
+ return result;
+ }
+ finally
+ {
+ rollbackIfActive( tx );
+ }
+ }
+
+ public long countObjects( Class<?> clazz, DeclarativeConstraint constraint )
+ {
+ PersistenceManager pm = getPersistenceManager();
+ Transaction tx = pm.currentTransaction();
+
+ Long result = null;
+
+ try
+ {
+ tx.begin();
+
+ Extent extent = pm.getExtent( clazz, true );
+ Query query = pm.newQuery( extent );
+
+ if ( constraint.getFilter() != null )
+ {
+ query.setFilter( constraint.getFilter() );
+ }
+
+ if ( constraint.getVariables() != null )
+ {
+ query.declareVariables( StringUtils.join( constraint.getVariables(), "; " ) );
+ }
+
+ if ( constraint.getFetchLimits() != null )
+ {
+ pm.getFetchPlan().addGroup( constraint.getFetchLimits() );
+ }
+
+ if ( constraint.getWhereCondition() != null )
+ {
+ query.setFilter( constraint.getWhereCondition() );
+ }
+
+ if ( constraint.getDeclaredImports() != null )
+ {
+ query.declareImports( StringUtils.join( constraint.getDeclaredImports(), ", " ) );
+ }
+
+ if ( constraint.getRange() != null )
+ {
+ query.setRange( constraint.getRange()[0], constraint.getRange()[1] );
+ }
+
+ query.setResult( "count(this)" );
+
+ if ( constraint.getDeclaredParameters() != null )
+ {
+ if ( constraint.getParameters() == null )
+ {
+ throw new JDOException( "Unable to use query, there are declared parameters, "
+ + "but no parameter objects to use." );
+ }
+
+ if ( constraint.getParameters().length != constraint.getDeclaredParameters().length )
+ {
+ throw new JDOException( "Unable to use query, there are <" + constraint.getDeclaredParameters().length
+ + "> declared parameters, yet there are <" + constraint.getParameters().length
+ + "> parameter objects to use. This should be equal." );
+ }
+
+ query.declareParameters( StringUtils.join( constraint.getDeclaredParameters(), ", " ) );
+
+ result = (Long) query.executeWithArray( constraint.getParameters() );
+ }
+ else
+ {
+ result = (Long) query.execute();
+ }
+ tx.commit();
+ }
+ finally
+ {
+ rollbackIfActive( tx );
+ }
+
+ return result;
+ }
}
{
return repositoryContentStatisticsDAO;
}
+
+ public long count( SimpleConstraint constraint )
+ {
+ return jdo.countObjects( constraint );
+ }
}
* under the License.
*/
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.Constraint;
+import org.apache.maven.archiva.database.DeclarativeConstraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaArtifactModel;
import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* JdoArtifactDAO
*
throws ObjectNotFoundException, ArchivaDatabaseException
{
List<ArchivaArtifactModel> results = (List<ArchivaArtifactModel>) jdo.queryObjects( ArchivaArtifactModel.class, constraint );
- if ( results == null )
- {
- return null;
- }
- List<ArchivaArtifact> ret = new ArrayList<ArchivaArtifact>();
- for ( ArchivaArtifactModel model : results )
+ List<ArchivaArtifact> ret = null;
+ if ( results != null )
{
- ret.add( new ArchivaArtifact( model ) );
+ ret = new ArrayList<ArchivaArtifact>();
+ for ( ArchivaArtifactModel model : results )
+ {
+ ret.add( new ArchivaArtifact( model ) );
+ }
}
return ret;
{
jdo.removeObject( artifact.getModel() );
}
+
+ public long countArtifacts( DeclarativeConstraint constraint )
+ {
+ return jdo.countObjects( ArchivaArtifactModel.class, constraint );
+ }
}
* under the License.
*/
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.database.ArchivaDAO;
-import org.apache.maven.archiva.database.ArchivaDatabaseException;
-import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.database.constraints.ArtifactsByRepositoryConstraint;
import org.apache.maven.archiva.database.constraints.MostRecentRepositoryScanStatistics;
import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint;
import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint;
-import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.RepositoryContentStatistics;
import org.apache.maven.archiva.repository.RepositoryException;
import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* ArchivaRepositoryScanningTaskExecutor
*
dbstats.setWhenGathered( stats.getWhenGathered() );
// total artifact count
- try
- {
- // note that when gathered is the end of the scan, so we look for all those before that time
- List<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts(
- new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) );
- dbstats.setTotalArtifactCount( artifacts.size() );
- }
- catch ( ObjectNotFoundException oe )
- {
- log.error( "Object not found in the database : " + oe.getMessage() );
- }
- catch ( ArchivaDatabaseException ae )
- {
- log.error( "Error occurred while querying artifacts for artifact count : " + ae.getMessage() );
- }
+ // note that when gathered is the end of the scan, so we look for all those before that time
+ dbstats.setTotalArtifactCount( dao.getArtifactDAO().countArtifacts(
+ new ArtifactsByRepositoryConstraint( arepo.getId(), stats.getWhenGathered(), "groupId", true ) ) );
// total repo size -- TODO: needs to exclude ignored files (eg .svn)
long size = FileUtils.sizeOfDirectory( new File( arepo.getLocation() ) );
List<String> repos = new ArrayList<String>();
repos.add( arepo.getId() );
- List<String> groupIds = (List<String>) dao.query( new UniqueGroupIdConstraint( repos ) );
- dbstats.setTotalGroupCount( groupIds.size() );
+ dbstats.setTotalGroupCount( dao.count( new UniqueGroupIdConstraint( repos ) ) );
- List<Object[]> artifactIds =
- (List<Object[]>) dao.query( new UniqueArtifactIdConstraint( arepo.getId(), true ) );
- dbstats.setTotalProjectCount( artifactIds.size() );
+ dbstats.setTotalProjectCount( dao.count( new UniqueArtifactIdConstraint( arepo.getId(), true ) ) );
return dbstats;
}
* under the License.
*/
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.Constraint;
+import org.apache.maven.archiva.database.DeclarativeConstraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.ArchivaArtifact;
+import java.util.List;
+
/**
* ArtifactDAO stub.
*
}
+ public long countArtifacts( DeclarativeConstraint constraint )
+ {
+ return artifacts.size();
+ }
+
public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier,
String type, String repositoryId )
throws ObjectNotFoundException, ArchivaDatabaseException
* under the License.
*/
-import java.io.Serializable;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.ProjectModelDAO;
import org.apache.maven.archiva.database.SimpleConstraint;
import org.apache.maven.archiva.model.RepositoryContentStatistics;
+import java.io.Serializable;
+import java.util.List;
+
/**
* AbstractManagedRepositoryArchivaDAOStub
*
{
return repoContentStatisticsDAO;
}
+
+ public long count( SimpleConstraint constraint )
+ {
+ return query( constraint ).size();
+ }
}
package org.apache.maven.archiva.web.action.admin.repositories;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
import junit.framework.Assert;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
import org.apache.maven.archiva.model.RepositoryContentStatistics;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
return repositoryContentStatisticsDAO;
}
+ public long count( SimpleConstraint constraint )
+ {
+ return query( constraint ).size();
+ }
+
public void setArtifactDao( ArtifactDAO artifactDao )
{
this.artifactDao = artifactDao;
* under the License.
*/
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.Constraint;
+import org.apache.maven.archiva.database.DeclarativeConstraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaArtifactModel;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* ArtifactDAOStub
*
}
+ public long countArtifacts( DeclarativeConstraint constraint )
+ {
+ return queryArtifacts( constraint ).size();
+ }
+
public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier,
String type, String repositoryId )
throws ObjectNotFoundException, ArchivaDatabaseException
}
public List<ArchivaArtifact> queryArtifacts( Constraint constraint )
- throws ObjectNotFoundException, ArchivaDatabaseException
{
List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();
* under the License.
*/
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.List;
-
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.Constraint;
-import org.apache.maven.archiva.database.ObjectNotFoundException;
+import org.apache.maven.archiva.database.DeclarativeConstraint;
import org.apache.maven.archiva.model.ArchivaArtifact;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
/**
* Stub used for RssFeedServlet unit test.
*
}
+ public long countArtifacts( DeclarativeConstraint constraint )
+ {
+ return queryArtifacts( constraint ).size();
+ }
+
public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier,
String type, String repositoryId )
- throws ObjectNotFoundException, ArchivaDatabaseException
{
// TODO Auto-generated method stub
return null;
}
public List<ArchivaArtifact> queryArtifacts( Constraint constraint )
- throws ObjectNotFoundException, ArchivaDatabaseException
{
List<ArchivaArtifact> artifacts = new ArrayList<ArchivaArtifact>();