001package ca.uhn.fhir.jpa.search.cache;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId;
024import ca.uhn.fhir.jpa.dao.data.ISearchResultDao;
025import ca.uhn.fhir.jpa.entity.Search;
026import ca.uhn.fhir.jpa.entity.SearchResult;
027import com.google.common.collect.Lists;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030import org.springframework.beans.factory.annotation.Autowired;
031import org.springframework.data.domain.Pageable;
032
033import javax.transaction.Transactional;
034import java.util.Collections;
035import java.util.List;
036
037import static ca.uhn.fhir.jpa.search.SearchCoordinatorSvcImpl.toPage;
038
039public class DatabaseSearchResultCacheSvcImpl implements ISearchResultCacheSvc {
040        private static final Logger ourLog = LoggerFactory.getLogger(DatabaseSearchResultCacheSvcImpl.class);
041
042        @Autowired
043        private ISearchResultDao mySearchResultDao;
044
045        @Override
046        @Transactional(Transactional.TxType.REQUIRED)
047        public List<ResourcePersistentId> fetchResultPids(Search theSearch, int theFrom, int theTo) {
048                final Pageable page = toPage(theFrom, theTo);
049                if (page == null) {
050                        return Collections.emptyList();
051                }
052
053                List<Long> retVal = mySearchResultDao
054                        .findWithSearchPid(theSearch.getId(), page)
055                        .getContent();
056
057                ourLog.debug("fetchResultPids for range {}-{} returned {} pids", theFrom, theTo, retVal.size());
058
059                return ResourcePersistentId.fromLongList(retVal);
060        }
061
062        @Override
063        @Transactional(Transactional.TxType.REQUIRED)
064        public List<ResourcePersistentId> fetchAllResultPids(Search theSearch) {
065                List<Long> retVal = mySearchResultDao.findWithSearchPidOrderIndependent(theSearch.getId());
066                ourLog.trace("fetchAllResultPids returned {} pids", retVal.size());
067                return ResourcePersistentId.fromLongList(retVal);
068        }
069
070        @Override
071        @Transactional(Transactional.TxType.REQUIRED)
072        public void storeResults(Search theSearch, List<ResourcePersistentId> thePreviouslyStoredResourcePids, List<ResourcePersistentId> theNewResourcePids) {
073                List<SearchResult> resultsToSave = Lists.newArrayList();
074
075                ourLog.trace("Storing {} results with {} previous for search", theNewResourcePids.size(), thePreviouslyStoredResourcePids.size());
076
077                int order = thePreviouslyStoredResourcePids.size();
078                for (ResourcePersistentId nextPid : theNewResourcePids) {
079                        SearchResult nextResult = new SearchResult(theSearch);
080                        nextResult.setResourcePid(nextPid.getIdAsLong());
081                        nextResult.setOrder(order);
082                        resultsToSave.add(nextResult);
083                        ourLog.trace("Saving ORDER[{}] Resource {}", order, nextResult.getResourcePid());
084
085                        order++;
086                }
087
088                mySearchResultDao.saveAll(resultsToSave);
089        }
090
091}