implements PR-025 asset cache model and repository

This commit is contained in:
2026-03-24 13:42:49 +00:00
parent 6a9e09bd88
commit 2870a0677b
5 changed files with 399 additions and 0 deletions
@@ -0,0 +1,165 @@
package p.packer.repositories;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.packer.PackerWorkspacePaths;
import p.packer.exceptions.PackerRegistryException;
import p.packer.messages.PackerProjectContext;
import p.packer.models.PackerAssetCacheEntry;
import p.packer.models.PackerFileCacheEntry;
import p.packer.models.PackerWorkspaceCacheState;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public final class FileSystemPackerCacheRepository {
private final ObjectMapper mapper;
public FileSystemPackerCacheRepository(ObjectMapper mapper) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
}
public PackerWorkspaceCacheState load(PackerProjectContext project) {
final Path cachePath = PackerWorkspacePaths.cachePath(project);
if (!Files.isRegularFile(cachePath)) {
return PackerWorkspaceCacheState.EMPTY;
}
try {
final CacheDocument document = mapper.readValue(cachePath.toFile(), CacheDocument.class);
final int schemaVersion = document.schemaVersion <= 0
? PackerWorkspaceCacheState.CURRENT_SCHEMA_VERSION
: document.schemaVersion;
if (schemaVersion != PackerWorkspaceCacheState.CURRENT_SCHEMA_VERSION) {
throw new PackerRegistryException("Unsupported cache schema_version: " + schemaVersion);
}
final List<PackerAssetCacheEntry> assets = new ArrayList<>();
if (document.assets != null) {
for (CacheAssetDocument asset : document.assets) {
if (asset == null) {
continue;
}
final List<PackerFileCacheEntry> files = new ArrayList<>();
if (asset.files != null) {
for (CacheFileDocument file : asset.files) {
if (file == null) {
continue;
}
files.add(new PackerFileCacheEntry(
normalizeRelativePath(file.relativePath),
file.mimeType,
file.size,
file.lastModified,
file.fingerprint,
file.metadata == null ? Map.of() : file.metadata));
}
}
validateAssetId(asset.assetId);
validateDuplicateRelativePaths(asset.assetId, files);
assets.add(new PackerAssetCacheEntry(asset.assetId, files));
}
}
validateDuplicateAssetIds(assets);
return new PackerWorkspaceCacheState(schemaVersion, assets.stream()
.sorted(Comparator.comparingInt(PackerAssetCacheEntry::assetId))
.toList());
} catch (IOException exception) {
throw new PackerRegistryException("Unable to load cache: " + cachePath, exception);
}
}
public void save(PackerProjectContext project, PackerWorkspaceCacheState state) {
final Path cachePath = PackerWorkspacePaths.cachePath(project);
final Path directory = PackerWorkspacePaths.registryDirectory(project);
final PackerWorkspaceCacheState safeState = Objects.requireNonNull(state, "state");
validateDuplicateAssetIds(safeState.assets());
safeState.assets().forEach(asset -> validateDuplicateRelativePaths(asset.assetId(), asset.files()));
try {
Files.createDirectories(directory);
final CacheDocument document = new CacheDocument();
document.schemaVersion = safeState.schemaVersion();
document.assets = safeState.assets().stream()
.map(asset -> new CacheAssetDocument(
asset.assetId(),
asset.files().stream()
.map(file -> new CacheFileDocument(
file.relativePath(),
file.mimeType(),
file.size(),
file.lastModified(),
file.fingerprint(),
file.metadata()))
.toList()))
.toList();
mapper.writerWithDefaultPrettyPrinter().writeValue(cachePath.toFile(), document);
} catch (IOException exception) {
throw new PackerRegistryException("Unable to save cache: " + cachePath, exception);
}
}
private void validateDuplicateAssetIds(List<PackerAssetCacheEntry> assets) {
final Set<Integer> assetIds = new HashSet<>();
for (PackerAssetCacheEntry asset : assets) {
validateAssetId(asset.assetId());
if (!assetIds.add(asset.assetId())) {
throw new PackerRegistryException("Duplicate asset_id in cache: " + asset.assetId());
}
}
}
private void validateDuplicateRelativePaths(int assetId, List<PackerFileCacheEntry> files) {
final Set<String> relativePaths = new HashSet<>();
for (PackerFileCacheEntry file : files) {
final String relativePath = normalizeRelativePath(file.relativePath());
if (!relativePaths.add(relativePath)) {
throw new PackerRegistryException("Duplicate cached file path for asset_id " + assetId + ": " + relativePath);
}
}
}
private void validateAssetId(int assetId) {
if (assetId <= 0) {
throw new PackerRegistryException("Cache asset_id must be positive");
}
}
private String normalizeRelativePath(String relativePath) {
final String normalized = Objects.requireNonNull(relativePath, "relativePath").trim().replace('\\', '/');
if (normalized.isBlank()) {
throw new PackerRegistryException("Cached relative path must not be blank");
}
final Path normalizedPath = Path.of(normalized).normalize();
if (normalizedPath.isAbsolute() || normalizedPath.startsWith("..")) {
throw new PackerRegistryException("Cached relative path is outside the trusted asset boundary: " + normalized);
}
return normalizedPath.toString().replace('\\', '/');
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static final class CacheDocument {
@JsonProperty("schema_version")
public int schemaVersion;
@JsonProperty("assets")
public List<CacheAssetDocument> assets = List.of();
}
@JsonIgnoreProperties(ignoreUnknown = true)
private record CacheAssetDocument(
@JsonProperty("asset_id") int assetId,
@JsonProperty("files") List<CacheFileDocument> files) {
}
@JsonIgnoreProperties(ignoreUnknown = true)
private record CacheFileDocument(
@JsonProperty("relative_path") String relativePath,
@JsonProperty("mime_type") String mimeType,
@JsonProperty("size") long size,
@JsonProperty("last_modified") long lastModified,
@JsonProperty("fingerprint") String fingerprint,
@JsonProperty("metadata") Map<String, Object> metadata) {
}
}