[MRM-1729] add MetadataRepository#hasMetadataFacet to prevent slow startup time.

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1423878 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-12-19 15:19:53 +00:00
parent 3143803202
commit 58afcb1114
10 changed files with 149 additions and 18 deletions

View File

@ -41,10 +41,6 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<!--dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
</dependency-->
<dependency>
<groupId>org.apache.archiva.redback.components.cache</groupId>
<artifactId>spring-cache-api</artifactId>

View File

@ -309,6 +309,7 @@ public class DefaultRepositoryArchivaTaskScheduler
MetadataRepository metadataRepository )
throws MetadataRepositoryException
{
return repositoryStatisticsManager.getLastStatistics( metadataRepository, repoConfig.getId() ) != null;
}

View File

@ -54,6 +54,17 @@ public interface MetadataRepository
List<String> getMetadataFacets( String repositoryId, String facetId )
throws MetadataRepositoryException;
/**
*
* @param repositoryId
* @param facetId
* @return true if the repository datas for this facetId
* @since 1.4-M4
* @throws MetadataRepositoryException
*/
boolean hasMetadataFacet( String repositoryId, String facetId )
throws MetadataRepositoryException;
MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name )
throws MetadataRepositoryException;

View File

@ -40,8 +40,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@ -49,9 +51,10 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
@RunWith (ArchivaSpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
@RunWith( ArchivaSpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
public abstract class AbstractMetadataRepositoryTest
extends TestCase
{
@ -703,6 +706,52 @@ public abstract class AbstractMetadataRepositoryTest
assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
}
@Test
public void hasMetadataFacetStart()
throws Exception
{
assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
}
@Test
public void hasMetadataFacet()
throws Exception
{
assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
Calendar cal = Calendar.getInstance();
repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
cal.add( Calendar.MINUTE, 2 );
repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
cal.add( Calendar.MINUTE, 2 );
repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
Assertions.assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
Assertions.assertThat( facets ).isNotNull().isEmpty();
}
@Test
public void testGetArtifacts()
throws Exception
@ -1231,6 +1280,7 @@ public abstract class AbstractMetadataRepositoryTest
Assertions.assertThat( artifactMetadatas ).isNotNull().isEmpty();
}
private static ProjectMetadata createProject()
{
return createProject( TEST_NAMESPACE );
@ -1274,6 +1324,51 @@ public abstract class AbstractMetadataRepositoryTest
}
}
private static class KindOfRepositoryStatistics
implements MetadataFacet
{
private String value;
private Date date;
static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
private KindOfRepositoryStatistics( String value, Date date )
{
this.value = value;
this.date = date;
}
public String getName()
{
return createNameFormat().format( date );
}
private static SimpleDateFormat createNameFormat()
{
SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
fmt.setTimeZone( UTC_TIME_ZONE );
return fmt;
}
public String getFacetId()
{
return KindOfRepositoryStatistics.class.getName();
}
public Map<String, String> toProperties()
{
return Collections.emptyMap();
}
public void fromProperties( Map<String, String> properties )
{
// no op
}
}
private static class TestMetadataFacet
implements MetadataFacet
{

View File

@ -400,6 +400,26 @@ public class JcrMetadataRepository
}
public boolean hasMetadataFacet( String repositoryId, String facetId )
throws MetadataRepositoryException
{
try
{
Node node = getJcrSession().getRootNode().getNode( getFacetPath( repositoryId, facetId ) );
return JcrUtils.getChildNodes( node ).iterator().hasNext();
}
catch ( PathNotFoundException e )
{
// ignored - the facet doesn't exist, so return false
return false;
}
catch ( RepositoryException e )
{
throw new MetadataRepositoryException( e.getMessage(), e );
}
}
public List<String> getMetadataFacets( String repositoryId, String facetId )
throws MetadataRepositoryException
{

View File

@ -24,6 +24,7 @@ import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import java.io.File;
@ -48,7 +49,7 @@ public class JcrMetadataRepositoryTest
super.setUp();
File directory = new File( "target/test-repositories" );
if (directory.exists())
if ( directory.exists() )
{
FileUtils.deleteDirectory( directory );
}
@ -86,4 +87,6 @@ public class JcrMetadataRepositoryTest
super.tearDown();
}
}

View File

@ -59,6 +59,12 @@ public class DefaultRepositoryStatisticsManager
private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
public boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
throws MetadataRepositoryException
{
return metadataRepository.hasMetadataFacet( repositoryId, RepositoryStatistics.FACET_ID );
}
public RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
throws MetadataRepositoryException
{

View File

@ -30,6 +30,9 @@ public interface RepositoryStatisticsManager
RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
throws MetadataRepositoryException;
boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
throws MetadataRepositoryException;
void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
Date endTime, long totalFiles, long newFiles )
throws MetadataRepositoryException;

View File

@ -21,10 +21,12 @@ package org.apache.archiva.metadata.repository.stats;
import junit.framework.TestCase;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.RepositorySessionFactory;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.TransientRepository;
import javax.inject.Inject;
import javax.jcr.ImportUUIDBehavior;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
@ -39,6 +41,7 @@ import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
import org.junit.After;
import org.junit.Before;
@ -46,6 +49,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Mockito.*;
@RunWith( ArchivaBlockJUnit4ClassRunner.class )
public class JcrRepositoryStatisticsGatheringTest
extends TestCase
@ -60,6 +64,9 @@ public class JcrRepositoryStatisticsGatheringTest
private MetadataRepository metadataRepository;
@Inject
private RepositorySessionFactory repositorySessionFactory;
private Session session;
@Override

11
pom.xml
View File

@ -1357,17 +1357,6 @@
</exclusion>
</exclusions>
</dependency>
<!--dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>