From 7c54387191a747672c222d7ab35bd5f4aa063001 Mon Sep 17 00:00:00 2001 From: Andrei Molnar <1995am@pm.me> Date: Tue, 17 Nov 2026 23:17:48 +0100 Subject: [PATCH] Add filesystem cache --- pom.xml | 2 +- .../sopa/calllog/CallLogRepository.java | 9 +++++-- .../sopa/data/FileSystemCache.java | 24 +++++++++++++++++++ .../sopa/data/ProjectMonitor.java | 2 ++ .../sopa/project/ProjectRepository.java | 8 ++++++- .../sopa/step/StepRepository.java | 10 +++++--- .../sopa/testcase/TestCaseRepository.java | 7 +++++- 7 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/molnarandrei/sopa/data/FileSystemCache.java diff --git a/pom.xml b/pom.xml index c1cd875..ae1a5c4 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.molnarandrei sopa - 1.0.2 + 1.0.3 sopa jar diff --git a/src/main/java/com/molnarandrei/sopa/calllog/CallLogRepository.java b/src/main/java/com/molnarandrei/sopa/calllog/CallLogRepository.java index 0a69f9b..28fbef9 100644 --- a/src/main/java/com/molnarandrei/sopa/calllog/CallLogRepository.java +++ b/src/main/java/com/molnarandrei/sopa/calllog/CallLogRepository.java @@ -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); } diff --git a/src/main/java/com/molnarandrei/sopa/data/FileSystemCache.java b/src/main/java/com/molnarandrei/sopa/data/FileSystemCache.java new file mode 100644 index 0000000..a27ed4c --- /dev/null +++ b/src/main/java/com/molnarandrei/sopa/data/FileSystemCache.java @@ -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 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)); + } +} diff --git a/src/main/java/com/molnarandrei/sopa/data/ProjectMonitor.java b/src/main/java/com/molnarandrei/sopa/data/ProjectMonitor.java index 2b10b27..cfe75c1 100644 --- a/src/main/java/com/molnarandrei/sopa/data/ProjectMonitor.java +++ b/src/main/java/com/molnarandrei/sopa/data/ProjectMonitor.java @@ -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)) diff --git a/src/main/java/com/molnarandrei/sopa/project/ProjectRepository.java b/src/main/java/com/molnarandrei/sopa/project/ProjectRepository.java index d7410e5..157a34a 100644 --- a/src/main/java/com/molnarandrei/sopa/project/ProjectRepository.java +++ b/src/main/java/com/molnarandrei/sopa/project/ProjectRepository.java @@ -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()); } } diff --git a/src/main/java/com/molnarandrei/sopa/step/StepRepository.java b/src/main/java/com/molnarandrei/sopa/step/StepRepository.java index 34924a4..4987557 100644 --- a/src/main/java/com/molnarandrei/sopa/step/StepRepository.java +++ b/src/main/java/com/molnarandrei/sopa/step/StepRepository.java @@ -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()); } } diff --git a/src/main/java/com/molnarandrei/sopa/testcase/TestCaseRepository.java b/src/main/java/com/molnarandrei/sopa/testcase/TestCaseRepository.java index e7e1f78..1edaddd 100644 --- a/src/main/java/com/molnarandrei/sopa/testcase/TestCaseRepository.java +++ b/src/main/java/com/molnarandrei/sopa/testcase/TestCaseRepository.java @@ -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()); } }