Class ResourceReferenceDAO
- java.lang.Object
-
- com.ibm.fhir.persistence.jdbc.dao.impl.ResourceReferenceDAO
-
- All Implemented Interfaces:
IResourceReferenceDAO,java.lang.AutoCloseable
- Direct Known Subclasses:
Db2ResourceReferenceDAO,DerbyResourceReferenceDAO,PostgresResourceReferenceDAO
public abstract class ResourceReferenceDAO extends java.lang.Object implements IResourceReferenceDAO, java.lang.AutoCloseable
DAO to handle maintenance of the local and external reference tables which contain the relationships described by "reference" elements in each resource (e.g. Observation.subject). The DAO uses a cache for looking up the ids for various entities. The DAO can create new entries, but these can only be used locally until the transaction commits, at which point they can be consolidated into the shared cache. This has the benefit that we reduce the number of times we need to lock the global cache, because we only update it once per transaction. For improved performance, we also make use of batch statements which are managed as member variables. This is why it's important to close this DAO before the transaction commits, ensuring that any outstanding DML batched but not yet executed is processed. Calling close does not close the provided Connection. That is up to the caller to manage. Close does close any statements which are opened inside the class.
-
-
Field Summary
Fields Modifier and Type Field Description protected static intBATCH_SIZE
-
Constructor Summary
Constructors Constructor Description ResourceReferenceDAO(IDatabaseTranslator t, java.sql.Connection c, java.lang.String schemaName, ICommonTokenValuesCache cache, INameIdCache<java.lang.Integer> parameterNameCache)Public constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddNormalizedValues(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs, java.util.Collection<ResourceProfileRec> profileRecs, java.util.Collection<ResourceTokenValueRec> tagRecs, java.util.Collection<ResourceTokenValueRec> securityRecs)Add TOKEN_VALUE_MAP records, creating any CODE_SYSTEMS and COMMON_TOKEN_VALUES as necessaryvoidclose()abstract voiddoCanonicalValuesUpsert(java.lang.String paramList, java.util.Collection<java.lang.String> sortedURLS)Insert any missing values into the common_canonical_values tableprotected voiddoCodeSystemsFetch(java.util.Map<java.lang.String,java.lang.Integer> idMap, java.lang.String inList, java.util.List<java.lang.String> sortedSystemNames)Fetch the code_system_id values for each of the code_system_name values in the sortedSystemNames list.abstract voiddoCodeSystemsUpsert(java.lang.String paramList, java.util.Collection<java.lang.String> sortedSystemNames)Insert any missing values into the code_systems tableprotected abstract voiddoCommonTokenValuesUpsert(java.lang.String paramList, java.util.Collection<CommonTokenValue> sortedTokenValues)Execute the insert (upsert) into the common_token_values table for the given collection of values.voidflush()Execute any statements with pending batch entriesprotected ICommonTokenValuesCachegetCache()Getter for theICommonTokenValuesCacheheld by this DAOprotected java.sql.ConnectiongetConnection()Getter for theConnectionheld by this DAOprotected intgetParameterNameId(java.lang.String parameterName)Get the id from the local (tenant-specific) identity cache, or read/create using the database if needed.ICommonTokenValuesCachegetResourceReferenceCache()Get the cache used by the DAOprotected java.lang.StringgetSchemaName()Getter for subclass access to the schemaNameprotected IDatabaseTranslatorgetTranslator()Getter for theIDatabaseTranslatorheld by this DAOprotected voidinsertResourceProfiles(java.lang.String resourceType, java.util.Collection<ResourceProfileRec> profiles)protected voidinsertResourceSecurity(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _security parameters to the resource-specific xx_SECURITY tableprotected voidinsertResourceTags(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert the tags referenced by the given collection of token value recordsprotected voidinsertResourceTokenRefs(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert the values in the resource-type-specific _resource_token_refs table.protected voidinsertSystemResourceProfiles(java.lang.String resourceType, java.util.Collection<ResourceProfileRec> profiles)Insert PROFILE parametersprotected voidinsertSystemResourceSecurity(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _security parametes to the whole-system LOGICAL_REOURCE_SECURITY tableprotected voidinsertSystemResourceTags(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _tag parameters to the whole-system LOGICAL_RESOURCE_TAGS tableprotected voidinsertSystemResourceTokenRefs(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert any whole-system parameters to the token_refs tablevoidpersist(java.util.Collection<ResourceTokenValueRec> records, java.util.Collection<ResourceProfileRec> profileRecs, java.util.Collection<ResourceTokenValueRec> tagRecs, java.util.Collection<ResourceTokenValueRec> securityRecs)Persist the records, which may span multiple resource typesjava.lang.IntegerreadCanonicalId(java.lang.String canonicalValue)Read the database canonical_id for the given valueCommonTokenValueResultreadCommonTokenValueId(java.lang.String codeSystem, java.lang.String tokenValue)Find the database id for the given token value and systemjava.util.List<java.lang.Long>readCommonTokenValueIdList(java.lang.String tokenValue)Fetch the list of matching common_token_value_id records for the given tokenValue.java.util.Set<CommonTokenValueResult>readCommonTokenValueIds(java.util.Collection<CommonTokenValue> tokenValues)Find database ids for a set of common token valuesprotected abstract intreadOrAddParameterNameId(java.lang.String parameterName)Fetch the id for the given parameter name from the database, creating a new entry if required.voidupsertCanonicalValues(java.util.List<ResourceProfileRec> profileValues)Add the missing values to the database (and get ids allocated)voidupsertCodeSystems(java.util.List<ResourceTokenValueRec> systems)Add all the systems we currently don't have in the database.voidupsertCommonTokenValues(java.util.List<ResourceTokenValueRec> values)Add reference value records for each unique reference name in the given list
-
-
-
Field Detail
-
BATCH_SIZE
protected static final int BATCH_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ResourceReferenceDAO
public ResourceReferenceDAO(IDatabaseTranslator t, java.sql.Connection c, java.lang.String schemaName, ICommonTokenValuesCache cache, INameIdCache<java.lang.Integer> parameterNameCache)
Public constructor- Parameters:
c-
-
-
Method Detail
-
getTranslator
protected IDatabaseTranslator getTranslator()
Getter for theIDatabaseTranslatorheld by this DAO- Returns:
-
getCache
protected ICommonTokenValuesCache getCache()
Getter for theICommonTokenValuesCacheheld by this DAO- Returns:
-
getConnection
protected java.sql.Connection getConnection()
Getter for theConnectionheld by this DAO- Returns:
-
getSchemaName
protected java.lang.String getSchemaName()
Getter for subclass access to the schemaName- Returns:
-
flush
public void flush() throws FHIRPersistenceExceptionDescription copied from interface:IResourceReferenceDAOExecute any statements with pending batch entries- Specified by:
flushin interfaceIResourceReferenceDAO- Throws:
FHIRPersistenceException
-
close
public void close() throws FHIRPersistenceException- Specified by:
closein interfacejava.lang.AutoCloseable- Throws:
FHIRPersistenceException
-
getResourceReferenceCache
public ICommonTokenValuesCache getResourceReferenceCache()
Description copied from interface:IResourceReferenceDAOGet the cache used by the DAO- Specified by:
getResourceReferenceCachein interfaceIResourceReferenceDAO- Returns:
-
readCommonTokenValueId
public CommonTokenValueResult readCommonTokenValueId(java.lang.String codeSystem, java.lang.String tokenValue)
Description copied from interface:IResourceReferenceDAOFind the database id for the given token value and system- Specified by:
readCommonTokenValueIdin interfaceIResourceReferenceDAO- Returns:
- the matching id from common_token_values.common_token_value_id or null if not found
-
readCommonTokenValueIds
public java.util.Set<CommonTokenValueResult> readCommonTokenValueIds(java.util.Collection<CommonTokenValue> tokenValues)
Description copied from interface:IResourceReferenceDAOFind database ids for a set of common token values- Specified by:
readCommonTokenValueIdsin interfaceIResourceReferenceDAO- Returns:
- a non-null, possibly-empty set of ids from common_token_values.common_token_value_id; CommonTokenValues with no corresponding record will be omitted from the set
-
readCanonicalId
public java.lang.Integer readCanonicalId(java.lang.String canonicalValue)
Description copied from interface:IResourceReferenceDAORead the database canonical_id for the given value- Specified by:
readCanonicalIdin interfaceIResourceReferenceDAO- Returns:
-
addNormalizedValues
public void addNormalizedValues(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs, java.util.Collection<ResourceProfileRec> profileRecs, java.util.Collection<ResourceTokenValueRec> tagRecs, java.util.Collection<ResourceTokenValueRec> securityRecs) throws FHIRPersistenceExceptionDescription copied from interface:IResourceReferenceDAOAdd TOKEN_VALUE_MAP records, creating any CODE_SYSTEMS and COMMON_TOKEN_VALUES as necessary- Specified by:
addNormalizedValuesin interfaceIResourceReferenceDAO- Throws:
FHIRPersistenceException
-
insertResourceTokenRefs
protected void insertResourceTokenRefs(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert the values in the resource-type-specific _resource_token_refs table. This is a simple batch insert because all the FKs have already been resolved and updated in the ResourceTokenValueRec records- Parameters:
resourceType-xrefs-
-
insertSystemResourceTokenRefs
protected void insertSystemResourceTokenRefs(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert any whole-system parameters to the token_refs table- Parameters:
resourceType-xrefs-
-
upsertCodeSystems
public void upsertCodeSystems(java.util.List<ResourceTokenValueRec> systems)
Add all the systems we currently don't have in the database. If all target databases handled MERGE properly this would be easy, but they don't so we go old-school with a negative outer join instead (which is pretty much what MERGE does behind the scenes anyway).- Parameters:
systems-
-
doCodeSystemsFetch
protected void doCodeSystemsFetch(java.util.Map<java.lang.String,java.lang.Integer> idMap, java.lang.String inList, java.util.List<java.lang.String> sortedSystemNames)Fetch the code_system_id values for each of the code_system_name values in the sortedSystemNames list.- Parameters:
idMap- the code_system_name -> code_system_id map to populateinList- a list of bind markers for the values in the sortedSystemNames listsortedSystemNames- the list of code_system_name values to fetch
-
upsertCanonicalValues
public void upsertCanonicalValues(java.util.List<ResourceProfileRec> profileValues)
Add the missing values to the database (and get ids allocated)- Parameters:
profileValues-
-
insertResourceProfiles
protected void insertResourceProfiles(java.lang.String resourceType, java.util.Collection<ResourceProfileRec> profiles)
-
insertSystemResourceProfiles
protected void insertSystemResourceProfiles(java.lang.String resourceType, java.util.Collection<ResourceProfileRec> profiles)Insert PROFILE parameters- Parameters:
resourceType-profiles-
-
insertResourceTags
protected void insertResourceTags(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert the tags referenced by the given collection of token value records- Parameters:
resourceType-xrefs-
-
insertSystemResourceTags
protected void insertSystemResourceTags(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _tag parameters to the whole-system LOGICAL_RESOURCE_TAGS table- Parameters:
resourceType-xrefs-
-
insertResourceSecurity
protected void insertResourceSecurity(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _security parameters to the resource-specific xx_SECURITY table- Parameters:
resourceType-xrefs-
-
insertSystemResourceSecurity
protected void insertSystemResourceSecurity(java.lang.String resourceType, java.util.Collection<ResourceTokenValueRec> xrefs)Insert _security parametes to the whole-system LOGICAL_REOURCE_SECURITY table- Parameters:
resourceType-xrefs-
-
doCodeSystemsUpsert
public abstract void doCodeSystemsUpsert(java.lang.String paramList, java.util.Collection<java.lang.String> sortedSystemNames)Insert any missing values into the code_systems table- Parameters:
paramList-systemNames- a sorted collection of system names
-
doCanonicalValuesUpsert
public abstract void doCanonicalValuesUpsert(java.lang.String paramList, java.util.Collection<java.lang.String> sortedURLS)Insert any missing values into the common_canonical_values table- Parameters:
paramList-urls-
-
upsertCommonTokenValues
public void upsertCommonTokenValues(java.util.List<ResourceTokenValueRec> values)
Add reference value records for each unique reference name in the given list- Parameters:
values-
-
doCommonTokenValuesUpsert
protected abstract void doCommonTokenValuesUpsert(java.lang.String paramList, java.util.Collection<CommonTokenValue> sortedTokenValues)Execute the insert (upsert) into the common_token_values table for the given collection of values. Note, this insert from negative outer join requires the database concurrency implementation to be correct. This does not work for Postgres, hence Postgres gets its own implementation of this method- Parameters:
paramList-tokenValues-
-
persist
public void persist(java.util.Collection<ResourceTokenValueRec> records, java.util.Collection<ResourceProfileRec> profileRecs, java.util.Collection<ResourceTokenValueRec> tagRecs, java.util.Collection<ResourceTokenValueRec> securityRecs) throws FHIRPersistenceException
Description copied from interface:IResourceReferenceDAOPersist the records, which may span multiple resource types- Specified by:
persistin interfaceIResourceReferenceDAO- Throws:
FHIRPersistenceException
-
readCommonTokenValueIdList
public java.util.List<java.lang.Long> readCommonTokenValueIdList(java.lang.String tokenValue)
Description copied from interface:IResourceReferenceDAOFetch the list of matching common_token_value_id records for the given tokenValue.- Specified by:
readCommonTokenValueIdListin interfaceIResourceReferenceDAO- Returns:
-
getParameterNameId
protected int getParameterNameId(java.lang.String parameterName) throws FHIRPersistenceDBConnectException, FHIRPersistenceDataAccessExceptionGet the id from the local (tenant-specific) identity cache, or read/create using the database if needed.- Parameters:
parameterName-- Returns:
- Throws:
FHIRPersistenceDBConnectExceptionFHIRPersistenceDataAccessException
-
readOrAddParameterNameId
protected abstract int readOrAddParameterNameId(java.lang.String parameterName) throws FHIRPersistenceDBConnectException, FHIRPersistenceDataAccessExceptionFetch the id for the given parameter name from the database, creating a new entry if required.- Parameters:
parameterName-- Returns:
- Throws:
FHIRPersistenceDBConnectExceptionFHIRPersistenceDataAccessException
-
-