|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +package org.eolang.maven; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.security.MessageDigest; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | +import java.util.Base64; |
| 13 | +import org.cactoos.Func; |
| 14 | +import org.cactoos.func.UncheckedFunc; |
| 15 | + |
| 16 | +/** |
| 17 | + * Simple cache mechanism. |
| 18 | + * @since 0.60 |
| 19 | + * @todo #4846:30min Make Cache thread-safe. |
| 20 | + * The Cache class lacks thread-safety mechanisms. If multiple threads call the apply method |
| 21 | + * concurrently with the same source file, there could be race conditions where both threads |
| 22 | + * determine the cache is stale and attempt to write simultaneously, potentially leading to |
| 23 | + * corrupted files or inconsistent state. Consider adding synchronization or using atomic file |
| 24 | + * operations. See ChCachedTest.java:63-86 for an example of how concurrency is tested in similar |
| 25 | + * caching classes in this codebase. |
| 26 | + * @todo #4846:30min Replace {@link FpDefault} with {@link Cache}. |
| 27 | + * The FpDefault class currently implements caching logic that is similar to the Cache class. |
| 28 | + * Refactor the codebase to use Cache instead of FpDefault for caching functionality to |
| 29 | + * improve code reuse and maintainability. |
| 30 | + */ |
| 31 | +final class Cache { |
| 32 | + |
| 33 | + /** |
| 34 | + * Base cache directory. |
| 35 | + */ |
| 36 | + private final Path base; |
| 37 | + |
| 38 | + /** |
| 39 | + * Compilation function. |
| 40 | + */ |
| 41 | + private final Func<Path, String> compilation; |
| 42 | + |
| 43 | + /** |
| 44 | + * Ctor. |
| 45 | + * @param base Base cache directory |
| 46 | + * @param compilation Compilation function |
| 47 | + */ |
| 48 | + Cache(final Path base, final Func<Path, String> compilation) { |
| 49 | + this.base = base; |
| 50 | + this.compilation = compilation; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check cache and apply compilation if needed. |
| 55 | + * @param source From file |
| 56 | + * @param target To file |
| 57 | + * @param tail Tail path in cache |
| 58 | + */ |
| 59 | + public void apply(final Path source, final Path target, final Path tail) { |
| 60 | + try { |
| 61 | + final String sha = Cache.sha(source); |
| 62 | + final Path hash = this.hash(tail); |
| 63 | + final Path cache = this.base.resolve(tail); |
| 64 | + if (Files.notExists(hash) |
| 65 | + || Files.notExists(cache) |
| 66 | + || !Files.readString(hash).equals(sha)) { |
| 67 | + final String content = new UncheckedFunc<>(this.compilation).apply(source); |
| 68 | + new Saved(sha, this.hash(tail)).value(); |
| 69 | + new Saved(content, cache).value(); |
| 70 | + new Saved(content, target).value(); |
| 71 | + } else { |
| 72 | + new Saved(Files.readString(cache), target).value(); |
| 73 | + } |
| 74 | + } catch (final IOException ioexception) { |
| 75 | + throw new IllegalStateException( |
| 76 | + "Failed to perform an IO operation with cache", |
| 77 | + ioexception |
| 78 | + ); |
| 79 | + } catch (final NoSuchAlgorithmException exception) { |
| 80 | + throw new IllegalStateException("SHA-256 hashing algorithm isn't found", exception); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get hash file path for the given tail. |
| 86 | + * @param tail Tail path |
| 87 | + * @return Hash file path |
| 88 | + */ |
| 89 | + private Path hash(final Path tail) { |
| 90 | + final Path full = this.base.resolve(tail.normalize()); |
| 91 | + return full.getParent().resolve(String.format("%s.sha256", full.getFileName().toString())); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Calculate SHA-256 hash of a file and return it as Base64 string. |
| 96 | + * @param file File path |
| 97 | + * @return Base64-encoded SHA-256 hash |
| 98 | + * @throws NoSuchAlgorithmException If SHA-256 algorithm is not available |
| 99 | + * @throws IOException If an I/O error occurs reading the file |
| 100 | + * @todo #4846:30min OutOfMemoryError for large files in cache. |
| 101 | + * The sha method reads the entire file into memory using Files.readAllBytes(file) which |
| 102 | + * could cause OutOfMemoryError for large files. Consider using a streaming approach with |
| 103 | + * MessageDigest.update() in a loop to hash the file in chunks, similar to how it's typically |
| 104 | + * done for large file hashing operations. |
| 105 | + */ |
| 106 | + private static String sha(final Path file) throws NoSuchAlgorithmException, IOException { |
| 107 | + final MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 108 | + final byte[] hash = digest.digest(Files.readAllBytes(file)); |
| 109 | + return Base64.getEncoder().encodeToString(hash); |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments