[MRM-556]

- respect set retention count when purging by number of days old (used same logic for purging by retention count)
- updated edit managed repository validation for the number of days old minimum range
- adjusted DaysOldRepositoryPurgeTest for the changes


git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@592297 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2007-11-06 06:16:12 +00:00
parent ed1fe1cb4c
commit 293bd8e2f2
5 changed files with 83 additions and 15 deletions

View File

@ -20,10 +20,12 @@
*/ */
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.apache.maven.archiva.common.utils.VersionComparator;
import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.common.utils.VersionUtil;
import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.indexer.RepositoryContentIndex; import org.apache.maven.archiva.indexer.RepositoryContentIndex;
import org.apache.maven.archiva.model.ArtifactReference; import org.apache.maven.archiva.model.ArtifactReference;
import org.apache.maven.archiva.model.VersionedReference;
import org.apache.maven.archiva.repository.ContentNotFoundException; import org.apache.maven.archiva.repository.ContentNotFoundException;
import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.ManagedRepositoryContent;
import org.apache.maven.archiva.repository.layout.LayoutException; import org.apache.maven.archiva.repository.layout.LayoutException;
@ -31,8 +33,11 @@
import java.io.File; import java.io.File;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -49,11 +54,14 @@ public class DaysOldRepositoryPurge
private int daysOlder; private int daysOlder;
private int retentionCount;
public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao, public DaysOldRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
int daysOlder, Map<String, RepositoryContentIndex> indices ) int daysOlder, int retentionCount, Map<String, RepositoryContentIndex> indices )
{ {
super( repository, artifactDao, indices ); super( repository, artifactDao, indices );
this.daysOlder = daysOlder; this.daysOlder = daysOlder;
this.retentionCount = retentionCount;
timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); timestampParser = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE ); timestampParser.setTimeZone( DateUtils.UTC_TIME_ZONE );
} }
@ -75,12 +83,29 @@ public void process( String path )
Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE ); Calendar olderThanThisDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
olderThanThisDate.add( Calendar.DATE, -daysOlder ); olderThanThisDate.add( Calendar.DATE, -daysOlder );
// respect retention count
VersionedReference reference = new VersionedReference();
reference.setGroupId( artifact.getGroupId() );
reference.setArtifactId( artifact.getArtifactId() );
reference.setVersion( artifact.getVersion() );
List<String> versions = new ArrayList<String>( repository.getVersions( reference ) );
Collections.sort( versions, VersionComparator.getInstance() );
// Is this a generic snapshot "1.0-SNAPSHOT" ? // Is this a generic snapshot "1.0-SNAPSHOT" ?
if ( VersionUtil.isGenericSnapshot( artifact.getVersion() ) ) if ( VersionUtil.isGenericSnapshot( artifact.getVersion() ) )
{ {
if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() ) if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
{ {
doPurgeAllRelated( artifactFile ); if ( retentionCount > versions.size() )
{
// Done. nothing to do here. skip it.
return;
}
purgeArtifact( versions, artifactFile );
} }
} }
// Is this a timestamp snapshot "1.0-20070822.123456-42" ? // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
@ -90,11 +115,24 @@ else if ( VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() ) if ( timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis() )
{ {
doPurgeAllRelated( artifactFile ); if ( retentionCount > versions.size() )
{
// Done. nothing to do here. skip it.
return;
}
purgeArtifact( versions, artifactFile );
} }
else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() ) else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
{ {
doPurgeAllRelated( artifactFile );
if ( retentionCount > versions.size() )
{
// Done. nothing to do here. skip it.
return;
}
purgeArtifact( versions, artifactFile );
} }
} }
} }
@ -102,6 +140,10 @@ else if ( artifactFile.lastModified() < olderThanThisDate.getTimeInMillis() )
{ {
throw new RepositoryPurgeException( le.getMessage() ); throw new RepositoryPurgeException( le.getMessage() );
} }
catch ( ContentNotFoundException e )
{
throw new RepositoryPurgeException( e.getMessage() );
}
} }
private Calendar uniqueSnapshotToCalendar( String version ) private Calendar uniqueSnapshotToCalendar( String version )
@ -152,4 +194,19 @@ private void doPurgeAllRelated( File artifactFile ) throws LayoutException
// TODO: Log this? // TODO: Log this?
} }
} }
private void purgeArtifact( List<String> versions, File artifactFile )
throws LayoutException
{
int countToPurge = versions.size() - retentionCount;
while( versions.iterator().hasNext() )
{
if ( countToPurge-- <= 0 )
{
break;
}
doPurgeAllRelated( artifactFile );
}
}
} }

View File

@ -149,7 +149,7 @@ public void beginScan( ManagedRepositoryConfiguration repository )
if ( repository.getDaysOlder() != 0 ) if ( repository.getDaysOlder() != 0 )
{ {
repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository repoPurge = new DaysOldRepositoryPurge( repositoryContent, dao.getArtifactDAO(), repository
.getDaysOlder(), indices ); .getDaysOlder(), repository.getRetentionCount(), indices );
} }
else else
{ {

View File

@ -35,18 +35,18 @@ public class DaysOldRepositoryPurgeTest
extends AbstractRepositoryPurgeTest extends AbstractRepositoryPurgeTest
{ {
private Map<String, RepositoryContentIndex> map;
protected void setUp() protected void setUp()
throws Exception throws Exception
{ {
super.setUp(); super.setUp();
Map<String, RepositoryContentIndex> map = new HashMap<String, RepositoryContentIndex>(); map = new HashMap<String, RepositoryContentIndex>();
map.put( "filecontent", new LuceneRepositoryContentIndexStub() ); map.put( "filecontent", new LuceneRepositoryContentIndexStub() );
map.put( "hashcodes", new LuceneRepositoryContentIndexStub() ); map.put( "hashcodes", new LuceneRepositoryContentIndexStub() );
map.put( "bytecode", new LuceneRepositoryContentIndexStub() ); map.put( "bytecode", new LuceneRepositoryContentIndexStub() );
repoPurge =
new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(), map );
} }
private void setLastModified( String dirPath ) private void setLastModified( String dirPath )
@ -62,6 +62,10 @@ private void setLastModified( String dirPath )
public void testByLastModified() public void testByLastModified()
throws Exception throws Exception
{ {
repoPurge =
new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
1, map );
populateDbForTestByLastModified(); populateDbForTestByLastModified();
String repoRoot = prepareTestRepo(); String repoRoot = prepareTestRepo();
@ -83,6 +87,10 @@ public void testByLastModified()
public void testMetadataDrivenSnapshots() public void testMetadataDrivenSnapshots()
throws Exception throws Exception
{ {
repoPurge =
new DaysOldRepositoryPurge( getRepository(), dao, getRepoConfiguration().getDaysOlder(),
getRepoConfiguration().getRetentionCount(), map );
populateDbForTestMetadataDrivenSnapshots(); populateDbForTestMetadataDrivenSnapshots();
String repoRoot = prepareTestRepo(); String repoRoot = prepareTestRepo();

View File

@ -46,8 +46,11 @@ public void deleteRecords( Collection records )
throws RepositoryIndexException throws RepositoryIndexException
{ {
// TODO Auto-generated method stub // TODO Auto-generated method stub
if( records.size() != 0 )
{
Assert.assertEquals( 2, records.size() ); Assert.assertEquals( 2, records.size() );
} }
}
public boolean exists() public boolean exists()
throws RepositoryIndexException throws RepositoryIndexException

View File

@ -46,7 +46,7 @@
</field> </field>
<field name="repository.daysOlder"> <field name="repository.daysOlder">
<field-validator type="int"> <field-validator type="int">
<param name="min">1</param> <param name="min">0</param>
<param name="max">1000</param> <param name="max">1000</param>
<message>Repository Purge By Days Older Than needs to be between ${min} and ${max}.</message> <message>Repository Purge By Days Older Than needs to be between ${min} and ${max}.</message>
</field-validator> </field-validator>