Node API - Find root by anchor method performance
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2021-2022 Nordix Foundation.\r
4  * Modifications Copyright (C) 2020-2021 Bell Canada.\r
5  * Modifications Copyright (C) 2020-2021 Pantheon.tech.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.cps.spi.repository;\r
24 \r
25 import java.util.Collection;\r
26 import java.util.List;\r
27 import java.util.Optional;\r
28 import javax.validation.constraints.NotNull;\r
29 import org.checkerframework.checker.nullness.qual.NonNull;\r
30 import org.onap.cps.spi.entities.AnchorEntity;\r
31 import org.onap.cps.spi.entities.DataspaceEntity;\r
32 import org.onap.cps.spi.entities.FragmentEntity;\r
33 import org.onap.cps.spi.entities.FragmentExtract;\r
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
35 import org.springframework.data.jpa.repository.JpaRepository;\r
36 import org.springframework.data.jpa.repository.Modifying;\r
37 import org.springframework.data.jpa.repository.Query;\r
38 import org.springframework.data.repository.query.Param;\r
39 import org.springframework.stereotype.Repository;\r
40 \r
41 @Repository\r
42 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery {\r
43 \r
44     Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
45                                                               @NonNull AnchorEntity anchorEntity,\r
46                                                               @NonNull String xpath);\r
47 \r
48     default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
49                                                            @NonNull AnchorEntity anchorEntity,\r
50                                                            @NonNull String xpath) {\r
51         return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
52             .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
53     }\r
54 \r
55     @Query(\r
56         value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
57         nativeQuery = true)\r
58     List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,\r
59                                                        @Param("anchor") int anchorId);\r
60 \r
61     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
62             + " CAST(attributes AS TEXT) AS attributes"\r
63             + " FROM FRAGMENT WHERE anchor_id = :anchorId",\r
64             nativeQuery = true)\r
65     List<FragmentExtract> findRootsByAnchorId(@Param("anchorId") int anchorId);\r
66 \r
67     /**\r
68      * find top level fragment by anchor.\r
69      *\r
70      * @param dataspaceEntity dataspace entity\r
71      * @param anchorEntity anchor entity\r
72      * @return FragmentEntity fragment entity\r
73      */\r
74     default List<FragmentExtract> getTopLevelFragments(DataspaceEntity dataspaceEntity,\r
75                                                        AnchorEntity anchorEntity) {\r
76         final List<FragmentExtract> fragmentExtracts = findRootsByAnchorId(anchorEntity.getId());\r
77         if (fragmentExtracts.isEmpty()) {\r
78             throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName());\r
79         }\r
80         return fragmentExtracts;\r
81     }\r
82 \r
83     List<FragmentEntity> findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity,\r
84                                                    @NonNull Collection<String> xpath);\r
85 \r
86     @Modifying\r
87     @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
88     void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
89 \r
90     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
91         + " CAST(attributes AS TEXT) AS attributes"\r
92         + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
93         + " AND ( xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%') )",\r
94            nativeQuery = true)\r
95     List<FragmentExtract> findByAnchorIdAndParentXpath(@Param("anchorId") int anchorId,\r
96                                                        @Param("parentXpath") String parentXpath);\r
97 }\r