Skip to content

Commit

Permalink
TASK: Only fetch content stream once for constraint checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Nov 9, 2024
1 parent 9c9f6b0 commit 8b55830
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
9 changes: 8 additions & 1 deletion Classes/CommandHandler/CommandHandlingDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphInterface;
use Neos\ContentRepository\Core\Projection\ContentGraph\ContentGraphReadModelInterface;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspace;
Expand Down Expand Up @@ -48,11 +49,17 @@ public function contentStreamExists(ContentStreamId $contentStreamId): bool
return $this->contentGraphReadModel->findContentStreamById($contentStreamId) !== null;
}

/**
* @throws ContentStreamDoesNotExistYet if there is no matching content stream
*/
public function isContentStreamClosed(ContentStreamId $contentStreamId): bool
{
$contentStream = $this->contentGraphReadModel->findContentStreamById($contentStreamId);
if ($contentStream === null) {
throw new \InvalidArgumentException(sprintf('Failed to find content stream with id "%s"', $contentStreamId->value), 1729863973);
throw new ContentStreamDoesNotExistYet(
'Content stream "' . $contentStreamId->value . '" does not exist.',
1521386692
);
}
return $contentStream->isClosed;
}
Expand Down
9 changes: 2 additions & 7 deletions Classes/Feature/Common/ConstraintChecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,9 @@ protected function requireContentStream(
CommandHandlingDependencies $commandHandlingDependencies
): ContentStreamId {
$contentStreamId = $commandHandlingDependencies->getContentGraph($workspaceName)->getContentStreamId();
if (!$commandHandlingDependencies->contentStreamExists($contentStreamId)) {
throw new ContentStreamDoesNotExistYet(
'Content stream for "' . $workspaceName->value . '" does not exist yet.',
1521386692
);
}
$closedState = $commandHandlingDependencies->isContentStreamClosed($contentStreamId);

if ($commandHandlingDependencies->isContentStreamClosed($contentStreamId)) {
if ($closedState) {
throw new ContentStreamIsClosed(
'Content stream "' . $contentStreamId->value . '" is closed.',
1710260081
Expand Down
32 changes: 13 additions & 19 deletions Classes/Feature/WorkspaceCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private function handlePublishWorkspace(
// no-op
return;
}
$workspaceContentStreamVersion = $this->requireOpenContentStreamVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($baseWorkspace, $commandHandlingDependencies);
$workspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

yield $this->closeContentStream(
$workspace->currentContentStreamId,
Expand Down Expand Up @@ -348,11 +348,9 @@ private function handleRebaseWorkspace(
): \Generator {
$workspace = $this->requireWorkspace($command->workspaceName, $commandHandlingDependencies);
$baseWorkspace = $this->requireBaseWorkspace($workspace, $commandHandlingDependencies);
if (!$commandHandlingDependencies->contentStreamExists($workspace->currentContentStreamId)) {
throw new \RuntimeException('Cannot rebase a workspace with a stateless content stream', 1711718314);
}
$workspaceContentStreamVersion = $this->requireOpenContentStreamVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($baseWorkspace, $commandHandlingDependencies);

$workspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

if (
$workspace->status === WorkspaceStatus::UP_TO_DATE
Expand Down Expand Up @@ -449,12 +447,8 @@ private function handlePublishIndividualNodesFromWorkspace(
return;
}

// todo check that fetching workspace throws if there is no content stream id for it
if (!$commandHandlingDependencies->contentStreamExists($workspace->currentContentStreamId)) {
throw new \RuntimeException('Cannot publish nodes on a workspace with a stateless content stream', 1710410114);
}
$workspaceContentStreamVersion = $this->requireOpenContentStreamVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($baseWorkspace, $commandHandlingDependencies);
$workspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

yield $this->closeContentStream(
$workspace->currentContentStreamId,
Expand Down Expand Up @@ -582,8 +576,8 @@ private function handleDiscardIndividualNodesFromWorkspace(
return;
}

$workspaceContentStreamVersion = $this->requireOpenContentStreamVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($baseWorkspace, $commandHandlingDependencies);
$workspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

yield $this->closeContentStream(
$workspace->currentContentStreamId,
Expand Down Expand Up @@ -678,8 +672,8 @@ private function handleDiscardWorkspace(
return;
}

$workspaceContentStreamVersion = $this->requireOpenContentStreamVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($baseWorkspace, $commandHandlingDependencies);
$workspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($workspace, $commandHandlingDependencies);
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

yield from $this->discardWorkspace(
$workspace,
Expand Down Expand Up @@ -747,7 +741,7 @@ private function handleChangeBaseWorkspace(
$newBaseWorkspace = $this->requireWorkspace($command->baseWorkspaceName, $commandHandlingDependencies);
$this->requireNonCircularRelationBetweenWorkspaces($workspace, $newBaseWorkspace, $commandHandlingDependencies);

$newBaseWorkspaceContentStreamVersion = $this->requireOpenContentStreamVersion($newBaseWorkspace, $commandHandlingDependencies);
$newBaseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($newBaseWorkspace, $commandHandlingDependencies);

yield $this->forkContentStream(
$command->newContentStreamId,
Expand Down Expand Up @@ -844,7 +838,7 @@ private function requireWorkspaceToNotExist(WorkspaceName $workspaceName, Comman
), 1715341085);
}

private function requireOpenContentStreamVersion(Workspace $workspace, CommandHandlingDependencies $commandHandlingDependencies): Version
private function requireOpenContentStreamAndVersion(Workspace $workspace, CommandHandlingDependencies $commandHandlingDependencies): Version
{
if ($commandHandlingDependencies->isContentStreamClosed($workspace->currentContentStreamId)) {
throw new ContentStreamIsClosed(
Expand Down

0 comments on commit 8b55830

Please sign in to comment.