|
| 1 | +// Copyright 2019-2026 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 | +#ifndef O2_FRAMEWORK_CCDBPATHTABLE_H_ |
| 12 | +#define O2_FRAMEWORK_CCDBPATHTABLE_H_ |
| 13 | + |
| 14 | +#include <Framework/Logger.h> |
| 15 | + |
| 16 | +#include <cstdint> |
| 17 | +#include <limits> |
| 18 | +#include <string> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace o2::framework |
| 22 | +{ |
| 23 | +// A CCDB path may be declared either as a plain path, or as a mapping from uniformity |
| 24 | +// value to path: "lo-hi=path;lo-hi=path;fallback". Ranges are inclusive and either bound |
| 25 | +// may be omitted ("-hi=path", "lo-=path"). An entry without '=' is an explicit fallback; |
| 26 | +// without one, a value matching no range is an error rather than a silent guess. |
| 27 | +// The mapping is data, carried in the schema metadata, so the fetcher needs no code from |
| 28 | +// the task that declared the column. |
| 29 | +struct PathTable { |
| 30 | + struct Range { |
| 31 | + int64_t lo; |
| 32 | + int64_t hi; |
| 33 | + std::string path; |
| 34 | + }; |
| 35 | + std::vector<Range> ranges; |
| 36 | + std::string fallback; |
| 37 | + bool hasFallback = false; |
| 38 | + |
| 39 | + static PathTable parse(std::string const& spec) |
| 40 | + { |
| 41 | + PathTable table; |
| 42 | + if (spec.find('=') == std::string::npos) { // plain path, the common case |
| 43 | + table.fallback = spec; |
| 44 | + table.hasFallback = true; |
| 45 | + return table; |
| 46 | + } |
| 47 | + size_t pos = 0; |
| 48 | + while (pos <= spec.size()) { |
| 49 | + auto end = spec.find(';', pos); |
| 50 | + auto entry = spec.substr(pos, end == std::string::npos ? std::string::npos : end - pos); |
| 51 | + pos = (end == std::string::npos) ? spec.size() + 1 : end + 1; |
| 52 | + if (entry.empty()) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + auto eq = entry.find('='); |
| 56 | + if (eq == std::string::npos) { |
| 57 | + table.fallback = entry; |
| 58 | + table.hasFallback = true; |
| 59 | + continue; |
| 60 | + } |
| 61 | + auto bounds = entry.substr(0, eq); |
| 62 | + auto dash = bounds.find('-'); |
| 63 | + if (dash == std::string::npos) { |
| 64 | + LOGP(fatal, R"(Malformed CCDB path mapping "{}": expected "lo-hi=path")", entry); |
| 65 | + } |
| 66 | + auto loStr = bounds.substr(0, dash); |
| 67 | + auto hiStr = bounds.substr(dash + 1); |
| 68 | + table.ranges.push_back({loStr.empty() ? std::numeric_limits<int64_t>::min() : std::stoll(loStr), |
| 69 | + hiStr.empty() ? std::numeric_limits<int64_t>::max() : std::stoll(hiStr), |
| 70 | + entry.substr(eq + 1)}); |
| 71 | + } |
| 72 | + return table; |
| 73 | + } |
| 74 | + |
| 75 | + std::string const& resolve(int64_t key, std::string const& column) const |
| 76 | + { |
| 77 | + for (auto const& range : ranges) { |
| 78 | + if (key >= range.lo && key <= range.hi) { |
| 79 | + return range.path; |
| 80 | + } |
| 81 | + } |
| 82 | + if (!hasFallback) { |
| 83 | + LOGP(fatal, R"(No CCDB path declared for {} at uniformity value {}; the declared mapping covers no such value and has no fallback entry)", |
| 84 | + column, key); |
| 85 | + } |
| 86 | + return fallback; |
| 87 | + } |
| 88 | +}; |
| 89 | +} // namespace o2::framework |
| 90 | + |
| 91 | +#endif // O2_FRAMEWORK_CCDBPATHTABLE_H_ |
0 commit comments