implements PR-027 runtime walkresult and cache integration

This commit is contained in:
2026-03-24 13:42:49 +00:00
parent 1442adc7b3
commit b4733a0e49
18 changed files with 512 additions and 52 deletions
@@ -1,15 +1,21 @@
package p.packer.services;
import p.packer.models.*;
import p.packer.repositories.PackerRuntimeAssetMaterializer;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
public final class PackerRuntimePatchService {
private final PackerAssetDeclarationParser declarationParser;
private final PackerRuntimeAssetMaterializer assetMaterializer;
public PackerRuntimePatchService(PackerAssetDeclarationParser declarationParser) {
public PackerRuntimePatchService(
PackerAssetDeclarationParser declarationParser,
PackerRuntimeAssetMaterializer assetMaterializer) {
this.declarationParser = Objects.requireNonNull(declarationParser, "declarationParser");
this.assetMaterializer = Objects.requireNonNull(assetMaterializer, "assetMaterializer");
}
public PackerRuntimeSnapshot afterCreateAsset(
@@ -23,13 +29,19 @@ public final class PackerRuntimePatchService {
final List<PackerRuntimeAsset> updatedAssets = new ArrayList<>(snapshot.assets().stream()
.filter(candidate -> !candidate.assetRoot().equals(assetRoot.toAbsolutePath().normalize()))
.toList());
updatedAssets.add(new PackerRuntimeAsset(
final var materialized = assetMaterializer.materialize(
assetRoot,
manifestPath,
Optional.of(entry),
parsed));
parsed,
snapshot.cacheState().findAsset(entry.assetId()));
updatedAssets.add(materialized.runtimeAsset());
updatedAssets.sort(Comparator.comparing(asset -> asset.assetRoot().toString(), String.CASE_INSENSITIVE_ORDER));
return new PackerRuntimeSnapshot(generation, updatedRegistry, updatedAssets);
return new PackerRuntimeSnapshot(
generation,
updatedRegistry,
updatedAssets,
mergeCacheState(snapshot.cacheState(), updatedRegistry, materialized.assetCacheEntry()));
}
public PackerRuntimeSnapshot afterRegisterAsset(
@@ -40,13 +52,17 @@ public final class PackerRuntimePatchService {
Path assetRoot) {
final List<PackerRuntimeAsset> updatedAssets = new ArrayList<>();
boolean patched = false;
Optional<PackerAssetCacheEntry> refreshedCacheEntry = Optional.empty();
for (PackerRuntimeAsset asset : snapshot.assets()) {
if (asset.assetRoot().equals(assetRoot.toAbsolutePath().normalize())) {
updatedAssets.add(new PackerRuntimeAsset(
final var materialized = assetMaterializer.materialize(
asset.assetRoot(),
asset.manifestPath(),
Optional.of(entry),
asset.parsedDeclaration()));
asset.parsedDeclaration(),
snapshot.cacheState().findAsset(entry.assetId()));
updatedAssets.add(materialized.runtimeAsset());
refreshedCacheEntry = materialized.assetCacheEntry();
patched = true;
} else {
updatedAssets.add(asset);
@@ -55,7 +71,11 @@ public final class PackerRuntimePatchService {
if (!patched) {
throw new IllegalStateException("Unable to patch runtime snapshot for unregistered asset: " + assetRoot);
}
return new PackerRuntimeSnapshot(generation, updatedRegistry, updatedAssets);
return new PackerRuntimeSnapshot(
generation,
updatedRegistry,
updatedAssets,
mergeCacheState(snapshot.cacheState(), updatedRegistry, refreshedCacheEntry));
}
public PackerRuntimeSnapshot afterDeleteAsset(
@@ -66,7 +86,11 @@ public final class PackerRuntimePatchService {
final List<PackerRuntimeAsset> updatedAssets = snapshot.assets().stream()
.filter(asset -> !asset.assetRoot().equals(assetRoot.toAbsolutePath().normalize()))
.toList();
return new PackerRuntimeSnapshot(generation, updatedRegistry, updatedAssets);
return new PackerRuntimeSnapshot(
generation,
updatedRegistry,
updatedAssets,
pruneCacheState(snapshot.cacheState(), updatedRegistry));
}
public PackerRuntimeSnapshot afterMoveAsset(
@@ -80,13 +104,17 @@ public final class PackerRuntimePatchService {
final PackerAssetDeclarationParseResult parsed = declarationParser.parse(targetManifestPath);
final List<PackerRuntimeAsset> updatedAssets = new ArrayList<>();
boolean patched = false;
Optional<PackerAssetCacheEntry> refreshedCacheEntry = Optional.empty();
for (PackerRuntimeAsset asset : snapshot.assets()) {
if (asset.assetRoot().equals(sourceRoot.toAbsolutePath().normalize())) {
updatedAssets.add(new PackerRuntimeAsset(
final var materialized = assetMaterializer.materialize(
targetRoot,
targetManifestPath,
updatedRegistryEntry,
parsed));
parsed,
updatedRegistryEntry.flatMap(entry -> snapshot.cacheState().findAsset(entry.assetId())));
updatedAssets.add(materialized.runtimeAsset());
refreshedCacheEntry = materialized.assetCacheEntry();
patched = true;
} else {
updatedAssets.add(asset);
@@ -96,7 +124,11 @@ public final class PackerRuntimePatchService {
throw new IllegalStateException("Unable to patch runtime snapshot for moved asset: " + sourceRoot);
}
updatedAssets.sort(Comparator.comparing(asset -> asset.assetRoot().toString(), String.CASE_INSENSITIVE_ORDER));
return new PackerRuntimeSnapshot(generation, updatedRegistry, updatedAssets);
return new PackerRuntimeSnapshot(
generation,
updatedRegistry,
updatedAssets,
mergeCacheState(snapshot.cacheState(), updatedRegistry, refreshedCacheEntry));
}
public PackerRuntimeSnapshot afterUpdateAssetContract(
@@ -108,26 +140,69 @@ public final class PackerRuntimePatchService {
final PackerAssetDeclarationParseResult parsed = declarationParser.parse(manifestPath);
final List<PackerRuntimeAsset> updatedAssets = new ArrayList<>();
boolean patched = false;
Optional<PackerAssetCacheEntry> refreshedCacheEntry = Optional.empty();
for (PackerRuntimeAsset asset : snapshot.assets()) {
if (asset.assetRoot().equals(assetRoot.toAbsolutePath().normalize())) {
updatedAssets.add(new PackerRuntimeAsset(
final var materialized = assetMaterializer.materialize(
asset.assetRoot(),
asset.manifestPath(),
asset.registryEntry(),
parsed));
parsed,
asset.registryEntry().flatMap(entry -> snapshot.cacheState().findAsset(entry.assetId())));
updatedAssets.add(materialized.runtimeAsset());
refreshedCacheEntry = materialized.assetCacheEntry();
patched = true;
} else {
updatedAssets.add(asset);
}
}
if (!patched) {
updatedAssets.add(new PackerRuntimeAsset(
final var materialized = assetMaterializer.materialize(
assetRoot,
manifestPath,
registryEntry,
parsed));
parsed,
registryEntry.flatMap(entry -> snapshot.cacheState().findAsset(entry.assetId())));
updatedAssets.add(materialized.runtimeAsset());
refreshedCacheEntry = materialized.assetCacheEntry();
updatedAssets.sort(Comparator.comparing(asset -> asset.assetRoot().toString(), String.CASE_INSENSITIVE_ORDER));
}
return new PackerRuntimeSnapshot(generation, snapshot.registry(), updatedAssets);
return new PackerRuntimeSnapshot(
generation,
snapshot.registry(),
updatedAssets,
mergeCacheState(snapshot.cacheState(), snapshot.registry(), refreshedCacheEntry));
}
private PackerWorkspaceCacheState mergeCacheState(
PackerWorkspaceCacheState currentCacheState,
PackerRegistryState registry,
Optional<PackerAssetCacheEntry> refreshedCacheEntry) {
final Map<Integer, PackerAssetCacheEntry> byAssetId = new LinkedHashMap<>();
currentCacheState.assets().forEach(entry -> byAssetId.put(entry.assetId(), entry));
refreshedCacheEntry.ifPresent(entry -> byAssetId.put(entry.assetId(), entry));
return pruneCacheEntries(byAssetId, registry);
}
private PackerWorkspaceCacheState pruneCacheState(
PackerWorkspaceCacheState currentCacheState,
PackerRegistryState registry) {
final Map<Integer, PackerAssetCacheEntry> byAssetId = new LinkedHashMap<>();
currentCacheState.assets().forEach(entry -> byAssetId.put(entry.assetId(), entry));
return pruneCacheEntries(byAssetId, registry);
}
private PackerWorkspaceCacheState pruneCacheEntries(
Map<Integer, PackerAssetCacheEntry> byAssetId,
PackerRegistryState registry) {
final Set<Integer> activeAssetIds = registry.assets().stream()
.map(PackerRegistryEntry::assetId)
.collect(Collectors.toSet());
return new PackerWorkspaceCacheState(
PackerWorkspaceCacheState.CURRENT_SCHEMA_VERSION,
byAssetId.values().stream()
.filter(entry -> activeAssetIds.contains(entry.assetId()))
.sorted(Comparator.comparingInt(PackerAssetCacheEntry::assetId))
.toList());
}
}