001/*
002 * #%L
003 * HAPI FHIR JPA Server
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.rest.api.server.RequestDetails;
024import jakarta.annotation.Nonnull;
025
026import java.util.function.Supplier;
027
028/**
029 * This service manages a partition-independent cache of resource identifiers.
030 *
031 * @since 8.6.0
032 */
033public interface IResourceIdentifierCacheSvc {
034
035        /**
036         * Retrieves (and creates if necessary) the PID associated with the
037         * given identifier system URL.
038         * <p>
039         * Thread-safety: This method is designed to be thread-safe, including in cases where
040         * multiple threads are attempting to create a new FHIR ID for the same identifier system.
041         * It will internally retry automatically if multiple threads attempt to create a new
042         * identifier system and will not fail in this case.
043         * </p>
044         * <p>
045         * Transactionality: This method will open a new transaction if one is not already open.
046         * </p>
047         *
048         * @param theRequestDetails The request details associated with the current request
049         * @param theRequestPartitionId The partition ID associated with the current request
050         * @param theSystem The <code>Identifier.system</code> value
051         * @return Returns a PID associated with the given identifier system URL.
052         */
053        long getOrCreateResourceIdentifierSystem(
054                        RequestDetails theRequestDetails, RequestPartitionId theRequestPartitionId, String theSystem);
055
056        /**
057         * Retrieves the FHIR ID associated with the given Patient identifier, creating a new
058         * record using the given Supplier if no existing record is found. This method enforces
059         * uniqueness on the identifier using a database constraint and will therefore only allow
060         * one FHIR ID to be associated with one Identifier. No uniqueness is enforced on the FHIR
061         * ID.
062         * <p>
063         * Thread safety: This method will fail with a constraint error if multiple threads
064         * attempt to assign a FHIR ID for the same system+value combination concurrently.
065         * </p>
066         * <p>
067         * Transactionality: This method will open a new transaction if one is not already open.
068         * </p>
069         *
070         * @param theSystem        The <code>Identifier.system</code> value
071         * @param theValue         The <code>Identifier.value</code> value
072         * @param theNewIdSupplier If no existing FHIR ID is found, a new entry will be created using this ID supplier
073         * @return The FHIR ID associated with this identifier
074         */
075        @Nonnull
076        String getFhirIdAssociatedWithUniquePatientIdentifier(
077                        RequestDetails theRequestDetails,
078                        RequestPartitionId theRequestPartitionId,
079                        String theSystem,
080                        String theValue,
081                        Supplier<String> theNewIdSupplier);
082}