Skip to content

Commit

Permalink
TASK: Optimise reference writing by splitting by scope
Browse files Browse the repository at this point in the history
... like in set serialized properties.

That way fewer `NodeReferencesWereSet` are emitted.
At most only one per scope of `PropertyScope`

Also the `PropertyScope` enum was marked internal as its not exposed in apis and no use for the user.
  • Loading branch information
mhsdesign committed Oct 31, 2024
1 parent bc2427c commit 4041870
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Neos\ContentRepository\Core\DimensionSpace\InterDimensionalVariationGraph;
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePoint;
use Neos\ContentRepository\Core\DimensionSpace\OriginDimensionSpacePointSet;
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetSerializedNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetSerializedNodeReferences;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
use Neos\ContentRepository\Core\SharedModel\Node\PropertyName;
Expand All @@ -25,7 +27,7 @@
* The property scope to be used in NodeType property declarations.
* Will affect node operations on properties in that they decide which of the node's variants will be modified as well.
*
* @api used as part of commands
* @internal implementation of the {@see SetSerializedNodeProperties} and {@see SetSerializedNodeReferences} handling
*/
enum PropertyScope: string implements \JsonSerializable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
use Neos\ContentRepository\Core\EventStore\Events;
use Neos\ContentRepository\Core\EventStore\EventsToPublish;
use Neos\ContentRepository\Core\Feature\Common\ConstraintChecks;
use Neos\ContentRepository\Core\Feature\RebaseableCommand;
use Neos\ContentRepository\Core\Feature\Common\NodeReferencingInternals;
use Neos\ContentRepository\Core\Feature\ContentStreamEventStreamName;
use Neos\ContentRepository\Core\Feature\NodeModification\Dto\PropertyScope;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetNodeReferences;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetSerializedNodeReferences;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Dto\SerializedNodeReferences;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Event\NodeReferencesWereSet;
use Neos\ContentRepository\Core\Feature\RebaseableCommand;
use Neos\ContentRepository\Core\NodeType\NodeType;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphInterface;
use Neos\ContentRepository\Core\Projection\ContentGraph\NodeAggregate;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
Expand Down Expand Up @@ -111,14 +112,9 @@ private function handleSetSerializedNodeReferences(
$sourceNodeAggregate->nodeTypeName
);

foreach ($command->references as $referencesByName) {
$referenceName = $referencesByName->referenceName;
$this->requireNodeTypeToDeclareReference($sourceNodeAggregate->nodeTypeName, $referenceName);
$scopeDeclaration = $sourceNodeType->getReferences()[$referenceName->value]['scope'] ?? '';
$scope = PropertyScope::tryFrom($scopeDeclaration) ?: PropertyScope::SCOPE_NODE;
// TODO: Optimize affected sets into one event

foreach ($referencesByName->references as $reference) {
foreach ($command->references as $referencesForName) {
$this->requireNodeTypeToDeclareReference($sourceNodeAggregate->nodeTypeName, $referencesForName->referenceName);
foreach ($referencesForName->references as $reference) {
$destinationNodeAggregate = $this->requireProjectedNodeAggregate(
$contentGraph,
$reference->targetNodeAggregateId
Expand All @@ -130,23 +126,25 @@ private function handleSetSerializedNodeReferences(
);
$this->requireNodeTypeToAllowNodesOfTypeInReference(
$sourceNodeAggregate->nodeTypeName,
$referencesByName->referenceName,
$referencesForName->referenceName,
$destinationNodeAggregate->nodeTypeName
);
}
}

foreach (self::splitReferencesByScope($command->references, $sourceNodeType) as $rawScope => $references) {
$scope = PropertyScope::from($rawScope);
$affectedOrigins = $scope->resolveAffectedOrigins(
$command->sourceOriginDimensionSpacePoint,
$sourceNodeAggregate,
$this->interDimensionalVariationGraph
);

$events[] = new NodeReferencesWereSet(
$contentGraph->getWorkspaceName(),
$contentGraph->getContentStreamId(),
$command->sourceNodeAggregateId,
$affectedOrigins,
SerializedNodeReferences::fromArray([$referencesByName]),
$references,
);
}

Expand All @@ -162,4 +160,22 @@ private function handleSetSerializedNodeReferences(
$expectedVersion
);
}

/**
* @return array<string,SerializedNodeReferences>
*/
private static function splitReferencesByScope(SerializedNodeReferences $nodeReferences, NodeType $nodeType): array
{
$referencesByScope = [];
foreach ($nodeReferences as $nodeReferenceForName) {
$scopeDeclaration = $nodeType->getReferences()[$nodeReferenceForName->referenceName->value]['scope'] ?? '';
$scope = PropertyScope::tryFrom($scopeDeclaration) ?: PropertyScope::SCOPE_NODE;
$referencesByScope[$scope->value][] = $nodeReferenceForName;
}

return array_map(
SerializedNodeReferences::fromArray(...),
$referencesByScope
);
}
}

0 comments on commit 4041870

Please sign in to comment.