ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Trigger
TrigConfiguration
TrigConfBase
src
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
12
#include "
TrigConfBase/TrigDBConnectionConfig.h
"
13
#include <stdexcept>
14
#include <regex>
15
#include <algorithm>
16
#include <cctype>
17
#include <sstream>
18
19
using
PSKeys
=
TrigConf::TrigDBConnectionConfig::PSKeys
;
20
using namespace
std
;
21
22
namespace
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 = sregex_iterator(
str
.begin(),
str
.end(), 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 = sregex_iterator(
str
.begin(),
str
.end(), 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
167
TrigConf::
168
TrigDBConnectionConfig::TrigDBConnectionConfig
(
DBType
type
,
169
const
string
& server,
170
unsigned
int
smKey,
171
const
string
& hltPsKeyStr)
172
:
TrigDBConnectionConfig
(
type
, server, smKey)
173
{
174
setHltKeysFromStr
(hltPsKeyStr);
175
}
176
178
TrigConf::
179
TrigDBConnectionConfig::TrigDBConnectionConfig
(
const
string
& connectionStr)
180
{
181
diggestStr
(connectionStr);
182
}
183
185
string
TrigConf::TrigDBConnectionConfig::toString
()
const
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
208
void
TrigConf::TrigDBConnectionConfig::diggestStr
(
const
string
&
str
)
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
())
220
setLvl1KeyFromStr
(val);
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
())
228
setRetrialPeriodFromStr
(val);
229
if
(!(val = extractValue(MAXRETR,
str
)).
empty
())
230
setMaxRetrialsFromStr
(val);
231
if
(!(val = extractValue(USEFRONT,
str
)).
empty
())
232
setUseFrontierFromStr
(val);
233
234
// only one of the following should be specified
235
if
(!(val = extractValue(HLTKEY,
str
)).
empty
())
236
setHltKeysFromStr
(val);
237
if
(!
m_hltkey
&& !(val = extractValue(HLTKEYS,
str
)).
empty
())
238
setHltKeysFromStr
(val);
239
}
240
242
void
TrigConf::TrigDBConnectionConfig::setTypeFromStr
(
const
string
& typeStr)
243
{
244
string
low = typeStr;
245
transform(low.begin(), low.end(), low.begin(), [](
unsigned
char
c){ return tolower(c); });
246
if
(low ==
"oracle"
)
247
m_type
=
Oracle
;
248
else
if
(low ==
"mysql"
)
249
m_type
=
MySQL
;
250
else
if
(low ==
"sqlite"
)
251
m_type
=
SQLite
;
252
else
253
m_type
=
DBLookup
;
254
}
255
257
void
258
TrigConf::TrigDBConnectionConfig::setHltKeysFromStr
(
const
string
& hltKeyStr)
259
{
260
if
(regex_match(hltKeyStr, re_uint))
261
m_hltkey
= stoi(hltKeyStr,
nullptr
, 0);
262
else
263
m_hltkeys
= parseHltPsKeys(hltKeyStr);
264
}
265
267
void
TrigConf::TrigDBConnectionConfig::setSmKeyFromStr
(
const
string
& smKeyStr)
268
{
269
m_smkey
= getUInt(smKeyStr, SMKEY);
270
}
271
273
void
274
TrigConf::TrigDBConnectionConfig::setLvl1KeyFromStr
(
const
string
& lvl1KeyStr)
275
{
276
m_lvl1key
= getUInt(lvl1KeyStr, LVL1KEY);
277
}
278
280
void
TrigConf::
281
TrigDBConnectionConfig::setRetrialPeriodFromStr
(
const
string
& retrialPeriodStr)
282
{
283
m_retrialPeriod
= getUInt(retrialPeriodStr, RETRPER);
284
}
285
287
void
TrigConf::
288
TrigDBConnectionConfig::setMaxRetrialsFromStr
(
const
string
& maxRetrialsStr)
289
{
290
m_maxRetrials
= getUInt(maxRetrialsStr, MAXRETR);
291
}
292
294
void
TrigConf::
295
TrigDBConnectionConfig::setUseFrontierFromStr
(
const
std::string& useFrontier)
296
{
297
m_useFrontier
=
static_cast<
bool
>
(getUInt(useFrontier, USEFRONT));
298
}
299
301
string
TrigConf::TrigDBConnectionConfig::hltKeysToString
()
const
302
{
303
ostringstream oss;
304
305
oss <<
'['
;
306
for
(PSKeys::const_iterator it =
m_hltkeys
.begin();
307
it !=
m_hltkeys
.end(); ++it)
308
{
309
if
(it !=
m_hltkeys
.begin())
310
oss <<
','
;
311
oss <<
'('
<< it->first <<
','
<< it->second <<
')'
;
312
}
313
oss <<
']'
;
314
315
return
oss.str();
316
}
PSKeys
TrigConf::TrigDBConnectionConfig::PSKeys PSKeys
Definition
TrigDBConnectionConfig.cxx:19
TrigDBConnectionConfig.h
TYPE
#define TYPE(CODE, TYP, IOTYP)
empty
static const Attributes_t empty
Definition
XmlStreamer.cxx:16
TrigConf::TrigDBConnectionConfig::m_useFrontier
bool m_useFrontier
Definition
TrigDBConnectionConfig.h:73
TrigConf::TrigDBConnectionConfig::m_hltkeys
PSKeys m_hltkeys
Definition
TrigDBConnectionConfig.h:66
TrigConf::TrigDBConnectionConfig::m_hltkey
unsigned int m_hltkey
Definition
TrigDBConnectionConfig.h:65
TrigConf::TrigDBConnectionConfig::m_smkey
unsigned int m_smkey
Definition
TrigDBConnectionConfig.h:64
TrigConf::TrigDBConnectionConfig::setMaxRetrialsFromStr
void setMaxRetrialsFromStr(const std::string &maxRetrialsStr)
Definition
TrigDBConnectionConfig.cxx:288
TrigConf::TrigDBConnectionConfig::DBType
DBType
Definition
TrigDBConnectionConfig.h:24
TrigConf::TrigDBConnectionConfig::Oracle
@ Oracle
Definition
TrigDBConnectionConfig.h:24
TrigConf::TrigDBConnectionConfig::MySQL
@ MySQL
Definition
TrigDBConnectionConfig.h:24
TrigConf::TrigDBConnectionConfig::DBLookup
@ DBLookup
Definition
TrigDBConnectionConfig.h:24
TrigConf::TrigDBConnectionConfig::TrigDBConnectionConfig
TrigDBConnectionConfig()=default
TrigConf::TrigDBConnectionConfig::typeToString
std::string typeToString() const
Definition
TrigDBConnectionConfig.h:118
TrigConf::TrigDBConnectionConfig::m_maxRetrials
unsigned int m_maxRetrials
Definition
TrigDBConnectionConfig.h:72
TrigConf::TrigDBConnectionConfig::m_lvl1key
unsigned int m_lvl1key
Definition
TrigDBConnectionConfig.h:67
TrigConf::TrigDBConnectionConfig::m_user
std::string m_user
Definition
TrigDBConnectionConfig.h:69
TrigConf::TrigDBConnectionConfig::setHltKeysFromStr
void setHltKeysFromStr(const std::string &hltKeyStr)
Definition
TrigDBConnectionConfig.cxx:258
TrigConf::TrigDBConnectionConfig::toString
std::string toString() const
Definition
TrigDBConnectionConfig.cxx:185
TrigConf::TrigDBConnectionConfig::setRetrialPeriodFromStr
void setRetrialPeriodFromStr(const std::string &retrialPeriodStr)
Definition
TrigDBConnectionConfig.cxx:281
TrigConf::TrigDBConnectionConfig::setTypeFromStr
void setTypeFromStr(const std::string &typeStr)
Definition
TrigDBConnectionConfig.cxx:242
TrigConf::TrigDBConnectionConfig::m_password
std::string m_password
Definition
TrigDBConnectionConfig.h:70
TrigConf::TrigDBConnectionConfig::setSmKeyFromStr
void setSmKeyFromStr(const std::string &smKeyStr)
Definition
TrigDBConnectionConfig.cxx:267
TrigConf::TrigDBConnectionConfig::m_retrialPeriod
unsigned int m_retrialPeriod
Definition
TrigDBConnectionConfig.h:71
TrigConf::TrigDBConnectionConfig::m_type
DBType m_type
Definition
TrigDBConnectionConfig.h:62
TrigConf::TrigDBConnectionConfig::m_schema
std::string m_schema
Definition
TrigDBConnectionConfig.h:68
TrigConf::TrigDBConnectionConfig::setLvl1KeyFromStr
void setLvl1KeyFromStr(const std::string &lvl1KeyStr)
Definition
TrigDBConnectionConfig.cxx:274
TrigConf::TrigDBConnectionConfig::PSKeys
std::vector< LumiPSPair > PSKeys
Definition
TrigDBConnectionConfig.h:26
TrigConf::TrigDBConnectionConfig::setUseFrontierFromStr
void setUseFrontierFromStr(const std::string &useFrontier)
Definition
TrigDBConnectionConfig.cxx:295
TrigConf::TrigDBConnectionConfig::m_server
std::string m_server
Definition
TrigDBConnectionConfig.h:63
TrigConf::TrigDBConnectionConfig::diggestStr
void diggestStr(const std::string &str)
Definition
TrigDBConnectionConfig.cxx:208
TrigConf::TrigDBConnectionConfig::hltKeysToString
std::string hltKeysToString() const
Definition
TrigDBConnectionConfig.cxx:301
SQLite
Definition
Statement.h:18
std
STL namespace.
str
Definition
BTagTrackIpAccessor.cxx:11
type
Generated on
for ATLAS Offline Software by
1.17.0