Class ResourceReferenceDAO
- java.lang.Object
-
- com.ibm.fhir.persistence.jdbc.dao.impl.ResourceReferenceDAO
-
- All Implemented Interfaces:
IResourceReferenceDAO,AutoCloseable
- Direct Known Subclasses:
Db2ResourceReferenceDAO,DerbyResourceReferenceDAO,PostgresResourceReferenceDAO
public abstract class ResourceReferenceDAO extends Object implements IResourceReferenceDAO, 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, Connection c, String schemaName, ICommonTokenValuesCache cache)Public constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddCommonTokenValues(String resourceType, Collection<ResourceTokenValueRec> xrefs)Add TOKEN_VALUE_MAP records, creating any CODE_SYSTEMS and COMMON_TOKEN_VALUES as necessaryvoidclose()voiddeleteExternalReferences(int resourceTypeId, String logicalId)Delete current external references for a given resource type and logical id.voiddeleteLocalReferences(long logicalResourceId)Delete current local references for a given resource described by its logical_resource_id.voiddeleteLogicalResourceCompartments(long logicalResourceId)Delete the membership this resource has with other compartmentsabstract voiddoCodeSystemsUpsert(String paramList, Collection<String> systemNames)Insert any missing values into the code_systems tableprotected abstract voiddoCommonTokenValuesUpsert(String paramList, Collection<CommonTokenValue> tokenValues)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 ConnectiongetConnection()Getter for theConnectionheld by this DAOICommonTokenValuesCachegetResourceReferenceCache()Get the cache used by the DAOprotected StringgetSchemaName()Getter for subclass access to the schemaNameprotected IDatabaseTranslatorgetTranslator()Getter for theIDatabaseTranslatorheld by this DAOprotected voidinsertResourceTokenRefs(String resourceType, Collection<ResourceTokenValueRec> xrefs)Insert the values in the resource-type-specific _resource_token_refs table.voidpersist(Collection<ResourceTokenValueRec> records)Persist the records, which may span multiple resource typesIntegerqueryExternalReferenceValueId(String externalReferenceValue)Find the database id for the given externalReferenceValueList<ExternalReferenceValue>queryExternalReferenceValues(String... externalReferenceValues)Get a list of matching records from external_reference_values.IntegerqueryExternalSystemId(String externalSystemName)Look up the database id for the given externalSystemNameList<ExternalSystem>queryExternalSystems(String... externalSystemNames)voidupsertCodeSystems(List<ResourceTokenValueRec> systems)Add all the systems we currently don't have in the database.voidupsertCommonTokenValues(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, Connection c, String schemaName, ICommonTokenValuesCache cache)
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 Connection getConnection()
Getter for theConnectionheld by this DAO- Returns:
-
getSchemaName
protected 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 interfaceAutoCloseable- Throws:
FHIRPersistenceException
-
getResourceReferenceCache
public ICommonTokenValuesCache getResourceReferenceCache()
Description copied from interface:IResourceReferenceDAOGet the cache used by the DAO- Specified by:
getResourceReferenceCachein interfaceIResourceReferenceDAO- Returns:
-
queryExternalSystemId
public Integer queryExternalSystemId(String externalSystemName)
Look up the database id for the given externalSystemName- Parameters:
externalSystemName-- Returns:
- the database id, or null if no record exists
-
queryExternalReferenceValueId
public Integer queryExternalReferenceValueId(String externalReferenceValue)
Find the database id for the given externalReferenceValue- Parameters:
externalReferenceValue-- Returns:
-
queryExternalReferenceValues
public List<ExternalReferenceValue> queryExternalReferenceValues(String... externalReferenceValues)
Get a list of matching records from external_reference_values. Cheaper to do as one query instead of individuals- Parameters:
externalReferenceValue-- Returns:
-
queryExternalSystems
public List<ExternalSystem> queryExternalSystems(String... externalSystemNames)
-
deleteExternalReferences
public void deleteExternalReferences(int resourceTypeId, String logicalId)Description copied from interface:IResourceReferenceDAODelete current external references for a given resource type and logical id. Typically called when creating a new version of a resource or when re-indexing- Specified by:
deleteExternalReferencesin interfaceIResourceReferenceDAO
-
deleteLocalReferences
public void deleteLocalReferences(long logicalResourceId)
Description copied from interface:IResourceReferenceDAODelete current local references for a given resource described by its logical_resource_id. Typically called when creating a new version of a resource or when re-indexing.- Specified by:
deleteLocalReferencesin interfaceIResourceReferenceDAO
-
deleteLogicalResourceCompartments
public void deleteLogicalResourceCompartments(long logicalResourceId)
Description copied from interface:IResourceReferenceDAODelete the membership this resource has with other compartments- Specified by:
deleteLogicalResourceCompartmentsin interfaceIResourceReferenceDAO
-
addCommonTokenValues
public void addCommonTokenValues(String resourceType, Collection<ResourceTokenValueRec> xrefs)
Description copied from interface:IResourceReferenceDAOAdd TOKEN_VALUE_MAP records, creating any CODE_SYSTEMS and COMMON_TOKEN_VALUES as necessary- Specified by:
addCommonTokenValuesin interfaceIResourceReferenceDAO
-
insertResourceTokenRefs
protected void insertResourceTokenRefs(String resourceType, 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-
-
upsertCodeSystems
public void upsertCodeSystems(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-
-
doCodeSystemsUpsert
public abstract void doCodeSystemsUpsert(String paramList, Collection<String> systemNames)
Insert any missing values into the code_systems table- Parameters:
paramList-systems-
-
upsertCommonTokenValues
public void upsertCommonTokenValues(List<ResourceTokenValueRec> values)
Add reference value records for each unique reference name in the given list- Parameters:
values-
-
doCommonTokenValuesUpsert
protected abstract void doCommonTokenValuesUpsert(String paramList, Collection<CommonTokenValue> tokenValues)
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(Collection<ResourceTokenValueRec> records)
Description copied from interface:IResourceReferenceDAOPersist the records, which may span multiple resource types- Specified by:
persistin interfaceIResourceReferenceDAO
-
-