00001 #include "CondCore/ORA/interface/Configuration.h"
00002 #include "CondCore/ORA/interface/Exception.h"
00003 #include "ContainerSchema.h"
00004 #include "DatabaseSession.h"
00005 #include "IDatabaseSchema.h"
00006 #include "MappingToSchema.h"
00007 #include "MappingDatabase.h"
00008 #include "MappingGenerator.h"
00009 #include "MappingRules.h"
00010 #include "ClassUtils.h"
00011
00012 #include "RelationalAccess/ISchema.h"
00013
00014 namespace ora {
00015
00016 void getTableHierarchyFromMappingElement( const MappingElement& source,
00017 std::map<std::string, std::set<std::string> >& tableList ){
00018 const std::string& tableName = source.tableName();
00019 std::map<std::string, std::set<std::string> >::iterator iTab = tableList.find( tableName );
00020 if( iTab ==tableList.end() ){
00021 std::set<std::string> dependencies;
00022 tableList.insert(std::make_pair( tableName, dependencies ) );
00023 }
00024 for( MappingElement::const_iterator iElem = source.begin();
00025 iElem != source.end(); iElem++ ){
00026 std::map<std::string, std::set<std::string> >::iterator iT = tableList.find( tableName );
00027 const std::string& innerTable = iElem->second.tableName();
00028 if( innerTable != tableName ){
00029 iT->second.insert( innerTable );
00030 }
00031 getTableHierarchyFromMappingElement( iElem->second, tableList );
00032 }
00033 }
00034
00035 void addFromTableHierarchy( const std::string& tableName,
00036 std::map<std::string, std::set<std::string> >& tableList,
00037 std::vector<std::string>& orderedList ){
00038 orderedList.push_back( tableName );
00039 std::map<std::string, std::set<std::string> >::const_iterator iDeps = tableList.find( tableName );
00040 if(iDeps != tableList.end() ){
00041 for( std::set<std::string>::const_iterator iDt = iDeps->second.begin();
00042 iDt != iDeps->second.end(); iDt++ ){
00043 addFromTableHierarchy( *iDt, tableList, orderedList );
00044 }
00045 }
00046 }
00047
00048 }
00049
00050 ora::ContainerSchema::ContainerSchema( int containerId,
00051 const std::string& containerName,
00052 const Reflex::Type& containerType,
00053 DatabaseSession& session ):
00054 m_containerId( containerId ),
00055 m_containerName( containerName ),
00056 m_className( containerType.Name( Reflex::SCOPED ) ),
00057 m_classDict( containerType ),
00058 m_session( session ),
00059 m_loaded( false ),
00060 m_containerSchemaSequences( session.schema() ),
00061 m_mapping(),
00062 m_dependentMappings(){
00063 }
00064
00065 ora::ContainerSchema::ContainerSchema( int containerId,
00066 const std::string& containerName,
00067 const std::string& className,
00068 DatabaseSession& session ):
00069 m_containerId( containerId ),
00070 m_containerName( containerName ),
00071 m_className( className ),
00072 m_classDict(),
00073 m_session( session ),
00074 m_loaded( false ),
00075 m_containerSchemaSequences( session.schema() ),
00076 m_mapping(),
00077 m_dependentMappings(){
00078 }
00079
00080 ora::ContainerSchema::~ContainerSchema(){
00081 for( std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.begin();
00082 iDep != m_dependentMappings.end(); ++iDep ){
00083 delete iDep->second;
00084 }
00085 }
00086
00087 void ora::ContainerSchema::initClassDict(){
00088 if( !m_classDict ) m_classDict = ClassUtils::lookupDictionary( m_className, false );
00089 if( !m_classDict ) throwException("Container class \""+m_className+"\" has not been found in the dictionary.",
00090 "ContainerSchema::initClassDict");
00091 }
00092
00093 void ora::ContainerSchema::create(){
00094
00095 initClassDict();
00096
00097 m_session.schema().containerHeaderTable().addContainer( m_containerId, m_containerName, m_className );
00098
00099
00100 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
00101 MappingGenerator mapGen( m_session.schema().storageSchema() );
00102 mapGen.createNewMapping( m_containerName, m_classDict, m_mapping );
00103 m_mapping.setVersion( newMappingVersion );
00104 m_session.mappingDatabase().storeMapping( m_mapping );
00105 m_session.mappingDatabase().insertClassVersion( m_classDict, 0, m_containerId, newMappingVersion, true );
00106
00107
00108 m_containerSchemaSequences.create( MappingRules::sequenceNameForContainer( m_containerName ) );
00109 for( std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.begin();
00110 iDep != m_dependentMappings.end(); ++iDep ){
00111 m_containerSchemaSequences.create( MappingRules::sequenceNameForDependentClass( m_containerName, iDep->first ));
00112 }
00113
00114 MappingToSchema mapping2Schema( m_session.schema().storageSchema() );
00115 mapping2Schema.create( m_mapping );
00116 m_loaded = true;
00117 }
00118
00119 void ora::ContainerSchema::getTableHierarchy( const std::set<std::string>& containerMappingVersions, std::vector<std::string>& destination ){
00120
00121 std::map< std::string, std::set<std::string> > tableHierarchy;
00122 std::set<std::string> topLevelTables;
00123 for( std::set<std::string>::const_iterator iV = containerMappingVersions.begin();
00124 iV!= containerMappingVersions.end(); ++iV ){
00125 MappingTree mapping;
00126 if( m_session.mappingDatabase().getMappingByVersion( *iV, mapping ) ){
00127 topLevelTables.insert( mapping.topElement().tableName() );
00128 getTableHierarchyFromMappingElement( mapping.topElement(), tableHierarchy );
00129 }
00130 }
00131 for(std::set<std::string>::const_iterator iMainT = topLevelTables.begin();
00132 iMainT != topLevelTables.end(); ++iMainT ){
00133 addFromTableHierarchy( *iMainT, tableHierarchy, destination );
00134 }
00135 }
00136
00137 void ora::ContainerSchema::drop(){
00138
00139 std::set<std::string> containerMappingVersions;
00140 m_session.mappingDatabase().getMappingVersionsForContainer( m_containerId, containerMappingVersions );
00141 std::vector<std::string> orderedTableList;
00142 getTableHierarchy( containerMappingVersions, orderedTableList );
00143
00144
00145 std::set<std::string> depClasses;
00146 m_session.mappingDatabase().getDependentClassesForContainer( m_containerId, depClasses );
00147
00148
00149 for( std::set<std::string>::const_iterator iM = containerMappingVersions.begin();
00150 iM != containerMappingVersions.end(); ++iM ){
00151 m_session.mappingDatabase().removeMapping( *iM );
00152 }
00153
00154 m_containerSchemaSequences.erase( MappingRules::sequenceNameForContainer( m_containerName ));
00155 for(std::set<std::string>::const_iterator iDepCl = depClasses.begin();
00156 iDepCl != depClasses.end(); iDepCl++){
00157 m_containerSchemaSequences.erase( MappingRules::sequenceNameForDependentClass( m_containerName, *iDepCl ) );
00158 }
00159
00160
00161 m_session.schema().containerHeaderTable().removeContainer( m_containerId );
00162
00163
00164 for(std::vector<std::string>::reverse_iterator iTable = orderedTableList.rbegin();
00165 iTable != orderedTableList.rend(); iTable++ ){
00166 m_session.schema().storageSchema().dropIfExistsTable( *iTable );
00167 }
00168
00169 }
00170
00171 void ora::ContainerSchema::evolve(){
00172 MappingGenerator mapGen( m_session.schema().storageSchema() );
00173
00174 MappingTree baseMapping;
00175 if( !m_session.mappingDatabase().getBaseMappingForContainer( m_classDict.Name(Reflex::SCOPED), m_containerId, baseMapping )){
00176 throwException("Base mapping has not been found in the database.",
00177 "ContainerSchema::evolve");
00178 }
00179 mapGen.createNewMapping( m_containerName, m_classDict, baseMapping, m_mapping );
00180 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
00181 m_mapping.setVersion( newMappingVersion );
00182 m_session.mappingDatabase().storeMapping( m_mapping );
00183 m_session.mappingDatabase().insertClassVersion( m_classDict, 0, m_containerId, newMappingVersion );
00184 MappingToSchema mapping2Schema( m_session.schema().storageSchema() );
00185 mapping2Schema.alter( m_mapping );
00186 m_loaded = true;
00187 }
00188
00189 void ora::ContainerSchema::evolve( const Reflex::Type& dependentClass, MappingTree& baseMapping ){
00190 std::string className = dependentClass.Name(Reflex::SCOPED);
00191 MappingGenerator mapGen( m_session.schema().storageSchema() );
00192 std::map<std::string,MappingTree*>::iterator iDep =
00193 m_dependentMappings.insert( std::make_pair( className, new MappingTree ) ).first;
00194 if( baseMapping.className() != dependentClass.Name(Reflex::SCOPED) ){
00195 throwException("Provided base mapping does not map class \""+dependentClass.Name(Reflex::SCOPED)+"\".",
00196 "ContainerSchema::evolve");
00197 }
00198 mapGen.createNewDependentMapping( dependentClass, m_mapping, baseMapping, *iDep->second );
00199 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
00200 iDep->second->setVersion( newMappingVersion );
00201 m_session.mappingDatabase().storeMapping( *iDep->second );
00202 m_session.mappingDatabase().insertClassVersion( dependentClass, 1, m_containerId, newMappingVersion, false );
00203 }
00204
00205 void ora::ContainerSchema::setAccessPermission( const std::string& principal,
00206 bool forWrite ){
00207 std::set<std::string> containerMappingVersions;
00208 m_session.mappingDatabase().getMappingVersionsForContainer( m_containerId, containerMappingVersions );
00209 std::vector<std::string> orderedTableList;
00210 getTableHierarchy( containerMappingVersions, orderedTableList );
00211 for( std::vector<std::string>::const_iterator iT = orderedTableList.begin();
00212 iT != orderedTableList.end(); iT++ ){
00213 setTableAccessPermission( m_session.schema().storageSchema().tableHandle( *iT ), principal, forWrite );
00214 }
00215 }
00216
00217 const Reflex::Type& ora::ContainerSchema::type(){
00218 return m_classDict;
00219 }
00220
00221 ora::MappingTree& ora::ContainerSchema::mapping( bool writeEnabled ){
00222 initClassDict();
00223 if(!m_loaded ){
00224 if( !m_session.mappingDatabase().getMappingForContainer( m_classDict, m_containerId, m_mapping ) ){
00225
00226 if( writeEnabled && m_session.configuration().properties().getFlag( Configuration::automaticSchemaEvolution() )){
00227 evolve();
00228 }
00229 } else {
00230 m_loaded = true;
00231 }
00232
00233 }
00234 if( m_mapping.topElement().find( m_className )==m_mapping.topElement().end() ){
00235 throwException( "Mapping for container class \""+m_className+"\" could not be loaded.",
00236 "ContainerSchema::mapping");
00237 }
00238 return m_mapping;
00239 }
00240
00241 bool ora::ContainerSchema::loadMappingForDependentClass( const Reflex::Type& dependentClassDict ){
00242 if( !dependentClassDict ) throwException("The dependent class has not been found in the dictionary.",
00243 "ContainerSchema::loadMappingForDependentClass");
00244 std::string className = dependentClassDict.Name(Reflex::SCOPED);
00245 std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.find( className );
00246 if( iDep == m_dependentMappings.end() ){
00247
00248 iDep = m_dependentMappings.insert( std::make_pair( className, new MappingTree ) ).first;
00249 if( ! m_session.mappingDatabase().getMappingForContainer( dependentClassDict, m_containerId, *iDep->second ) ){
00250 m_dependentMappings.erase( className );
00251 return false;
00252 }
00253 }
00254 return true;
00255 }
00256
00257 void ora::ContainerSchema::create( const Reflex::Type& dependentClassDict ){
00258 std::string className = dependentClassDict.Name(Reflex::SCOPED);
00259 std::map<std::string,MappingTree*>::iterator iDep =
00260 m_dependentMappings.insert( std::make_pair( className, new MappingTree ) ).first;
00261 MappingGenerator mapGen( m_session.schema().storageSchema() );
00262 MappingToSchema mapping2Schema( m_session.schema().storageSchema() );
00263 mapGen.createNewDependentMapping( dependentClassDict, m_mapping, *iDep->second );
00264 mapping2Schema.create( *iDep->second );
00265 std::string newMappingVersion = m_session.mappingDatabase().newMappingVersionForContainer( m_containerName );
00266 iDep->second->setVersion( newMappingVersion );
00267 m_session.mappingDatabase().storeMapping( *iDep->second );
00268 m_session.mappingDatabase().insertClassVersion( dependentClassDict, 1, m_containerId, newMappingVersion, true );
00269 m_containerSchemaSequences.create( MappingRules::sequenceNameForDependentClass( m_containerName, className ));
00270 }
00271
00272 void ora::ContainerSchema::extend( const Reflex::Type& dependentClassDict ){
00273 std::string className = dependentClassDict.Name(Reflex::SCOPED);
00274 MappingTree baseMapping;
00275 if( !m_session.mappingDatabase().getBaseMappingForContainer( className,
00276 m_containerId, baseMapping ) ){
00277 create( dependentClassDict );
00278 } else {
00279 evolve( dependentClassDict, baseMapping );
00280 }
00281 }
00282
00283 bool ora::ContainerSchema::extendIfRequired( const Reflex::Type& dependentClassDict ){
00284 bool ret = false;
00285 if( ! loadMappingForDependentClass( dependentClassDict ) ){
00286 extend( dependentClassDict );
00287 ret = true;
00288 }
00289 return ret;
00290 }
00291
00292 ora::MappingElement& ora::ContainerSchema::mappingForDependentClass( const Reflex::Type& dependentClassDict,
00293 bool writeEnabled ){
00294 std::string className = dependentClassDict.Name(Reflex::SCOPED);
00295 if( ! loadMappingForDependentClass( dependentClassDict ) ){
00296 if( writeEnabled ){
00297
00298 MappingTree baseMapping;
00299 if( !m_session.mappingDatabase().getBaseMappingForContainer( className,
00300 m_containerId, baseMapping ) ){
00301
00302 if( m_session.configuration().properties().getFlag( Configuration::automaticDatabaseCreation()) ||
00303 m_session.configuration().properties().getFlag( Configuration::automaticContainerCreation() ) ){
00304 create( dependentClassDict );
00305 }
00306 } else {
00307
00308 if( m_session.configuration().properties().getFlag( Configuration::automaticSchemaEvolution() )){
00309 evolve( dependentClassDict, baseMapping );
00310 }
00311 }
00312 }
00313 }
00314 std::map<std::string,MappingTree*>::iterator iDep = m_dependentMappings.find( className );
00315 if( iDep == m_dependentMappings.end() ){
00316 throwException( "Mapping for class \""+ className + "\" is not available in the database.",
00317 "ContainerSchema::mappingForDependentClass");
00318 }
00319 return iDep->second->topElement();
00320 }
00321
00322 bool ora::ContainerSchema::mappingForDependentClasses( std::vector<ora::MappingElement>& destination ){
00323 return m_session.mappingDatabase().getDependentMappingsForContainer( m_containerId, destination );
00324 }
00325
00326 ora::Sequences& ora::ContainerSchema::containerSequences(){
00327 return m_containerSchemaSequences;
00328 }
00329
00330 ora::IBlobStreamingService* ora::ContainerSchema::blobStreamingService(){
00331 return m_session.configuration().blobStreamingService();
00332 }
00333
00334 ora::IReferenceHandler* ora::ContainerSchema::referenceHandler(){
00335 return m_session.configuration().referenceHandler();
00336 }
00337
00338 const std::string& ora::ContainerSchema::mappingVersion(){
00339 return m_mapping.version();
00340 }
00341
00342 int ora::ContainerSchema::containerId(){
00343 return m_containerId;
00344 }
00345
00346 const std::string& ora::ContainerSchema::containerName(){
00347 return m_containerName;
00348 }
00349
00350 const std::string& ora::ContainerSchema::className(){
00351 return m_className;
00352 }
00353
00354 coral::ISchema& ora::ContainerSchema::storageSchema(){
00355 return m_session.schema().storageSchema();
00356 }
00357
00358 ora::DatabaseSession& ora::ContainerSchema::dbSession(){
00359 return m_session;
00360 }
00361
00362
00363