Query initial repository location only once at creation

This commit is contained in:
René Pfeuffer
2018-11-23 16:34:38 +01:00
parent c8fc673ce0
commit 282b5687a6
4 changed files with 35 additions and 21 deletions

View File

@@ -10,8 +10,8 @@ import java.io.File;
* <p> * <p>
* <b>WARNING:</b> The Locations provided with this class may not be used from the plugins to store any plugin specific files. * <b>WARNING:</b> The Locations provided with this class may not be used from the plugins to store any plugin specific files.
* <p> * <p>
* Please use the {@link sonia.scm.store.DataStoreFactory } and the {@link sonia.scm.store.DataStore} classes to store data * Please use the {@link sonia.scm.store.DataStoreFactory } and the {@link sonia.scm.store.DataStore} classes to store data<br>
* Please use the {@link sonia.scm.store.BlobStoreFactory } and the {@link sonia.scm.store.BlobStore} classes to store binary files * Please use the {@link sonia.scm.store.BlobStoreFactory } and the {@link sonia.scm.store.BlobStore} classes to store binary files<br>
* Please use the {@link sonia.scm.store.ConfigurationStoreFactory} and the {@link sonia.scm.store.ConfigurationStore} classes to store configurations * Please use the {@link sonia.scm.store.ConfigurationStoreFactory} and the {@link sonia.scm.store.ConfigurationStore} classes to store configurations
* *
* @author Mohamed Karray * @author Mohamed Karray
@@ -28,12 +28,26 @@ public class InitialRepositoryLocationResolver {
this.context = context; this.context = context;
} }
public File getDefaultDirectory(Repository repository) { public InitialRepositoryLocation getRelativeRepositoryPath(Repository repository) {
String initialRepoFolder = getRelativeRepositoryPath(repository); String relativePath = DEFAULT_REPOSITORY_PATH + File.separator + repository.getId();
return new File(context.getBaseDirectory(), initialRepoFolder); return new InitialRepositoryLocation(new File(context.getBaseDirectory(), relativePath), relativePath);
} }
public String getRelativeRepositoryPath(Repository repository) { public static class InitialRepositoryLocation {
return DEFAULT_REPOSITORY_PATH + File.separator + repository.getId(); private final File absolutePath;
private final String relativePath;
public InitialRepositoryLocation(File absolutePath, String relativePath) {
this.absolutePath = absolutePath;
this.relativePath = relativePath;
}
public File getAbsolutePath() {
return absolutePath;
}
public String getRelativePath() {
return relativePath;
}
} }
} }

View File

@@ -6,13 +6,12 @@ import javax.inject.Inject;
import java.io.File; import java.io.File;
/** /**
*
* A Location Resolver for File based Repository Storage. * A Location Resolver for File based Repository Storage.
* * <p>
* WARNING: The Locations provided with this class may not be used from the plugins to store any plugin specific files. * <b>WARNING:</b> The Locations provided with this class may not be used from the plugins to store any plugin specific files.
* * <p>
* Please use the {@link sonia.scm.store.DataStoreFactory } and the {@link sonia.scm.store.DataStore} classes to store data * Please use the {@link sonia.scm.store.DataStoreFactory } and the {@link sonia.scm.store.DataStore} classes to store data<br>
* Please use the {@link sonia.scm.store.BlobStoreFactory } and the {@link sonia.scm.store.BlobStore} classes to store binary files * Please use the {@link sonia.scm.store.BlobStoreFactory } and the {@link sonia.scm.store.BlobStore} classes to store binary files<br>
* Please use the {@link sonia.scm.store.ConfigurationStoreFactory} and the {@link sonia.scm.store.ConfigurationStore} classes to store configurations * Please use the {@link sonia.scm.store.ConfigurationStoreFactory} and the {@link sonia.scm.store.ConfigurationStore} classes to store configurations
* *
* @author Mohamed Karray * @author Mohamed Karray
@@ -35,6 +34,6 @@ public class RepositoryLocationResolver {
PathBasedRepositoryDAO pathBasedRepositoryDAO = (PathBasedRepositoryDAO) repositoryDAO; PathBasedRepositoryDAO pathBasedRepositoryDAO = (PathBasedRepositoryDAO) repositoryDAO;
return pathBasedRepositoryDAO.getPath(repository).toFile(); return pathBasedRepositoryDAO.getPath(repository).toFile();
} }
return initialRepositoryLocationResolver.getDefaultDirectory(repository); return initialRepositoryLocationResolver.getRelativeRepositoryPath(repository).getAbsolutePath();
} }
} }

View File

@@ -34,8 +34,9 @@ public class InitialRepositoryLocationResolverTest {
InitialRepositoryLocationResolver resolver = new InitialRepositoryLocationResolver(context); InitialRepositoryLocationResolver resolver = new InitialRepositoryLocationResolver(context);
Repository repository = new Repository(); Repository repository = new Repository();
repository.setId("ABC"); repository.setId("ABC");
File directory = resolver.getDefaultDirectory(repository); InitialRepositoryLocationResolver.InitialRepositoryLocation directory = resolver.getRelativeRepositoryPath(repository);
assertThat(directory).isEqualTo(new File(context.getBaseDirectory(), "repositories/ABC")); assertThat(directory.getAbsolutePath()).isEqualTo(new File(context.getBaseDirectory(), "repositories/ABC"));
assertThat(directory.getRelativePath()).isEqualTo( "repositories/ABC");
} }
} }

View File

@@ -39,6 +39,7 @@ import sonia.scm.NotFoundException;
import sonia.scm.SCMContextProvider; import sonia.scm.SCMContextProvider;
import sonia.scm.io.FileSystem; import sonia.scm.io.FileSystem;
import sonia.scm.repository.InitialRepositoryLocationResolver; import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.InitialRepositoryLocationResolver.InitialRepositoryLocation;
import sonia.scm.repository.InternalRepositoryException; import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.NamespaceAndName; import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.PathBasedRepositoryDAO; import sonia.scm.repository.PathBasedRepositoryDAO;
@@ -111,14 +112,13 @@ public class XmlRepositoryDAO
@Override @Override
public void add(Repository repository) { public void add(Repository repository) {
File repositoryRootDirectory = initialRepositoryLocationResolver.getDefaultDirectory(repository); InitialRepositoryLocation initialLocation = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository);
try { try {
fileSystem.create(repositoryRootDirectory); fileSystem.create(initialLocation.getAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
throw new InternalRepositoryException(repository, "could not create directory for repository data: " + repositoryRootDirectory, e); throw new InternalRepositoryException(repository, "could not create directory for repository data: " + initialLocation.getAbsolutePath(), e);
} }
String relativeRepositoryPath = initialRepositoryLocationResolver.getRelativeRepositoryPath(repository); RepositoryPath repositoryPath = new RepositoryPath(initialLocation.getRelativePath(), repository.getId(), repository.clone());
RepositoryPath repositoryPath = new RepositoryPath(relativeRepositoryPath, repository.getId(), repository.clone());
repositoryPath.setToBeSynchronized(true); repositoryPath.setToBeSynchronized(true);
synchronized (store) { synchronized (store) {
db.add(repositoryPath); db.add(repositoryPath);