From cfc849c113d64ac9dc045abe79d36e77007f6bb9 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Wed, 26 Jul 2023 09:24:44 +0200 Subject: [PATCH 1/4] IBX-5936: Added `ContentTypeHandler::loadContentTypesByFieldDefinitionIdentifier` method --- .../Persistence/Content/Type/Handler.php | 5 ++++ .../Persistence/Cache/ContentTypeHandler.php | 18 ++++++++++++++ .../Legacy/Content/Type/Gateway.php | 2 ++ .../Content/Type/Gateway/DoctrineDatabase.php | 14 +++++++++++ .../Type/Gateway/ExceptionConversion.php | 9 +++++++ .../Legacy/Content/Type/Handler.php | 10 ++++++++ .../Content/Type/MemoryCachingHandler.php | 16 +++++++++++++ .../settings/storage_engines/cache.yml | 1 + .../Cache/ContentTypeHandlerTest.php | 1 + .../Content/Type/ContentTypeHandlerTest.php | 24 +++++++++++++++++++ 10 files changed, 100 insertions(+) diff --git a/src/contracts/Persistence/Content/Type/Handler.php b/src/contracts/Persistence/Content/Type/Handler.php index c6ab11b1fd..c115ac4e93 100644 --- a/src/contracts/Persistence/Content/Type/Handler.php +++ b/src/contracts/Persistence/Content/Type/Handler.php @@ -91,6 +91,11 @@ public function loadContentTypes($groupId, $status = Type::STATUS_DEFINED); */ public function loadContentTypeList(array $contentTypeIds): array; + /** + * @return \Ibexa\Contracts\Core\Persistence\Content\Type[] + */ + public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array; + /** * Loads a content type by id and status. * diff --git a/src/lib/Persistence/Cache/ContentTypeHandler.php b/src/lib/Persistence/Cache/ContentTypeHandler.php index 134311faaf..47a4f25204 100644 --- a/src/lib/Persistence/Cache/ContentTypeHandler.php +++ b/src/lib/Persistence/Cache/ContentTypeHandler.php @@ -26,6 +26,7 @@ class ContentTypeHandler extends AbstractInMemoryPersistenceHandler implements C private const BY_IDENTIFIER_SUFFIX = 'by_identifier_suffix'; private const CONTENT_TYPE_LIST_BY_GROUP_IDENTIFIER = 'content_type_list_by_group'; private const BY_REMOTE_SUFFIX = 'by_remote_suffix'; + private const CONTENT_TYPE_LIST_BY_FIELD_DEFINITION_IDENTIFIER = 'content_type_list_by_field_definition_identifier'; private const TYPE_MAP_IDENTIFIER = 'type_map'; private const CONTENT_FIELDS_TYPE_IDENTIFIER = 'content_fields_type'; private const TYPE_WITHOUT_VALUE_IDENTIFIER = 'type_without_value'; @@ -303,6 +304,23 @@ function () use ($remoteId) { ); } + /** + * {@inheritdoc} + */ + public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array + { + return $this->getListCacheValue( + $this->cacheIdentifierGenerator->generateKey(self::CONTENT_TYPE_LIST_BY_FIELD_DEFINITION_IDENTIFIER, [$identifier], true), + function () use ($identifier): array { + return $this->persistenceHandler->contentTypeHandler()->loadContentTypesByFieldDefinitionIdentifier($identifier); + }, + $this->getTypeTags, + $this->getTypeKeys, + null, + [$identifier] + ); + } + /** * {@inheritdoc} */ diff --git a/src/lib/Persistence/Legacy/Content/Type/Gateway.php b/src/lib/Persistence/Legacy/Content/Type/Gateway.php index 592354e40e..9abdf52714 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Gateway.php +++ b/src/lib/Persistence/Legacy/Content/Type/Gateway.php @@ -116,6 +116,8 @@ abstract public function updateType(int $typeId, int $status, Type $type): void; */ abstract public function loadTypesListData(array $typeIds): array; + abstract public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array; + abstract public function loadTypeData(int $typeId, int $status): array; abstract public function loadTypeDataByIdentifier(string $identifier, int $status): array; diff --git a/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php b/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php index f947a15989..4251e81ba6 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php +++ b/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php @@ -949,6 +949,20 @@ public function loadTypesListData(array $typeIds): array return $query->execute()->fetchAll(); } + public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array + { + $query = $this->getLoadTypeQueryBuilder(); + $query + ->where( + $query->expr()->eq( + 'a.data_type_string', + $query->createNamedParameter($identifier) + ) + ); + + return $query->execute()->fetchAllAssociative(); + } + public function loadTypeData(int $typeId, int $status): array { $query = $this->getLoadTypeQueryBuilder(); diff --git a/src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php b/src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php index 2b32c7d8d9..fd6229651d 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php +++ b/src/lib/Persistence/Legacy/Content/Type/Gateway/ExceptionConversion.php @@ -245,6 +245,15 @@ public function loadTypeDataByRemoteId(string $remoteId, int $status): array } } + public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array + { + try { + return $this->innerGateway->loadTypesDataByFieldDefinitionIdentifier($identifier); + } catch (PDOException $e) { + throw DatabaseException::wrap($e); + } + } + public function countInstancesOfType(int $typeId): int { try { diff --git a/src/lib/Persistence/Legacy/Content/Type/Handler.php b/src/lib/Persistence/Legacy/Content/Type/Handler.php index 4f04557b13..8ade46c4bd 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Handler.php +++ b/src/lib/Persistence/Legacy/Content/Type/Handler.php @@ -195,6 +195,16 @@ public function loadContentTypeList(array $contentTypeIds): array ); } + /** + * @return \Ibexa\Contracts\Core\Persistence\Content\Type[] + */ + public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array + { + return $this->mapper->extractTypesFromRows( + $this->contentTypeGateway->loadTypesDataByFieldDefinitionIdentifier($identifier) + ); + } + /** * Loads a content type by id and status. * diff --git a/src/lib/Persistence/Legacy/Content/Type/MemoryCachingHandler.php b/src/lib/Persistence/Legacy/Content/Type/MemoryCachingHandler.php index 4452567ea3..9294d4769d 100644 --- a/src/lib/Persistence/Legacy/Content/Type/MemoryCachingHandler.php +++ b/src/lib/Persistence/Legacy/Content/Type/MemoryCachingHandler.php @@ -186,6 +186,22 @@ public function loadContentTypeList(array $contentTypeIds): array return $contentTypes; } + /** + * {@inheritdoc} + */ + public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array + { + $cacheKey = 'ibx-ctlbfdi-' . $identifier; + + $types = $this->cache->get($cacheKey); + if ($types === null) { + $types = $this->innerHandler->loadContentTypesByFieldDefinitionIdentifier($identifier); + $this->storeTypeCache($types, $cacheKey); + } + + return $types; + } + /** * {@inheritdoc} */ diff --git a/src/lib/Resources/settings/storage_engines/cache.yml b/src/lib/Resources/settings/storage_engines/cache.yml index 1fb13b0c3d..89953faf04 100644 --- a/src/lib/Resources/settings/storage_engines/cache.yml +++ b/src/lib/Resources/settings/storage_engines/cache.yml @@ -125,6 +125,7 @@ parameters: content_type_group: 'ctg-%s' content_type_group_with_id_suffix: 'ctg-%s-bi' content_type_group_with_by_remote_suffix: 'ctg-%s-br' + content_type_list_by_field_definition_identifier: 'ctlbfdi-%s' content_type_group_list: 'ctgl-%s' content_type_list_by_group: 'ctlbg-%s' image_variation: 'ig' diff --git a/tests/lib/Persistence/Cache/ContentTypeHandlerTest.php b/tests/lib/Persistence/Cache/ContentTypeHandlerTest.php index d5ee9bcfab..c6073f4640 100644 --- a/tests/lib/Persistence/Cache/ContentTypeHandlerTest.php +++ b/tests/lib/Persistence/Cache/ContentTypeHandlerTest.php @@ -204,6 +204,7 @@ public function providerForCachedLoadMethodsHit(): array ], ['loadAllGroups', [], 'ibx-ctgl', null, null, [['content_type_group_list', [], true]], ['ibx-ctgl'], [3 => $group]], ['loadContentTypes', [3, 0], 'ibx-ctlbg-3', null, null, [['content_type_list_by_group', [3], true]], ['ibx-ctlbg-3'], [$type]], + ['loadContentTypesByFieldDefinitionIdentifier', [3, 0], 'ibx-ctlbfdi-3', null, null, [['content_type_list_by_field_definition_identifier', [3], true]], ['ibx-ctlbfdi-3'], [$type]], ['loadContentTypeList', [[5]], 'ibx-ct-5', null, null, [['content_type', [], true]], ['ibx-ct'], [5 => $type], true], ['load', [5, 0], 'ibx-ct-5', null, null, [['content_type', [], true]], ['ibx-ct'], $type], [ diff --git a/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php b/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php index 053cd536a9..ce85e08acd 100644 --- a/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php +++ b/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php @@ -284,6 +284,30 @@ public function testLoadContentTypeList(): void ); } + public function testLoadContentTypesByFieldDefinitionIdentifier(): void + { + $gatewayMock = $this->getGatewayMock(); + $gatewayMock->expects($this->once()) + ->method('loadTypesDataByFieldDefinitionIdentifier') + ->with(self::equalTo('ezstring')) + ->willReturn([]); + + $mapperMock = $this->getMapperMock(); + $mapperMock->expects(self::once()) + ->method('extractTypesFromRows') + ->with($this->equalTo([])) + ->willReturn([23 => new Type()]); + + $handler = $this->getHandler(); + $types = $handler->loadContentTypesByFieldDefinitionIdentifier('ezstring'); + + self::assertEquals( + [23 => new Type()], + $types, + 'Types not loaded correctly' + ); + } + public function testLoad() { $gatewayMock = $this->getGatewayMock(); From e2b08a55eadd67a8a50f746693eea8ad7dcc2a06 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Mon, 31 Jul 2023 09:28:32 +0200 Subject: [PATCH 2/4] cr remarks --- src/lib/Persistence/Cache/ContentTypeHandler.php | 3 --- src/lib/Persistence/Legacy/Content/Type/Gateway.php | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/Persistence/Cache/ContentTypeHandler.php b/src/lib/Persistence/Cache/ContentTypeHandler.php index 47a4f25204..7ca287dbef 100644 --- a/src/lib/Persistence/Cache/ContentTypeHandler.php +++ b/src/lib/Persistence/Cache/ContentTypeHandler.php @@ -304,9 +304,6 @@ function () use ($remoteId) { ); } - /** - * {@inheritdoc} - */ public function loadContentTypesByFieldDefinitionIdentifier(string $identifier): array { return $this->getListCacheValue( diff --git a/src/lib/Persistence/Legacy/Content/Type/Gateway.php b/src/lib/Persistence/Legacy/Content/Type/Gateway.php index 9abdf52714..769f917ee6 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Gateway.php +++ b/src/lib/Persistence/Legacy/Content/Type/Gateway.php @@ -116,6 +116,9 @@ abstract public function updateType(int $typeId, int $status, Type $type): void; */ abstract public function loadTypesListData(array $typeIds): array; + /** + * @return array + */ abstract public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): array; abstract public function loadTypeData(int $typeId, int $status): array; From eb9df5a59a7b31670286c970e1238e29b95e9af8 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Mon, 31 Jul 2023 09:33:18 +0200 Subject: [PATCH 3/4] added index to `ezcontentclass_attr_dts.data_type_string` column --- src/bundle/Core/Resources/config/storage/legacy/schema.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bundle/Core/Resources/config/storage/legacy/schema.yaml b/src/bundle/Core/Resources/config/storage/legacy/schema.yaml index bac2657eb2..85d9c076c0 100644 --- a/src/bundle/Core/Resources/config/storage/legacy/schema.yaml +++ b/src/bundle/Core/Resources/config/storage/legacy/schema.yaml @@ -141,6 +141,7 @@ tables: ezcontentclass_attribute: indexes: ezcontentclass_attr_ccid: { fields: [contentclass_id] } + ezcontentclass_attr_dts: { fields: [data_type_string] } id: id: { type: integer, nullable: false, options: { autoincrement: true } } version: { type: integer, nullable: false, options: { default: '0' } } From f7120068a5842bd4c45feb3508c6528e2ca6386f Mon Sep 17 00:00:00 2001 From: konradoboza Date: Mon, 31 Jul 2023 09:53:02 +0200 Subject: [PATCH 4/4] cr remarks vol.2 --- .../Legacy/Content/Type/Gateway/DoctrineDatabase.php | 2 +- .../Legacy/Content/Type/ContentTypeHandlerTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php b/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php index 4251e81ba6..be8e087ed0 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php +++ b/src/lib/Persistence/Legacy/Content/Type/Gateway/DoctrineDatabase.php @@ -953,7 +953,7 @@ public function loadTypesDataByFieldDefinitionIdentifier(string $identifier): ar { $query = $this->getLoadTypeQueryBuilder(); $query - ->where( + ->andWhere( $query->expr()->eq( 'a.data_type_string', $query->createNamedParameter($identifier) diff --git a/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php b/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php index ce85e08acd..2226bc1508 100644 --- a/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php +++ b/tests/lib/Persistence/Legacy/Content/Type/ContentTypeHandlerTest.php @@ -289,13 +289,13 @@ public function testLoadContentTypesByFieldDefinitionIdentifier(): void $gatewayMock = $this->getGatewayMock(); $gatewayMock->expects($this->once()) ->method('loadTypesDataByFieldDefinitionIdentifier') - ->with(self::equalTo('ezstring')) + ->with('ezstring') ->willReturn([]); $mapperMock = $this->getMapperMock(); $mapperMock->expects(self::once()) ->method('extractTypesFromRows') - ->with($this->equalTo([])) + ->with([]) ->willReturn([23 => new Type()]); $handler = $this->getHandler();