// $Id: IDbiConfigStream.cxx,v 1.1 2011/01/18 05:49:19 finch Exp $ #include #include "IDbiConfigStream.hxx" #include "IDbiFieldType.hxx" #include "IDbiTableProxy.hxx" #include "IDbiDatabaseManager.hxx" #include "IDbiWriter.hxx" #include #include #include "IDbiRegistry.hxx" #include "IDbiCfg.hxx" #include "IDbiCascader.hxx" using std::endl; ClassImp(COMET::IDbiConfigStream) // Definition of static data members // ********************************* COMET::IVldContext COMET::IDbiConfigStream::fgDefaultContext(COMET::DbiDetector::kNear, COMET::DbiSimFlag::kData, COMET::IVldTimeStamp() ); // Definition of all member functions (static or otherwise) // ******************************************************* // // - ordered: ctors, dtor, operators then in alphabetical order. //..................................................................... COMET::IDbiConfigStream::IDbiConfigStream() : fCFSet(0) { // // // Purpose: Default constructor // // Arguments: None. // // Return: n/a // // Contact: N. West // // Specification:- // ============= // // o Create dummy ConfigStream. // Program Notes:- // ============= // None. COMETDebug( "Creating COMET::IDbiConfigStream" << " "); } //..................................................................... COMET::IDbiConfigStream::IDbiConfigStream(const string& SoftName, const std::string& ConfigName, COMET::IVldContext vc, IDbi::Task task, const std::string& tableName) : fCFSet(0), fCFSetTable(tableName,vc,task), fConfigName(ConfigName), fSoftwName(SoftName) { // // // Purpose: Standard constructor // // Arguments: // SoftwName in Name of the software system to be configured // ConfigName in Name of the configuration set. // Default: "default" // vc in Context. // Default: IDbi::fgDefaultContext. // task in The task of the configuration. // Default: 0 // tableName in Name of configuration data table. // Default: "SOFTWARE_CONFIGURATION" // // Return: n/a // // Contact: N. West // // Specification:- // ============= // // o Create ConfigStream from specified table. // Program Notes:- // ============= // None. COMETDebug( "Creating COMET::IDbiConfigStream" << " "); // Search for row matching software and configuration names. int rowNum = fCFSetTable.GetNumRows()-1; while ( rowNum >= 0 ) { fCFSet = fCFSetTable.GetRow(rowNum); if ( fCFSet->GetParamValue(0) == fSoftwName && fCFSet->GetParamValue(1) == fConfigName ) { fVRec = *fCFSetTable.GetValidityRec(fCFSet); COMETLog( "COMET::IDbiConfigStream for " << fSoftwName << "," << fConfigName << " has validity rec: " << fVRec << " and aggregate no.: " << fCFSet->GetAggregateNo() << " "); return; } --rowNum; } // Cannot find matching row, leave configuration data as null // and set up a validity rec that can be used if creating a // new row. fCFSet = 0; fVRec.SetDbNo(0); fVRec.SetTableProxy(&COMET::IDbiResultSetHandle::GetTableProxy(tableName)); COMET::IVldTimeStamp start(1970,1,1,0,0,0); COMET::IVldTimeStamp end(2038,1,1,0,0,0); COMET::IVldRange vr(127,127,start,end,"COMET::IDbiConfigStream"); fVRec.SetVldRange(vr); COMETLog( "COMET::IDbiConfigStream for " << fSoftwName << "," << fConfigName << " has no existing entry; creating validity rec: " << fVRec << " "); } //..................................................................... COMET::IDbiConfigStream::~IDbiConfigStream() { // // // Purpose: Destructor // // Arguments: // None. // // Return: n/a // // Contact: N. West // // Specification:- // ============= // // o Destroy ConfigStream. // Program Notes:- // ============= // None. COMETDebug( "Destroying COMET::IDbiConfigStream" << " "); } //..................................................................... std::ostream& operator<<(std::ostream& os, const COMET::IDbiConfigStream& cfStream) { // // // Purpose: Output status of COMET::IDbiConfigStream on ostream. // // Arguments: // os in ostream to be outout // cfStream in COMET::IDbiConfigStream whise status is to be output. // // Return: Updated ostream // // Contact: N. West // // Specification:- // ============= // // o Output status of COMET::IDbiConfigStream on ostream. // Program Notes:- // ============= // None. const COMET::IDbiConfigSet* cfSet = cfStream.GetConfigSet(); if ( cfSet ) os << "COMET::IDbiConfigSet contains: " << *cfSet << endl; else os << "COMET::IDbiConfigSet is empty! " << endl; return os; } //..................................................................... const COMET::IDbiConfigStream& COMET::IDbiConfigStream::operator>>(IDbiRegistry* reg) { // // // Purpose: Stream configuration data into IDbiRegistry object. // // Arguments: // reg in Empty IDbiRegistry object (contents ignored) // out Filled IDbiRegistry object. // // Return: Original COMET::IDbiConfigStream object. // // Contact: N. West // // Specification:- // ============= // // o Erase the contents of reg and refill from the COMET::IDbiConfigSet // owned by this COMET::IDbiConfigStream. // Program Notes:- // ============= // None. if ( ! reg ) return *this; // Record the current state of reg and then clear it. Bool_t keysLocked = reg->KeysLocked(); Bool_t valuesLocked = reg->ValuesLocked(); reg->UnLockKeys(); reg->UnLockValues(); reg->Clear(); // Use the owned COMET::IDbiConfigSet to fill reg. if ( fCFSet ) { UInt_t numParams = fCFSet->GetNumParams(); // Handle configuration tables. if ( numParams == 3 && fCFSet->GetParamName(2) == "CONFIG_DATA" ) { istringstream is(fCFSet->GetParamValue(2)); reg->ReadStream(is); } else { COMETSevere( "Attempting to fill IDbiRegistry from a table with " << numParams << " columns (should be 3) using column named " << fCFSet->GetParamName(2) << " (should be CONFIG_DATA)." << " "); } } if ( keysLocked ) reg->LockKeys(); if ( valuesLocked ) reg->LockValues(); return *this; } //..................................................................... COMET::IDbiConfigStream& COMET::IDbiConfigStream::operator<<(const IDbiRegistry* reg) { // // // Purpose: Stream configuration data from IDbiRegistry object. // // Arguments: // // Contact: N. West // // Specification:- // ============= // // o Refill internal fCFSetModified from reg.. // Program Notes:- // ============= // This does NOT write to the database. To do that first use this // method to refill the configuration and then call the Write method. if ( fSoftwName == "" ) { COMETSevere( "Cannot fill (<<): No software name defined." << " "); return *this; } COMET::IDbiFieldType stringType(IDbi::kString); ostringstream os; reg->PrintStream(os); fCFSetModified.Clear(); fCFSetModified.PushBack("SOFTW_NAME", fSoftwName, stringType); fCFSetModified.PushBack("CONFIG_NAME",fConfigName, stringType); fCFSetModified.PushBack("CONFIG_DATA",os.str(), stringType); if ( fVRec.GetAggregateNo() > 0 ) fCFSetModified.SetAggregateNo( fVRec.GetAggregateNo()); fCFSet = &fCFSetModified; return *this; } //..................................................................... Bool_t COMET::IDbiConfigStream::Write(UInt_t dbNo, const std::string& logComment, Bool_t localTest ) { // // // Purpose: Write configuration data to the database. // // Arguments: // dbNo in Database number in cascade (starting at 0). // Default: 0. // logComment in Reason for update. // Default: "". // localTest in Set true to use local SEQNOs (doesn't require authorising DB). // Default: false. // // Return: True if I/O successful. if ( ! fCFSet ) { COMETSevere( "No configuration data to write out." << " "); return false; } // If no aggregate number has been asigned so far, but fCFSet non-zero, then must // be creating a new software/config combination with the data in fCFSetModified. // Use a global seqno number (or local if localTest) to define a unique aggregate number. int requireGlobal = localTest ? -1 : 1; if ( fVRec.GetAggregateNo() < 0 ) { COMET::IDbiCascader& cas = COMET::IDbiDatabaseManager::Instance().GetCascader(); Int_t aggNo = cas.AllocateSeqNo(fVRec.GetTableProxy()->GetTableName(),requireGlobal,dbNo); if ( aggNo <= IDbi::kMAXLOCALSEQNO && ! localTest ) { COMETSevere( "Cannot write out configuration data: no authorising entry in cascade." << " "); return false; } fVRec.SetAggregateNo(aggNo); fCFSetModified.SetAggregateNo(aggNo); COMETDebug( "Aggregate number: " << aggNo << " allocated to entry " << fSoftwName << "," << fConfigName << " in table " << fVRec.GetTableProxy()->GetTableName() << " "); } COMET::IDbiWriter writer(fVRec,dbNo,logComment); writer.SetRequireGlobalSeqno(requireGlobal); writer.SetOverlayCreationDate(); writer << *fCFSet; return writer.Close(); }