ATLAS Offline Software
Loading...
Searching...
No Matches
TrigDBConnectionConfig.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5/*
6 * @file TrigDBConnectionConfig.cxx
7 * @brief Configuration for a Trigger DB connection, with string serialization
8 * @author Ricardo Abreu
9 * $Id: TrigDBConnectionConfig.cxx 569440 2013-11-08 17:32:05Z ricab $
10 */
11
13#include <stdexcept>
14#include <boost/regex.hpp>
15#include <boost/algorithm/string.hpp>
16#include <sstream>
17
19using namespace boost;
20using namespace std;
21
22namespace
23{
24
26 // parameter names //
28 const string TYPE = "type"
29 , SERVER = "server"
30 , SMKEY = "smkey"
31 , HLTKEY = "hltkey"
32 , HLTKEYS = "hltkeys"
33 , LVL1KEY = "lvl1key"
34 , SCHEMA = "schema"
35 , USER = "user"
36 , PWD = "password"
37 , RETRPER = "retrialperiod"
38 , MAXRETR = "maxretrials"
39 , USEFRONT = "usefrontier"
40 ;
41
43 // Element separator //
45 const string SEP = ";";
46
48 // Regex element framgments //
50
51 const string e_pair_open(R"(\s*\‍()") // pair open: possible space + (
52 , e_pair_separate(",") // pair separator
53 , e_pair_close(R"(\)\s*)") // pair close: ) + possible space
54 , e_uint_core_dec("\\d+") // simple positive decimal
55 , e_uint_core_oct("0[0-7]+") // simple positive octal
56 , e_uint_core_hex("0x[\\da-f]+") // simple positive hex
57 // (needs case insensitive)
58 , e_uint_neg_zero("-0+|-0x0+") // exceptional zeros (with minus)
59 , e_list_open(R"(\s*\[)") // list open: possible space + [
60 , e_list_separate(",") // list separator
61 , e_list_close(R"(\]\s*)") // list close: ] + possible space
62
63 , e_param_open(R"((?:^|)" // start of line or
64 + SEP + "|" // separator or
65 R"((?<=)" // past
66 + SEP + "" // separator
67 R"())\s*)") // all followed by possible space
68 // (needed when iterating over
69 // successive parameters)
70 , e_param_eq("\\s*=") // separates param name from val
71 , e_param_close("(?:" // (open non capture group)
72 + SEP + // either separator or
73 "|$)") // end of line (close group)
74 , e_param_val(R"(\s*(.*?)\s*)") // captured value surrounded by
75 // possible spaces
76 ;
77
79 // Regex combination //
81
82 // Non negative integer: possible spaces, followed by one of the following:
83 // - possible plus sign followed by hex, oct, or dec representation;
84 // - zero representations with minus sign;
85 // Followed by possible spaces
86 const string e_uint(R"(\s*(?:\+?(?:)" + e_uint_core_hex + "|"
87 + e_uint_core_oct + "|"
88 + e_uint_core_dec + ")|"
89 + e_uint_neg_zero + R"()\s*)");
90
91 // A pair of non negative integers, with a marking group for each of them
92 const string e_pair(e_pair_open + "(" + e_uint + ")"
93 + e_pair_separate + "(" + e_uint + ")"
94 + e_pair_close);
95
96 // an empty list
97 const string e_list_empty(e_list_open + "\\s*" + e_list_close);
98 // a (possibly empty) list of pairs of non negative integers
99 const string e_list(e_list_open + e_pair
100 + "(?:" + e_list_separate + e_pair + ")*"
101 + e_list_close + "|" + e_list_empty);
102
103 // actual regexes
104 const regex re_uint(e_uint),
105 re_pair(e_pair),
106 re_list(e_list);
107
109 unsigned int getUInt(const string& str, const string& aname)
110 {
111 int ret = stoi(str, nullptr, 0);
112 if(ret < 0)
113 throw invalid_argument("The string specifying the " + aname + " does not "
114 "represent a valid non-negative integer: " + str);
115
116 return ret;
117 }
118
120 PSKeys parseHltPsKeys(const string& str)
121 {
122 PSKeys ret;
123 // first validade the format
124 if(regex_match(str, re_list))
125 // iterate over the pairs that are contained in the list
126 for(sregex_iterator it = make_regex_iterator(str, re_pair);
127 it != sregex_iterator(); ++it)
128 // push back a pair with the two captured integers
129 ret.push_back(make_pair(stoi((*it)[1], nullptr, 0),
130 stoi((*it)[2], nullptr, 0)));
131 else
132 throw invalid_argument("The string specifying the HLT PS Keys is badly "
133 "formated: " + str);
134 return ret;
135 }
136
138 string extractValue(const string& name, const string& str)
139 {
140 string val;
141 regex re_param(e_param_open
142 + name
143 + e_param_eq
144 + e_param_val
145 + e_param_close);
146
147 // get to the last occurrence
148 for(sregex_iterator it = make_regex_iterator(str, re_param);
149 it != sregex_iterator(); ++it)
150 val = (*it)[1]; // if no match, we never get here and end up returning ""
151
152 return val;
153 }
154
156 template <typename T>
157 void outputParam(ostringstream& oss, const string& name, const T& value,
158 bool first=false)
159 {
160 if(!first)
161 oss << SEP;
162 oss << name << "=" << value;
163 }
164}
165
169 const string& server,
170 unsigned int smKey,
171 const string& hltPsKeyStr)
172 : TrigDBConnectionConfig(type, server, smKey)
173{
174 setHltKeysFromStr(hltPsKeyStr);
175}
176
179TrigDBConnectionConfig::TrigDBConnectionConfig(const string& connectionStr)
180{
181 diggestStr(connectionStr);
182}
183
186{
187 ostringstream oss;
188 outputParam(oss, TYPE, typeToString(), true);
189 outputParam(oss, SERVER, m_server);
190 outputParam(oss, SMKEY, m_smkey);
191 outputParam(oss, LVL1KEY, m_lvl1key);
192 outputParam(oss, SCHEMA, m_schema);
193 outputParam(oss, USER, m_user);
194 outputParam(oss, PWD, m_password);
195 outputParam(oss, RETRPER, m_retrialPeriod);
196 outputParam(oss, MAXRETR, m_maxRetrials);
197 outputParam(oss, USEFRONT, m_useFrontier);
198
199 if(m_hltkey)
200 outputParam(oss, HLTKEY, m_hltkey);
201 else
202 outputParam(oss, HLTKEYS, hltKeysToString());
203
204 return oss.str();
205}
206
209{
210 string val;
211
212 // for each attribute, if a value was provided in the connectionString, set it
213 if(!(val = extractValue(TYPE, str)).empty())
214 setTypeFromStr(val);
215 if(!(val = extractValue(SERVER, str)).empty())
216 m_server = val;
217 if(!(val = extractValue(SMKEY, str)).empty())
218 setSmKeyFromStr(val);
219 if(!(val = extractValue(LVL1KEY, str)).empty())
221 if(!(val = extractValue(SCHEMA, str)).empty())
222 m_schema = val;
223 if(!(val = extractValue(USER, str)).empty())
224 m_user = val;
225 if(!(val = extractValue(PWD, str)).empty())
226 m_password = val;
227 if(!(val = extractValue(RETRPER, str)).empty())
229 if(!(val = extractValue(MAXRETR, str)).empty())
231 if(!(val = extractValue(USEFRONT, str)).empty())
233
234 // only one of the following should be specified
235 if(!(val = extractValue(HLTKEY, str)).empty())
237 if(!m_hltkey && !(val = extractValue(HLTKEYS, str)).empty())
239}
240
243{
244 string low = algorithm::to_lower_copy(typeStr);
245 if(low == "oracle")
246 m_type = Oracle;
247 else if(low == "mysql")
248 m_type = MySQL;
249 else if(low == "sqlite")
250 m_type = SQLite;
251 else
253}
254
256void
258{
259 if(regex_match(hltKeyStr, re_uint))
260 m_hltkey = stoi(hltKeyStr, nullptr, 0);
261 else
262 m_hltkeys = parseHltPsKeys(hltKeyStr);
263}
264
267{
268 m_smkey = getUInt(smKeyStr, SMKEY);
269}
270
272void
274{
275 m_lvl1key = getUInt(lvl1KeyStr, LVL1KEY);
276}
277
280TrigDBConnectionConfig::setRetrialPeriodFromStr(const string& retrialPeriodStr)
281{
282 m_retrialPeriod = getUInt(retrialPeriodStr, RETRPER);
283}
284
287TrigDBConnectionConfig::setMaxRetrialsFromStr(const string& maxRetrialsStr)
288{
289 m_maxRetrials = getUInt(maxRetrialsStr, MAXRETR);
290}
291
294TrigDBConnectionConfig::setUseFrontierFromStr(const std::string& useFrontier)
295{
296 m_useFrontier = static_cast<bool>(getUInt(useFrontier, USEFRONT));
297}
298
301{
302 ostringstream oss;
303
304 oss << '[';
305 for(PSKeys::const_iterator it = m_hltkeys.begin();
306 it != m_hltkeys.end(); ++it)
307 {
308 if(it != m_hltkeys.begin())
309 oss << ',';
310 oss << '(' << it->first << ',' << it->second << ')';
311 }
312 oss << ']';
313
314 return oss.str();
315}
TrigConf::TrigDBConnectionConfig::PSKeys PSKeys
#define TYPE(CODE, TYP, IOTYP)
static const Attributes_t empty
void setMaxRetrialsFromStr(const std::string &maxRetrialsStr)
void setHltKeysFromStr(const std::string &hltKeyStr)
void setRetrialPeriodFromStr(const std::string &retrialPeriodStr)
void setTypeFromStr(const std::string &typeStr)
void setSmKeyFromStr(const std::string &smKeyStr)
void setLvl1KeyFromStr(const std::string &lvl1KeyStr)
void setUseFrontierFromStr(const std::string &useFrontier)
void diggestStr(const std::string &str)
STL namespace.