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
@@ -13,6 +13,7 @@ import p.packer.messages.assets.*;
import p.packer.messages.diagnostics.PackerDiagnosticCategory;
import p.packer.messages.diagnostics.PackerDiagnosticSeverity;
import p.packer.models.*;
import p.packer.repositories.FileSystemPackerCacheRepository;
import p.packer.repositories.PackerRuntimeRegistry;
import java.io.IOException;
@@ -28,6 +29,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
private final PackerAssetActionReadService actionReadService;
private final PackerRuntimePatchService runtimePatchService;
private final PackerRuntimeRegistry runtimeRegistry;
private final FileSystemPackerCacheRepository cacheRepository;
private final PackerProjectWriteCoordinator writeCoordinator;
private final PackerEventSink eventSink;
@@ -38,6 +40,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
PackerAssetActionReadService actionReadService,
PackerRuntimePatchService runtimePatchService,
PackerRuntimeRegistry runtimeRegistry,
FileSystemPackerCacheRepository cacheRepository,
PackerProjectWriteCoordinator writeCoordinator,
PackerEventSink eventSink) {
this.mapper = Objects.requireNonNull(mapper, "mapper");
@@ -46,6 +49,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
this.actionReadService = Objects.requireNonNull(actionReadService, "actionReadService");
this.runtimePatchService = Objects.requireNonNull(runtimePatchService, "runtimePatchService");
this.runtimeRegistry = Objects.requireNonNull(runtimeRegistry, "runtimeRegistry");
this.cacheRepository = Objects.requireNonNull(cacheRepository, "cacheRepository");
this.writeCoordinator = Objects.requireNonNull(writeCoordinator, "writeCoordinator");
this.eventSink = Objects.requireNonNull(eventSink, "eventSink");
}
@@ -84,6 +88,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
final var registryEntry = registryByRoot.get(assetRoot);
final var parsed = runtimeAsset.parsedDeclaration();
diagnostics.addAll(parsed.diagnostics());
diagnostics.addAll(runtimeAsset.walkDiagnostics());
diagnostics.addAll(identityMismatchDiagnostics(registryEntry, parsed, assetManifestPath));
final var summary = buildSummary(project, assetRoot, registryEntry, parsed);
assets.add(summary);
@@ -220,13 +225,14 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
writeManifest(manifestPath, request, entry.assetUuid());
final PackerRegistryState updated = workspaceFoundation.appendAllocatedEntry(registry, entry);
workspaceFoundation.saveRegistry(project, updated);
runtimeRegistry.update(project, (snapshot, generation) -> runtimePatchService.afterCreateAsset(
final var runtime = runtimeRegistry.update(project, (snapshot, generation) -> runtimePatchService.afterCreateAsset(
snapshot,
generation,
updated,
entry,
assetRoot,
manifestPath));
saveRuntimeCache(project, runtime.snapshot());
final CreateAssetResult result = new CreateAssetResult(
PackerOperationStatus.SUCCESS,
"Asset created: " + relativeAssetRoot,
@@ -270,12 +276,13 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
declaration.assetUuid());
final PackerRegistryState updated = workspaceFoundation.appendAllocatedEntry(registry, entry);
workspaceFoundation.saveRegistry(project, updated);
runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterRegisterAsset(
final var runtime = runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterRegisterAsset(
currentSnapshot,
generation,
updated,
entry,
assetRoot));
saveRuntimeCache(project, runtime.snapshot());
final RegisterAssetResult result = new RegisterAssetResult(
PackerOperationStatus.SUCCESS,
"Asset registered: " + relativeAssetRoot,
@@ -316,11 +323,12 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
if (!updatedRegistry.equals(registry)) {
workspaceFoundation.saveRegistry(project, updatedRegistry);
}
runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterDeleteAsset(
final var runtime = runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterDeleteAsset(
currentSnapshot,
generation,
updatedRegistry,
assetRoot));
saveRuntimeCache(project, runtime.snapshot());
final DeleteAssetResult result = new DeleteAssetResult(
PackerOperationStatus.SUCCESS,
"Asset deleted: " + relativeAssetRoot,
@@ -372,7 +380,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
if (!updatedRegistry.equals(registry)) {
workspaceFoundation.saveRegistry(project, updatedRegistry);
}
runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterMoveAsset(
final var runtime = runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterMoveAsset(
currentSnapshot,
generation,
updatedRegistry,
@@ -380,6 +388,7 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
sourceRoot,
targetRoot,
targetManifestPath));
saveRuntimeCache(project, runtime.snapshot());
final AssetReference canonicalReference = updatedEntry
.map(packerRegistryEntry -> AssetReference.forAssetId(packerRegistryEntry.assetId()))
.orElseGet(() -> AssetReference.forRelativeAssetRoot(targetRelativeRoot));
@@ -558,6 +567,10 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
return PackerWorkspacePaths.relativeAssetRoot(project, assetRoot).replace('\\', '/');
}
private void saveRuntimeCache(PackerProjectContext project, PackerRuntimeSnapshot snapshot) {
cacheRepository.save(project, snapshot.cacheState());
}
private PackerRegistryState removeRegistryEntry(
PackerRegistryState registry,
PackerRegistryEntry entry) {
@@ -628,12 +641,13 @@ public final class FileSystemPackerWorkspaceService implements PackerWorkspaceSe
try {
patchManifestContract(manifest, request);
mapper.writerWithDefaultPrettyPrinter().writeValue(manifestPath.toFile(), manifest);
runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterUpdateAssetContract(
final var runtime = runtimeRegistry.update(project, (currentSnapshot, generation) -> runtimePatchService.afterUpdateAssetContract(
currentSnapshot,
generation,
assetRoot,
manifestPath,
evaluation.resolved().registryEntry()));
saveRuntimeCache(project, runtime.snapshot());
return new UpdateAssetContractResponse(true, null);
} catch (IOException exception) {
return new UpdateAssetContractResponse(false, "Unable to update asset contract: " + exception.getMessage());
@@ -42,6 +42,7 @@ public final class PackerAssetDetailsService {
final var manifestPath = runtimeAsset.manifestPath();
final var parsed = runtimeAsset.parsedDeclaration();
diagnostics.addAll(parsed.diagnostics());
diagnostics.addAll(runtimeAsset.walkDiagnostics());
if (!parsed.valid()) {
return failureResult(project, request.assetReference(), resolved, diagnostics);
}
@@ -198,4 +199,4 @@ public final class PackerAssetDetailsService {
}
return requestedReference;
}
}
}
@@ -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());
}
}