Merge pull request #1693 from jamesagnew/im_20200131_remove_circular_dependency_forcedid

Changes to schema to loosen dependencies involving Forced ID table
This commit is contained in:
IanMMarshall 2020-02-18 14:16:09 -05:00 committed by GitHub
commit fba28950ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 96 additions and 37 deletions

View File

@ -0,0 +1,5 @@
---
type: change
issue: 1693
title: Adjusted schema definitions for Resource and Resource History tables to eliminate circular
dependencies with Forced ID table and to improve performance when expunging large numbers of resources.

View File

@ -78,8 +78,6 @@ public class ExpungeEverythingService {
ourLog.info("BEGINNING GLOBAL $expunge");
myTxTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
myTxTemplate.execute(t -> {
counter.addAndGet(doExpungeEverythingQuery("UPDATE " + ResourceHistoryTable.class.getSimpleName() + " d SET d.myForcedId = null"));
counter.addAndGet(doExpungeEverythingQuery("UPDATE " + ResourceTable.class.getSimpleName() + " d SET d.myForcedId = null"));
counter.addAndGet(doExpungeEverythingQuery("UPDATE " + TermCodeSystem.class.getSimpleName() + " d SET d.myCurrentVersion = null"));
return null;
});

View File

@ -30,7 +30,6 @@ import org.junit.Test;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;
import org.springframework.test.context.TestPropertySource;
import java.util.ArrayList;
import java.util.Arrays;
@ -163,7 +162,7 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test {
}
@Test
public void testFetchTotalAccurateForSlowLoading() throws InterruptedException {
public void testFetchTotalAccurateForSlowLoading() {
create200Patients();
mySearchCoordinatorSvcImpl.setLoadingThrottleForUnitTests(25);
@ -836,8 +835,6 @@ public class FhirResourceDaoR4SearchOptimizedTest extends BaseJpaR4Test {
assertEquals(1, myCaptureQueriesListener.countSelectQueriesForCurrentThread());
assertEquals(4, myCaptureQueriesListener.countInsertQueriesForCurrentThread());
assertEquals(0, myCaptureQueriesListener.countDeleteQueriesForCurrentThread());
// Because of the forced ID's bidirectional link HFJ_RESOURCE <-> HFJ_FORCED_ID
assertEquals(1, myCaptureQueriesListener.countUpdateQueriesForCurrentThread());
runInTransaction(() -> {
assertEquals(1, myResourceTableDao.count());
assertEquals(1, myResourceHistoryTableDao.count());

View File

@ -56,10 +56,20 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
init360(); // 20180918 - 20181112
init400(); // 20190401 - 20190814
init410(); // 20190815 - 20191014
init420(); // 20191015 - present
init420(); // 20191015 - 20200217
init430(); // 20200218 - present
}
protected void init420() { // 20191015 - present
protected void init430() { // 20200218 - present
Builder version = forVersion(VersionEnum.V4_3_0);
// Eliminate circular dependency.
version.onTable("HFJ_RESOURCE").dropColumn("20200218.1", "FORCED_ID_PID");
version.onTable("HFJ_RES_VER").dropColumn("20200218.2", "FORCED_ID_PID");
version.onTable("HFJ_RES_VER").addForeignKey("20200218.3", "FK_RESOURCE_HISTORY_RESOURCE").toColumn("RES_ID").references("HFJ_RESOURCE", "RES_ID");
}
protected void init420() { // 20191015 - 20200217
Builder version = forVersion(VersionEnum.V4_2_0);
// TermValueSetConceptDesignation
@ -331,13 +341,10 @@ public class HapiFhirJpaMigrationTasks extends BaseMigrationTasks<VersionEnum> {
termValueSetConceptDesignationTable.addColumn("USE_CODE").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 500);
termValueSetConceptDesignationTable.addColumn("USE_DISPLAY").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 500);
termValueSetConceptDesignationTable.addColumn("VAL").nonNullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 500);
// This index turned out not to be needed so it is disabled
termValueSetConceptDesignationTable
.addIndex("20190801.6", "IDX_VALUESET_C_DSGNTN_VAL")
.unique(false)
.withColumns("VAL")
.doNothing();
.withColumns("VAL");
// TermCodeSystemVersion
version.startSectionWithMessage("Processing table: TRM_CODESYSTEM_VER");

View File

@ -45,11 +45,6 @@ public abstract class BaseHasResource implements IBaseResourceEntity, IBasePersi
@OptimisticLock(excluded = true)
private FhirVersionEnum myFhirVersion;
@OneToOne(optional = true, fetch = FetchType.LAZY, cascade = {}, orphanRemoval = false)
@JoinColumn(name = "FORCED_ID_PID")
@OptimisticLock(excluded = true)
private ForcedId myForcedId;
@Column(name = "HAS_TAGS", nullable = false)
@OptimisticLock(excluded = true)
private boolean myHasTags;
@ -96,29 +91,13 @@ public abstract class BaseHasResource implements IBaseResourceEntity, IBasePersi
myFhirVersion = theFhirVersion;
}
public ForcedId getForcedId() {
return myForcedId;
}
abstract public ForcedId getForcedId();
public void setForcedId(ForcedId theForcedId) {
myForcedId = theForcedId;
}
abstract public void setForcedId(ForcedId theForcedId);
@Override
public abstract Long getId();
@Override
public IdDt getIdDt() {
if (getForcedId() == null) {
Long id = getResourceId();
return new IdDt(getResourceType() + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
} else {
// Avoid a join query if possible
String forcedId = getTransientForcedId() != null ? getTransientForcedId() : getForcedId().getForcedId();
return new IdDt(getResourceType() + '/' + forcedId + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
}
}
@Override
public boolean isDeleted() {
return myDeleted != null;

View File

@ -21,6 +21,8 @@ package ca.uhn.fhir.jpa.model.entity;
*/
import ca.uhn.fhir.jpa.model.cross.ResourcePersistentId;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.Constants;
import org.hibernate.annotations.OptimisticLock;
import javax.persistence.*;
@ -54,7 +56,11 @@ public class ResourceHistoryTable extends BaseHasResource implements Serializabl
@Column(name = "PID")
private Long myId;
@Column(name = "RES_ID")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RES_ID", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "FK_RESOURCE_HISTORY_RESOURCE"))
private ResourceTable myResourceTable;
@Column(name = "RES_ID", nullable = false, updatable = false, insertable = false)
private Long myResourceId;
@Column(name = "RES_TYPE", length = ResourceTable.RESTYPE_LEN, nullable = false)
@ -165,4 +171,35 @@ public class ResourceHistoryTable extends BaseHasResource implements Serializabl
public ResourcePersistentId getPersistentId() {
return new ResourcePersistentId(myResourceId);
}
public ResourceTable getResourceTable() {
return myResourceTable;
}
public void setResourceTable(ResourceTable theResourceTable) {
myResourceTable = theResourceTable;
}
@Override
public IdDt getIdDt() {
if (getResourceTable().getForcedId() == null) {
Long id = getResourceId();
return new IdDt(getResourceType() + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
} else {
// Avoid a join query if possible
String forcedId = getTransientForcedId() != null ? getTransientForcedId() : getResourceTable().getForcedId().getForcedId();
return new IdDt(getResourceType() + '/' + forcedId + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
}
}
@Override
public ForcedId getForcedId() {
return getResourceTable().getForcedId();
}
@Override
public void setForcedId(ForcedId theForcedId) {
getResourceTable().setForcedId(theForcedId);
}
}

View File

@ -23,6 +23,8 @@ package ca.uhn.fhir.jpa.model.entity;
import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
import ca.uhn.fhir.jpa.model.cross.ResourcePersistentId;
import ca.uhn.fhir.jpa.model.search.IndexNonDeletedInterceptor;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
@ -193,26 +195,36 @@ public class ResourceTable extends BaseHasResource implements Serializable, IBas
@OneToMany(mappedBy = "myTargetResource", cascade = {}, fetch = FetchType.LAZY, orphanRemoval = false)
@OptimisticLock(excluded = true)
private Collection<ResourceLink> myResourceLinksAsTarget;
@Column(name = "RES_TYPE", length = RESTYPE_LEN, nullable = false)
@Field
@OptimisticLock(excluded = true)
private String myResourceType;
@OneToMany(mappedBy = "myResource", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@OptimisticLock(excluded = true)
private Collection<SearchParamPresent> mySearchParamPresents;
@OneToMany(mappedBy = "myResource", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@OptimisticLock(excluded = true)
private Set<ResourceTag> myTags;
@Transient
private transient boolean myUnchangedInCurrentOperation;
@Version
@Column(name = "RES_VER")
private long myVersion;
@OneToMany(mappedBy = "myResourceTable", fetch = FetchType.LAZY)
private Collection<ResourceHistoryProvenanceEntity> myProvenance;
@Transient
private transient ResourceHistoryTable myCurrentVersionEntity;
@OneToOne(optional = true, fetch = FetchType.EAGER, cascade = {}, orphanRemoval = false, mappedBy = "myResource")
private ForcedId myForcedId;
@Override
public ResourceTag addTag(TagDefinition theTag) {
for (ResourceTag next : getTags()) {
@ -543,7 +555,7 @@ public class ResourceTable extends BaseHasResource implements Serializable, IBas
retVal.setUpdated(getUpdated());
retVal.setFhirVersion(getFhirVersion());
retVal.setDeleted(getDeleted());
retVal.setForcedId(getForcedId());
retVal.setResourceTable(this);
retVal.getTags().clear();
@ -600,4 +612,28 @@ public class ResourceTable extends BaseHasResource implements Serializable, IBas
public ResourcePersistentId getPersistentId() {
return new ResourcePersistentId(getId());
}
@Override
public ForcedId getForcedId() {
return myForcedId;
}
@Override
public void setForcedId(ForcedId theForcedId) {
myForcedId = theForcedId;
}
@Override
public IdDt getIdDt() {
if (getForcedId() == null) {
Long id = getResourceId();
return new IdDt(getResourceType() + '/' + id + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
} else {
// Avoid a join query if possible
String forcedId = getTransientForcedId() != null ? getTransientForcedId() : getForcedId().getForcedId();
return new IdDt(getResourceType() + '/' + forcedId + '/' + Constants.PARAM_HISTORY + '/' + getVersion());
}
}
}