// DataHeaderList.cxx #include "dataset_atlaspool/DataHeaderList.h" #include #include #include #include "POOLCore/POOLContext.h" #include "FileCatalog/IFileCatalog.h" #include "FileCatalog/URIParser.h" #include "DataSvc/DataSvcFactory.h" #include "DataSvc/DataSvcContext.h" #include "PersistencySvc/ITransaction.h" #include "PersistencySvc/ISession.h" #include "SealBase/SharedLibrary.h" #include "SealBase/SharedLibraryError.h" #include "SGTools/DataHeader.h" #include "dataset_util/ssystem.h" #include "dataset_util/FileName.h" #include "dataset_util/FileStatus.h" using std::string; using std::ostream; using std::cerr; using std::endl; using dset::DataHeaderList; //********************************************************************** // Local definitions. //********************************************************************** namespace { // Initialization. int init() { static int stat = 1; if ( stat ) { //std::cout << "***** Initializing POOL ******" << std::endl; // Loads the seal message stream pool::POOLContext::loadComponent( "SEAL/Services/Message Service" ); // Set the verbosity threshold to warnings pool::POOLContext::setMessageVerbosityLevel( seal::Msg::Warning ); stat = 0; } return stat; } int force_init = init(); // Load dictionary. // Returns 0 for success. int load_dict() { //const std::string dictlibrary = "SGToolsDict"; // For 8.3.0 and later //const std::string dictlibrary = "DataModelDict"; // For release 8.1.0 static int stat = 1; bool noisy = false; if ( stat != 0 ) { try { if ( noisy ) std::cout << "Loading SealSTLDict" << std::endl; seal::SharedLibrary:: load( seal::SharedLibrary::libname("SealSTLDict")); if ( noisy ) std::cout << "Loading EventInfoDict" << std::endl; seal::SharedLibrary:: load( seal::SharedLibrary::libname("EventInfoDict")); if ( noisy ) std::cout << "Loading SGToolsDict" << std::endl; seal::SharedLibrary:: load( seal::SharedLibrary::libname("SGToolsDict")); if ( noisy ) std::cout << "Loading StoreGateDict" << std::endl; } catch ( seal::SharedLibraryError *error) { throw std::runtime_error( error->explainSelf().c_str() ); } stat = 0; } return stat; } int loadit = load_dict(); // Create default XML file catalog name from the given name. // Path is stripped and extension is replaced with _catalog.xml. // E.g. /home/mydir/myfile.dat --> myfile_catalog.xml. string xml_catalog_name(string dbname) { string cname = FileName(dbname).nopath_name(); string::size_type ipos = cname.rfind('.'); if ( ipos != string::npos ) { cname = cname.substr(0, ipos); } cname += "_catalog.xml"; return cname; } } // end unnamed namespace //********************************************************************** // Member functions. //********************************************************************** // Constructor. DataHeaderList:: DataHeaderList(string dbname, string collname, string catname) : m_error(0), m_pfc(0), m_pds(0), m_pcol(0), m_dbname(dbname), m_collname(collname), m_catname(catname), m_tmpdir("") { // Check input file existence. /* FileStatus fdstat(m_dbname); if ( ! fdstat.exists() ) { cerr << "DataHeaderList: Unable to find file" << endl; cerr << " " << m_dbname << endl; m_error = 1; return; } */ // Assign catalog name in temporary directory if not already assigned. bool newcat = false; if ( m_catname == "" ) { m_tmpdir = "/tmp/DataHeaderList_XXXXXX"; mkdtemp(&m_tmpdir[0]); newcat = true; m_catname = m_tmpdir + "/" + xml_catalog_name(m_dbname); } // If catalog name was assigned here, make sure file is in catalog. // Put file in catalog (if it is not already there). if ( newcat ) { //std::cout << "++++++++ Creating file catalog ++++++++" << std::endl; string com = "export POOL_CATALOG=xmlcatalog_file:" + m_catname + "; pool_insertFileToCatalog " + m_dbname + " 1>/dev/null; 2>&1"; std::cout << com << std::endl; ssystem(com); //std::cout << "+++++++++++ Catalog created +++++++++++" << std::endl; } FileStatus fcstat(m_catname); if ( ! fcstat.exists() ) { cerr << "DataHeaderList: Unable to open catalog for file" << endl; cerr << " " << m_dbname << endl; m_error = 2; return; } if ( m_collname == "" ) { m_collname = "POOLContainer_DataHeader"; } m_pfc = new pool::IFileCatalog; try { string contact = "xmlcatalog_file:" + m_catname; pool::URIParser p(contact); p.parse(); m_pfc->addReadCatalog(p.contactstring()); } catch ( seal::Exception ex ) { std::cout << ex << std::endl; std::cout << "DataHeaderList> " << m_catname << std::endl; m_error = 3; } m_pfc->connect(); m_pfc->start(); m_pds = pool::DataSvcFactory::instance(m_pfc); m_pds->transaction().start(pool::ITransaction::READ); string ctype = "ImplicitCollection"; string cname = "PFN:" + m_dbname; try { m_pcol = new DHCollection(m_pds, ctype, cname, m_collname, pool::ICollection::READ); if ( m_pcol->name() != m_collname ) { std::cout << "DataHeaderList> " << m_pcol->name() << std::endl; std::cout << "DataHeaderList> " << collname << std::endl; m_error = 4; } } catch ( seal::Exception ex ) { std::cout << ex << std::endl; m_error = 5; } if ( m_error ) { delete m_pcol; m_pcol = 0; return; } } //********************************************************************** // Destructor. DataHeaderList::~DataHeaderList() { if ( m_pds != 0 ) { m_pds->transaction().commit(); m_pds->session().disconnectAll(); } delete m_pfc; delete m_pds; delete m_pcol; if ( m_tmpdir.size() > 4 && m_tmpdir.substr(0,4)=="/tmp" ) { ssystem("rm -rf " + m_tmpdir); } } //********************************************************************** // Validity. bool DataHeaderList::is_valid() const { return m_error == 0; } //********************************************************************** // Error status. int DataHeaderList::error() const { return m_error; } //********************************************************************** // DB name. string DataHeaderList::database_name() const { return m_dbname; } //********************************************************************** // Collection name. string DataHeaderList::collection_name() const { return m_collname; } //********************************************************************** // Collection name. string DataHeaderList::file_catalog_name() const { return m_catname; } //********************************************************************** // Data service. pool::IDataSvc* DataHeaderList::data_service() const { assert( m_pds != 0 ); return m_pds; } //********************************************************************** // Iterator. DataHeaderList::Iterator DataHeaderList::iterator() const { int stat = load_dict(); assert( stat == 0 ); if ( m_pcol == 0 ) { assert( false ); //return Iterator(); } return m_pcol->select(); } //********************************************************************** //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const dset::DataHeaderList& rhs) { if ( ! rhs.is_valid() ) { lhs << "Invalid DataHeaderList -- error " << rhs.error(); } else { lhs << "DataHeaderList for " << rhs.collection_name() << " in DB " << rhs.database_name() ; } return lhs; } //**********************************************************************