
001/*- 002 * #%L 003 * HAPI FHIR JPA Server 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.search; 021 022import ca.uhn.fhir.jpa.api.config.JpaStorageSettings; 023import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc; 024import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; 025import ca.uhn.fhir.rest.api.server.IBundleProvider; 026import ca.uhn.fhir.rest.api.server.RequestDetails; 027import ca.uhn.fhir.rest.server.SimpleBundleProvider; 028import org.hl7.fhir.instance.model.api.IBaseResource; 029 030import javax.annotation.Nullable; 031import java.util.Collections; 032import java.util.List; 033import java.util.function.Supplier; 034 035/** 036 * Figure out how we're going to run the query up front, and build a branchless strategy object. 037 */ 038public class SearchStrategyFactory { 039 private final JpaStorageSettings myStorageSettings; 040 @Nullable 041 private final IFulltextSearchSvc myFulltextSearchSvc; 042 043 public interface ISearchStrategy extends Supplier<IBundleProvider> { 044 045 } 046 047 // someday 048// public class DirectHSearch implements ISearchStrategy {}; 049// public class JPAOffsetSearch implements ISearchStrategy {}; 050// public class JPASavedSearch implements ISearchStrategy {}; 051// public class JPAHybridHSearchSavedSearch implements ISearchStrategy {}; 052// public class SavedSearchAdaptorStrategy implements ISearchStrategy {}; 053 054 public SearchStrategyFactory(JpaStorageSettings theStorageSettings, @Nullable IFulltextSearchSvc theFulltextSearchSvc) { 055 myStorageSettings = theStorageSettings; 056 myFulltextSearchSvc = theFulltextSearchSvc; 057 } 058 059 public boolean isSupportsHSearchDirect(String theResourceType, SearchParameterMap theParams, RequestDetails theRequestDetails) { 060 return 061 myFulltextSearchSvc != null && 062 myStorageSettings.isStoreResourceInHSearchIndex() && 063 myStorageSettings.isAdvancedHSearchIndexing() && 064 myFulltextSearchSvc.supportsAllOf(theParams) && 065 theParams.getSummaryMode() == null && 066 theParams.getSearchTotalMode() == null; 067 } 068 069 public ISearchStrategy makeDirectStrategy(String theSearchUUID, String theResourceType, SearchParameterMap theParams, RequestDetails theRequestDetails) { 070 return () -> { 071 if (myFulltextSearchSvc == null) { 072 return new SimpleBundleProvider(Collections.emptyList(), theSearchUUID); 073 } 074 075 List<IBaseResource> resources = myFulltextSearchSvc.searchForResources(theResourceType, theParams); 076 SimpleBundleProvider result = new SimpleBundleProvider(resources, theSearchUUID); 077 result.setSize(resources.size()); 078 return result; 079 }; 080 } 081 082}