Skip to content

Commit

Permalink
TASK: Introduce nodes AND affected DSP on `WorkspaceWasPartiallyDisca…
Browse files Browse the repository at this point in the history
…rded` to allow for future extension to only discard changes in a specific dimension

Also by calculating the `NodeIdsToPublishOrDiscard` by the _actual_ events we can know for sure there is no gibberish in the event.
Previously for example discarding a non-existing node or a node in a not valid DSP would just save that directly into the event, which is not correct as that did not happen like that. It was merely a bad intention.
  • Loading branch information
mhsdesign committed Nov 29, 2024
1 parent 529037d commit be1f1fe
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
use Neos\ContentRepository\Core\CommandHandler\CommandInterface;
use Neos\ContentRepository\Core\CommandHandler\CommandSimulatorFactory;
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
use Neos\ContentRepository\Core\EventStore\DecoratedEvent;
use Neos\ContentRepository\Core\EventStore\EventNormalizer;
use Neos\ContentRepository\Core\EventStore\Events;
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
use Neos\ContentRepository\Core\Feature\Common\EmbedsAffectedDimensionSpacePoints;
use Neos\ContentRepository\Core\Feature\Common\EmbedsNodeAggregateId;
use Neos\ContentRepository\Core\Feature\Common\PublishableToWorkspaceInterface;
use Neos\ContentRepository\Core\Feature\Common\RebasableToOtherWorkspaceInterface;
use Neos\ContentRepository\Core\Feature\ContentStreamClosing\Event\ContentStreamWasClosed;
Expand All @@ -46,6 +49,8 @@
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\DiscardWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\PublishIndividualNodesFromWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Command\PublishWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Dto\NodeIdsToPublishOrDiscard;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Dto\NodeIdToPublishOrDiscard;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasDiscarded;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPartiallyDiscarded;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPartiallyPublished;
Expand All @@ -59,6 +64,7 @@
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamIsClosed;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceHasNoBaseWorkspaceName;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
Expand Down Expand Up @@ -599,6 +605,8 @@ private function handleDiscardIndividualNodesFromWorkspace(
return;
}

$nodesToDiscard = $this->extractNodeAggregateIdsAndAffectedDimensionSpacePoints($commandsToDiscard);

yield $this->closeContentStream(
$workspace->currentContentStreamId,
$workspaceContentStreamVersion
Expand Down Expand Up @@ -644,7 +652,7 @@ static function ($handle) use ($commandsToKeep): void {
$command->workspaceName,
$command->newContentStreamId,
$workspace->currentContentStreamId,
$command->nodesToDiscard,
$nodesToDiscard,
)
),
ExpectedVersion::ANY()
Expand All @@ -659,6 +667,41 @@ static function ($handle) use ($commandsToKeep): void {
yield $this->removeContentStreamWithoutConstraintChecks($workspace->currentContentStreamId);
}

private function extractNodeAggregateIdsAndAffectedDimensionSpacePoints(RebaseableCommands $commands): NodeIdsToPublishOrDiscard
{
/** @var array<string,DimensionSpacePointSet> $affectedDimensionSpacePointsByNodeAggregateId */
$affectedDimensionSpacePointsByNodeAggregateId = [];
foreach ($commands as $command) {
$event = $this->eventNormalizer->denormalize($command->originalEvent);
assert($event instanceof EmbedsNodeAggregateId);
if (!$event instanceof EmbedsAffectedDimensionSpacePoints) {
// todo fix change node type and rename case!!! either event must contain all dsp or we need to use the variation graph here?
// -> all dimension would be forward compatible if we ever allow node type change for one dsp ...
continue;
}
$affectedDimensionSpacePoints = $affectedDimensionSpacePointsByNodeAggregateId[$event->getNodeAggregateId()->value] ?? null;

$affectedDimensionSpacePointsByNodeAggregateId[$event->getNodeAggregateId()->value] =
$affectedDimensionSpacePoints !== null
? $affectedDimensionSpacePoints->getUnion($event->getAffectedDimensionSpacePoints())
: $event->getAffectedDimensionSpacePoints();

}

$nodeAggregateIdsWithDimension = [];
foreach ($affectedDimensionSpacePointsByNodeAggregateId as $nodeId => $affectedDimensionSpacePoints) {
foreach ($affectedDimensionSpacePoints as $dimensionSpacePoint) {
$nodeAggregateIdsWithDimension[] = new NodeIdToPublishOrDiscard(
NodeAggregateId::fromString($nodeId),
$dimensionSpacePoint
);
}
}

// todo optimise format like: [{NodeAggregateId -> DimensionSpacePointSet}] and rename collection to NodeAggregateIdsWithDimensionSpacePoints (less clumsy) ... in the event this happened already ... no TO ...!
return NodeIdsToPublishOrDiscard::create(...$nodeAggregateIdsWithDimension);
}

/**
* @throws BaseWorkspaceDoesNotExist
* @throws WorkspaceDoesNotExist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\Feature\Common\EmbedsWorkspaceName;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Dto\NodeIdsToPublishOrDiscard;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

Expand All @@ -35,7 +35,7 @@ public function __construct(
* The old content stream, which contains ALL the data (discarded and non-discarded)
*/
public ContentStreamId $previousContentStreamId,
public NodeAggregateIds $discardedNodes,
public NodeIdsToPublishOrDiscard $discardedNodes,
) {
}

Expand All @@ -46,21 +46,11 @@ public function getWorkspaceName(): WorkspaceName

public static function fromArray(array $values): self
{
$discardedNodes = [];
foreach ($values['discardedNodes'] as $discardedNode) {
if (is_array($discardedNode)) {
// legacy case:
$discardedNodes[] = $discardedNode['nodeAggregateId'];
continue;
}
$discardedNodes[] = $discardedNode;
}

return new self(
WorkspaceName::fromString($values['workspaceName']),
ContentStreamId::fromString($values['newContentStreamId']),
ContentStreamId::fromString($values['previousContentStreamId']),
NodeAggregateIds::fromArray($discardedNodes),
NodeIdsToPublishOrDiscard::fromArray($values['discardedNodes']),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePoint;
use Neos\ContentRepository\Core\DimensionSpace\DimensionSpacePointSet;
use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\Feature\Common\EmbedsContentStreamId;
use Neos\ContentRepository\Core\Feature\Common\EmbedsWorkspaceName;
use Neos\ContentRepository\Core\Feature\DimensionSpaceAdjustment\Event\DimensionSpacePointWasMoved;
use Neos\ContentRepository\Core\Feature\NodeCreation\Event\NodeAggregateWithNodeWasCreated;
Expand All @@ -27,7 +26,6 @@
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\EventStore\Model\EventEnvelope;
use Neos\Neos\AssetUsage\Service\AssetUsageIndexingService;
Expand Down Expand Up @@ -146,13 +144,18 @@ private function discardWorkspace(WorkspaceName $workspaceName): void
$this->assetUsageIndexingService->removeIndexForWorkspace($this->contentRepositoryId, $workspaceName);
}

private function discardNodes(WorkspaceName $workspaceName, NodeAggregateIds $nodeIds): void
private function discardNodes(WorkspaceName $workspaceName, NodeIdsToPublishOrDiscard $nodeIds): void
{
foreach ($nodeIds as $nodeId) {
$this->assetUsageIndexingService->removeAssetUsagesOfWorkspaceWithAllPropertiesInAllDimensions(
if (!$nodeId->dimensionSpacePoint) {
// NodeAggregateTypeWasChanged and NodeAggregateNameWasChanged don't impact asset usage
continue;
}
$this->assetUsageIndexingService->removeIndexForWorkspaceNameNodeAggregateIdAndDimensionSpacePoint(
$this->contentRepositoryId,
$workspaceName,
$nodeId,
$nodeId->nodeAggregateId,
$nodeId->dimensionSpacePoint
);
}
}
Expand Down
19 changes: 0 additions & 19 deletions Neos.Neos/Classes/AssetUsage/Domain/AssetUsageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,6 @@ public function removeAssetUsagesOfWorkspaceWithAllProperties(
]);
}

public function removeAssetUsagesOfWorkspaceWithAllPropertiesInAllDimensions(
ContentRepositoryId $contentRepositoryId,
WorkspaceName $workspaceName,
NodeAggregateId $nodeAggregateId,
): void {
$sql = <<<SQL
DELETE FROM {$this->getTableName()}
WHERE contentrepositoryid = :contentRepositoryId
AND workspacename = :workspaceName
AND nodeAggregateId = :nodeAggregateId
SQL;

$this->dbal->executeStatement($sql, [
'contentRepositoryId' => $contentRepositoryId->value,
'workspaceName' => $workspaceName->value,
'nodeAggregateId' => $nodeAggregateId->value
]);
}

/**
* @param WorkspaceName[] $workspaceNames
*/
Expand Down
12 changes: 0 additions & 12 deletions Neos.Neos/Classes/AssetUsage/Service/AssetUsageIndexingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,6 @@ public function removeIndexForWorkspaceNameNodeAggregateIdAndDimensionSpacePoint
);
}

public function removeAssetUsagesOfWorkspaceWithAllPropertiesInAllDimensions(
ContentRepositoryId $contentRepositoryId,
WorkspaceName $workspaceName,
NodeAggregateId $nodeAggregateId
): void {
$this->assetUsageRepository->removeAssetUsagesOfWorkspaceWithAllPropertiesInAllDimensions(
$contentRepositoryId,
$workspaceName,
$nodeAggregateId
);
}

public function removeIndexForWorkspace(
ContentRepositoryId $contentRepositoryId,
WorkspaceName $workspaceName
Expand Down

0 comments on commit be1f1fe

Please sign in to comment.