Add filesystem cache

This commit is contained in:
andrei committed 2026-11-17 23:18:43 +01:00
1 parent a7397355ed
commit 7c54387191
7 files changed
+54 -8

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>com.molnarandrei</groupId>
<artifactId>sopa</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>sopa</name>
<packaging>jar</packaging>
@@ -1,6 +1,7 @@
package com.molnarandrei.sopa.calllog;
import com.molnarandrei.sopa.data.DoesNotExistException;
import com.molnarandrei.sopa.data.FileSystemCache;
import com.molnarandrei.sopa.data.EmptyException;
import com.molnarandrei.sopa.data.ProjectMonitors;
import com.molnarandrei.sopa.data.RepositoryException;
@@ -77,7 +78,7 @@ public class CallLogRepository {
throw new DoesNotExistException(responsePath, "Unreadable");
String response = "";
try {
response = Files.readString(responsePath);
response = FileSystemCache.read(responsePath);
} catch (IOException raise) {
throw new RepositoryException(responsePath, "IO error", raise);
}
@@ -100,6 +101,7 @@ public class CallLogRepository {
var yaml = Yaml.toYaml(callLog);
try {
Files.writeString(responsePath, yaml);
FileSystemCache.remove(responsePath);
} catch (IOException io) {
throw new RepositoryException(responsePath, "Cannot write response history", io);
}
@@ -128,8 +130,10 @@ public class CallLogRepository {
if (Files.isRegularFile(responsePath)) responses.add(responsePath);
responses.sort(NEWEST_FIRST);
for (int index = limit; index < responses.size(); index++)
for (int index = limit; index < responses.size(); index++) {
Files.deleteIfExists(responses.get(index));
FileSystemCache.remove(responses.get(index));
}
} catch (IOException io) {
log.log(Level.WARNING, "Cannot delete files over limit of %s".formatted(limit), io);
}
@@ -152,6 +156,7 @@ public class CallLogRepository {
public static void delete(Path responsePath) throws RepositoryException{
try {
Files.deleteIfExists(responsePath);
FileSystemCache.remove(responsePath);
} catch (IOException io) {
throw new RepositoryException(responsePath, "Cannot delete response log.", io);
}
@@ -0,0 +1,24 @@
package com.molnarandrei.sopa.data;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class FileSystemCache {
private static final Map<Path, String> files = new ConcurrentHashMap<>();
public static String read(Path path) throws IOException {
var content = files.get(path);
if (content == null) {
content = Files.readString(path);
files.put(path, content);
}
return content;
}
public static void remove(Path path) {
files.keySet().removeIf(cached -> cached.startsWith(path));
}
}
@@ -43,6 +43,7 @@ public class ProjectMonitor extends PathAlterationListener {
public void onFileChange(Path file) {
if (isOutsideProjectTree(file)) return;
log.info("File changed %s.".formatted(file));
FileSystemCache.remove(file);
if (isProjectFile(file))
Events.emit(new ProjectEvent(ProjectEvent.CHANGED, file));
else if (isTestCaseFile(file))
@@ -78,6 +79,7 @@ public class ProjectMonitor extends PathAlterationListener {
public void onFileDelete(Path file) {
if (isOutsideProjectTree(file)) return;
log.info("File deleted: %s.".formatted(file));
FileSystemCache.remove(file);
if (isProjectFile(file))
Events.emit(new ProjectEvent(ProjectEvent.REMOVED, file));
else if (isTestCaseFile(file))
@@ -3,6 +3,7 @@ package com.molnarandrei.sopa.project;
import com.molnarandrei.sopa.data.Trash;
import com.molnarandrei.sopa.Utils;
import com.molnarandrei.sopa.data.DoesNotExistException;
import com.molnarandrei.sopa.data.FileSystemCache;
import com.molnarandrei.sopa.data.EmptyException;
import com.molnarandrei.sopa.data.ProjectMonitors;
import com.molnarandrei.sopa.data.RepositoryException;
@@ -28,7 +29,7 @@ public class ProjectRepository {
public static Project get(Path projectPath) throws RepositoryException {
if (! Files.isReadable(projectPath)) throw new DoesNotExistException(projectPath, "Unreadable");
try {
var project = Files.readString(projectPath);
var project = FileSystemCache.read(projectPath);
if (project.isBlank())
throw new EmptyException(projectPath, "Project file is empty");
var obj = Yaml.toProject(project);
@@ -52,6 +53,7 @@ public class ProjectRepository {
try {
var yaml = Yaml.toYaml(project);
Files.writeString(projectPath, yaml);
FileSystemCache.remove(projectPath);
ProjectMonitors.trigger(projectPath.getParent());
} catch (IOException io) {
throw new RepositoryException(projectPath, "Cannot write project", io);
@@ -84,6 +86,7 @@ public class ProjectRepository {
if (Files.size(projectFile) == 0) {
var yaml = Yaml.toYaml(project);
Files.writeString(projectFile, yaml);
FileSystemCache.remove(projectFile);
}
} catch (IOException io) {
throw new RepositoryException(projectFile, "Could not create project file", io);
@@ -155,6 +158,7 @@ public class ProjectRepository {
throw new RepositoryException(projectPath, "Could not delete project", io);
} finally {
FileSystemCache.remove(projectDirectory);
ProjectMonitors.trigger(projectDirectory);
}
}
@@ -165,6 +169,8 @@ public class ProjectRepository {
return Trash.keep(projectPath.getParent());
} catch (IOException io) {
throw new RepositoryException(projectPath, "Could not delete project", io);
} finally {
FileSystemCache.remove(projectPath.getParent());
}
}
@@ -3,6 +3,7 @@ package com.molnarandrei.sopa.step;
import com.molnarandrei.sopa.data.Trash;
import com.molnarandrei.sopa.testcase.TestCaseRepository;
import com.molnarandrei.sopa.data.DoesNotExistException;
import com.molnarandrei.sopa.data.FileSystemCache;
import com.molnarandrei.sopa.data.EmptyException;
import com.molnarandrei.sopa.data.FileNames;
import com.molnarandrei.sopa.data.ProjectMonitors;
@@ -17,10 +18,8 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import static com.molnarandrei.sopa.Utils.baseNameOf;
import static com.molnarandrei.sopa.Utils.extractNumber;
@@ -35,7 +34,7 @@ public class StepRepository {
public static Step get(Path stepPath) throws RepositoryException {
if (! Files.isReadable(stepPath)) throw new DoesNotExistException(stepPath, "Unreadable");
try {
var step = Files.readString(stepPath);
var step = FileSystemCache.read(stepPath);
if (step.isBlank())
throw new EmptyException(stepPath, "Step file is empty");
var obj = Yaml.toStep(step);
@@ -55,6 +54,7 @@ public class StepRepository {
try {
var yaml = Yaml.toYaml(step);
Files.writeString(stepPath, yaml);
FileSystemCache.remove(stepPath);
ProjectMonitors.trigger(stepPath.getParent().getParent().getParent());
} catch (IOException io) {
throw new RepositoryException(stepPath, "Cannot write step", io);
@@ -100,6 +100,7 @@ public class StepRepository {
var destination = renameIndexed(toTestCaseFolderPath.resolve(baseName + "-" + (highestIndex + 1)));
try {
Files.move(fromStepFolderPath, destination);
FileSystemCache.remove(fromStepFolderPath);
} catch (IOException io) {
throw new RepositoryException(fromStepPath, "Cannot move step to '%s'".formatted(destination), io);
}
@@ -179,6 +180,7 @@ public class StepRepository {
if (source.equals(destination)) return;
try {
Files.move(source, destination);
FileSystemCache.remove(source);
} catch (IOException io) {
log.log(Level.SEVERE, "While reordering, cannot shift step '%s' to '%s'".formatted(source, destination), io);
throw new RepositoryException(reportedPath, "Cannot shift step to '%s'".formatted(destination), io);
@@ -206,6 +208,7 @@ public class StepRepository {
} catch (IOException io) {
throw new RepositoryException(stepPath, "Cannot delete step '%s'".formatted(stepFolderPath), io);
} finally {
FileSystemCache.remove(stepFolderPath);
ProjectMonitors.trigger(stepPath.getParent().getParent().getParent());
}
}
@@ -217,6 +220,7 @@ public class StepRepository {
} catch (IOException io) {
throw new RepositoryException(stepPath, "Cannot delete step '%s'".formatted(stepPath.getParent()), io);
} finally {
FileSystemCache.remove(stepPath.getParent());
ProjectMonitors.trigger(stepPath.getParent().getParent().getParent());
}
}
@@ -3,6 +3,7 @@ package com.molnarandrei.sopa.testcase;
import com.molnarandrei.sopa.data.Trash;
import com.molnarandrei.sopa.Utils;
import com.molnarandrei.sopa.data.DoesNotExistException;
import com.molnarandrei.sopa.data.FileSystemCache;
import com.molnarandrei.sopa.data.EmptyException;
import com.molnarandrei.sopa.data.ProjectMonitors;
import com.molnarandrei.sopa.data.RepositoryException;
@@ -27,7 +28,7 @@ public class TestCaseRepository {
public static TestCase get(Path testCasePath) throws RepositoryException {
if (! Files.isReadable(testCasePath)) throw new DoesNotExistException(testCasePath, "Unreadable");
try {
var testCase = Files.readString(testCasePath);
var testCase = FileSystemCache.read(testCasePath);
if (testCase.isBlank())
throw new EmptyException(testCasePath, "TestCase file is empty");
var obj = Yaml.toTestCase(testCase);
@@ -47,6 +48,7 @@ public class TestCaseRepository {
try {
var yaml = Yaml.toYaml(testCase);
Files.writeString(testCasePath, yaml);
FileSystemCache.remove(testCasePath);
ProjectMonitors.trigger(testCasePath.getParent().getParent());
} catch (IOException io) {
throw new RepositoryException(testCasePath, "Cannot write testcase", io);
@@ -73,6 +75,7 @@ public class TestCaseRepository {
);
try {
Files.move(fromTestCaseDirectory, destination);
FileSystemCache.remove(fromTestCaseDirectory);
} catch (IOException io) {
throw new RepositoryException(fromTestCasePath, "Cannot move testcase to '%s'".formatted(destination), io);
}
@@ -142,6 +145,7 @@ public class TestCaseRepository {
throw new RepositoryException(testCaseDirectory, "Could not delete testcase", io);
} finally {
FileSystemCache.remove(testCaseDirectory);
ProjectMonitors.trigger(testCasePath.getParent().getParent());
}
}
@@ -165,6 +169,7 @@ public class TestCaseRepository {
} catch (IOException io) {
throw new RepositoryException(testCasePath, "Could not delete testcase", io);
} finally {
FileSystemCache.remove(testCasePath.getParent());
ProjectMonitors.trigger(testCasePath.getParent().getParent());
}
}