mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Merge with 2.0.0-m3
This commit is contained in:
@@ -61,7 +61,8 @@ class PathDatabase {
|
||||
|
||||
private void ensureParentDirectoryExists() {
|
||||
Path parent = storePath.getParent();
|
||||
if (!Files.exists(parent)) {
|
||||
// Files.exists is slow on java 8
|
||||
if (!parent.toFile().exists()) {
|
||||
try {
|
||||
Files.createDirectories(parent);
|
||||
} catch (IOException ex) {
|
||||
|
||||
@@ -47,12 +47,11 @@ import sonia.scm.store.StoreConstants;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Clock;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author Sebastian Sdorra
|
||||
@@ -69,49 +68,52 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
private final InitialRepositoryLocationResolver locationResolver;
|
||||
private final FileSystem fileSystem;
|
||||
|
||||
@VisibleForTesting
|
||||
Clock clock = Clock.systemUTC();
|
||||
private final Map<String, Path> pathById;
|
||||
private final Map<String, Repository> byId;
|
||||
private final Map<NamespaceAndName, Repository> byNamespaceAndName;
|
||||
|
||||
private final Clock clock;
|
||||
|
||||
private Long creationTime;
|
||||
private Long lastModified;
|
||||
|
||||
private Map<String, Path> pathById;
|
||||
private Map<String, Repository> byId;
|
||||
private Map<NamespaceAndName, Repository> byNamespaceAndName;
|
||||
|
||||
@Inject
|
||||
public XmlRepositoryDAO(SCMContextProvider context, InitialRepositoryLocationResolver locationResolver, FileSystem fileSystem) {
|
||||
this(context, locationResolver, fileSystem, Clock.systemUTC());
|
||||
}
|
||||
|
||||
XmlRepositoryDAO(SCMContextProvider context, InitialRepositoryLocationResolver locationResolver, FileSystem fileSystem, Clock clock) {
|
||||
this.context = context;
|
||||
this.locationResolver = locationResolver;
|
||||
this.fileSystem = fileSystem;
|
||||
|
||||
this.clock = clock;
|
||||
this.creationTime = clock.millis();
|
||||
|
||||
this.pathById = new LinkedHashMap<>();
|
||||
this.byId = new LinkedHashMap<>();
|
||||
this.byNamespaceAndName = new LinkedHashMap<>();
|
||||
this.pathById = new ConcurrentHashMap<>();
|
||||
this.byId = new ConcurrentHashMap<>();
|
||||
this.byNamespaceAndName = new ConcurrentHashMap<>();
|
||||
|
||||
pathDatabase = new PathDatabase(createStorePath());
|
||||
pathDatabase = new PathDatabase(resolveStorePath());
|
||||
read();
|
||||
}
|
||||
|
||||
private void read() {
|
||||
Path storePath = createStorePath();
|
||||
Path storePath = resolveStorePath();
|
||||
|
||||
if (!Files.exists(storePath)) {
|
||||
return;
|
||||
// Files.exists is slow on java 8
|
||||
if (storePath.toFile().exists()) {
|
||||
pathDatabase.read(this::onLoadDates, this::onLoadRepository);
|
||||
}
|
||||
|
||||
pathDatabase.read(this::loadDates, this::loadRepository);
|
||||
}
|
||||
|
||||
private void loadDates(Long creationTime, Long lastModified) {
|
||||
private void onLoadDates(Long creationTime, Long lastModified) {
|
||||
this.creationTime = creationTime;
|
||||
this.lastModified = lastModified;
|
||||
}
|
||||
|
||||
private void loadRepository(String id, Path repositoryPath) {
|
||||
Path metadataPath = createMetadataPath(context.resolve(repositoryPath));
|
||||
private void onLoadRepository(String id, Path repositoryPath) {
|
||||
Path metadataPath = resolveMetadataPath(context.resolve(repositoryPath));
|
||||
|
||||
Repository repository = metadataStore.read(metadataPath);
|
||||
|
||||
@@ -121,7 +123,7 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
Path createStorePath() {
|
||||
Path resolveStorePath() {
|
||||
return context.getBaseDirectory()
|
||||
.toPath()
|
||||
.resolve(StoreConstants.CONFIG_DIRECTORY_NAME)
|
||||
@@ -130,7 +132,7 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
Path createMetadataPath(Path repositoryPath) {
|
||||
Path resolveMetadataPath(Path repositoryPath) {
|
||||
return repositoryPath.resolve(StoreConstants.REPOSITORY_METADATA.concat(StoreConstants.FILE_EXTENSION));
|
||||
}
|
||||
|
||||
@@ -159,7 +161,7 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
try {
|
||||
fileSystem.create(resolvedPath.toFile());
|
||||
|
||||
Path metadataPath = createMetadataPath(resolvedPath);
|
||||
Path metadataPath = resolveMetadataPath(resolvedPath);
|
||||
metadataStore.write(metadataPath, repository);
|
||||
|
||||
synchronized (this) {
|
||||
@@ -227,7 +229,7 @@ public class XmlRepositoryDAO implements PathBasedRepositoryDAO {
|
||||
}
|
||||
|
||||
Path repositoryPath = context.resolve(getPath(repository.getId()));
|
||||
Path metadataPath = createMetadataPath(repositoryPath);
|
||||
Path metadataPath = resolveMetadataPath(repositoryPath);
|
||||
metadataStore.write(metadataPath, clone);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@@ -44,7 +45,7 @@ public final class XmlStreams {
|
||||
}
|
||||
|
||||
public static XMLStreamReader createReader(Path path) throws IOException, XMLStreamException {
|
||||
return createReader(Files.newBufferedReader(path, Charsets.UTF_8));
|
||||
return createReader(Files.newBufferedReader(path, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static XMLStreamReader createReader(File file) throws IOException, XMLStreamException {
|
||||
@@ -57,7 +58,7 @@ public final class XmlStreams {
|
||||
|
||||
|
||||
public static IndentXMLStreamWriter createWriter(Path path) throws IOException, XMLStreamException {
|
||||
return createWriter(Files.newBufferedWriter(path, Charsets.UTF_8));
|
||||
return createWriter(Files.newBufferedWriter(path, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static IndentXMLStreamWriter createWriter(File file) throws IOException, XMLStreamException {
|
||||
|
||||
@@ -67,11 +67,10 @@ class XmlRepositoryDAOTest {
|
||||
}
|
||||
|
||||
private XmlRepositoryDAO createDAO() {
|
||||
XmlRepositoryDAO dao = new XmlRepositoryDAO(context, locationResolver, fileSystem);
|
||||
|
||||
Clock clock = mock(Clock.class);
|
||||
when(clock.millis()).then(ic -> atomicClock.incrementAndGet());
|
||||
dao.clock = clock;
|
||||
|
||||
XmlRepositoryDAO dao = new XmlRepositoryDAO(context, locationResolver, fileSystem, clock);
|
||||
|
||||
return dao;
|
||||
}
|
||||
@@ -83,8 +82,8 @@ class XmlRepositoryDAOTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnCreationTimeAfterCreation() {
|
||||
long now = System.currentTimeMillis();
|
||||
assertThat(dao.getCreationTime()).isBetween(now - 200, now + 200);
|
||||
long now = atomicClock.get();
|
||||
assertThat(dao.getCreationTime()).isEqualTo(now);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -286,7 +285,7 @@ class XmlRepositoryDAOTest {
|
||||
Repository heartOfGold = createHeartOfGold();
|
||||
dao.add(heartOfGold);
|
||||
|
||||
Path storePath = dao.createStorePath();
|
||||
Path storePath = dao.resolveStorePath();
|
||||
assertThat(storePath).isRegularFile();
|
||||
|
||||
String content = content(storePath);
|
||||
@@ -305,7 +304,7 @@ class XmlRepositoryDAOTest {
|
||||
dao.add(heartOfGold);
|
||||
|
||||
Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
|
||||
Path metadataPath = dao.createMetadataPath(repositoryDirectory);
|
||||
Path metadataPath = dao.resolveMetadataPath(repositoryDirectory);
|
||||
|
||||
assertThat(metadataPath).isRegularFile();
|
||||
|
||||
@@ -324,7 +323,7 @@ class XmlRepositoryDAOTest {
|
||||
dao.modify(heartOfGold);
|
||||
|
||||
Path repositoryDirectory = getAbsolutePathFromDao(heartOfGold.getId());
|
||||
Path metadataPath = dao.createMetadataPath(repositoryDirectory);
|
||||
Path metadataPath = dao.resolveMetadataPath(repositoryDirectory);
|
||||
|
||||
String content = content(metadataPath);
|
||||
assertThat(content).contains("Awesome Spaceship");
|
||||
|
||||
Reference in New Issue
Block a user