Skip to content

Commit

Permalink
Reduced Ibexa\Core\Persistence\Legacy\Content\Type\Mapper::extractTyp…
Browse files Browse the repository at this point in the history
…esFromRows computational complexity from n^2 to 2n
  • Loading branch information
adamwojs committed Oct 21, 2024
1 parent c338dc5 commit f555028
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/lib/Persistence/Legacy/Content/Type/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace Ibexa\Core\Persistence\Legacy\Content\Type;

use Ibexa\Contracts\Core\Persistence\Content\Type;
Expand Down Expand Up @@ -57,9 +58,9 @@ public function __construct(
*
* @param \Ibexa\Contracts\Core\Persistence\Content\Type\Group\CreateStruct $struct
*
* @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group
* @todo $description is not supported by database, yet
*
* @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group
*/
public function createGroupFromCreateStruct(GroupCreateStruct $struct)
{
Expand Down Expand Up @@ -119,6 +120,16 @@ public function extractTypesFromRows(array $rows, bool $keepTypeIdAsKey = false)
$types = [];
$fields = [];

$rowsByAttributeId = [];
foreach ($rows as $row) {
$attributeId = (int)$row['ezcontentclass_attribute_id'];
if (!isset($rowsByAttributeId[$attributeId])) {
$rowsByAttributeId[$attributeId] = [];
}

$rowsByAttributeId[$attributeId][] = $row;
}

foreach ($rows as $row) {
$typeId = (int)$row['ezcontentclass_id'];
if (!isset($types[$typeId])) {
Expand All @@ -128,9 +139,7 @@ public function extractTypesFromRows(array $rows, bool $keepTypeIdAsKey = false)
$fieldId = (int)$row['ezcontentclass_attribute_id'];

if ($fieldId && !isset($fields[$fieldId])) {
$fieldDataRows = array_filter($rows, static function (array $row) use ($fieldId) {
return (int) $row['ezcontentclass_attribute_id'] === (int) $fieldId;
});
$fieldDataRows = $rowsByAttributeId[$fieldId];

$multilingualData = $this->extractMultilingualData($fieldDataRows);

Expand Down

0 comments on commit f555028

Please sign in to comment.