FileCatalogMetadata_service.cc
Go to the documentation of this file.
4 #include "rapidjson/document.h"
5 #include "rapidjson/error/en.h"
7 
8 namespace {
9  std::vector<std::string>
10  maybeTranslate(std::vector<std::string> names)
11  {
12  cet::transform_all(names, names.begin(), art::NewToOld{});
13  cet::sort_all(names);
14  return names;
15  }
16 
17  bool
18  search_translated_all(std::vector<std::string> const& s,
19  std::string const& md)
20  {
21  return cet::search_all(s, art::NewToOld{}(md));
22  }
23 }
24 
27  : checkSyntax_{config().checkSyntax()}
28  , mdToInherit_{maybeTranslate(config().metadataFromInput())}
29 {
30  std::string appFam;
31  if (config().applicationFamily(appFam)) {
32  addMetadataString("applicationFamily", appFam);
33  }
34 
35  std::string appVer;
36  if (config().applicationVersion(appVer)) {
37  addMetadataString("applicationVersion", appVer);
38  }
39 
40  // Always write out fileType -- either by inheriting from the input
41  // file or by overriding via the FHiCL parameter "fileType".
42  if (!search_translated_all(mdToInherit_, "file_type")) {
43  addMetadataString("file_type", config().fileType());
44  }
45 
46  std::string rt;
47  if (!search_translated_all(mdToInherit_, "run_type") &&
48  config().runType(rt)) {
49  addMetadataString("run_type", rt);
50  }
51 
52  std::string g;
53  if (config().group(g)) {
54  addMetadataString("group", g);
55  }
56 
58  if (config().processID(pid)) {
59  addMetadataString("process_id", pid);
60  }
61 }
62 
63 void
65  std::string const& value)
66 {
67  if (checkSyntax_) {
68  // rapidjson claims to be largely re-entrant, according to
69  // https://github.com/miloyip/rapidjson/issues/141. Therefore, we
70  // do not worry about locking this part of the function.
71  rapidjson::Document d;
72  std::string checkString("{ ");
73  checkString += cet::canonical_string(key);
74  checkString += " : ";
75  checkString += value;
76  checkString += " }";
77  if (d.Parse(checkString.c_str()).HasParseError()) {
78  auto const nSpaces = d.GetErrorOffset();
80  << "art::FileCatalogMetadata::addMetadata() JSON syntax error:\n"
81  << rapidjson::GetParseError_En(d.GetParseError())
82  << " Faulty key/value clause:\n"
83  << checkString << "\n"
84  << (nSpaces ? std::string(nSpaces, '-') : "") << "^\n";
85  }
86  }
87  std::lock_guard<std::mutex> lock{service_mutex()};
88  md_.emplace_back(key, value);
89 }
90 
91 // Standard constructor / maker is just fine.
FileCatalogMetadata(Parameters const &config)
std::vector< std::string > mdToInherit_
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
Definition: config.py:1
bool search_translated_all(std::vector< std::string > const &s, std::string const &md)
void addMetadataString(std::string const &key, std::string const &value)
fileType
Definition: mkDefs.py:112
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
#define IGNORE_FALLTHROUGH_START
auto transform_all(Container &, OutputIt, UnaryOp)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
bool canonical_string(std::string const &str, std::string &result)
void addMetadata(std::string const &key, std::string const &value)
std::vector< std::string > maybeTranslate(std::vector< std::string > names)
static std::mutex & service_mutex()