Skip to content

Commit bb92715

Browse files
committed
Remove CachingPythonFileReader and related file-level caching features for simplification and refactoring.
1 parent 262877a commit bb92715

File tree

9 files changed

+36
-200
lines changed

9 files changed

+36
-200
lines changed

cache/python-executor-spring-boot-cache-autoconfigure/src/main/java/io/maksymuimanov/python/autoconfigure/FileCacheLevelCondition.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

cache/python-executor-spring-boot-cache-autoconfigure/src/main/java/io/maksymuimanov/python/autoconfigure/PythonCacheAutoConfiguration.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.maksymuimanov.python.cache.HashCacheKeyGenerator;
66
import io.maksymuimanov.python.executor.CachingPythonExecutor;
77
import io.maksymuimanov.python.executor.PythonExecutor;
8-
import io.maksymuimanov.python.file.CachingPythonFileReader;
98
import io.maksymuimanov.python.file.PythonFileReader;
109
import io.maksymuimanov.python.processor.CachingPythonProcessor;
1110
import io.maksymuimanov.python.processor.PythonProcessor;
@@ -48,7 +47,6 @@
4847
*
4948
* @see PythonCacheProperties
5049
* @see CacheKeyGenerator
51-
* @see CachingPythonFileReader
5250
* @see CachingPythonResolverHolder
5351
* @see CachingPythonExecutor
5452
* @see CachingPythonProcessor
@@ -73,26 +71,6 @@ public CacheKeyGenerator cacheKeyGenerator(PythonCacheProperties cacheProperties
7371
return new HashCacheKeyGenerator(keyProperties.getHashAlgorithm(), keyProperties.getCharset(), keyProperties.getDelimiter());
7472
}
7573

76-
/**
77-
* Wraps the existing {@link PythonFileReader} with caching capabilities
78-
* when file cache level is enabled.
79-
*
80-
* @param cacheProperties non-null Python cache configuration properties
81-
* @param pythonFileReader non-null delegate {@link PythonFileReader} bean
82-
* @param cacheManager non-null Spring cache manager for cache resolution
83-
* @return a caching-enabled {@link PythonFileReader} bean marked as primary
84-
*/
85-
@Bean
86-
@Primary
87-
@ConditionalOnBean(PythonFileReader.class)
88-
@Conditional(FileCacheLevelCondition.class)
89-
public PythonFileReader cachingPythonFileHandler(PythonCacheProperties cacheProperties,
90-
PythonFileReader pythonFileReader,
91-
CacheManager cacheManager) {
92-
var nameProperties = cacheProperties.getName();
93-
return new CachingPythonFileReader(nameProperties.getFileBodies(), nameProperties.getFilePaths(), pythonFileReader, cacheManager);
94-
}
95-
9674
/**
9775
* Wraps the existing {@link PythonResolverHolder} with caching capabilities
9876
* when resolver cache level is enabled.

cache/python-executor-spring-boot-cache-autoconfigure/src/main/java/io/maksymuimanov/python/autoconfigure/PythonCacheProperties.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.maksymuimanov.python.cache.HashCacheKeyGenerator;
44
import io.maksymuimanov.python.executor.CachingPythonExecutor;
5-
import io.maksymuimanov.python.file.CachingPythonFileReader;
65
import io.maksymuimanov.python.processor.CachingPythonProcessor;
76
import io.maksymuimanov.python.resolver.CachingPythonResolverHolder;
87
import lombok.Getter;
@@ -37,7 +36,6 @@
3736
* }</pre>
3837
*
3938
* @see HashCacheKeyGenerator
40-
* @see CachingPythonFileReader
4139
* @see CachingPythonResolverHolder
4240
* @see CachingPythonExecutor
4341
* @see CachingPythonProcessor
@@ -54,7 +52,7 @@ public class PythonCacheProperties {
5452
/**
5553
* Defines which parts of the Python processing flow are cached.
5654
*/
57-
private PythonCacheLevel[] levels = new PythonCacheLevel[]{PythonCacheLevel.FILE, PythonCacheLevel.PROCESSOR};
55+
private PythonCacheLevel[] levels = new PythonCacheLevel[]{PythonCacheLevel.PROCESSOR};
5856
/**
5957
* Configuration properties defining cache names for each cache segment.
6058
*/
@@ -71,7 +69,7 @@ public class PythonCacheProperties {
7169
* </p>
7270
*/
7371
public enum PythonCacheLevel {
74-
FILE, RESOLVER, EXECUTOR, PROCESSOR
72+
RESOLVER, EXECUTOR, PROCESSOR
7573
}
7674

7775
/**
@@ -82,14 +80,6 @@ public enum PythonCacheLevel {
8280
*/
8381
@Getter @Setter
8482
public static class NameProperties {
85-
/**
86-
* Cache name for storing resolved Python file paths.
87-
*/
88-
private String filePaths = "pythonFilePathsCache";
89-
/**
90-
* Cache name for storing Python file contents.
91-
*/
92-
private String fileBodies = "pythonFileBodiesCache";
9383
/**
9484
* Cache name for storing resolved Python scripts.
9585
*/

cache/python-executor-spring-boot-cache-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,11 @@
3131
"name": "spring.python.cache.levels",
3232
"description": "Defines which parts of the Python processing flow are cached.",
3333
"defaultValue": [
34-
"file",
3534
"processor"
3635
],
3736
"type": "io.maksymuimanov.python.autoconfigure.PythonCacheProperties$PythonCacheLevel[]",
3837
"sourceType": "io.maksymuimanov.python.autoconfigure.PythonCacheProperties"
3938
},
40-
{
41-
"name": "spring.python.cache.name.file-paths",
42-
"description": "Cache name for storing resolved Python file paths.",
43-
"defaultValue": "pythonFilePathsCache",
44-
"type": "java.lang.String",
45-
"sourceType": "io.maksymuimanov.python.autoconfigure.PythonCacheProperties$NameProperties"
46-
},
47-
{
48-
"name": "spring.python.cache.name.file-bodies",
49-
"description": "Cache name for storing Python file contents (script bodies).",
50-
"defaultValue": "pythonFileBodiesCache",
51-
"type": "java.lang.String",
52-
"sourceType": "io.maksymuimanov.python.autoconfigure.PythonCacheProperties$NameProperties"
53-
},
5439
{
5540
"name": "spring.python.cache.name.resolver",
5641
"description": "Cache name for storing resolved Python scripts",
@@ -98,10 +83,6 @@
9883
{
9984
"name": "spring.python.cache.levels",
10085
"values": [
101-
{
102-
"value": "file",
103-
"description": "Caches Python inside the PythonFileReader."
104-
},
10586
{
10687
"value": "resolver",
10788
"description": "Caches Python inside the PythonResolverHolder."

cache/python-executor-spring-boot-cache-core/src/main/java/io/maksymuimanov/python/file/CachingPythonFileReader.java

Lines changed: 0 additions & 101 deletions
This file was deleted.

cache/python-executor-spring-boot-cache-core/src/main/java/io/maksymuimanov/python/file/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

common/python-executor-common/src/main/java/io/maksymuimanov/python/file/PythonFileReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io.maksymuimanov.python.script.PythonScript;
44

5-
import java.nio.file.Path;
5+
import java.io.InputStream;
66

77
/**
88
* Defines reading operations for working with Python script files, including validation, I/O,
@@ -32,10 +32,10 @@ public interface PythonFileReader {
3232
PythonScript readScript(PythonScript pythonScript);
3333

3434
/**
35-
* Returns a {@link Path} object representing the location of the given script.
35+
* Returns an {@link InputStream} object representing the input stream of the given script.
3636
*
3737
* @param path non-{@code null} file system path
38-
* @return non-{@code null} {@link Path} instance pointing to the script
38+
* @return non-{@code null} {@link InputStream} instance pointing to the script
3939
*/
40-
Path getScriptPath(String path);
40+
InputStream getInputStream(String path);
4141
}

common/python-executor-common/src/main/java/io/maksymuimanov/python/response/PythonExecutionResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
import org.jspecify.annotations.Nullable;
44

5+
import java.util.Optional;
6+
57
/**
68
* Response wrapper for Python script execution result.
79
*
810
* @param <R> the type of the execution result body
911
* @param body the result returned from executing the Python script
1012
*/
1113
public record PythonExecutionResponse<R>(@Nullable R body) {
14+
public Optional<R> bodyOptional() {
15+
return Optional.ofNullable(body);
16+
}
1217
}

core/python-executor-core/src/main/java/io/maksymuimanov/python/file/BasicPythonFileReader.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
import java.io.BufferedReader;
99
import java.io.IOException;
10-
import java.nio.file.Files;
11-
import java.nio.file.Path;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
import java.util.Map;
13+
import java.util.concurrent.ConcurrentHashMap;
1214
import java.util.stream.Collectors;
1315

1416
/**
@@ -29,8 +31,13 @@
2931
*/
3032
@RequiredArgsConstructor
3133
public class BasicPythonFileReader implements PythonFileReader {
34+
private final Map<String, String> fileCache;
3235
private final String rootPath;
3336

37+
public BasicPythonFileReader(String rootPath) {
38+
this(new ConcurrentHashMap<>(), rootPath);
39+
}
40+
3441
/**
3542
* Reads the content of a Python script file resolved from the given path string.
3643
*
@@ -40,30 +47,35 @@ public class BasicPythonFileReader implements PythonFileReader {
4047
*/
4148
@Override
4249
public PythonScript readScript(PythonScript script) {
43-
String source = script.getSource();
44-
Path scriptPath = this.getScriptPath(source);
45-
try (BufferedReader bufferedReader = Files.newBufferedReader(scriptPath)) {
46-
String body = bufferedReader.lines().collect(Collectors.joining("\n"));
50+
try {
51+
String source = script.getSource();
52+
String body = this.fileCache.computeIfAbsent(source, path -> {
53+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.getInputStream(path)))) {
54+
return bufferedReader.lines().collect(Collectors.joining("\n"));
55+
} catch (Exception e) {
56+
throw new PythonFileException(e);
57+
}
58+
});
4759
script.getBuilder().appendAll(body);
4860
return script;
49-
} catch (IOException e) {
61+
} catch (Exception e) {
5062
throw new PythonFileException(e);
5163
}
5264
}
5365

5466
/**
55-
* Resolves the {@link Path} for the script file by appending the given relative path
67+
* Resolves the {@link InputStream} for the script file by appending the given relative path
5668
* to the base path configured in {@link BasicPythonFileReader#rootPath}.
5769
*
5870
* @param path the relative path string of the script file, must be non-null
59-
* @return the resolved absolute {@link Path} to the script file
60-
* @throws PythonFileException if the resource cannot be resolved as a file path
71+
* @return the resolved {@link InputStream} to the script file
72+
* @throws PythonFileException if the resource cannot be resolved
6173
*/
6274
@Override
63-
public Path getScriptPath(String path) {
75+
public InputStream getInputStream(String path) {
6476
try {
65-
ClassPathResource classPathResource = new ClassPathResource(rootPath + path);
66-
return classPathResource.getFile().toPath();
77+
ClassPathResource classPathResource = new ClassPathResource(this.rootPath + path);
78+
return classPathResource.getInputStream();
6779
} catch (IOException e) {
6880
throw new PythonFileException(e);
6981
}

0 commit comments

Comments
 (0)