CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondCore/TagCollection/src/TagCollectionRetriever.cc

Go to the documentation of this file.
00001 //
00002 // Package:    CondCore/TagCollection
00003 // Class:      TagCollectionRetriever
00004 //
00005 // Author:      Zhen Xie
00006 //
00007 #include "CondCore/TagCollection/interface/TagCollectionRetriever.h"
00008 #include "CondCore/TagCollection/interface/TagDBNames.h"
00009 #include "RelationalAccess/ISchema.h"
00010 #include "RelationalAccess/ITable.h"
00011 #include "RelationalAccess/IQuery.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "CoralBase/AttributeList.h"
00014 #include "CoralBase/Attribute.h"
00015 #include "RelationalAccess/SchemaException.h"
00016 //#include "CondCore/DBCommon/interface/Exception.h"
00017 #include "CondCore/TagCollection/interface/Exception.h"
00018 
00019 //#include <iostream>
00020 cond::TagCollectionRetriever::TagCollectionRetriever( cond::DbSession& coraldb ):
00021   m_coraldb(coraldb)
00022 {}
00023 
00024 
00025 cond::TagCollectionRetriever::TagCollectionRetriever( cond::DbSession& coraldb, 
00026                                                       std::string const & prefix, 
00027                                                       std::string const & postfix) :
00028   m_coraldb(coraldb), pfnEditor(prefix,postfix)
00029 {}
00030 
00031  
00032 
00033 cond::TagCollectionRetriever::~TagCollectionRetriever(){}
00034 
00035 void 
00036 cond::TagCollectionRetriever::getTagCollection( const std::string& globaltag,
00037                                                 std::set<cond::TagMetadata >& result){
00038   if(!m_coraldb.nominalSchema().existsTable(cond::tagInventoryTable)){
00039     throw cond::nonExistentGlobalTagInventoryException("TagCollectionRetriever::getTagCollection");
00040   }
00041   std::pair<std::string,std::string> treenodepair=parseglobaltag(globaltag);
00042   std::string treename=treenodepair.first;
00043   std::string nodename=treenodepair.second;
00044   //std::cout<<"treename "<<treename<<std::endl;
00045   //std::cout<<"nodename "<<nodename<<std::endl;
00046   std::string treetablename(cond::tagTreeTablePrefix);
00047   if( !treename.empty() ){
00048     for(unsigned int i=0; i<treename.size(); ++i){
00049       treename[i]=std::toupper(treename[i]);    
00050     }
00051     treetablename+="_";
00052     treetablename+=treename;
00053   }
00054   if( !m_coraldb.nominalSchema().existsTable(treetablename) ){
00055     throw cond::nonExistentGlobalTagException("TagCollectionRetriever::getTagCollection",globaltag);
00056   }
00057   coral::IQuery* query=m_coraldb.nominalSchema().newQuery();
00058   //std::cout<<"treetablename "<<treetablename<<std::endl;
00059   query->addToTableList( treetablename, "p1" );
00060   query->addToTableList( treetablename, "p2" );
00061   query->addToOutputList( "p1.tagid" );
00062   query->setRowCacheSize( 100 );
00063   coral::AttributeList bindData;
00064   bindData.extend( "nodelabel",typeid(std::string) );
00065   bindData.extend( "tagid",typeid(unsigned int) );
00066   bindData["tagid"].data<unsigned int>()=0;
00067   bindData["nodelabel"].data<std::string>()=nodename;
00068   query->setCondition( "p1.lft BETWEEN p2.lft AND p2.rgt AND p2.nodelabel = :nodelabel AND p1.tagid <> :tagid", bindData );
00069   coral::AttributeList qresult;
00070   qresult.extend("tagid", typeid(unsigned int));
00071   query->defineOutput(qresult);
00072   std::vector<unsigned int> leaftagids;
00073   leaftagids.reserve(100);
00074   coral::ICursor& cursor = query->execute();
00075   while( cursor.next() ) {
00076     const coral::AttributeList& row = cursor.currentRow();
00077     leaftagids.push_back(row["tagid"].data<unsigned int>());
00078   }
00079   cursor.close();
00080   delete query;
00081   std::vector<unsigned int>::iterator it;
00082   std::vector<unsigned int>::iterator itBeg=leaftagids.begin();
00083   std::vector<unsigned int>::iterator itEnd=leaftagids.end();
00084   coral::ITable& tagInventorytable=m_coraldb.nominalSchema().tableHandle(cond::tagInventoryTable);
00085   for( it=itBeg; it!=itEnd; ++it ){
00086     coral::IQuery* leaftagquery=tagInventorytable.newQuery();
00087     leaftagquery->addToOutputList( "tagname" );
00088     leaftagquery->addToOutputList( "pfn" );
00089     leaftagquery->addToOutputList( "recordname" );
00090     leaftagquery->addToOutputList( "objectname" );
00091     leaftagquery->addToOutputList( "labelname" );
00092     coral::AttributeList myresult;
00093     myresult.extend("tagname",typeid(std::string));
00094     myresult.extend("pfn",typeid(std::string));
00095     myresult.extend("recordname",typeid(std::string));
00096     myresult.extend("objectname",typeid(std::string));
00097     myresult.extend("labelname",typeid(std::string));
00098     leaftagquery->defineOutput( myresult );
00099     coral::AttributeList bindVariableList;
00100     bindVariableList.extend("tagid",typeid(unsigned int));
00101     leaftagquery->setCondition( "tagid = :tagid",bindVariableList );
00102     leaftagquery->limitReturnedRows(1,0);
00103     bindVariableList["tagid"].data<unsigned int>()=*it;
00104     coral::ICursor& cursor2 =leaftagquery->execute();
00105     if( cursor2.next() ){
00106       const coral::AttributeList& row = cursor2.currentRow();
00107       cond::TagMetadata tagmetadata;
00108       std::string tagname=row["tagname"].data<std::string>();
00109       tagmetadata.tag=tagname;
00110       tagmetadata.pfn=pfnEditor(row["pfn"].data<std::string>());
00111       tagmetadata.recordname=row["recordname"].data<std::string>();
00112       tagmetadata.objectname=row["objectname"].data<std::string>();
00113       tagmetadata.labelname=row["labelname"].data<std::string>();
00114       if(! result.insert(tagmetadata).second ){
00115         throw cond::Exception("cond::TagCollectionRetriever::getTagCollection tag "+tagname+" from "+tagmetadata.pfn+" already exist, cannot insert in the tag collection ");
00116       }
00117     }
00118     cursor2.close();
00119     delete leaftagquery;
00120   }
00121 }
00122 
00123 std::pair<std::string,std::string>
00124 cond::TagCollectionRetriever::parseglobaltag(const std::string& globaltag){
00125   std::pair<std::string,std::string> result;
00126   std::size_t pos=globaltag.find("::");
00127   if(pos==std::string::npos){
00128     result.first="";
00129     result.second=globaltag;
00130   }else{
00131     result.first=globaltag.substr(0,pos);
00132     result.second=globaltag.substr(pos+2);
00133   }
00134   return result;
00135 }
00136