Skip to content

Commit

Permalink
!!! TASK: Require WorkspaceName in getSubgraph instead of `Conten…
Browse files Browse the repository at this point in the history
…tStreamId`
  • Loading branch information
mhsdesign committed Mar 19, 2024
1 parent f6e4e7f commit 53a1687
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\Exception\RootNodeAggregateDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Exception\WorkspaceDoesNotExist;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateClassification;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
Expand Down Expand Up @@ -80,17 +81,20 @@ public function __construct(
}

final public function getSubgraph(
ContentStreamId $contentStreamId,
WorkspaceName $workspaceName,
DimensionSpacePoint $dimensionSpacePoint,
VisibilityConstraints $visibilityConstraints
): ContentSubgraphInterface {
$index = $contentStreamId->value . '-' . $dimensionSpacePoint->hash . '-' . $visibilityConstraints->getHash();
$index = $workspaceName->value . '-' . $dimensionSpacePoint->hash . '-' . $visibilityConstraints->getHash();
if (!isset($this->subgraphs[$index])) {
$contentStreamId = $this->findCurrentContentStreamIdForWorkspaceName($workspaceName);
if (!$contentStreamId) {
throw WorkspaceDoesNotExist::butWasSupposedTo($workspaceName);
}
$this->subgraphs[$index] = new ContentSubgraphWithRuntimeCaches(
new ContentSubgraph(
$this->contentRepositoryId,
// todo accept Workspace
WorkspaceName::forLive(),
$workspaceName,
$contentStreamId,
$dimensionSpacePoint,
$visibilityConstraints,
Expand Down Expand Up @@ -356,7 +360,7 @@ public function countNodes(): int
try {
return (int)$result->fetchOne();
} catch (DriverException | DBALException $e) {
throw new \RuntimeException(sprintf('Failed to fetch rows from database: %s', $e->getMessage()), 1701444590, $e);
throw new \RuntimeException(sprintf('Failed to count rows in database: %s', $e->getMessage()), 1701444590, $e);
}
}

Expand Down Expand Up @@ -429,4 +433,27 @@ private function fetchRows(QueryBuilder $queryBuilder): array
throw new \RuntimeException(sprintf('Failed to fetch rows from database: %s', $e->getMessage()), 1701444358, $e);
}
}

private function findCurrentContentStreamIdForWorkspaceName(WorkspaceName $workspaceName): ?ContentStreamId
{
$query = $this->createQueryBuilder()
->select('workspaces.currentContentStreamId')
->from($this->tableNamePrefix . '_workspaces', 'workspaces')
->where('workspaces.workspaceName = :workspaceName')
->setParameter(':workspaceName', $workspaceName->value);

$result = $query->execute();
if (!$result instanceof Result) {
throw new \RuntimeException(sprintf('Failed to fetch contents stream for workspace. Expected result to be of type %s, got: %s', Result::class, get_debug_type($result)), 1710883555);
}
try {
$contentStreamId = $result->fetchOne();
if ($contentStreamId === false) {
return null;
}
return ContentStreamId::fromString((string)$contentStreamId);
} catch (DriverException | DBALException $e) {
throw new \RuntimeException(sprintf('Failed to fetch row in database: %s', $e->getMessage()), 1710883554, $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

/**
* The PostgreSQL adapter content hypergraph
Expand All @@ -50,14 +51,17 @@ final class ContentHypergraph implements ContentGraphInterface
private NodeFactory $nodeFactory;

/**
* @phpstan-ignore-next-line unused
* @var array|ContentSubhypergraph[]
*/
private array $subhypergraphs;

public function __construct(
PostgresDbalClientInterface $databaseClient,
NodeFactory $nodeFactory,
/** @phpstan-ignore-next-line unused */
private readonly ContentRepositoryId $contentRepositoryId,
/** @phpstan-ignore-next-line unused */
private readonly NodeTypeManager $nodeTypeManager,
private readonly string $tableNamePrefix
) {
Expand All @@ -66,10 +70,13 @@ public function __construct(
}

public function getSubgraph(
ContentStreamId $contentStreamId,
WorkspaceName $workspaceName,
DimensionSpacePoint $dimensionSpacePoint,
VisibilityConstraints $visibilityConstraints
): ContentSubgraphInterface {
throw new \BadMethodCallException('The postgres adapter is not functional.');

/*
$index = $contentStreamId->value . '-' . $dimensionSpacePoint->hash . '-' . $visibilityConstraints->getHash();
if (!isset($this->subhypergraphs[$index])) {
$this->subhypergraphs[$index] = new ContentSubhypergraph(
Expand All @@ -85,6 +92,7 @@ public function getSubgraph(
}
return $this->subhypergraphs[$index];
*/
}

public function findRootNodeAggregateByType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private function handleChangeNodeAggregateType(
$tetheredNodeName = NodeName::fromString($serializedTetheredNodeName);

$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$node->subgraphIdentity->contentStreamId,
$node->identity->workspaceName,
$node->originDimensionSpacePoint->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

/**
* This is the MAIN ENTRY POINT for the Content Repository. This class exists only
Expand All @@ -40,7 +41,7 @@ interface ContentGraphInterface extends ProjectionStateInterface
* @api main API method of ContentGraph
*/
public function getSubgraph(
ContentStreamId $contentStreamId,
WorkspaceName $workspaceName,
DimensionSpacePoint $dimensionSpacePoint,
VisibilityConstraints $visibilityConstraints
): ContentSubgraphInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* By using the content graph for the content repository
* one can build a subgraph with the right perspective to find this node:
*
* $workspace = $contentRepository->getWorkspaceFinder()->findOneByName($identity->workspaceName);
* $subgraph = $contentGraph->getSubgraph(
* $workspace->currentContentStreamId,
* $identity->workspaceName,
* $nodeIdentity->dimensionSpacePoint,
* // resolve also all disabled nodes
* VisibilityConstraints::withoutRestrictions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function () use ($tetheredNodeAggregate) {
if ($foundMissingOrDisallowedTetheredNodes === false) {
foreach ($originDimensionSpacePoints as $originDimensionSpacePoint) {
$subgraph = $this->contentRepository->getContentGraph()->getSubgraph(
// todo add workspace to node aggregate
$nodeAggregate->contentStreamId,
$originDimensionSpacePoint->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Neos\ContentRepository\Core\Projection\ContentGraph\Filter\FindRootNodeAggregatesFilter;
use Neos\ContentRepository\Core\Projection\ContentGraph\VisibilityConstraints;
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\Workspace\WorkspaceName;
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
Expand Down Expand Up @@ -142,22 +143,26 @@ public function createVariantsRecursivelyCommand(string $source, string $target,
$contentRepositoryId = ContentRepositoryId::fromString($contentRepository);
$sourceSpacePoint = DimensionSpacePoint::fromJsonString($source);
$targetSpacePoint = OriginDimensionSpacePoint::fromJsonString($target);
$workspaceName = WorkspaceName::fromString($workspace);

$contentRepositoryInstance = $this->contentRepositoryRegistry->get($contentRepositoryId);
$workspaceInstance = $contentRepositoryInstance->getWorkspaceFinder()->findOneByName(WorkspaceName::fromString($workspace));
if ($workspaceInstance === null) {
$this->outputLine('<error>Workspace "%s" does not exist</error>', [$workspace]);

try {
$sourceSubgraph = $contentRepositoryInstance->getContentGraph()->getSubgraph(
$workspaceName,
$sourceSpacePoint,
VisibilityConstraints::withoutRestrictions()
);
} catch (WorkspaceDoesNotExist $exception) {
$this->outputLine('<error>Workspace "%s" does not exist</error>', [$workspaceName->value]);
$this->quit(1);
}

$this->outputLine('Creating <b>%s</b> to <b>%s</b> in workspace <b>%s</b> (content repository <b>%s</b>)', [$sourceSpacePoint->toJson(), $targetSpacePoint->toJson(), $workspaceInstance->workspaceName->value, $contentRepositoryId->value]);
$this->outputLine('Resolved content stream <b>%s</b>', [$workspaceInstance->currentContentStreamId->value]);
$this->outputLine('Creating <b>%s</b> to <b>%s</b> in workspace <b>%s</b> (content repository <b>%s</b>)', [$sourceSpacePoint->toJson(), $targetSpacePoint->toJson(), $workspaceName->value, $contentRepositoryId->value]);

$sourceSubgraph = $contentRepositoryInstance->getContentGraph()->getSubgraph(
$workspaceInstance->currentContentStreamId,
$sourceSpacePoint,
VisibilityConstraints::withoutRestrictions()
);
// todo also refactor the getContentGraph to operate on live workspaces
$workspaceInstance = $contentRepositoryInstance->getWorkspaceFinder()->findOneByName($workspaceName);
assert($workspaceInstance !== null);

$rootNodeAggregates = $contentRepositoryInstance->getContentGraph()
->findRootNodeAggregates($workspaceInstance->currentContentStreamId, FindRootNodeAggregatesFilter::create());
Expand All @@ -170,7 +175,7 @@ public function createVariantsRecursivelyCommand(string $source, string $target,
$rootNodeAggregate->nodeAggregateId,
$sourceSubgraph,
$targetSpacePoint,
$workspaceInstance->workspaceName,
$workspaceName,
$contentRepositoryInstance,
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public function resetFactoryInstance(ContentRepositoryId $contentRepositoryId):

public function subgraphForNode(Node $node): ContentSubgraphInterface
{
$contentRepository = $this->get($node->subgraphIdentity->contentRepositoryId);
$contentRepository = $this->get($node->identity->contentRepositoryId);
return $contentRepository->getContentGraph()->getSubgraph(
$node->subgraphIdentity->contentStreamId,
$node->subgraphIdentity->dimensionSpacePoint,
$node->identity->workspaceName,
$node->identity->dimensionSpacePoint,
$node->subgraphIdentity->visibilityConstraints
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function isAssetUsageStillValid(AssetUsage $usage): bool
$dimensionSpacePoint = $usage->originDimensionSpacePoint->toDimensionSpacePoint();

$subGraph = $this->contentGraph->getSubgraph(
// todo use workspace name instead!!!
$usage->contentStreamId,
$dimensionSpacePoint,
VisibilityConstraints::withoutRestrictions()
Expand Down
2 changes: 1 addition & 1 deletion Neos.Neos/Classes/Controller/Backend/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function uploadAssetAction(Asset $asset, string $metadata, string $node,

$node = $contentRepository->getContentGraph()
->getSubgraph(
$nodeAddress->contentStreamId,
$nodeAddress->workspaceName,
$nodeAddress->dimensionSpacePoint,
VisibilityConstraints::withoutRestrictions()
)
Expand Down
4 changes: 2 additions & 2 deletions Neos.Neos/Classes/Controller/Frontend/NodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function previewAction(string $node): void
$nodeAddress = NodeAddressFactory::create($contentRepository)->createFromUriString($node);

$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$nodeAddress->contentStreamId,
$nodeAddress->workspaceName,
$nodeAddress->dimensionSpacePoint,
$visibilityConstraints
);
Expand Down Expand Up @@ -202,7 +202,7 @@ public function showAction(string $node): void
}

$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$nodeAddress->contentStreamId,
$nodeAddress->workspaceName,
$nodeAddress->dimensionSpacePoint,
VisibilityConstraints::frontend()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,22 +740,26 @@ protected function computeSiteChanges(Workspace $selectedWorkspace, ContentRepos
);

foreach ($changes as $change) {
$contentStreamId = $change->contentStreamId;

if ($change->deleted) {
// If we deleted a node, there is no way for us to anymore find the deleted node in the ContentStream
// where the node was deleted.
// Thus, to figure out the rootline for display, we check the *base workspace* Content Stream.
//
// This is safe because the UI basically shows what would be removed once the deletion is published.
$baseWorkspace = $this->getBaseWorkspaceWhenSureItExists($selectedWorkspace, $contentRepository);
$contentStreamId = $baseWorkspace->currentContentStreamId;

$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$baseWorkspace->workspaceName,
$change->originDimensionSpacePoint->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
);
} else {
$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$selectedWorkspace->workspaceName,
$change->originDimensionSpacePoint->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
);
}
$subgraph = $contentRepository->getContentGraph()->getSubgraph(
$contentStreamId,
$change->originDimensionSpacePoint->toDimensionSpacePoint(),
VisibilityConstraints::withoutRestrictions()
);

$node = $subgraph->findNodeById($change->nodeAggregateId);
if ($node) {
Expand Down Expand Up @@ -862,11 +866,11 @@ protected function computeSiteChanges(Workspace $selectedWorkspace, ContentRepos
*/
protected function getOriginalNode(
Node $modifiedNode,
ContentStreamId $baseContentStreamId,
WorkspaceName $baseWorkspaceName,
ContentRepository $contentRepository,
): ?Node {
$baseSubgraph = $contentRepository->getContentGraph()->getSubgraph(
$baseContentStreamId,
$baseWorkspaceName,
$modifiedNode->subgraphIdentity->dimensionSpacePoint,
VisibilityConstraints::withoutRestrictions()
);
Expand All @@ -892,8 +896,7 @@ protected function renderContentChanges(
$originalNode = null;
if ($currentWorkspace !== null) {
$baseWorkspace = $this->getBaseWorkspaceWhenSureItExists($currentWorkspace, $contentRepository);
$baseContentStreamId = $baseWorkspace->currentContentStreamId;
$originalNode = $this->getOriginalNode($changedNode, $baseContentStreamId, $contentRepository);
$originalNode = $this->getOriginalNode($changedNode, $baseWorkspace->workspaceName, $contentRepository);
}


Expand Down
Loading

0 comments on commit 53a1687

Please sign in to comment.