From f29abd716733537bfe77de961c46652c78a96aad Mon Sep 17 00:00:00 2001 From: Diederik Muylwyk Date: Fri, 30 Aug 2019 19:31:05 -0400 Subject: [PATCH 1/4] Fixed ResourceTable lookups for pre-expansion and code validation. --- .../main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java | 2 +- .../fhir/jpa/term/BaseHapiTerminologySvcImpl.java | 14 +++++++++----- .../fhir/jpa/term/TerminologySvcImplDstu3Test.java | 5 +++-- .../fhir/jpa/term/TerminologySvcImplR4Test.java | 5 +++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java index 7a7f588a72f..6f3bac4aba6 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java @@ -149,7 +149,7 @@ public class DaoConfig { /** * EXPERIMENTAL - Do not use in production! Do not change default of {@code false}! */ - private boolean myPreExpandValueSetsExperimental = false; + private boolean myPreExpandValueSetsExperimental = true; private boolean myFilterParameterEnabled = false; private StoreMetaSourceInformation myStoreMetaSourceInformation = StoreMetaSourceInformation.SOURCE_URI_AND_REQUEST_ID; /** diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java index ed8c94a000c..8c8843c6ee9 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java @@ -487,7 +487,8 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, Optional optionalTermValueSet; if (theValueSetToExpand.hasId()) { - Long valueSetResourcePid = IDao.RESOURCE_PID.get(theValueSetToExpand); + ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSetToExpand.getIdElement(), null); + Long valueSetResourcePid = resourceTable.getId(); optionalTermValueSet = myValueSetDao.findByResourcePid(valueSetResourcePid); } else if (theValueSetToExpand.hasUrl()) { optionalTermValueSet = myValueSetDao.findByUrl(theValueSetToExpand.getUrl()); @@ -956,7 +957,8 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, @Override public boolean isValueSetPreExpandedForCodeValidation(ValueSet theValueSet) { - Long valueSetResourcePid = IDao.RESOURCE_PID.get(theValueSet); + ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSet.getIdElement(), null); + Long valueSetResourcePid = resourceTable.getId(); Optional optionalTermValueSet = myValueSetDao.findByResourcePid(valueSetResourcePid); if (!optionalTermValueSet.isPresent()) { @@ -980,7 +982,8 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, ValueSet theValueSet, String theSystem, String theCode, String theDisplay, Coding theCoding, CodeableConcept theCodeableConcept) { ValidateUtil.isNotNullOrThrowUnprocessableEntity(theValueSet.hasId(), "ValueSet.id is required"); - Long valueSetResourcePid = IDao.RESOURCE_PID.get(theValueSet); + ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSet.getIdElement(), null); + Long valueSetResourcePid = resourceTable.getId(); List concepts = new ArrayList<>(); if (isNotBlank(theCode)) { @@ -1625,7 +1628,8 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, if (theCodeSystem.getContent() == CodeSystem.CodeSystemContentMode.COMPLETE || theCodeSystem.getContent() == null || theCodeSystem.getContent() == CodeSystem.CodeSystemContentMode.NOTPRESENT) { ourLog.info("CodeSystem {} has a status of {}, going to store concepts in terminology tables", theResourceEntity.getIdDt().getValue(), theCodeSystem.getContentElement().getValueAsString()); - Long codeSystemResourcePid = IDao.RESOURCE_PID.get(theCodeSystem); + ResourceTable resourceTable = (ResourceTable) myCodeSystemResourceDao.readEntity(theCodeSystem.getIdElement(), null); + Long codeSystemResourcePid = resourceTable.getId(); TermCodeSystemVersion persCs = myCodeSystemVersionDao.findCurrentVersionForCodeSystemResourcePid(codeSystemResourcePid); if (persCs != null) { ourLog.info("Code system version already exists in database"); @@ -1822,7 +1826,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, ourLog.info("Done storing TermConceptMap."); } - @Scheduled(fixedDelay = 600000) // 10 minutes. + @Scheduled(fixedDelay = 6000) // 10 minutes. @Override public synchronized void preExpandDeferredValueSetsToTerminologyTables() { if (isNotSafeToPreExpandValueSets()) { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplDstu3Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplDstu3Test.java index 12253ff265e..e097e543aff 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplDstu3Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplDstu3Test.java @@ -1,7 +1,6 @@ package ca.uhn.fhir.jpa.term; import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.dao.IDao; import ca.uhn.fhir.jpa.dao.dstu3.BaseJpaDstu3Test; import ca.uhn.fhir.jpa.entity.TermCodeSystem; import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; @@ -601,7 +600,9 @@ public class TerminologySvcImplDstu3Test extends BaseJpaDstu3Test { new TransactionTemplate(myTxManager).execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus theStatus) { - TermCodeSystem codeSystem = myTermCodeSystemDao.findByResourcePid(IDao.RESOURCE_PID.get(codeSystemResource)); + ResourceTable resourceTable = (ResourceTable) myCodeSystemDao.readEntity(codeSystemResource.getIdElement(), null); + Long codeSystemResourcePid = resourceTable.getId(); + TermCodeSystem codeSystem = myTermCodeSystemDao.findByResourcePid(codeSystemResourcePid); assertEquals(CS_URL, codeSystem.getCodeSystemUri()); assertEquals("SYSTEM NAME", codeSystem.getName()); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplR4Test.java index 261ea694ca5..89263f497b2 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologySvcImplR4Test.java @@ -2,7 +2,6 @@ package ca.uhn.fhir.jpa.term; import ca.uhn.fhir.context.support.IContextValidationSupport; import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.dao.IDao; import ca.uhn.fhir.jpa.dao.IFhirResourceDaoValueSet.ValidateCodeResult; import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; import ca.uhn.fhir.jpa.entity.*; @@ -1591,7 +1590,9 @@ public class TerminologySvcImplR4Test extends BaseJpaR4Test { new TransactionTemplate(myTxManager).execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus theStatus) { - TermCodeSystem codeSystem = myTermCodeSystemDao.findByResourcePid(IDao.RESOURCE_PID.get(codeSystemResource)); + ResourceTable resourceTable = (ResourceTable) myCodeSystemDao.readEntity(codeSystemResource.getIdElement(), null); + Long codeSystemResourcePid = resourceTable.getId(); + TermCodeSystem codeSystem = myTermCodeSystemDao.findByResourcePid(codeSystemResourcePid); assertEquals(CS_URL, codeSystem.getCodeSystemUri()); assertEquals("SYSTEM NAME", codeSystem.getName()); From 548647defeebdefdb5b6f75be38448ec386f9a4a Mon Sep 17 00:00:00 2001 From: Diederik Muylwyk Date: Fri, 30 Aug 2019 19:31:45 -0400 Subject: [PATCH 2/4] Toggle pre-expansion config. --- .../src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java index 6f3bac4aba6..7a7f588a72f 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/DaoConfig.java @@ -149,7 +149,7 @@ public class DaoConfig { /** * EXPERIMENTAL - Do not use in production! Do not change default of {@code false}! */ - private boolean myPreExpandValueSetsExperimental = true; + private boolean myPreExpandValueSetsExperimental = false; private boolean myFilterParameterEnabled = false; private StoreMetaSourceInformation myStoreMetaSourceInformation = StoreMetaSourceInformation.SOURCE_URI_AND_REQUEST_ID; /** From 16963881b0cca028489cd0846fc7b78f35957f1a Mon Sep 17 00:00:00 2001 From: Diederik Muylwyk Date: Fri, 30 Aug 2019 19:36:34 -0400 Subject: [PATCH 3/4] Reset pre-expansion schedule. --- .../java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java index 8c8843c6ee9..8ca7a3bc03b 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java @@ -1826,7 +1826,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, ourLog.info("Done storing TermConceptMap."); } - @Scheduled(fixedDelay = 6000) // 10 minutes. + @Scheduled(fixedDelay = 600000) // 10 minutes. @Override public synchronized void preExpandDeferredValueSetsToTerminologyTables() { if (isNotSafeToPreExpandValueSets()) { From c34d8458ef6c4ee811cdba5a5bf374d312c9f6cf Mon Sep 17 00:00:00 2001 From: Diederik Muylwyk Date: Mon, 2 Sep 2019 18:16:46 -0400 Subject: [PATCH 4/4] Refactor getting resource PIDs with utility methods. --- .../jpa/term/BaseHapiTerminologySvcImpl.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java index 8ca7a3bc03b..23252a7d708 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/BaseHapiTerminologySvcImpl.java @@ -55,6 +55,7 @@ import org.hibernate.search.query.dsl.BooleanJunction; import org.hibernate.search.query.dsl.QueryBuilder; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.instance.model.api.IBaseCoding; +import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.r4.model.*; @@ -487,8 +488,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, Optional optionalTermValueSet; if (theValueSetToExpand.hasId()) { - ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSetToExpand.getIdElement(), null); - Long valueSetResourcePid = resourceTable.getId(); + Long valueSetResourcePid = getValueSetResourcePid(theValueSetToExpand.getIdElement()); optionalTermValueSet = myValueSetDao.findByResourcePid(valueSetResourcePid); } else if (theValueSetToExpand.hasUrl()) { optionalTermValueSet = myValueSetDao.findByUrl(theValueSetToExpand.getUrl()); @@ -957,8 +957,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, @Override public boolean isValueSetPreExpandedForCodeValidation(ValueSet theValueSet) { - ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSet.getIdElement(), null); - Long valueSetResourcePid = resourceTable.getId(); + Long valueSetResourcePid = getValueSetResourcePid(theValueSet.getIdElement()); Optional optionalTermValueSet = myValueSetDao.findByResourcePid(valueSetResourcePid); if (!optionalTermValueSet.isPresent()) { @@ -982,8 +981,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, ValueSet theValueSet, String theSystem, String theCode, String theDisplay, Coding theCoding, CodeableConcept theCodeableConcept) { ValidateUtil.isNotNullOrThrowUnprocessableEntity(theValueSet.hasId(), "ValueSet.id is required"); - ResourceTable resourceTable = (ResourceTable) myValueSetResourceDao.readEntity(theValueSet.getIdElement(), null); - Long valueSetResourcePid = resourceTable.getId(); + Long valueSetResourcePid = getValueSetResourcePid(theValueSet.getIdElement()); List concepts = new ArrayList<>(); if (isNotBlank(theCode)) { @@ -1161,6 +1159,27 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, protected abstract CodeSystem getCodeSystemFromContext(String theSystem); + private Long getCodeSystemResourcePid(IIdType theIdType) { + return getCodeSystemResourcePid(theIdType, null); + } + + private Long getCodeSystemResourcePid(IIdType theIdType, RequestDetails theRequestDetails) { + return getResourcePid(myCodeSystemResourceDao, theIdType, theRequestDetails); + } + + private Long getValueSetResourcePid(IIdType theIdType) { + return getValueSetResourcePid(theIdType, null); + } + + private Long getValueSetResourcePid(IIdType theIdType, RequestDetails theRequestDetails) { + return getResourcePid(myValueSetResourceDao, theIdType, theRequestDetails); + } + + private Long getResourcePid(IFhirResourceDao theResourceDao, IIdType theIdType, RequestDetails theRequestDetails) { + ResourceTable resourceTable = (ResourceTable) theResourceDao.readEntity(theIdType, theRequestDetails); + return resourceTable.getId(); + } + private void persistChildren(TermConcept theConcept, TermCodeSystemVersion theCodeSystem, IdentityHashMap theConceptsStack, int theTotalConcepts) { if (theConceptsStack.put(theConcept, PLACEHOLDER_OBJECT) != null) { return; @@ -1628,8 +1647,7 @@ public abstract class BaseHapiTerminologySvcImpl implements IHapiTerminologySvc, if (theCodeSystem.getContent() == CodeSystem.CodeSystemContentMode.COMPLETE || theCodeSystem.getContent() == null || theCodeSystem.getContent() == CodeSystem.CodeSystemContentMode.NOTPRESENT) { ourLog.info("CodeSystem {} has a status of {}, going to store concepts in terminology tables", theResourceEntity.getIdDt().getValue(), theCodeSystem.getContentElement().getValueAsString()); - ResourceTable resourceTable = (ResourceTable) myCodeSystemResourceDao.readEntity(theCodeSystem.getIdElement(), null); - Long codeSystemResourcePid = resourceTable.getId(); + Long codeSystemResourcePid = getCodeSystemResourcePid(theCodeSystem.getIdElement()); TermCodeSystemVersion persCs = myCodeSystemVersionDao.findCurrentVersionForCodeSystemResourcePid(codeSystemResourcePid); if (persCs != null) { ourLog.info("Code system version already exists in database");