diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellIdentifierRepository.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellIdentifierRepository.java index 449c613c..91f3d133 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellIdentifierRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellIdentifierRepository.java @@ -29,6 +29,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface ShellIdentifierRepository extends JpaRepository { @@ -50,6 +51,52 @@ sid.shellId.id IN ( GROUP BY filtersid.shellId.id HAVING COUNT(*) = :keyValueCombinationsSize ) - """) + """ ) List findMinimalShellIdsBySpecificAssetIds( List keyValueCombinations, int keyValueCombinationsSize ); + + /** + * Returns external shell ids for the given keyValueCombinations. + * External shell ids matching the conditions below are returned: + * - specificAssetIds match exactly the keyValueCombinations + * - if externalSubjectId (tenantId) is not null it must match the tenantId + * + * + * To be able to properly index the key and value conditions, the query does not use any functions. + * Computed indexes cannot be created for mutable functions like CONCAT in Postgres. + * + * @param keyValueCombinations the keys values to search for as tuples + * @param keyValueCombinationsSize the size of the key value combinations + * @return external shell ids for the given key value combinations + */ + @Query( value = """ + SELECT s.id_external + FROM shell s + JOIN shell_identifier si ON s.id = si.fk_shell_id + WHERE + CONCAT(si.namespace, si.identifier) IN (:keyValueCombinations) + AND ( + :tenantId = :owningTenantId + OR si.namespace = :globalAssetId + OR EXISTS ( + SELECT 1 + FROM SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE_KEY sider + JOIN SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE sies ON sider.FK_SI_EXTERNAL_SUBJECT_REFERENCE_ID = sies.id + WHERE + ( + sider.ref_key_value = :tenantId + OR ( sider.ref_key_value = :publicWildcardPrefix AND si.namespace IN (:publicWildcardAllowedTypes) ) + ) + AND sies.FK_SHELL_IDENTIFIER_EXTERNAL_SUBJECT_ID = si.id + ) + ) + GROUP BY s.id_external + HAVING COUNT(*) = :keyValueCombinationsSize + """, nativeQuery = true ) + List findExternalShellIdsByIdentifiersByExactMatch( @Param( "keyValueCombinations" ) List keyValueCombinations, + @Param( "keyValueCombinationsSize" ) int keyValueCombinationsSize, + @Param( "tenantId" ) String tenantId, + @Param( "publicWildcardPrefix" ) String publicWildcardPrefix, + @Param( "publicWildcardAllowedTypes" ) List publicWildcardAllowedTypes, + @Param( "owningTenantId" ) String owningTenantId, + @Param( "globalAssetId" ) String globalAssetId ); } diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java index 4d5ceef3..95650b64 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/repository/ShellRepository.java @@ -79,52 +79,6 @@ Optional findByIdExternalAndExternalSubjectId( @Param( "idExternal" ) Str List findShellsByIdExternalIsIn( Set idExternals ); - /** - * Returns external shell ids for the given keyValueCombinations. - * External shell ids matching the conditions below are returned: - * - specificAssetIds match exactly the keyValueCombinations - * - if externalSubjectId (tenantId) is not null it must match the tenantId - * - * - * To be able to properly index the key and value conditions, the query does not use any functions. - * Computed indexes cannot be created for mutable functions like CONCAT in Postgres. - * - * @param keyValueCombinations the keys values to search for as tuples - * @param keyValueCombinationsSize the size of the key value combinations - * @return external shell ids for the given key value combinations - */ - @Query( value = """ - SELECT s.id_external - FROM shell s - JOIN shell_identifier si ON s.id = si.fk_shell_id - WHERE - CONCAT(si.namespace, si.identifier) IN (:keyValueCombinations) - AND ( - :tenantId = :owningTenantId - OR si.namespace = :globalAssetId - OR EXISTS ( - SELECT 1 - FROM SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE_KEY sider - JOIN SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE sies ON sider.FK_SI_EXTERNAL_SUBJECT_REFERENCE_ID = sies.id - WHERE - ( - sider.ref_key_value = :tenantId - OR ( sider.ref_key_value = :publicWildcardPrefix AND si.namespace IN (:publicWildcardAllowedTypes) ) - ) - AND sies.FK_SHELL_IDENTIFIER_EXTERNAL_SUBJECT_ID = si.id - ) - ) - GROUP BY s.id_external - HAVING COUNT(*) = :keyValueCombinationsSize - """, nativeQuery = true ) - List findExternalShellIdsByIdentifiersByExactMatch( @Param( "keyValueCombinations" ) List keyValueCombinations, - @Param( "keyValueCombinationsSize" ) int keyValueCombinationsSize, - @Param( "tenantId" ) String tenantId, - @Param( "publicWildcardPrefix" ) String publicWildcardPrefix, - @Param( "publicWildcardAllowedTypes" ) List publicWildcardAllowedTypes, - @Param( "owningTenantId" ) String owningTenantId, - @Param( "globalAssetId" ) String globalAssetId ); - /** * Returns external shell ids for the given keyValueCombinations. * External shell ids that match any keyValueCombinations are returned. diff --git a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java index 9a478b41..ee402096 100644 --- a/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java +++ b/backend/src/main/java/org/eclipse/tractusx/semantics/registry/service/ShellService.java @@ -204,7 +204,7 @@ public ShellCollectionDto findAllShells( Integer pageSize, String cursorVal, Str Page currentPage = shellRepository.findAll( specification, ofSize( fetchSize ) ); List shells = shellAccessHandler.filterListOfShellProperties( currentPage.stream().toList(), externalSubjectId ); shells.stream() - .limit( (long) pageSize - foundList.size() ) + .limit( (long) fetchSize - foundList.size() ) .forEach( foundList::add ); hasNext = currentPage.hasNext(); } @@ -278,7 +278,7 @@ public GetAllAssetAdministrationShellIdsByAssetLink200Response findExternalShell .forEach( assetIdList::add ); nextCursor = getCursorEncoded( allVisible, assetIdList ); } else { - List queryResult = shellRepository.findExternalShellIdsByIdentifiersByExactMatch( keyValueCombinations, + List queryResult = shellIdentifierRepository.findExternalShellIdsByIdentifiersByExactMatch( keyValueCombinations, keyValueCombinations.size(), externalSubjectId, externalSubjectIdWildcardPrefix, externalSubjectIdWildcardAllowedTypes, owningTenantId, ShellIdentifier.GLOBAL_ASSET_ID_KEY ); pageSize = getPageSize( pageSize );