Class PhysicalDataModel
- java.lang.Object
-
- org.linuxforhealth.fhir.database.utils.model.PhysicalDataModel
-
- All Implemented Interfaces:
IDataModel
public class PhysicalDataModel extends java.lang.Object implements IDataModel
Represents the set of tables and other schema objects that make up the schema we want to build and manage. This is created as code and not in a DDL text file on purpose. Doing so makes it much easier to test.
-
-
Constructor Summary
Constructors Constructor Description PhysicalDataModel(boolean distributed)Default constructor.PhysicalDataModel(PhysicalDataModel... federatedModels)Constructor supporting federated data models
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FunctionDefaddFunction(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges)adds the function to the model.FunctionDefaddFunction(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges, int distributeByParamNum)adds the function to the model.voidaddObject(IDatabaseObject obj)Just a general object we don't need to know the details ofProcedureDefaddProcedure(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges)Add a stored procedure definition.voidaddTable(Table t)Add the table to the list of objects in this modelvoidapply(ISchemaAdapter target, SchemaApplyContext context)Apply the entire model to the target in ordervoidapplyDistributionRules(ISchemaAdapter target, java.util.function.Supplier<ITransaction> transactionSupplier)Make a pass over all the objects and apply any distribution rules they may have (e.g.voidapplyFunctions(ISchemaAdapter adapter, SchemaApplyContext context)Apply all the functions in the order in which they were added to the modelvoidapplyGrants(ISchemaAdapter target, java.lang.String groupName, java.lang.String username)Apply the grants for the given group to the uservoidapplyProcedureAndFunctionGrants(ISchemaAdapter target, java.lang.String groupName, java.lang.String username)Grant the privileges for any registered procedures and functions to the given usernamevoidapplyProcedures(ISchemaAdapter adapter, SchemaApplyContext context)Apply all the procedures in the order in which they were added to the modelvoidapplyWithHistory(ISchemaAdapter target, SchemaApplyContext context, IVersionHistoryService vhs)Apply all the objects linearly, but using the version history service to determine what's new and what already existsvoidcollect(ITaskCollector tc, ISchemaAdapter target, SchemaApplyContext context, ITransactionProvider tp, IVersionHistoryService vhs)Collect all the database objects we know of, describing their interdependencies so that the task collector implementation can execute them in parallel.voiddrop(ISchemaAdapter target)Drop the lotvoiddrop(ISchemaAdapter target, java.lang.String tagGroup, java.lang.String tag)Drop the model from the target database.voiddropForeignKeyConstraints(ISchemaAdapter target, java.lang.String tagGroup, java.lang.String tag)Drop all foreign key constraints on tables in this model.voiddropSplitTransaction(ISchemaAdapter target, ITransactionProvider transactionProvider, java.lang.String tagGroup, java.lang.String tag)Split the drop in multiple (smaller) transactions, which can be helpful to reduce memory utilization in some scenariosTablefindTable(java.lang.String schemaName, java.lang.String tableName)Look up the table definitionbooleanisDistributed()Is the target database distributed (e.g.voidprocessObjectsWithTag(java.lang.String tagName, java.lang.String tagValue, java.util.function.Consumer<IDatabaseObject> c)Call the consumer for each object matching the given tag name/value tuplevoidprocessTablesInSchema(java.lang.String schemaName, java.util.function.Consumer<Table> c)Call the givenConsumerfor each of the tables found in the schema identified by schemaNamejava.util.Collection<IDatabaseObject>searchByTag(java.lang.String tagName, java.lang.String tagValue)Find all the objects matching the given tag name and value.voidvisit(DataModelVisitor v)Visits all objects in the data modelvoidvisit(DataModelVisitor v, java.lang.String tagGroup, java.lang.String tag, java.util.function.Supplier<ITransaction> transactionSupplier)Visit all objects which have the given tagGroup and tagvoidvisitReverse(DataModelVisitor v)Visit all objects in reverse order
-
-
-
Constructor Detail
-
PhysicalDataModel
public PhysicalDataModel(boolean distributed)
Default constructor. No federated models
-
PhysicalDataModel
public PhysicalDataModel(PhysicalDataModel... federatedModels)
Constructor supporting federated data models- Parameters:
federatedModels-
-
-
Method Detail
-
addTable
public void addTable(Table t)
Add the table to the list of objects in this model- Parameters:
t-
-
addObject
public void addObject(IDatabaseObject obj)
Just a general object we don't need to know the details of- Parameters:
obj-
-
collect
public void collect(ITaskCollector tc, ISchemaAdapter target, SchemaApplyContext context, ITransactionProvider tp, IVersionHistoryService vhs)
Collect all the database objects we know of, describing their interdependencies so that the task collector implementation can execute them in parallel. This greatly reduces the amount of time it takes to provision a schema.- Parameters:
tc- collects and manages the object creation tasks and their dependenciestarget- the target database adaptercontext- to control how the schema is builttp-vhs-
-
apply
public void apply(ISchemaAdapter target, SchemaApplyContext context)
Apply the entire model to the target in order- Parameters:
target-context-
-
applyDistributionRules
public void applyDistributionRules(ISchemaAdapter target, java.util.function.Supplier<ITransaction> transactionSupplier)
Make a pass over all the objects and apply any distribution rules they may have (e.g. for Citus). We have to process a large number of tables, which can cause shared memory issues for Citus if we try and do this in a single transaction, hence the need for a transactionSupplier- Parameters:
target-
-
applyWithHistory
public void applyWithHistory(ISchemaAdapter target, SchemaApplyContext context, IVersionHistoryService vhs)
Apply all the objects linearly, but using the version history service to determine what's new and what already exists- Parameters:
target-vhs-
-
applyProcedures
public void applyProcedures(ISchemaAdapter adapter, SchemaApplyContext context)
Apply all the procedures in the order in which they were added to the model- Parameters:
adapter-
-
applyFunctions
public void applyFunctions(ISchemaAdapter adapter, SchemaApplyContext context)
Apply all the functions in the order in which they were added to the model- Parameters:
adapter-
-
drop
public void drop(ISchemaAdapter target, java.lang.String tagGroup, java.lang.String tag)
Drop the model from the target database. This is done in reverse order- Parameters:
target-tagGroup-tag-
-
dropSplitTransaction
public void dropSplitTransaction(ISchemaAdapter target, ITransactionProvider transactionProvider, java.lang.String tagGroup, java.lang.String tag)
Split the drop in multiple (smaller) transactions, which can be helpful to reduce memory utilization in some scenarios- Parameters:
target-transactionProvider-tagGroup-tag-
-
dropForeignKeyConstraints
public void dropForeignKeyConstraints(ISchemaAdapter target, java.lang.String tagGroup, java.lang.String tag)
Drop all foreign key constraints on tables in this model. Typically done prior to dropping the actual tables when removing a schema- Parameters:
target-tagGroup-tag-
-
visit
public void visit(DataModelVisitor v, java.lang.String tagGroup, java.lang.String tag, java.util.function.Supplier<ITransaction> transactionSupplier)
Visit all objects which have the given tagGroup and tag- Parameters:
v-tagGroup-tag-transactionSupplier-
-
visit
public void visit(DataModelVisitor v)
Visits all objects in the data model- Parameters:
v-
-
visitReverse
public void visitReverse(DataModelVisitor v)
Visit all objects in reverse order- Parameters:
v-
-
drop
public void drop(ISchemaAdapter target)
Drop the lot- Parameters:
target-
-
addProcedure
public ProcedureDef addProcedure(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges)
Add a stored procedure definition. The givenSupplierwill be called upon to provide the DDL body for the procedure at the point in time it is being applied to the database, not when constructing the model.- Parameters:
schemaName-objectName- the name of the procedure objectversion-templateProvider- supplier of the procedure textdependencies-privileges-- Returns:
-
addFunction
public FunctionDef addFunction(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges)
adds the function to the model.- Parameters:
schemaName-objectName-version-templateProvider-dependencies-privileges-- Returns:
-
addFunction
public FunctionDef addFunction(java.lang.String schemaName, java.lang.String objectName, int version, java.util.function.Supplier<java.lang.String> templateProvider, java.util.Collection<IDatabaseObject> dependencies, java.util.Collection<GroupPrivilege> privileges, int distributeByParamNum)
adds the function to the model.- Parameters:
schemaName-objectName-version-templateProvider-dependencies-privileges-distributeByParamNum-- Returns:
-
findTable
public Table findTable(java.lang.String schemaName, java.lang.String tableName)
Description copied from interface:IDataModelLook up the table definition- Specified by:
findTablein interfaceIDataModel- Returns:
-
searchByTag
public java.util.Collection<IDatabaseObject> searchByTag(java.lang.String tagName, java.lang.String tagValue)
Find all the objects matching the given tag name and value. Think of the tagName as the indexed field, and the tagValue as the matching rows (which are the objects we're trying to locate).- Parameters:
tagName-tagValue-- Returns:
-
processTablesInSchema
public void processTablesInSchema(java.lang.String schemaName, java.util.function.Consumer<Table> c)Call the givenConsumerfor each of the tables found in the schema identified by schemaName- Parameters:
schemaName-c-
-
processObjectsWithTag
public void processObjectsWithTag(java.lang.String tagName, java.lang.String tagValue, java.util.function.Consumer<IDatabaseObject> c)Call the consumer for each object matching the given tag name/value tuple- Parameters:
tagName-tagValue-c-
-
applyGrants
public void applyGrants(ISchemaAdapter target, java.lang.String groupName, java.lang.String username)
Apply the grants for the given group to the user- Parameters:
target-groupName-username-
-
applyProcedureAndFunctionGrants
public void applyProcedureAndFunctionGrants(ISchemaAdapter target, java.lang.String groupName, java.lang.String username)
Grant the privileges for any registered procedures and functions to the given username- Parameters:
target-groupName-username-
-
isDistributed
public boolean isDistributed()
Description copied from interface:IDataModelIs the target database distributed (e.g. with sharding)?- Specified by:
isDistributedin interfaceIDataModel- Returns:
-
-