Skip to content

Commit

Permalink
NGSTACK-836 AncestorIndexer and AncestorResolver changes and swap loc…
Browse files Browse the repository at this point in the history
…ation handler fix
  • Loading branch information
Katarina Miočić committed Jul 11, 2024
1 parent 1bd6ec2 commit 9a4a88a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,34 @@ public function indexSingle(Location $location): void
$this->searchHandler->indexLocation($ancestor);
}

/**
* @param \Ibexa\Contracts\Core\Persistence\Content\Location $location
*/
public function indexSingleForSwapLocation(Location $location, Location $swappedLocation): void
{
$ancestor = $this->ancestorResolver->resolveAncestorForSwapLocation($location, $swappedLocation);

if ($ancestor === null) {
return;
}

try {
$content = $this->contentHandler->load($ancestor->contentId);
} catch (NotFoundException) {
return;
}

$this->searchHandler->indexContent($content);
$this->searchHandler->indexLocation($ancestor);
}

/**
* @param \Ibexa\Contracts\Core\Persistence\Content\Location $location
*/
public function indexSingleForParentLocation(Location $location): void
{
$this->indexSingle($location);

$ancestor = $this->ancestorResolver->resolveAncestorForParentLocation($location);

if ($ancestor === null) {
Expand Down Expand Up @@ -71,8 +94,6 @@ public function indexMultiple(array $locations): void
*/
public function indexMultipleForParentLocation(array $locations): void
{
$this->indexMultiple($locations);

foreach ($locations as $location) {
$this->indexSingleForParentLocation($location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,28 @@ public function resolveAncestor(Location $location): ?Location
do {
$match = $this->matchPath($ancestry);

if ($match === 0) {
if ($match === true) {
return end($ancestry);
}
} while (is_int($match) && $this->addToAncestry($ancestry));
} while (is_bool($match) && $this->addToAncestry($ancestry));

return null;
}

public function resolveAncestorForSwapLocation(Location $location, Location $swappedLocation): ?Location
{
$contentInfo = $this->contentHandler->loadContentInfo($swappedLocation->contentId);
$contentType = $this->contentTypeHandler->load($contentInfo->contentTypeId);
$contentTypeIdentifier = $contentType->identifier;
$ancestry = [$location];

do {
$match = $this->matchPath($ancestry, $contentTypeIdentifier);

if ($match === true) {
return end($ancestry);
}
} while (is_bool($match) && $this->addToAncestry($ancestry));

return null;
}
Expand Down Expand Up @@ -70,33 +88,46 @@ public function resolveAncestorForParentLocation(Location $location): ?Location
*
* @param \Ibexa\Contracts\Core\Persistence\Content\Location[] $ancestry
*/
private function matchPath(array $ancestry): false|int
private function matchPath(array $ancestry, ?string $firstContentTypeIdentifier = null): ?bool
{
$ancestryPath = $this->getAncestryPath($ancestry);
$ancestryPath = $this->getAncestryPath($ancestry, $firstContentTypeIdentifier);

if ($ancestryPath === null) {
return false;
}
$isPartialMatch = false;

foreach ($this->ancestorPathGenerator->getPaths() as $path) {
if (str_starts_with($path, $ancestryPath)) {
return mb_strlen($path) - mb_strlen($ancestryPath);
if (str_starts_with($path, $ancestryPath . '/')) {
$isPartialMatch = true;
}

if ($path === $ancestryPath) {
return true;
}
}

return false;
if ($isPartialMatch) {
return false;
}

return null;
}

/**
* @param \Ibexa\Contracts\Core\Persistence\Content\Location[] $ancestry
*/
private function getAncestryPath(array $ancestry): ?string
private function getAncestryPath(array $ancestry, ?string $firstContentTypeIdentifier = null): ?string
{
$pathElements = [];

foreach ($ancestry as $location) {
foreach ($ancestry as $index => $location) {
try {
$pathElements[] = $this->getContentTypeIdentifier($location);
if ($index === 0 && $firstContentTypeIdentifier !== null) {
$pathElements[] = $firstContentTypeIdentifier;
} else {
$pathElements[] = $this->getContentTypeIdentifier($location);
}
} catch (NotFoundException) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public function __construct(

public function __invoke(SwapLocation $message): void
{
$this->reindexForLocation($message->location1Id);
$this->reindexForLocation($message->location2Id);
$this->reindexForLocation($message->location1Id, $message->location2Id);
$this->reindexForLocation($message->location2Id, $message->location1Id);
}

private function reindexForLocation(int $locationId): void
private function reindexForLocation(int $locationId, int $swappedLocationId): void
{
try {
$location = $this->locationHandler->load($locationId);
Expand All @@ -43,6 +43,20 @@ private function reindexForLocation(int $locationId): void
return;
}

$this->ancestorIndexer->indexSingle($location);
try {
$swappedLocation = $this->locationHandler->load($swappedLocationId);
} catch (NotFoundException) {
$this->logger->info(
sprintf(
'%s: Location #%d is gone, aborting',
$this::class,
$swappedLocationId,
),
);

return;
}

$this->ancestorIndexer->indexSingleForSwapLocation($location, $swappedLocation);
}
}

0 comments on commit 9a4a88a

Please sign in to comment.