001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
004 * %%
005 * Copyright (C) 2014 - 2025 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.cache;
021
022import ca.uhn.fhir.interceptor.model.RequestPartitionId;
023import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
024import com.google.common.annotations.VisibleForTesting;
025import org.hl7.fhir.instance.model.api.IBaseResource;
026
027import java.util.Set;
028
029/**
030 * This component holds an in-memory list of all registered {@link IResourceChangeListener} instances along
031 * with their caches and other details needed to maintain those caches.  Register an {@link IResourceChangeListener} instance
032 * with this service to be notified when resources you care about are changed.  This service quickly notifies listeners
033 * of changes that happened on the local process and also eventually notifies listeners of changes that were made by
034 * remote processes.
035 */
036public interface IResourceChangeListenerRegistry {
037
038        /**
039         * Register a listener in order to be notified whenever a resource matching the provided SearchParameterMap
040         * changes in any way.  If the change happened on the same jvm process where this registry resides, then the listener will be called
041         * within {@link ResourceChangeListenerCacheRefresherImpl#LOCAL_REFRESH_INTERVAL_MS} of the change happening.  If the change happened
042         * on a different jvm process, then the listener will be called within the time specified in theRemoteRefreshIntervalMs parameter.
043         * @param theResourceName           the type of the resource the listener should be notified about (e.g. "Subscription" or "SearchParameter")
044         * @param theSearchParameterMap     the listener will only be notified of changes to resources that match this map
045         * @param theResourceChangeListener the listener that will be called whenever resource changes are detected
046         * @param theRemoteRefreshIntervalMs the number of milliseconds between checking the database for changed resources that match the search parameter map
047         * @throws ca.uhn.fhir.parser.DataFormatException      if theResourceName is not a valid resource type in the FhirContext
048         * @throws IllegalArgumentException if theSearchParamMap cannot be evaluated in-memory
049         * @return RegisteredResourceChangeListener a handle to the created cache that can be used to manually refresh the cache if required
050         */
051        IResourceChangeListenerCache registerResourceResourceChangeListener(
052                        String theResourceName,
053                        RequestPartitionId theRequestPartitionId,
054                        SearchParameterMap theSearchParameterMap,
055                        IResourceChangeListener theResourceChangeListener,
056                        long theRemoteRefreshIntervalMs);
057
058        /**
059         * Unregister a listener from this service
060         *
061         * @param theResourceChangeListener
062         */
063        void unregisterResourceResourceChangeListener(IResourceChangeListener theResourceChangeListener);
064
065        /**
066         * Unregister a listener from this service using its cache handle
067         *
068         * @param theResourceChangeListenerCache
069         */
070        void unregisterResourceResourceChangeListener(IResourceChangeListenerCache theResourceChangeListenerCache);
071
072        @VisibleForTesting
073        void clearListenersForUnitTest();
074
075        /**
076         *
077         * @param theCache
078         * @return true if theCache is registered
079         */
080        boolean contains(IResourceChangeListenerCache theCache);
081
082        /**
083         * Called by the {@link ResourceChangeListenerRegistryInterceptor} when a resource is changed to invalidate matching
084         * caches so their listeners are notified the next time the caches are refreshed.
085         * @param theResource the resource that changed that might trigger a refresh
086         */
087        void requestRefreshIfWatching(IBaseResource theResource);
088
089        /**
090         * @return a set of resource names watched by the registered listeners
091         */
092        Set<String> getWatchedResourceNames();
093}