-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Oracle: Ensure migrated database still takes large resource text updates #5629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lukedegruchy
merged 19 commits into
rel_7_0
from
ld-20240124-oracle-hfj-res-ver-clob-migration
Jan 26, 2024
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d27e999
First pass at fix to Oracle HFJ_RES_VER.RES_TEXT_VC migration.
e97bb65
First stab at agreed upon solution.
f336a4f
Fix error with 4001 by removing unnecessary annotation.
37d08b0
Spotless and TODO.
e7cf49e
Remove annotation for good and set length to LONG32.
002867f
Fix copyright year.
e22b611
Finalize changelog.
bafba17
Remove migration changes. Fix unit test.
4e70435
Fix compile error.
b37f194
Log output.
3222dd1
Refactor resource history code into new ResourceHistoryCalculator.
d08b5c0
Spotless.
9beac96
Convert record to POJO.
5b1a478
Restore pre-17 switch statement.
38f4d74
Finalize new resource history calculator code and tests.
6a6b488
Spotless.
b54f300
Remove logging.
014b7ce
Update hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7…
3d14da1
Code review feedback.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...in/resources/ca/uhn/hapi/fhir/changelog/7_0_0/5633-oracle-hfj-res-ver-clob-migration.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| type: fix | ||
| issue: 5633 | ||
| title: "Previously, migrations from 2023-02 or earlier would fail to update HFJ_RES_VER.RES_TEXT_VC to CLOB. | ||
| This has been fixed." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/ResourceHistoryCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| package ca.uhn.fhir.jpa.dao; | ||
|
|
||
| import ca.uhn.fhir.context.FhirContext; | ||
| import ca.uhn.fhir.jpa.model.entity.ResourceEncodingEnum; | ||
| import ca.uhn.fhir.jpa.model.entity.ResourceHistoryTable; | ||
| import ca.uhn.fhir.jpa.model.entity.ResourceTable; | ||
| import ca.uhn.fhir.parser.IParser; | ||
| import com.google.common.hash.HashCode; | ||
| import com.google.common.hash.HashFunction; | ||
| import com.google.common.hash.Hashing; | ||
| import jakarta.annotation.Nonnull; | ||
| import jakarta.annotation.Nullable; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.hl7.fhir.instance.model.api.IBaseResource; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Responsible for various resource history-centric and {@link FhirContext} aware operations called by | ||
| * {@link BaseHapiFhirDao} or {@link BaseHapiFhirResourceDao} that require knowledge of whether an Oracle database is | ||
| * being used. | ||
| */ | ||
| public class ResourceHistoryCalculator { | ||
| private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceHistoryCalculator.class); | ||
| private static final HashFunction SHA_256 = Hashing.sha256(); | ||
|
|
||
| private final FhirContext myFhirContext; | ||
| private final boolean myIsOracleDialect; | ||
|
|
||
| public ResourceHistoryCalculator(FhirContext theFhirContext, boolean theIsOracleDialect) { | ||
| myFhirContext = theFhirContext; | ||
| myIsOracleDialect = theIsOracleDialect; | ||
| } | ||
|
|
||
| ResourceHistoryState calculateResourceHistoryState( | ||
| IBaseResource theResource, ResourceEncodingEnum theEncoding, List<String> theExcludeElements) { | ||
| final String encodedResource = encodeResource(theResource, theEncoding, theExcludeElements); | ||
| final byte[] resourceBinary; | ||
| final String resourceText; | ||
| final ResourceEncodingEnum encoding; | ||
| final HashCode hashCode; | ||
|
|
||
| if (myIsOracleDialect) { | ||
| resourceText = null; | ||
| resourceBinary = getResourceBinary(theEncoding, encodedResource); | ||
| encoding = theEncoding; | ||
| hashCode = SHA_256.hashBytes(resourceBinary); | ||
| } else { | ||
| resourceText = encodedResource; | ||
| resourceBinary = null; | ||
| encoding = ResourceEncodingEnum.JSON; | ||
| hashCode = SHA_256.hashUnencodedChars(encodedResource); | ||
| } | ||
|
|
||
| return new ResourceHistoryState(resourceText, resourceBinary, encoding, hashCode); | ||
| } | ||
|
|
||
| boolean conditionallyAlterHistoryEntity( | ||
| ResourceTable theEntity, ResourceHistoryTable theHistoryEntity, String theResourceText) { | ||
| if (!myIsOracleDialect) { | ||
| ourLog.debug( | ||
| "Storing text of resource {} version {} as inline VARCHAR", | ||
| theEntity.getResourceId(), | ||
| theHistoryEntity.getVersion()); | ||
| theHistoryEntity.setResourceTextVc(theResourceText); | ||
| theHistoryEntity.setResource(null); | ||
| theHistoryEntity.setEncoding(ResourceEncodingEnum.JSON); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| boolean isResourceHistoryChanged( | ||
| ResourceHistoryTable theCurrentHistoryVersion, | ||
| @Nullable byte[] theResourceBinary, | ||
| @Nullable String resourceText) { | ||
| if (myIsOracleDialect) { | ||
| return !Arrays.equals(theCurrentHistoryVersion.getResource(), theResourceBinary); | ||
| } | ||
|
|
||
| return !StringUtils.equals(theCurrentHistoryVersion.getResourceTextVc(), resourceText); | ||
| } | ||
|
|
||
| String encodeResource( | ||
| IBaseResource theResource, ResourceEncodingEnum theEncoding, List<String> theExcludeElements) { | ||
| final IParser parser = theEncoding.newParser(myFhirContext); | ||
| parser.setDontEncodeElements(theExcludeElements); | ||
| return parser.encodeResourceToString(theResource); | ||
| } | ||
|
|
||
| /** | ||
| * helper for returning the encoded byte array of the input resource string based on the theEncoding. | ||
| * | ||
| * @param theEncoding the theEncoding to used | ||
| * @param theEncodedResource the resource to encode | ||
| * @return byte array of the resource | ||
| */ | ||
| @Nonnull | ||
| static byte[] getResourceBinary(ResourceEncodingEnum theEncoding, String theEncodedResource) { | ||
| switch (theEncoding) { | ||
| case JSON: | ||
| return theEncodedResource.getBytes(StandardCharsets.UTF_8); | ||
| case JSONC: | ||
| return GZipUtil.compress(theEncodedResource); | ||
| default: | ||
| return new byte[0]; | ||
| } | ||
| } | ||
|
|
||
| void populateEncodedResource( | ||
| EncodedResource theEncodedResource, | ||
| String theEncodedResourceString, | ||
| @Nullable byte[] theResourceBinary, | ||
| ResourceEncodingEnum theEncoding) { | ||
| if (myIsOracleDialect) { | ||
| populateEncodedResourceInner(theEncodedResource, null, theResourceBinary, theEncoding); | ||
| } else { | ||
| populateEncodedResourceInner(theEncodedResource, theEncodedResourceString, null, ResourceEncodingEnum.JSON); | ||
| } | ||
| } | ||
|
|
||
| private void populateEncodedResourceInner( | ||
| EncodedResource encodedResource, | ||
| String encodedResourceString, | ||
| byte[] theResourceBinary, | ||
| ResourceEncodingEnum theEncoding) { | ||
| encodedResource.setResourceText(encodedResourceString); | ||
| encodedResource.setResourceBinary(theResourceBinary); | ||
| encodedResource.setEncoding(theEncoding); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.