001/*-
002 * #%L
003 * HAPI FHIR JPA Model
004 * %%
005 * Copyright (C) 2014 - 2024 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.config;
021
022/**
023 * @since 5.0.0
024 */
025public class PartitionSettings {
026
027        private boolean myPartitioningEnabled = false;
028        private CrossPartitionReferenceMode myAllowReferencesAcrossPartitions = CrossPartitionReferenceMode.NOT_ALLOWED;
029        private boolean myIncludePartitionInSearchHashes = false;
030        private boolean myUnnamedPartitionMode;
031        private Integer myDefaultPartitionId;
032        private boolean myAlwaysOpenNewTransactionForDifferentPartition;
033
034        /**
035         * Should we always open a new database transaction if the partition context changes
036         *
037         * @since 6.6.0
038         */
039        public boolean isAlwaysOpenNewTransactionForDifferentPartition() {
040                return myAlwaysOpenNewTransactionForDifferentPartition;
041        }
042
043        /**
044         * Should we always open a new database transaction if the partition context changes
045         *
046         * @since 6.6.0
047         */
048        public void setAlwaysOpenNewTransactionForDifferentPartition(
049                        boolean theAlwaysOpenNewTransactionForDifferentPartition) {
050                myAlwaysOpenNewTransactionForDifferentPartition = theAlwaysOpenNewTransactionForDifferentPartition;
051        }
052
053        /**
054         * If set to <code>true</code> (default is <code>false</code>) the <code>PARTITION_ID</code> value will be factored into the
055         * hash values used in the <code>HFJ_SPIDX_xxx</code> tables, removing the need to explicitly add a selector
056         * on this column in queries. If set to <code>false</code>, an additional selector is used instead, which may perform
057         * better when using native database partitioning features.
058         * <p>
059         * This setting has no effect if partitioning is not enabled via {@link #isPartitioningEnabled()}.
060         * </p>
061         */
062        public boolean isIncludePartitionInSearchHashes() {
063                return myIncludePartitionInSearchHashes;
064        }
065
066        /**
067         * If set to <code>true</code> (default is <code>false</code>) the <code>PARTITION_ID</code> value will be factored into the
068         * hash values used in the <code>HFJ_SPIDX_xxx</code> tables, removing the need to explicitly add a selector
069         * on this column in queries. If set to <code>false</code>, an additional selector is used instead, which may perform
070         * better when using native database partitioning features.
071         * <p>
072         * This setting has no effect if partitioning is not enabled via {@link #isPartitioningEnabled()}.
073         * </p>
074         */
075        public PartitionSettings setIncludePartitionInSearchHashes(boolean theIncludePartitionInSearchHashes) {
076                myIncludePartitionInSearchHashes = theIncludePartitionInSearchHashes;
077                return this;
078        }
079
080        /**
081         * If enabled (default is <code>false</code>) the JPA server will support data partitioning
082         *
083         * @since 5.0.0
084         */
085        public boolean isPartitioningEnabled() {
086                return myPartitioningEnabled;
087        }
088
089        /**
090         * If enabled (default is <code>false</code>) the JPA server will support data partitioning
091         *
092         * @since 5.0.0
093         */
094        public void setPartitioningEnabled(boolean theMultiTenancyEnabled) {
095                myPartitioningEnabled = theMultiTenancyEnabled;
096        }
097
098        /**
099         * Should resources references be permitted to cross partition boundaries. Default is {@link CrossPartitionReferenceMode#NOT_ALLOWED}.
100         *
101         * @since 5.0.0
102         */
103        public CrossPartitionReferenceMode getAllowReferencesAcrossPartitions() {
104                return myAllowReferencesAcrossPartitions;
105        }
106
107        /**
108         * Should resources references be permitted to cross partition boundaries. Default is {@link CrossPartitionReferenceMode#NOT_ALLOWED}.
109         *
110         * @since 5.0.0
111         */
112        public void setAllowReferencesAcrossPartitions(CrossPartitionReferenceMode theAllowReferencesAcrossPartitions) {
113                myAllowReferencesAcrossPartitions = theAllowReferencesAcrossPartitions;
114        }
115
116        /**
117         * If set to <code>true</code> (default is <code>false</code>), partitions will be unnamed and all IDs from {@link Integer#MIN_VALUE} through
118         * {@link Integer#MAX_VALUE} will be allowed without needing to be created ahead of time.
119         *
120         * @since 5.5.0
121         */
122        public boolean isUnnamedPartitionMode() {
123                return myUnnamedPartitionMode;
124        }
125
126        /**
127         * If set to <code>true</code> (default is <code>false</code>), partitions will be unnamed and all IDs from {@link Integer#MIN_VALUE} through
128         * {@link Integer#MAX_VALUE} will be allowed without needing to be created ahead of time.
129         *
130         * @since 5.5.0
131         */
132        public void setUnnamedPartitionMode(boolean theUnnamedPartitionMode) {
133                myUnnamedPartitionMode = theUnnamedPartitionMode;
134        }
135
136        /**
137         * If set, the given ID will be used for the default partition. The default is
138         * <code>null</code> which will result in the use of a null value for default
139         * partition items.
140         *
141         * @since 5.5.0
142         */
143        public Integer getDefaultPartitionId() {
144                return myDefaultPartitionId;
145        }
146
147        /**
148         * If set, the given ID will be used for the default partition. The default is
149         * <code>null</code> which will result in the use of a null value for default
150         * partition items.
151         *
152         * @since 5.5.0
153         */
154        public void setDefaultPartitionId(Integer theDefaultPartitionId) {
155                myDefaultPartitionId = theDefaultPartitionId;
156        }
157
158        /**
159         * If enabled the JPA server will allow unqualified cross partition reference
160         */
161        public boolean isAllowUnqualifiedCrossPartitionReference() {
162                return myAllowReferencesAcrossPartitions.equals(
163                                PartitionSettings.CrossPartitionReferenceMode.ALLOWED_UNQUALIFIED);
164        }
165
166        public enum CrossPartitionReferenceMode {
167
168                /**
169                 * References between resources are not allowed to cross partition boundaries
170                 */
171                NOT_ALLOWED,
172
173                /**
174                 * References can cross partition boundaries, with an assumption that boundaries
175                 * will be managed by the database.
176                 */
177                ALLOWED_UNQUALIFIED,
178        }
179
180        public enum BlockPatientCompartmentUpdateMode {
181                /**
182                 * Resource updates which would change resource's patient compartment are blocked.
183                 */
184                ALWAYS,
185
186                /**
187                 * Resource updates which would change resource's patient compartment are blocked
188                 * when Partition Selection Mode is PATIENT_ID
189                 */
190                DEFAULT,
191
192                /**
193                 * Resource updates which would change resource's patient compartment are allowed.
194                 */
195                NEVER,
196        }
197}