+ fragmentEntities = prefetchDescendantsForFragmentEntities(fetchDescendantsOption, anchorEntity,
+ fragmentEntities);
+ return createDataNodesFromFragmentEntities(fetchDescendantsOption, fragmentEntities);
+ }
+
+ @Override
+ public List<DataNode> queryDataNodesAcrossAnchors(final String dataspaceName, final String cpsPath,
+ final FetchDescendantsOption fetchDescendantsOption) {
+ return queryDataNodes(dataspaceName, QUERY_ACROSS_ANCHORS, cpsPath, fetchDescendantsOption);
+ }
+
+ private Collection<FragmentEntity> prefetchDescendantsForFragmentEntities(
+ final FetchDescendantsOption fetchDescendantsOption,
+ final AnchorEntity anchorEntity,
+ final Collection<FragmentEntity> proxiedFragmentEntities) {
+ if (FetchDescendantsOption.OMIT_DESCENDANTS.equals(fetchDescendantsOption)) {
+ return proxiedFragmentEntities;
+ }
+
+ final List<Long> fragmentEntityIds = proxiedFragmentEntities.stream()
+ .map(FragmentEntity::getId).collect(Collectors.toList());
+
+ final List<FragmentExtract> fragmentExtracts =
+ fragmentRepository.findExtractsWithDescendantsByIds(fragmentEntityIds, fetchDescendantsOption.getDepth());
+
+ if (anchorEntity == ALL_ANCHORS) {
+ final Collection<Integer> anchorIds = fragmentExtracts.stream()
+ .map(FragmentExtract::getAnchorId).collect(Collectors.toSet());
+ final List<AnchorEntity> anchorEntities = anchorRepository.findAllById(anchorIds);
+ final Map<Integer, AnchorEntity> anchorEntityPerId = anchorEntities.stream()
+ .collect(Collectors.toMap(AnchorEntity::getId, Function.identity()));
+ return FragmentEntityArranger.toFragmentEntityTreesAcrossAnchors(anchorEntityPerId, fragmentExtracts);
+ }
+ return FragmentEntityArranger.toFragmentEntityTrees(anchorEntity, fragmentExtracts);