CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
edm::FileLocator Class Reference

#include <FileLocator.h>

Classes

struct  Rule
 

Public Member Functions

 FileLocator (std::string const &catUrl, bool fallback)
 
std::string lfn (std::string const &ipfn) const
 
std::string pfn (std::string const &ilfn) const
 
 ~FileLocator ()
 

Private Types

typedef std::map< std::string, RulesProtocolRules
 
typedef std::vector< RuleRules
 

Private Member Functions

std::string applyRules (ProtocolRules const &protocolRules, std::string const &protocol, std::string const &destination, bool direct, std::string name) const
 
std::string convert (std::string const &input, ProtocolRules const &rules, bool direct) const
 
void init (std::string const &catUrl, bool fallback)
 
void parseRule (xercesc::DOMNode *ruleNode, ProtocolRules &rules)
 

Private Attributes

std::string m_destination
 
ProtocolRules m_directRules
 
std::string m_filename
 
std::string m_fileType
 
ProtocolRules m_inverseRules
 
std::vector< std::string > m_protocols
 

Static Private Attributes

static int s_numberOfInstances = 0
 

Detailed Description

Definition at line 13 of file FileLocator.h.

Member Typedef Documentation

typedef std::map<std::string, Rules> edm::FileLocator::ProtocolRules
private

Definition at line 36 of file FileLocator.h.

typedef std::vector<Rule> edm::FileLocator::Rules
private

Definition at line 35 of file FileLocator.h.

Constructor & Destructor Documentation

edm::FileLocator::FileLocator ( std::string const &  catUrl,
bool  fallback 
)
explicit

Definition at line 47 of file FileLocator.cc.

References MillePedeFileConverter_cfg::e, Exception, init(), s_numberOfInstances, AlCaHLTBitMon_QueryRunRegistry::string, toolbox::toString(), and cms::concurrency::xercesInitialize().

48  : m_destination("any") {
49  try {
50  // << "Xerces-c initialization Number "
51  // << s_numberOfInstances <<
52  if (s_numberOfInstances == 0) {
54  }
55  }
56  catch (XMLException const& e) {
57  // << "Xerces-c error in initialization \n"
58  // << "Exception message is: \n"
59  // << toString(e.getMessage()) <<
60  throw
61  cms::Exception("TrivialFileCatalog", std::string("Fatal Error on edm::FileLocator:")+ toString(e.getMessage()));
62  }
64 
65  init(catUrl, fallback);
66 
67  // std::cout << m_protocols.size() << " protocols" << std::endl;
68  // std::cout << m_directRules[m_protocols[0]].size() << " rules" << std::endl;
69  }
static int s_numberOfInstances
Definition: FileLocator.h:26
std::string m_destination
Definition: FileLocator.h:59
void xercesInitialize()
Definition: Xerces.cc:18
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
void init(std::string const &catUrl, bool fallback)
Definition: FileLocator.cc:129
edm::FileLocator::~FileLocator ( )

Definition at line 71 of file FileLocator.cc.

References AlCaHLTBitMon_QueryRunRegistry::string.

72  {}

Member Function Documentation

std::string edm::FileLocator::applyRules ( ProtocolRules const &  protocolRules,
std::string const &  protocol,
std::string const &  destination,
bool  direct,
std::string  name 
) const
private

Definition at line 242 of file FileLocator.cc.

References mps_fire::i, dataset::name, cmsCodeRulesChecker::rules, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by convert().

246  {
247 
248  // std::cerr << "Calling apply rules with protocol: " << protocol << "\n destination: " << destination << "\n " << " on name " << name << std::endl;
249 
250  ProtocolRules::const_iterator const rulesIterator = protocolRules.find(protocol);
251  if (rulesIterator == protocolRules.end()) {
252  return "";
253  }
254 
255  Rules const& rules = (*(rulesIterator)).second;
256 
257  std::smatch destinationMatches;
258  std::smatch nameMatches;
259 
260  /* Look up for a matching rule*/
261  for (Rules::const_iterator i = rules.begin(); i != rules.end(); ++i) {
262 
263  if (!std::regex_match(destination, destinationMatches, i->destinationMatch)) {
264  continue;
265  }
266 
267  if (!std::regex_match(name, i->pathMatch)) {
268  continue;
269  }
270 
271  // std::cerr << "Rule " << i->pathMatch << "matched! " << std::endl;
272 
273  std::string const chain = i->chain;
274  if ((direct == true) && (chain != "")) {
275  name = applyRules(protocolRules, chain, destination, direct, name);
276  if (name.empty()) {
277  return "";
278  }
279  }
280 
281  std::regex_match(name, nameMatches, i->pathMatch);
282  name = replaceWithRegexp(nameMatches, i->result);
283 
284  if ((direct == false) && (chain != "")) {
285  name = applyRules(protocolRules, chain, destination, direct, name);
286  }
287  return name;
288  }
289  return "";
290  }
Definition: chain.py:1
std::vector< Rule > Rules
Definition: FileLocator.h:35
std::string applyRules(ProtocolRules const &protocolRules, std::string const &protocol, std::string const &destination, bool direct, std::string name) const
Definition: FileLocator.cc:242
std::string edm::FileLocator::convert ( std::string const &  input,
ProtocolRules const &  rules,
bool  direct 
) const
private

Definition at line 85 of file FileLocator.cc.

References applyRules(), m_destination, m_protocols, MillePedeFileConverter_cfg::out, pi, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by lfn(), and pfn().

85  {
86  std::string out = "";
87 
88  for (size_t pi = 0, pe = m_protocols.size(); pi != pe; ++pi) {
89  out = applyRules(rules, m_protocols[pi], m_destination, direct, input);
90  if (!out.empty())
91  return out;
92  }
93  return out;
94  }
std::string m_destination
Definition: FileLocator.h:59
static std::string const input
Definition: EdmProvDump.cc:44
const Double_t pi
std::vector< std::string > m_protocols
Definition: FileLocator.h:58
std::string applyRules(ProtocolRules const &protocolRules, std::string const &protocol, std::string const &destination, bool direct, std::string name) const
Definition: FileLocator.cc:242
void edm::FileLocator::init ( std::string const &  catUrl,
bool  fallback 
)
private

Definition at line 129 of file FileLocator.cc.

References findQualityFiles::comma, cms::xerces::cStr(), MillePedeFileConverter_cfg::e, Exception, mps_fire::i, edm::Service< T >::isAvailable(), m_destination, m_directRules, m_filename, m_inverseRules, m_protocols, TSGForRoadSearch_cfi::option, createfilelist::parser, parseRule(), cmsCodeRulesChecker::rules, split, AlCaHLTBitMon_QueryRunRegistry::string, and cms::xerces::uStr().

Referenced by FileLocator().

129  {
130  std::string m_url = catUrl;
131 
132  if (m_url.empty()) {
133  Service<SiteLocalConfig> localconfservice;
134  if (!localconfservice.isAvailable())
135  throw cms::Exception("TrivialFileCatalog", "edm::SiteLocalConfigService is not available");
136 
137  m_url = (fallback ? localconfservice->fallbackDataCatalog() : localconfservice->dataCatalog());
138  }
139 
140  // std::cout << "Connecting to the catalog " << m_url << std::endl;
141 
142  if (m_url.find("file:") == std::string::npos) {
143  throw cms::Exception("TrivialFileCatalog", "TrivialFileCatalog::connect: Malformed url for file catalog configuration");
144  }
145 
146  m_url = m_url.erase(0, m_url.find(":") + 1);
147 
148  std::vector<std::string> tokens;
149  boost::algorithm::split(tokens, m_url, boost::is_any_of(std::string("?")));
150  m_filename = tokens[0];
151 
152  if (tokens.size() == 2) {
153  std::string const options = tokens[1];
154  std::vector<std::string> optionTokens;
155  boost::algorithm::split(optionTokens, options, boost::is_any_of(std::string("&")));
156 
157  std::string const equalSign("=");
158  std::string const comma(",");
159 
160  for (size_t oi = 0, oe = optionTokens.size(); oi != oe; ++oi) {
161  std::string const option = optionTokens[oi];
162  std::vector<std::string> argTokens;
163  boost::algorithm::split(argTokens, option, boost::is_any_of(equalSign));
164 
165  if (argTokens.size() != 2) {
166  throw cms::Exception("TrivialFileCatalog", "TrivialFileCatalog::connect: Malformed url for file catalog configuration");
167  }
168 
169  if (argTokens[0] == "protocol") {
170  boost::algorithm::split(m_protocols, argTokens[1], boost::is_any_of(comma));
171  } else if (argTokens[0] == "destination") {
172  m_destination = argTokens[1];
173  }
174  }
175  }
176 
177  if (m_protocols.empty()) {
178  throw cms::Exception("TrivialFileCatalog", "TrivialFileCatalog::connect: protocol was not supplied in the contact string");
179  }
180 
181  std::ifstream configFile;
182  configFile.open(m_filename.c_str());
183 
184  //
185  // std::cout << "Using catalog configuration " << m_filename << std::endl;
186 
187  if (!configFile.good() || !configFile.is_open()) {
188  throw cms::Exception("TrivialFileCatalog", "TrivialFileCatalog::connect: Unable to open trivial file catalog " + m_filename);
189  }
190 
191  configFile.close();
192 
193  auto parser = std::make_unique<XercesDOMParser>();
194  try {
195  parser->setValidationScheme(XercesDOMParser::Val_Auto);
196  parser->setDoNamespaces(false);
197  parser->parse(m_filename.c_str());
198  DOMDocument* doc = parser->getDocument();
199  assert(doc);
200 
201  /* trivialFileCatalog matches the following xml schema
202  FIXME: write a proper DTD
203  <storage-mapping>
204  <lfn-to-pfn protocol="direct" destination-match=".*"
205  path-match="lfn/guid match regular expression"
206  result="/castor/cern.ch/cms/$1"/>
207  <pfn-to-lfn protocol="srm"
208  path-match="lfn/guid match regular expression"
209  result="$1"/>
210  </storage-mapping>
211  */
212 
213  /*first of all do the lfn-to-pfn bit*/
214  {
215  DOMNodeList* rules = doc->getElementsByTagName(uStr("lfn-to-pfn").ptr());
216  XMLSize_t const ruleTagsNum = rules->getLength();
217 
218  // FIXME: we should probably use a DTD for checking validity
219 
220  for (XMLSize_t i = 0; i < ruleTagsNum; ++i) {
221  DOMNode* ruleNode = rules->item(i);
222  parseRule(ruleNode, m_directRules);
223  }
224  }
225  /*Then we handle the pfn-to-lfn bit*/
226  {
227  DOMNodeList* rules = doc->getElementsByTagName(uStr("pfn-to-lfn").ptr());
228  XMLSize_t ruleTagsNum = rules->getLength();
229 
230  for (XMLSize_t i = 0; i < ruleTagsNum; ++i) {
231  DOMNode* ruleNode = rules->item(i);
232  parseRule(ruleNode, m_inverseRules);
233  }
234  }
235  }
236  catch (xercesc::DOMException const& e) {
237  throw cms::Exception("TrivialFileCatalog") << "Xerces XML parser threw this exception: " << cStr(e.getMessage()).ptr() << std::endl;
238  }
239  }
std::string m_destination
Definition: FileLocator.h:59
ProtocolRules m_inverseRules
Definition: FileLocator.h:54
ZStr< char > cStr(XMLCh const *str)
ZStr< XMLCh > uStr(char const *str)
std::string m_filename
Definition: FileLocator.h:57
ProtocolRules m_directRules
Definition: FileLocator.h:52
void parseRule(xercesc::DOMNode *ruleNode, ProtocolRules &rules)
Definition: FileLocator.cc:97
std::vector< std::string > m_protocols
Definition: FileLocator.h:58
double split
Definition: MVATrainer.cc:139
std::string edm::FileLocator::lfn ( std::string const &  ipfn) const

Definition at line 80 of file FileLocator.cc.

References convert(), m_inverseRules, and AlCaHLTBitMon_QueryRunRegistry::string.

80  {
81  return convert(ipfn, m_inverseRules, false);
82  }
ProtocolRules m_inverseRules
Definition: FileLocator.h:54
std::string convert(std::string const &input, ProtocolRules const &rules, bool direct) const
Definition: FileLocator.cc:85
void edm::FileLocator::parseRule ( xercesc::DOMNode *  ruleNode,
ProtocolRules rules 
)
private

Definition at line 97 of file FileLocator.cc.

References Exception, edm::FileLocator::Rule::pathMatch, mps_fire::result, AlCaHLTBitMon_QueryRunRegistry::string, toolbox::toString(), and cms::xerces::uStr().

Referenced by init().

97  {
98  if (!ruleNode) {
99  throw cms::Exception("TrivialFileCatalog", std::string("TrivialFileCatalog::connect: Malformed trivial catalog"));
100  }
101 
102  // ruleNode is actually always a DOMElement because it's the result of
103  // a `getElementsByTagName()` in the calling method.
104  DOMElement* ruleElement = static_cast<DOMElement *>(ruleNode);
105 
106  std::string const protocol = toString(ruleElement->getAttribute(uStr("protocol").ptr()));
107  std::string destinationMatchRegexp = toString(ruleElement->getAttribute(uStr("destination-match").ptr()));
108 
109  if (destinationMatchRegexp.empty()) {
110  destinationMatchRegexp = ".*";
111  }
112 
113  std::string const pathMatchRegexp
114  = toString(ruleElement->getAttribute(uStr("path-match").ptr()));
115  std::string const result
116  = toString(ruleElement->getAttribute(uStr("result").ptr()));
117  std::string const chain
118  = toString(ruleElement->getAttribute(uStr("chain").ptr()));
119 
120  Rule rule;
121  rule.pathMatch.assign(pathMatchRegexp);
122  rule.destinationMatch.assign(destinationMatchRegexp);
123  rule.result = result;
124  rule.chain = chain;
125  rules[protocol].push_back(rule);
126  }
Definition: chain.py:1
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
ZStr< XMLCh > uStr(char const *str)
std::string edm::FileLocator::pfn ( std::string const &  ilfn) const

Definition at line 75 of file FileLocator.cc.

References convert(), m_directRules, and AlCaHLTBitMon_QueryRunRegistry::string.

75  {
76  return convert(ilfn, m_directRules, true);
77  }
std::string convert(std::string const &input, ProtocolRules const &rules, bool direct) const
Definition: FileLocator.cc:85
ProtocolRules m_directRules
Definition: FileLocator.h:52

Member Data Documentation

std::string edm::FileLocator::m_destination
private

Definition at line 59 of file FileLocator.h.

Referenced by convert(), and init().

ProtocolRules edm::FileLocator::m_directRules
private

Direct rules are used to do the mapping from LFN to PFN.

Definition at line 52 of file FileLocator.h.

Referenced by init(), and pfn().

std::string edm::FileLocator::m_filename
private

Definition at line 57 of file FileLocator.h.

Referenced by init().

std::string edm::FileLocator::m_fileType
private

Definition at line 56 of file FileLocator.h.

ProtocolRules edm::FileLocator::m_inverseRules
private

Inverse rules are used to do the mapping from PFN to LFN

Definition at line 54 of file FileLocator.h.

Referenced by init(), and lfn().

std::vector<std::string> edm::FileLocator::m_protocols
private

Definition at line 58 of file FileLocator.h.

Referenced by convert(), and init().

int edm::FileLocator::s_numberOfInstances = 0
staticprivate

For the time being the only allowed configuration item is a prefix to be added to the GUID/LFN.

Definition at line 26 of file FileLocator.h.

Referenced by FileLocator().