-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDataDescriptorMatcher.cxx
More file actions
524 lines (479 loc) · 17.5 KB
/
DataDescriptorMatcher.cxx
File metadata and controls
524 lines (479 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Framework/CompilerBuiltins.h"
#include "Framework/DataDescriptorMatcher.h"
#include "Framework/DataMatcherWalker.h"
#include "Framework/DataProcessingHeader.h"
#include "Framework/VariantHelpers.h"
#include "Framework/RuntimeError.h"
#include "Headers/DataHeader.h"
#include "Headers/Stack.h"
#include <iostream>
namespace o2::framework::data_matcher
{
ContextElement::Value const& VariableContext::get(size_t pos) const
{
// First we check if there is any pending update
for (size_t i = 0; i < mPerformedUpdates; ++i) {
if (mUpdates[i].position == pos) {
return mUpdates[i].newValue;
}
}
// Otherwise we return the element.
return mElements.at(pos).value;
}
void VariableContext::publish(void (*callback)(VariableContext const&, TimesliceSlot slot, void*), void* context, TimesliceSlot slot)
{
bool anyPublish = false;
for (size_t i = 0; i < MAX_MATCHING_VARIABLE; i++) {
auto& element = mElements[i];
if (element.commitVersion == element.publishVersion) {
continue;
}
element.publishVersion = element.commitVersion;
anyPublish = true;
}
if (anyPublish) {
callback(*this, slot, context);
}
}
void VariableContext::commit()
{
for (size_t i = 0; i < mPerformedUpdates; ++i) {
auto& element = mElements[mUpdates[i].position];
element.value = mUpdates[i].newValue;
element.commitVersion++;
}
mPerformedUpdates = 0;
}
void VariableContext::reset()
{
mPerformedUpdates = 0;
for (auto& element : mElements) {
element.value = None{};
}
}
bool OriginValueMatcher::match(header::DataHeader const& header, VariableContext& context) const
{
if (auto ref = std::get_if<ContextRef>(&mValue)) {
auto& variable = context.get(ref->index);
if (auto value = std::get_if<std::string>(&variable)) {
return strncmp(header.dataOrigin.str, value->c_str(), header::DataOrigin::size) == 0;
}
auto maxSize = strnlen(header.dataOrigin.str, header::DataOrigin::size);
context.put({ref->index, std::string(header.dataOrigin.str, maxSize)});
return true;
} else if (auto s = std::get_if<std::string>(&mValue)) {
return strncmp(header.dataOrigin.str, s->c_str(), header::DataOrigin::size) == 0;
}
throw runtime_error("Mismatching type for variable");
}
bool DescriptionValueMatcher::match(header::DataHeader const& header, VariableContext& context) const
{
if (auto ref = std::get_if<ContextRef>(&mValue)) {
auto& variable = context.get(ref->index);
if (auto value = std::get_if<std::string>(&variable)) {
return strncmp(header.dataDescription.str, value->c_str(), header::DataDescription::size) == 0;
}
auto maxSize = strnlen(header.dataDescription.str, header::DataDescription::size);
context.put({ref->index, std::string(header.dataDescription.str, maxSize)});
return true;
} else if (auto s = std::get_if<std::string>(&this->mValue)) {
return strncmp(header.dataDescription.str, s->c_str(), header::DataDescription::size) == 0;
}
throw runtime_error("Mismatching type for variable");
}
bool SubSpecificationTypeValueMatcher::match(header::DataHeader const& header, VariableContext& context) const
{
if (auto ref = std::get_if<ContextRef>(&mValue)) {
auto& variable = context.get(ref->index);
if (auto value = std::get_if<header::DataHeader::SubSpecificationType>(&variable)) {
return header.subSpecification == *value;
}
context.put({ref->index, header.subSpecification});
return true;
} else if (auto v = std::get_if<header::DataHeader::SubSpecificationType>(&mValue)) {
return header.subSpecification == *v;
}
throw runtime_error("Mismatching type for variable");
}
/// This will match the timing information which is currently in
/// the DataProcessingHeader. Notice how we apply the scale to the
/// actual values found.
bool StartTimeValueMatcher::match(header::DataHeader const& dh, DataProcessingHeader const& dph, VariableContext& context) const
{
if (auto ref = std::get_if<ContextRef>(&mValue)) {
auto& variable = context.get(ref->index);
if (auto value = std::get_if<uint64_t>(&variable)) {
return (dph.startTime / mScale) == *value;
}
context.put({ref->index, dph.startTime / mScale});
// We always put in 12 the creation time
context.put({CREATIONTIME_POS, dph.creation});
// We always put in 13 the runNumber
context.put({RUNNUMBER_POS, dh.runNumber});
// We always put in 14 the tfCounter
context.put({TFCOUNTER_POS, dh.tfCounter});
// We always put in 15 the firstTForbit
context.put({FIRSTTFORBIT_POS, dh.firstTForbit});
return true;
} else if (auto v = std::get_if<uint64_t>(&mValue)) {
return (dph.startTime / mScale) == *v;
}
throw runtime_error("Mismatching type for variable");
}
DataDescriptorMatcher::DataDescriptorMatcher(DataDescriptorMatcher const& other)
: mOp{other.mOp},
mLeft{ConstantValueMatcher{false}},
mRight{ConstantValueMatcher{false}}
{
if (auto pval0 = std::get_if<OriginValueMatcher>(&other.mLeft)) {
mLeft = *pval0;
} else if (auto pval1 = std::get_if<DescriptionValueMatcher>(&other.mLeft)) {
mLeft = *pval1;
} else if (auto pval2 = std::get_if<SubSpecificationTypeValueMatcher>(&other.mLeft)) {
mLeft = *pval2;
} else if (auto pval3 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&other.mLeft)) {
mLeft = std::move(std::make_unique<DataDescriptorMatcher>(*pval3->get()));
} else if (auto pval4 = std::get_if<ConstantValueMatcher>(&other.mLeft)) {
mLeft = *pval4;
} else if (auto pval5 = std::get_if<StartTimeValueMatcher>(&other.mLeft)) {
mLeft = *pval5;
} else {
std::cerr << (other.mLeft.index() == std::variant_npos) << std::endl;
O2_BUILTIN_UNREACHABLE();
}
if (auto pval0 = std::get_if<OriginValueMatcher>(&other.mRight)) {
mRight = *pval0;
} else if (auto pval1 = std::get_if<DescriptionValueMatcher>(&other.mRight)) {
mRight = *pval1;
} else if (auto pval2 = std::get_if<SubSpecificationTypeValueMatcher>(&other.mRight)) {
mRight = *pval2;
} else if (auto pval3 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&other.mRight)) {
mRight = std::move(std::make_unique<DataDescriptorMatcher>(*pval3->get()));
} else if (auto pval4 = std::get_if<ConstantValueMatcher>(&other.mRight)) {
mRight = *pval4;
} else if (auto pval5 = std::get_if<StartTimeValueMatcher>(&other.mRight)) {
mRight = *pval5;
} else {
O2_BUILTIN_UNREACHABLE();
}
}
DataDescriptorMatcher& DataDescriptorMatcher::operator=(DataDescriptorMatcher const& other)
{
return *this = DataDescriptorMatcher(other);
}
/// Unary operator on a node
DataDescriptorMatcher::DataDescriptorMatcher(Op op, Node&& lhs, Node&& rhs)
: mOp{op},
mLeft{std::move(lhs)},
mRight{std::move(rhs)}
{
}
/// @return true if the (sub-)query associated to this matcher will
/// match the provided @a spec, false otherwise.
bool DataDescriptorMatcher::match(ConcreteDataMatcher const& matcher, VariableContext& context) const
{
header::DataHeader dh;
dh.dataOrigin = matcher.origin;
dh.dataDescription = matcher.description;
dh.subSpecification = matcher.subSpec;
DataProcessingHeader dph;
dph.startTime = 0;
header::Stack s{dh, dph};
return this->match(reinterpret_cast<char const*>(s.data()), context);
}
/// @return true if the (sub-)query associated to this matcher will
/// match the provided @a spec, false otherwise.
bool DataDescriptorMatcher::match(ConcreteDataTypeMatcher const& matcher, VariableContext& context) const
{
header::DataHeader dh;
dh.dataOrigin = matcher.origin;
dh.dataDescription = matcher.description;
dh.subSpecification = 0;
DataProcessingHeader dph;
dph.startTime = 0;
header::Stack s{dh, dph};
return this->match(reinterpret_cast<char const*>(s.data()), context);
}
bool DataDescriptorMatcher::match(header::DataHeader const& header, VariableContext& context) const
{
return this->match(reinterpret_cast<char const*>(&header), context);
}
bool DataDescriptorMatcher::match(header::Stack const& stack, VariableContext& context) const
{
return this->match(reinterpret_cast<char const*>(stack.data()), context);
}
// actual polymorphic matcher which is able to cast the pointer to the correct
// kind of header.
bool DataDescriptorMatcher::match(char const* d, VariableContext& context) const
{
bool leftValue = false, rightValue = false;
// FIXME: Using std::visit is not API compatible due to a new
// exception being thrown. This is the ABI compatible version.
// Replace with:
//
// auto eval = [&d](auto&& arg) -> bool {
// using T = std::decay_t<decltype(arg)>;
// if constexpr (std::is_same_v<T, std::unique_ptr<DataDescriptorMatcher>>) {
// return arg->match(d, context);
// if constexpr (std::is_same_v<T, ConstantValueMatcher>) {
// return arg->match(d);
// } else {
// return arg.match(d, context);
// }
// };
// switch (mOp) {
// case Op::Or:
// return std::visit(eval, mLeft) || std::visit(eval, mRight);
// case Op::And:
// return std::visit(eval, mLeft) && std::visit(eval, mRight);
// case Op::Xor:
// return std::visit(eval, mLeft) ^ std::visit(eval, mRight);
// case Op::Just:
// return std::visit(eval, mLeft);
// case Op::Not:
// return !std::visit(eval, mLeft);
// }
// When we drop support for macOS 10.13
if (auto pval0 = std::get_if<OriginValueMatcher>(&mLeft)) {
auto dh = o2::header::get<header::DataHeader*>(d);
if (dh == nullptr) {
throw runtime_error("Cannot find DataHeader");
}
leftValue = pval0->match(*dh, context);
} else if (auto pval1 = std::get_if<DescriptionValueMatcher>(&mLeft)) {
auto dh = o2::header::get<header::DataHeader*>(d);
if (dh == nullptr) {
throw runtime_error("Cannot find DataHeader");
}
leftValue = pval1->match(*dh, context);
} else if (auto pval2 = std::get_if<SubSpecificationTypeValueMatcher>(&mLeft)) {
auto dh = o2::header::get<header::DataHeader*>(d);
if (dh == nullptr) {
throw runtime_error("Cannot find DataHeader");
}
leftValue = pval2->match(*dh, context);
} else if (auto pval3 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&mLeft)) {
leftValue = (*pval3)->match(d, context);
} else if (auto pval4 = std::get_if<ConstantValueMatcher>(&mLeft)) {
leftValue = pval4->match();
} else if (auto pval5 = std::get_if<StartTimeValueMatcher>(&mLeft)) {
auto dh = o2::header::get<header::DataHeader*>(d);
auto dph = o2::header::get<DataProcessingHeader*>(d);
if (dph == nullptr) {
throw runtime_error("Cannot find DataProcessingHeader");
}
leftValue = pval5->match(*dh, *dph, context);
} else {
throw runtime_error("Bad parsing tree");
}
// Common speedup.
if (mOp == Op::And && leftValue == false) {
return false;
}
if (mOp == Op::Or && leftValue == true) {
return true;
}
if (mOp == Op::Just) {
return leftValue;
}
if (mOp == Op::Not) {
return !leftValue;
}
if (auto pval0 = std::get_if<OriginValueMatcher>(&mRight)) {
auto dh = o2::header::get<header::DataHeader*>(d);
rightValue = pval0->match(*dh, context);
} else if (auto pval1 = std::get_if<DescriptionValueMatcher>(&mRight)) {
auto dh = o2::header::get<header::DataHeader*>(d);
rightValue = pval1->match(*dh, context);
} else if (auto pval2 = std::get_if<SubSpecificationTypeValueMatcher>(&mRight)) {
auto dh = o2::header::get<header::DataHeader*>(d);
rightValue = pval2->match(*dh, context);
} else if (auto pval3 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&mRight)) {
rightValue = (*pval3)->match(d, context);
} else if (auto pval4 = std::get_if<ConstantValueMatcher>(&mRight)) {
rightValue = pval4->match();
} else if (auto pval5 = std::get_if<StartTimeValueMatcher>(&mRight)) {
auto dh = o2::header::get<header::DataHeader*>(d);
auto dph = o2::header::get<DataProcessingHeader*>(d);
rightValue = pval5->match(*dh, *dph, context);
}
// There are cases in which not having a rightValue might be legitimate,
// so we do not throw an exception.
switch (mOp) {
case Op::Or:
return leftValue || rightValue;
case Op::And:
return leftValue && rightValue;
case Op::Xor:
return leftValue ^ rightValue;
case Op::Just:
return leftValue;
case Op::Not:
return !leftValue;
}
throw runtime_error("Bad parsing tree");
};
bool DataDescriptorMatcher::operator==(DataDescriptorMatcher const& other) const
{
if (other.mOp != this->mOp) {
return false;
}
bool leftValue = false;
{
auto v1 = std::get_if<OriginValueMatcher>(&this->mLeft);
auto v2 = std::get_if<OriginValueMatcher>(&other.mLeft);
if (v1 && v2 && *v1 == *v2) {
leftValue = true;
}
}
{
auto v1 = std::get_if<DescriptionValueMatcher>(&this->mLeft);
auto v2 = std::get_if<DescriptionValueMatcher>(&other.mLeft);
if (v1 && v2 && *v1 == *v2) {
leftValue = true;
}
}
{
auto v1 = std::get_if<SubSpecificationTypeValueMatcher>(&this->mLeft);
auto v2 = std::get_if<SubSpecificationTypeValueMatcher>(&other.mLeft);
if (v1 && v2 && *v1 == *v2) {
leftValue = true;
}
}
{
auto v1 = std::get_if<ConstantValueMatcher>(&this->mLeft);
auto v2 = std::get_if<ConstantValueMatcher>(&other.mLeft);
if (v1 && v2 && *v1 == *v2) {
leftValue = true;
}
}
{
auto v1 = std::get_if<StartTimeValueMatcher>(&this->mLeft);
auto v2 = std::get_if<StartTimeValueMatcher>(&other.mLeft);
if (v1 && v2 && *v1 == *v2) {
leftValue = true;
}
}
{
auto v1 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&this->mLeft);
auto v2 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&other.mLeft);
if (v1 && v2 && v1->get() && v2->get() && (**v1 == **v2)) {
leftValue = true;
}
}
// Shortcut the fact that the left side is different.
if (leftValue == false) {
return false;
}
if (mOp == Op::Just) {
return leftValue;
}
if (mOp == Op::Not) {
return leftValue;
}
{
auto v1 = std::get_if<OriginValueMatcher>(&this->mRight);
auto v2 = std::get_if<OriginValueMatcher>(&other.mRight);
if (v1 && v2 && *v1 == *v2) {
return true;
}
}
{
auto v1 = std::get_if<DescriptionValueMatcher>(&this->mRight);
auto v2 = std::get_if<DescriptionValueMatcher>(&other.mRight);
if (v1 && v2 && *v1 == *v2) {
return true;
}
}
{
auto v1 = std::get_if<SubSpecificationTypeValueMatcher>(&this->mRight);
auto v2 = std::get_if<SubSpecificationTypeValueMatcher>(&other.mRight);
if (v1 && v2 && *v1 == *v2) {
return true;
}
}
{
auto v1 = std::get_if<ConstantValueMatcher>(&this->mRight);
auto v2 = std::get_if<ConstantValueMatcher>(&other.mRight);
if (v1 && v2 && *v1 == *v2) {
return true;
}
}
{
auto v1 = std::get_if<StartTimeValueMatcher>(&this->mRight);
auto v2 = std::get_if<StartTimeValueMatcher>(&other.mRight);
if (v1 && v2 && *v1 == *v2) {
return true;
}
}
{
auto v1 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&this->mRight);
auto v2 = std::get_if<std::unique_ptr<DataDescriptorMatcher>>(&other.mRight);
if (v1 && v2 && v1->get() && v2->get() && (**v1 == **v2)) {
return true;
}
}
// We alredy know the left side is true.
return false;
}
std::ostream& operator<<(std::ostream& os, DataDescriptorMatcher const& matcher)
{
auto edgeWalker = overloaded{
[&os](EdgeActions::EnterNode action) {
os << "(" << action.node->mOp;
if (action.node->mOp == DataDescriptorMatcher::Op::Just ||
action.node->mOp == DataDescriptorMatcher::Op::Not) {
return ChildAction::VisitLeft;
}
return ChildAction::VisitBoth;
},
[&os](EdgeActions::EnterLeft) { os << " "; },
[&os](EdgeActions::ExitLeft) { os << " "; },
[&os](EdgeActions::EnterRight) { os << " "; },
[&os](EdgeActions::ExitRight) { os << " "; },
[&os](EdgeActions::ExitNode) { os << ")"; },
[&os](auto) {}};
auto leafWalker = overloaded{
[&os](OriginValueMatcher const& origin) { os << "origin:" << origin; },
[&os](DescriptionValueMatcher const& description) { os << "description:" << description; },
[&os](SubSpecificationTypeValueMatcher const& subSpec) { os << "subSpec:" << subSpec; },
[&os](StartTimeValueMatcher const& startTime) { os << "startTime:" << startTime; },
[&os](ConstantValueMatcher const& constant) {},
[&os](auto t) { os << "not implemented " << typeid(decltype(t)).name(); }};
DataMatcherWalker::walk(matcher,
edgeWalker,
leafWalker);
return os;
}
std::ostream& operator<<(std::ostream& os, DataDescriptorMatcher::Op const& op)
{
switch (op) {
case DataDescriptorMatcher::Op::And:
os << "and";
break;
case DataDescriptorMatcher::Op::Or:
os << "or";
break;
case DataDescriptorMatcher::Op::Just:
os << "just";
break;
case DataDescriptorMatcher::Op::Not:
os << "not";
break;
case DataDescriptorMatcher::Op::Xor:
os << "xor";
break;
}
return os;
}
} // namespace o2::framework::data_matcher