001/*-
002 * #%L
003 * HAPI FHIR JPA Model
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.model.dialect;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.jpa.model.entity.StorageSettings;
024import ca.uhn.fhir.jpa.util.ISequenceValueMassager;
025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
026import org.apache.commons.lang3.Validate;
027import org.hibernate.HibernateException;
028import org.hibernate.MappingException;
029import org.hibernate.boot.model.relational.Database;
030import org.hibernate.boot.model.relational.ExportableProducer;
031import org.hibernate.boot.model.relational.SqlStringGenerationContext;
032import org.hibernate.engine.spi.SharedSessionContractImplementor;
033import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
034import org.hibernate.id.IdentifierGenerator;
035import org.hibernate.id.OptimizableGenerator;
036import org.hibernate.id.PersistentIdentifierGenerator;
037import org.hibernate.id.enhanced.Optimizer;
038import org.hibernate.id.enhanced.SequenceStyleGenerator;
039import org.hibernate.id.enhanced.StandardOptimizerDescriptor;
040import org.hibernate.service.ServiceRegistry;
041import org.hibernate.type.Type;
042import org.springframework.beans.factory.annotation.Autowired;
043
044import java.io.Serializable;
045import java.util.Properties;
046
047import static ca.uhn.fhir.jpa.model.util.JpaConstants.NO_MORE_PID;
048
049/**
050 * This is a sequence generator that wraps the Hibernate default sequence generator {@link SequenceStyleGenerator}
051 * and by default will therefore work exactly as the default would, but allows for customization.
052 */
053@SuppressWarnings("unused")
054public class HapiSequenceStyleGenerator
055                implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, ExportableProducer {
056        public static final String ID_MASSAGER_TYPE_KEY = "hapi_fhir.sequence_generator_massager";
057        private final SequenceStyleGenerator myGen = new SequenceStyleGenerator();
058
059        @Autowired
060        private StorageSettings myStorageSettings;
061
062        private ISequenceValueMassager myIdMassager;
063        private boolean myConfigured;
064        private String myGeneratorName;
065
066        @Override
067        public boolean supportsBulkInsertionIdentifierGeneration() {
068                return myGen.supportsBulkInsertionIdentifierGeneration();
069        }
070
071        @Override
072        public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext theContext) {
073                return myGen.determineBulkInsertionIdentifierGenerationSelectFragment(theContext);
074        }
075
076        @Override
077        public Serializable generate(SharedSessionContractImplementor theSession, Object theObject)
078                        throws HibernateException {
079                Long nextVal = doGenerate(theSession, theObject);
080                /*
081                 * This should never happen since the sequence starts at 1, but if someone ever manually messes with sequences
082                 * or the sequence otherwise gets messed up, we don't want to end up with a resource using this PID which has
083                 * a special meaning to HAPI.
084                 */
085                if (NO_MORE_PID.equals(nextVal)) {
086                        // retry once
087                        nextVal = doGenerate(theSession, theObject);
088                }
089
090                if (NO_MORE_PID.equals(nextVal)) {
091                        // fail if we're stuck here.
092                        throw new InternalErrorException(
093                                        Msg.code(2791) + "Resource ID generator provided illegal value: " + nextVal + " / " + nextVal);
094                }
095                return nextVal;
096        }
097
098        private Long doGenerate(SharedSessionContractImplementor theSession, Object theObject) {
099                Long retVal = myIdMassager != null ? myIdMassager.generate(myGeneratorName) : null;
100                if (retVal == null) {
101                        Long next = (Long) myGen.generate(theSession, theObject);
102
103                        retVal = myIdMassager.massage(myGeneratorName, next);
104                }
105                return retVal;
106        }
107
108        @Override
109        public void configure(Type theType, Properties theParams, ServiceRegistry theServiceRegistry)
110                        throws MappingException {
111
112                myIdMassager = theServiceRegistry.getService(ISequenceValueMassager.class);
113                if (myIdMassager == null) {
114                        myIdMassager = new ISequenceValueMassager.NoopSequenceValueMassager();
115                }
116
117                // Create a HAPI FHIR sequence style generator
118                myGeneratorName = theParams.getProperty(IdentifierGenerator.GENERATOR_NAME);
119                Validate.notBlank(myGeneratorName, "No generator name found");
120
121                Properties props = new Properties(theParams);
122
123                // We start the sequence with an initial value larger than the increment value to avoid
124                // a an interaction between the pooled_lo optimizer and out-of-order sequence reads on AWS limitless
125                // that generate negative values.  We can't switch to pooled_hi in a backwards-compatible way.
126                props.put(OptimizableGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
127                props.put(OptimizableGenerator.INITIAL_PARAM, 1000);
128                props.put(OptimizableGenerator.INCREMENT_PARAM, 50);
129                props.put(IdentifierGenerator.GENERATOR_NAME, myGeneratorName);
130
131                myGen.configure(theType, props, theServiceRegistry);
132
133                myConfigured = true;
134        }
135
136        @Override
137        public void registerExportables(Database database) {
138                myGen.registerExportables(database);
139        }
140
141        @Override
142        public void initialize(SqlStringGenerationContext context) {
143                myGen.initialize(context);
144        }
145
146        @Override
147        public boolean supportsJdbcBatchInserts() {
148                return myGen.supportsJdbcBatchInserts();
149        }
150
151        @Override
152        public Optimizer getOptimizer() {
153                return myGen.getOptimizer();
154        }
155}