00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #include "CdbBdb/CdbBdbLoadList.hh"
00012 #include "CdbBdb/CdbBdbObjectFactory.hh"
00013
00014 #include "CdbBase/Cdb.hh"
00015 #include "CdbBase/CdbPathName.hh"
00016 #include "CdbBase/CdbCondition.hh"
00017
00018 #include "BbrStdUtils/Tokenize.hh"
00019 #include "BbrStdUtils/String.hh"
00020 using namespace babar::String;
00021
00022 #include "ErrLogger/ErrLog.hh"
00023
00024 #include <fstream>
00025
00026 #include <string>
00027 #include <vector>
00028 #include <algorithm>
00029 using std::endl;
00030 using std::fstream;
00031 using std::ifstream;
00032
00033 namespace {
00034
00035
00036
00037
00038
00039 class MyObjectFactory : public CdbBdbObjectFactory {
00040 public:
00041
00042 MyObjectFactory( CdbBdbCreateCondObject theObjectCreatorFunction,
00043 const std::string& theObjectSource ) :
00044 _objectCreatorFunction(theObjectCreatorFunction),
00045 _objectSource(theObjectSource)
00046 { }
00047
00048 virtual CdbStatus doCreate( BdbHandle(BdbObject)& theProduct,
00049 const BdbRefAny& theHint );
00050
00051 private:
00052
00053 CdbBdbCreateCondObject _objectCreatorFunction;
00054 std::string _objectSource;
00055 };
00056
00057 CdbStatus
00058 MyObjectFactory::doCreate( BdbHandle(BdbObject)& theProduct,
00059 const BdbRefAny& theHint )
00060 {
00061 BdbRef(BdbObject) oRef;
00062 if( _objectCreatorFunction( _objectSource.c_str( ),
00063 const_cast<BdbRefAny&>(theHint),
00064 oRef )) {
00065 theProduct = oRef;
00066 return CdbStatus::Success;
00067 }
00068 return CdbStatus::Error;
00069 }
00070
00071
00072
00073
00074
00075 class StorePoint {
00076 public:
00077
00078 StorePoint( const char* theObjectSource = 0,
00079 const BdbTime& theBeginTime = BdbTime::minusInfinity ) :
00080 str(theObjectSource),
00081 begin(theBeginTime)
00082 { }
00083
00084 StorePoint( const StorePoint& thePoint ) :
00085 str (thePoint.str),
00086 begin(thePoint.begin)
00087 { }
00088
00089 StorePoint& operator=( const StorePoint& thePoint )
00090 {
00091 str = thePoint.str;
00092 begin = thePoint.begin;
00093 return *this;
00094 }
00095
00096 std::string str;
00097 BdbTime begin;
00098 };
00099 };
00100
00101
00102
00103
00104
00105 CdbBdbLoadList::CdbBdbLoadList( const char* sysname,
00106 const char* containername,
00107 CdbBdbCreateCondObject function ) :
00108 _sysname(sysname),
00109 _contname(containername),
00110 _createfunction(function)
00111 { }
00112
00113 CdbBdbLoadList::~CdbBdbLoadList()
00114 { }
00115
00116 BdbStatus
00117 CdbBdbLoadList::storeObjects( const char* filename )
00118 {
00119
00120
00121 BdbStatus status = BdbcSuccess;
00122
00123
00124
00125 ifstream datafile( filename );
00126 if( datafile.good( )) {
00127
00128
00129
00130 std::vector<StorePoint> objects;
00131 {
00132
00133
00134 std::string zonename( "UTC" );
00135
00136
00137
00138 std::string fline;
00139
00140 getline( datafile, fline );
00141
00142 while( ! datafile.eof( )) {
00143
00144
00145
00146 if(( ! fline.empty( )) && fline.find( "#", 0, 1 ) == std::string::npos ) {
00147
00148
00149
00150 Tokenize linetokens( fline );
00151
00152
00153
00154 std::string date = linetokens( );
00155 std::string time = linetokens( );
00156
00157
00158
00159
00160 if( date.empty() || (time.empty() && date != "+Infinity" && date != "-Infinity" ) )
00161 ErrMsg(fatal) << "badly formatted contents of the file to be loaded \"" << filename << "\"" << endl
00162 << "at line \"" << fline << "\"." << endmsg;
00163
00164
00165
00166 if ( 0 == compare_nocase( date, "Zone" )) {
00167 zonename = time;
00168 } else {
00169
00170
00171
00172 BdbTime bdbtime(0);
00173 {
00174 BdbTime::Zone zone = BdbTime::UTC;
00175 if( 0 == compare_nocase( zonename, "UTC" ));
00176 else if( 0 == compare_nocase( zonename, "Local" )) zone = BdbTime::Local;
00177 else {
00178 ErrMsg(error) << "unrecognized timezone \"" << zonename << "\"" << endl
00179 << "when reading file \"" << filename << "\"" << endl
00180 << "at line \"" << fline << "\"." << endmsg;
00181 return BdbcError;
00182 }
00183 if( ! BdbTime::parseTime( date, time, zone, bdbtime )) {
00184
00185 ErrMsg(error) << "failed to translate data and time \"" << date << "\" \"" << time << "\"" << endl
00186 << "when reading file \"" << filename << "\"" << endl
00187 << "at line \"" << fline << "\"." << endmsg;
00188 return BdbcError;
00189 }
00190 }
00191
00192
00193
00194 std::string token;
00195 std::string objstring;
00196
00197 while( ! ( token = linetokens( )).empty( )) {
00198
00199 if( ! objstring.empty( )) objstring.append( " " );
00200 objstring.append( token );
00201 }
00202
00203 if( ! objstring.empty( )) {
00204
00205
00206
00207 objects.push_back( StorePoint( objstring.c_str( ),
00208 bdbtime ));
00209
00210 } else {
00211
00212
00213
00214 objects.push_back( StorePoint( "",
00215 bdbtime ));
00216 }
00217 }
00218 }
00219
00220
00221
00222 getline( datafile, fline );
00223 }
00224 }
00225
00226
00227
00228
00229 std::vector<StorePoint>::size_type num = objects.size( );
00230
00231 if( num < 2 ) {
00232 ErrMsg(error) << "insuffient contents of the objects list file \"" << filename << "\""
00233 << " : Conditions load aborted " << endmsg;
00234 status = BdbcError;
00235 } else {
00236
00237 startTransaction( BdbcUpdate );
00238 {
00239 std::string cName = std::string( CdbPathName::separator( )) +
00240 _sysname +
00241 std::string( CdbPathName::separator( )) +
00242 _contname;
00243
00244 CdbConditionPtr cPtr;
00245 if( CdbStatus::Success != CdbCondition::instance( cPtr,
00246 cName.c_str( ))) {
00247 ErrMsg(error) << "failed to locate condition \"" << cName << "\""
00248 << " in the database : Conditions load aborted " << endmsg;
00249 status = BdbcError;
00250 } else {
00251
00252 for( std::vector<StorePoint>::size_type i = 0; i < num - 1; ++i ) {
00253
00254 MyObjectFactory oFactory( _createfunction, objects[i].str );
00255
00256 if( CdbStatus::Success != cPtr->storeObject( oFactory,
00257 objects[i].begin,
00258 objects[i+1].begin )) {
00259
00260 ErrMsg(fatal) << "error creating and storing object(s) from file \"" << filename << "\""
00261 << " : Conditions load aborted " << endmsg;
00262 }
00263 }
00264 }
00265 }
00266 endTransaction( );
00267 }
00268
00269 } else {
00270 ErrMsg(error) << "can't open object list file \"" << filename << "\"" << endmsg;
00271 status = BdbcError;
00272 }
00273 return status;
00274 }