CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TagCollectionRetriever.cc
Go to the documentation of this file.
1 //
2 // Package: CondCore/TagCollection
3 // Class: TagCollectionRetriever
4 //
5 // Author: Zhen Xie
6 //
9 #include "RelationalAccess/ISchema.h"
10 #include "RelationalAccess/ITable.h"
11 #include "RelationalAccess/IQuery.h"
12 #include "RelationalAccess/ICursor.h"
13 #include "CoralBase/AttributeList.h"
14 #include "CoralBase/Attribute.h"
15 #include "RelationalAccess/SchemaException.h"
17 
19  m_coraldb(coraldb)
20 {}
21 
22 
24  std::string const & prefix,
25  std::string const & postfix) :
26  m_coraldb(coraldb), pfnEditor(prefix,postfix)
27 {}
28 
29 
30 
32 
34  return m_coraldb.nominalSchema().existsTable(cond::tagInventoryTable);
35 }
36 
37 #include <iostream>
39  if(!existsTagDatabase()){
40  throw cond::nonExistentGlobalTagInventoryException("TagCollectionRetriever::selectTagCollection");
41  }
42  std::pair<std::string,std::string> treenodepair=parseglobaltag(globaltag);
43  std::string treename=treenodepair.first;
44  std::string nodename=treenodepair.second;
46  if( !treename.empty() ){
47  for(unsigned int i=0; i<treename.size(); ++i){
48  treename[i]=std::toupper(treename[i]);
49  }
50  treetablename+="_";
51  treetablename+=treename;
52  }
53  return m_coraldb.nominalSchema().existsTable(treetablename);
54 }
55 
56 void
58  std::set<cond::TagMetadata >& result){
59  if(!selectTagCollection( globaltag, result ) )
60  throw cond::nonExistentGlobalTagException("TagCollectionRetriever::getTagCollection",globaltag);
61 }
62 
63 bool
65  std::set<cond::TagMetadata >& result){
66  if(!m_coraldb.nominalSchema().existsTable(cond::tagInventoryTable)){
67  throw cond::nonExistentGlobalTagInventoryException("TagCollectionRetriever::selectTagCollection");
68  }
69  std::pair<std::string,std::string> treenodepair=parseglobaltag(globaltag);
70  std::string treename=treenodepair.first;
71  std::string nodename=treenodepair.second;
72  //std::cout<<"treename "<<treename<<std::endl;
73  //std::cout<<"nodename "<<nodename<<std::endl;
75  if( !treename.empty() ){
76  for(unsigned int i=0; i<treename.size(); ++i){
77  treename[i]=std::toupper(treename[i]);
78  }
79  treetablename+="_";
80  treetablename+=treename;
81  }
82  if( !m_coraldb.nominalSchema().existsTable(treetablename) ) return false;
83 
84  coral::IQuery* query=m_coraldb.nominalSchema().newQuery();
85  //std::cout<<"treetablename "<<treetablename<<std::endl;
86  query->addToTableList( treetablename, "p1" );
87  query->addToTableList( treetablename, "p2" );
88  query->addToOutputList( "p1.tagid" );
89  query->setRowCacheSize( 100 );
90  coral::AttributeList bindData;
91  bindData.extend( "nodelabel",typeid(std::string) );
92  bindData.extend( "tagid",typeid(unsigned int) );
93  bindData["tagid"].data<unsigned int>()=0;
94  bindData["nodelabel"].data<std::string>()=nodename;
95  query->setCondition( "p1.lft BETWEEN p2.lft AND p2.rgt AND p2.nodelabel = :nodelabel AND p1.tagid <> :tagid", bindData );
96  coral::AttributeList qresult;
97  qresult.extend("tagid", typeid(unsigned int));
98  query->defineOutput(qresult);
99  std::vector<unsigned int> leaftagids;
100  leaftagids.reserve(100);
101  coral::ICursor& cursor = query->execute();
102  while( cursor.next() ) {
103  const coral::AttributeList& row = cursor.currentRow();
104  leaftagids.push_back(row["tagid"].data<unsigned int>());
105  }
106  cursor.close();
107  delete query;
108  std::vector<unsigned int>::iterator it;
109  std::vector<unsigned int>::iterator itBeg=leaftagids.begin();
110  std::vector<unsigned int>::iterator itEnd=leaftagids.end();
111  coral::ITable& tagInventorytable=m_coraldb.nominalSchema().tableHandle(cond::tagInventoryTable);
112  for( it=itBeg; it!=itEnd; ++it ){
113  coral::IQuery* leaftagquery=tagInventorytable.newQuery();
114  leaftagquery->addToOutputList( "tagname" );
115  leaftagquery->addToOutputList( "pfn" );
116  leaftagquery->addToOutputList( "recordname" );
117  leaftagquery->addToOutputList( "objectname" );
118  leaftagquery->addToOutputList( "labelname" );
119  coral::AttributeList myresult;
120  myresult.extend("tagname",typeid(std::string));
121  myresult.extend("pfn",typeid(std::string));
122  myresult.extend("recordname",typeid(std::string));
123  myresult.extend("objectname",typeid(std::string));
124  myresult.extend("labelname",typeid(std::string));
125  leaftagquery->defineOutput( myresult );
126  coral::AttributeList bindVariableList;
127  bindVariableList.extend("tagid",typeid(unsigned int));
128  leaftagquery->setCondition( "tagid = :tagid",bindVariableList );
129  leaftagquery->limitReturnedRows(1,0);
130  bindVariableList["tagid"].data<unsigned int>()=*it;
131  coral::ICursor& cursor2 =leaftagquery->execute();
132  if( cursor2.next() ){
133  const coral::AttributeList& row = cursor2.currentRow();
134  cond::TagMetadata tagmetadata;
135  std::string tagname=row["tagname"].data<std::string>();
136  tagmetadata.tag=tagname;
137  tagmetadata.pfn=pfnEditor(row["pfn"].data<std::string>());
138  tagmetadata.recordname=row["recordname"].data<std::string>();
139  tagmetadata.objectname=row["objectname"].data<std::string>();
140  tagmetadata.labelname=row["labelname"].data<std::string>();
141  if(! result.insert(tagmetadata).second ){
142  throw cond::Exception("cond::TagCollectionRetriever::getTagCollection tag "+tagname+" from "+tagmetadata.pfn+" already exist, cannot insert in the tag collection ");
143  }
144  }
145  cursor2.close();
146  delete leaftagquery;
147  }
148  return true;
149 }
150 
151 std::pair<std::string,std::string>
153  std::pair<std::string,std::string> result;
154  std::size_t pos=globaltag.find("::");
155  if(pos==std::string::npos){
156  result.first="";
157  result.second=globaltag;
158  }else{
159  result.first=globaltag.substr(0,pos);
160  result.second=globaltag.substr(pos+2);
161  }
162  return result;
163 }
164 
int i
Definition: DBlmapReader.cc:9
bool selectTagCollection(const std::string &globaltag, std::set< cond::TagMetadata > &result)
list globaltag
Definition: align_cfg.py:7
void getTagCollection(const std::string &globaltag, std::set< cond::TagMetadata > &result)
std::string objectname
Definition: TagMetadata.h:12
std::string labelname
Definition: TagMetadata.h:11
const std::string tagTreeTablePrefix
std::string tag
Definition: TagMetadata.h:8
std::pair< std::string, std::string > parseglobaltag(const std::string &globaltag)
parse global tag string returns result in pair &lt;treename,nodename&gt;
tuple result
Definition: query.py:137
const std::string tagInventoryTable
bool existsTagCollection(const std::string &globaltag)
std::string pfn
Definition: TagMetadata.h:9
TagCollectionRetriever(cond::DbSession &coraldb)
constructor
tuple query
Definition: o2o.py:269
std::string recordname
Definition: TagMetadata.h:10