Project
Loading...
Searching...
No Matches
DataDescriptorQueryBuilder.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14#include "Framework/InputSpec.h"
15
16#include <memory>
17#include <optional>
18#include <string>
19#include <vector>
20#include <regex>
21#include <iostream>
22
23using namespace o2::framework::data_matcher;
24
25namespace o2::framework
26{
27
54
55std::vector<InputSpec> DataDescriptorQueryBuilder::parse(char const* config)
56{
57 std::vector<QueryBuilderState> states;
58 states.push_back(IN_BEGIN_QUERY);
59
60 std::vector<InputSpec> result;
61 char const* next = config;
62 char const* cur = config;
63 char const* expectedSeparators;
64 char* endptr;
65 std::string errorString;
66 std::unique_ptr<DataDescriptorMatcher> currentMatcher;
67 std::vector<data_matcher::Node> nodes;
68 std::optional<std::string> currentBinding;
69 std::optional<std::string> currentOrigin;
70 std::optional<std::string> currentDescription;
71 std::optional<std::string> currentKey;
72 std::optional<std::string> currentValue;
73 std::optional<header::DataHeader::SubSpecificationType> currentSubSpec;
74 std::optional<uint64_t> currentTimeModulo;
75 Lifetime currentLifetime = Lifetime::Timeframe;
76 size_t currentNumber;
77
78 auto error = [&errorString, &states](std::string const& s) {
79 errorString = s;
80 states.push_back(IN_ERROR);
81 };
82
83 auto pushState = [&states](QueryBuilderState state) {
84 states.push_back(state);
85 };
86
87 auto checkModifier = [&states, &cur, &next, &error](QueryBuilderState state, const char* mod) {
88 const char* modifier = nullptr;
89 if (mod[0] != '\0' && (modifier = strpbrk(cur, mod)) != nullptr) {
90 switch (*modifier) {
91 case '!':
92 states.push_back(IN_NEGATION);
93 break;
94 default:
95 error("invalid modifier '" + std::string(modifier, 1) + "'");
96 return;
97 }
98 next = ++cur;
99 }
100 states.push_back(state);
101 };
102
103 auto token = [&states, &expectedSeparators](QueryBuilderState state, char const* sep) {
104 states.push_back(state);
105 expectedSeparators = sep;
106 };
107
108 auto assignLastStringMatch = [&next, &cur, &error, &pushState](std::string const& what, int maxSize, std::optional<std::string>& s, QueryBuilderState nextState) {
109 if ((next - cur == 0) || (next - cur > maxSize)) {
110 error(what + " needs to be between 1 and " + std::to_string(maxSize) + " char long");
111 return false;
112 }
113 s = std::string(cur, next - cur);
114 if (memchr("\0;", *next, 2)) {
115 pushState(IN_END_MATCHER);
116 cur = next;
117 return true;
118 }
119 pushState(nextState);
120 cur = next + 1;
121 return true;
122 };
123
124 auto assignLastNumericMatch = [&next, &cur, &error, &pushState, &currentNumber](std::string const&, auto& value,
125 QueryBuilderState nextState) {
126 if ((next - cur == 0)) {
127 error("number expected");
128 return false;
129 }
130 value = currentNumber;
131 if (memchr("\0;", *next, 2)) {
132 pushState(IN_END_MATCHER);
133 cur = next;
134 return true;
135 }
136 pushState(nextState);
137 cur = next + 1;
138 return true;
139 };
140
141 std::vector<ConfigParamSpec> attributes;
142
143 auto buildMatchingTree = [&nodes](std::string const& binding, std::vector<ConfigParamSpec> attributes) -> InputSpec {
144 auto lastMatcher =
145 std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Just,
147 for (size_t ni = 0, ne = nodes.size(); ni < ne; ++ni) {
148 auto& node = nodes[nodes.size() - 1 - ni];
149 auto tmp = std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::And,
150 std::move(node),
151 std::move(lastMatcher));
152 assert(lastMatcher.get() == nullptr);
153 lastMatcher = std::move(tmp);
154 }
155 Lifetime lifetime = Lifetime::Timeframe;
156 for (auto& attribute : attributes) {
157 if (attribute.name == "lifetime" && attribute.defaultValue.get<std::string>() == "condition") {
158 lifetime = Lifetime::Condition;
159 }
160 if (attribute.name == "lifetime" && attribute.defaultValue.get<std::string>() == "sporadic") {
161 lifetime = Lifetime::Sporadic;
162 }
163 }
164 return InputSpec{binding, std::move(*lastMatcher), lifetime, attributes};
165 };
166
167 auto pushMatcher = [&nodes, &states](auto&& matcher) {
168 if (states.empty() == false && states.back() == IN_NEGATION) {
169 states.pop_back();
170 auto notMatcher = std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Xor,
171 std::move(matcher),
173 nodes.push_back(std::move(notMatcher));
174 } else {
175 nodes.push_back(std::move(matcher));
176 }
177 };
178
179 while (states.empty() == false) {
180 auto const state = states.back();
181 states.pop_back();
182
183 switch (state) {
184 case IN_STRING: {
185 next = strpbrk(cur, expectedSeparators);
186 if (next == nullptr) {
187 next = cur + strlen(cur);
188 }
189 } break;
190 case IN_NUMBER: {
191 currentNumber = strtoll(cur, &endptr, 0);
192 if (endptr == cur) {
193 error("Expected a number");
194 }
195 next = endptr;
196 } break;
197 case IN_BEGIN_QUERY: {
198 (*cur == 0) ? pushState(IN_END_QUERY)
199 : pushState(IN_BEGIN_MATCHER);
200 } break;
201 case IN_BEGIN_MATCHER: {
202 nodes.clear();
203 attributes.clear();
204 // Reset per-query state, so a ;-separated query cannot inherit it.
205 currentBinding.reset();
206 currentOrigin.reset();
207 currentDescription.reset();
208 currentSubSpec.reset();
209 currentTimeModulo.reset();
210 currentKey.reset();
211 currentValue.reset();
212 pushState(IN_BEGIN_BINDING);
213 } break;
214 case IN_BEGIN_BINDING: {
215 pushState(IN_END_BINDING);
216 token(IN_STRING, ":/;,");
217 } break;
218 case IN_END_BINDING: {
219 // We are at the end of the string already.
220 // This is really an origin...
221 if (memchr("\0/;", *next, 3)) {
222 pushState(IN_END_ORIGIN);
223 continue;
224 }
225 if (next - cur == 0) {
226 error("empty binding string");
227 continue;
228 }
229 currentBinding = std::string(cur, next - cur);
230 pushState(IN_BEGIN_ORIGIN);
231 cur = next + 1;
232 } break;
233 case IN_BEGIN_ORIGIN: {
234 pushState(IN_END_ORIGIN);
235 token(IN_STRING, "/;?");
236 } break;
237 case IN_END_ORIGIN: {
238 if (*next == '/' && assignLastStringMatch("origin", 4, currentOrigin, IN_BEGIN_DESCRIPTION)) {
239 nodes.push_back(OriginValueMatcher{*currentOrigin});
240 } else if (*next == ';' && assignLastStringMatch("origin", 4, currentOrigin, IN_END_MATCHER)) {
241 nodes.push_back(OriginValueMatcher{*currentOrigin});
242 } else if (*next == '?' && assignLastStringMatch("origin", 4, currentOrigin, IN_BEGIN_ATTRIBUTES)) {
243 nodes.push_back(OriginValueMatcher{*currentOrigin});
244 } else if (*next == '\0' && assignLastStringMatch("origin", 4, currentOrigin, IN_END_MATCHER)) {
245 nodes.push_back(OriginValueMatcher{*currentOrigin});
246 } else {
247 error("origin needs to be between 1 and 4 char long");
248 }
249 } break;
251 pushState(IN_END_DESCRIPTION);
252 token(IN_STRING, "/;?");
253 } break;
254 case IN_END_DESCRIPTION: {
255 if (*next == '/' && assignLastStringMatch("description", 16, currentDescription, IN_BEGIN_SUBSPEC)) {
256 nodes.push_back(DescriptionValueMatcher{*currentDescription});
257 } else if (*next == ';' && assignLastStringMatch("description", 16, currentDescription, IN_END_MATCHER)) {
258 nodes.push_back(DescriptionValueMatcher{*currentDescription});
259 } else if (*next == '?' && assignLastStringMatch("description", 16, currentDescription, IN_BEGIN_ATTRIBUTES)) {
260 nodes.push_back(DescriptionValueMatcher{*currentDescription});
261 } else if (*next == '\0' && assignLastStringMatch("description", 16, currentDescription, IN_END_MATCHER)) {
262 nodes.push_back(DescriptionValueMatcher{*currentDescription});
263 } else {
264 error("description needs to be between 1 and 16 char long");
265 }
266 } break;
267 case IN_BEGIN_SUBSPEC: {
268 checkModifier(IN_END_SUBSPEC, "!");
269 token(IN_NUMBER, ";%?");
270 } break;
271 case IN_END_SUBSPEC: {
272 if (*next == '%' && assignLastNumericMatch("subspec", currentSubSpec, IN_BEGIN_TIMEMODULO)) {
273 } else if (*next == '?' && assignLastNumericMatch("subspec", currentSubSpec, IN_BEGIN_ATTRIBUTES)) {
274 } else if (*next == ';' && assignLastNumericMatch("subspec", currentSubSpec, IN_END_MATCHER)) {
275 } else if (*next == '\0' && assignLastNumericMatch("subspec", currentSubSpec, IN_END_MATCHER)) {
276 } else {
277 error("Expected a number");
278 break;
279 }
280 auto backup = states.back();
281 states.pop_back();
282 pushMatcher(SubSpecificationTypeValueMatcher{*currentSubSpec});
283 states.push_back(backup);
284 } break;
285 case IN_BEGIN_TIMEMODULO: {
286 pushState(IN_END_TIMEMODULO);
287 token(IN_NUMBER, ";?");
288 } break;
289 case IN_END_TIMEMODULO: {
290 assignLastNumericMatch("timemodulo", currentTimeModulo, IN_ERROR);
291 } break;
292 case IN_END_MATCHER: {
293 if (*cur == ';' && *(cur + 1) == '\0') {
294 error("Remove trailing ;");
295 continue;
296 }
297 // A query without a binding, e.g. "TST/A1", is named after its origin.
298 result.push_back(buildMatchingTree(currentBinding.value_or(currentOrigin.value_or("")), attributes));
299 if (*cur == '\0') {
300 pushState(IN_END_QUERY);
301 } else if (*cur == ';') {
302 cur += 1;
303 pushState(IN_BEGIN_MATCHER);
304 } else {
305 error("Unexpected character " + std::string(cur, 1));
306 }
307 } break;
308 case IN_BEGIN_ATTRIBUTES: {
309 pushState(IN_BEGIN_KEY);
310 } break;
311 case IN_BEGIN_KEY: {
312 pushState(IN_END_KEY);
313 token(IN_STRING, "=");
314 } break;
315 case IN_END_KEY: {
316 if (*next == '=') {
317 assignLastStringMatch("key", 1000, currentKey, IN_BEGIN_VALUE);
318 } else {
319 error("missing value for attribute key");
320 }
321 } break;
322 case IN_BEGIN_VALUE: {
323 pushState(IN_END_VALUE);
324 token(IN_STRING, "&;");
325 } break;
326 case IN_END_VALUE: {
327 QueryBuilderState nextState;
328 if (*next == '&') {
329 nextState = IN_BEGIN_KEY;
330 } else if (*next == ';' || *next == '\0') {
331 nextState = IN_END_ATTRIBUTES;
332 } else {
333 error("missing value for string value");
334 continue;
335 }
336 if (!assignLastStringMatch("value", 1000, currentValue, nextState)) {
337 continue; // The error is pushed and currentValue is not set.
338 }
339 if (*currentKey == "lifetime" && currentValue == "condition") {
340 currentLifetime = Lifetime::Condition;
341 }
342 if (*currentKey == "ccdb-run-dependent") {
343 int val = currentValue == "false" ? 0 : (currentValue == "true" ? 1 : std::stoi(*currentValue));
344 attributes.push_back(ConfigParamSpec{*currentKey, VariantType::Int, val, {}});
345 } else {
346 attributes.push_back(ConfigParamSpec{*currentKey, VariantType::String, *currentValue, {}});
347 }
348 } break;
349 case IN_END_ATTRIBUTES: {
350 pushState(IN_END_MATCHER);
351 } break;
352 case IN_NEGATION: {
353 error("property modifiers should have been handled before when inserting previous matcher");
354 } break;
355 case IN_ERROR: {
356 throw std::runtime_error("Parse error: " + errorString);
357 } break;
358 case IN_END_QUERY: {
359 } break;
360 default: {
361 error("Unhandled state");
362 } break;
363 }
364 }
365 return std::move(result);
366}
367
369{
370 static const std::regex delim(",");
371
372 std::sregex_token_iterator end;
373 std::sregex_token_iterator iter(config.begin(),
374 config.end(),
375 delim,
376 -1);
377
378 std::unique_ptr<DataDescriptorMatcher> next, result;
379
380 for (; iter != end; ++iter) {
381 std::smatch m;
382 auto s = iter->str();
383 auto newNode = buildNode(s);
384
385 if (result.get() == nullptr) {
386 result = std::move(newNode);
387 } else {
388 next = std::move(std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Or,
389 std::move(result),
390 std::move(newNode)));
391 result = std::move(next);
392 }
393 }
394
395 return std::move(DataDescriptorQuery{{}, std::move(result)});
396}
397
399{
400 static const std::regex delim1(",");
401 static const std::regex delim2(":");
402
403 std::sregex_token_iterator end;
404 std::sregex_token_iterator iter1(config.begin(),
405 config.end(),
406 delim1,
407 -1);
408
409 std::unique_ptr<DataDescriptorMatcher> next, result;
410
411 // looping over ','-separated items
412 for (; iter1 != end; ++iter1) {
413 auto s = iter1->str();
414
415 // get first part of item
416 std::sregex_token_iterator iter2(s.begin(),
417 s.end(),
418 delim2,
419 -1);
420 if (iter2 == end) {
421 continue;
422 }
423 s = iter2->str();
424
425 // create the corresponding DataDescriptorMatcher
426 std::smatch m;
427 auto newNode = buildNode(s);
428
429 if (result.get() == nullptr) {
430 result = std::move(newNode);
431 } else {
432 next = std::move(std::make_unique<DataDescriptorMatcher>(DataDescriptorMatcher::Op::Or,
433 std::move(result),
434 std::move(newNode)));
435 result = std::move(next);
436 }
437 }
438
439 return std::move(DataDescriptorQuery{{}, std::move(result)});
440}
441
442std::unique_ptr<DataDescriptorMatcher> DataDescriptorQueryBuilder::buildNode(std::string const& nodeString)
443{
444
445 auto m = getTokens(nodeString);
446
447 std::unique_ptr<DataDescriptorMatcher> next;
448 auto newNode = std::make_unique<DataDescriptorMatcher>(
449 DataDescriptorMatcher::Op::And,
451 std::make_unique<DataDescriptorMatcher>(
452 DataDescriptorMatcher::Op::And,
455
456 return newNode;
457}
458
459std::vector<std::string> DataDescriptorQueryBuilder::getTokens(std::string const& nodeString)
460{
461
462 static const std::regex specTokenRE(R"re((\w{1,4})/(\w{1,16})/(\d*))re");
463 std::smatch match;
464
465 std::regex_match(nodeString, match, specTokenRE);
466
467 std::vector<std::string> result;
468 for (size_t i = 0; i < 4; ++i) {
469 result.push_back(match[i].str());
470 }
471 return result;
472}
473
474} // namespace o2::framework
std::string binding
size_t maxSize
std::string currentKey
std::unique_ptr< expressions::Node > node
int32_t i
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
char error[CURL_ERROR_SIZE]
SurfaceTrackState state
Something which can be matched against a header::DataDescription.
Something which can be matched against a header::DataOrigin.
Matcher on actual time, as reported in the DataProcessingHeader.
Something which can be matched against a header::SubSpecificationType.
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
const GLfloat * m
Definition glcorearb.h:4066
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint GLuint end
Definition glcorearb.h:469
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLfloat * val
Definition glcorearb.h:1582
GLuint * states
Definition glcorearb.h:4932
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
Lifetime
Possible Lifetime of objects being exchanged by the DPL.
Definition Lifetime.h:18
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
static std::vector< InputSpec > parse(const char *s="")
static std::unique_ptr< data_matcher::DataDescriptorMatcher > buildNode(std::string const &nodeString)
static DataDescriptorQuery buildFromExtendedKeepConfig(std::string const &config)
deprecated?
static std::vector< std::string > getTokens(std::string const &nodeString)
static DataDescriptorQuery buildFromKeepConfig(std::string const &config)
A typesafe reference to an element of the context.
const std::string str