FileCatalogMetadata_service.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
10 #include "fhiclcpp/types/Atom.h"
13 #include "hep_concurrency/RecursiveMutex.h"
14 #include "rapidjson/document.h"
15 #include "rapidjson/error/en.h"
16 
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 using namespace std;
22 using namespace hep::concurrency;
23 
24 namespace art {
25 
26  namespace {
27  detail::OldToNew const oldToNew{};
28  vector<string>
29  maybeTranslate(vector<string> names)
30  {
31  cet::transform_all(names, names.begin(), oldToNew);
32  cet::sort_all(names);
33  return names;
34  }
35 
36  bool
37  search_translated_all(vector<string> const& s, string const& md)
38  {
39  return cet::search_all(s, oldToNew(md));
40  }
41  } // unnamed namespace
42 
43  FileCatalogMetadata::FileCatalogMetadata(
45  : checkSyntax_{config().checkSyntax()}
46  , mdToInherit_{maybeTranslate(config().metadataFromInput())}
47  {
48  string appFam;
49  if (config().applicationFamily(appFam)) {
50  addMetadataString("application.family", appFam);
51  }
52  string appVer;
53  if (config().applicationVersion(appVer)) {
54  addMetadataString("application.version", appVer);
55  }
56 
57  // Always write out fileType -- either by inheriting from the input
58  // file or by overriding via the FHiCL parameter "fileType".
59  if (!search_translated_all(mdToInherit_, "file_type")) {
60  addMetadataString("file_type", config().fileType());
61  }
62  string rt;
63  if (!search_translated_all(mdToInherit_, "run_type") &&
64  config().runType(rt)) {
65  addMetadataString("art.run_type", rt);
66  }
67  string grp;
68  if (config().group(grp)) {
69  addMetadataString("group", grp);
70  }
71  string pid;
72  if (config().processID(pid)) {
73  addMetadataString("process_id", pid);
74  }
75  }
76 
77  bool
79  {
80  return checkSyntax_;
81  }
82 
83  void
84  FileCatalogMetadata::addMetadataString(string const& key, string const& value)
85  {
87  }
88 
89  void
90  FileCatalogMetadata::addMetadata(string const& key, string const& value)
91  {
92  RecursiveMutexSentry sentry{mutex_, __func__};
93  if (checkSyntax_) {
94  // rapidjson claims to be largely re-entrant, according to
95  // https://github.com/miloyip/rapidjson/issues/141. Therefore, we
96  // do not worry about locking this part of the function.
97  rapidjson::Document d;
98  string checkString("{ ");
99  checkString += cet::canonical_string(key);
100  checkString += " : ";
101  checkString += value;
102  checkString += " }";
103  if (d.Parse(checkString.c_str()).HasParseError()) {
104  auto const nSpaces = d.GetErrorOffset();
106  << "FileCatalogMetadata::addMetadata() JSON syntax error:\n"
107  << rapidjson::GetParseError_En(d.GetParseError())
108  << " Faulty key/value clause:\n"
109  << checkString << "\n"
110  << (nSpaces ? string(nSpaces, '-') : "") << "^\n";
111  }
112  }
113  md_.emplace_back(key, value);
114  }
115 
116  void
118  {
119  RecursiveMutexSentry sentry{mutex_, __func__};
120  if (mdToInherit_.empty()) {
121  return;
122  }
123  if (!imd_) {
124  imd_ = make_unique<InheritedMetadata>(mdToInherit_, mdFromInput);
125  } else {
126  imd_->check_values(mdFromInput);
127  }
128  for (auto const& pr : imd_->entries()) {
129  addMetadataString(oldToNew(pr.first), pr.second);
130  }
131  }
132 
133  void
135  {
136  RecursiveMutexSentry sentry{mutex_, __func__};
137  cet::copy_all(md_, back_inserter(coll));
138  }
139 
140 } // namespace art
141 
std::vector< std::string > mdToInherit_
std::vector< std::pair< std::string, std::string >> collection_type
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:88
void setMetadataFromInput(collection_type const &coll)
Definition: config.py:1
hep::concurrency::RecursiveMutex mutex_
void addMetadataString(std::string const &key, std::string const &value)
fileType
Definition: mkDefs.py:113
void sort_all(RandCont &)
const XML_Char * s
Definition: expat.h:262
bool search_all(FwdCont const &, Datum const &)
const XML_Char int const XML_Char * value
Definition: expat.h:331
Float_t d
Definition: plot.C:236
auto transform_all(Container &, OutputIt, UnaryOp)
std::unique_ptr< InheritedMetadata > imd_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
auto copy_all(FwdCont &, FwdIter)
bool canonical_string(std::string const &str, std::string &result)
void addMetadata(std::string const &key, std::string const &value)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
void getMetadata(collection_type &coll) const