Skip to content

Commit 4fcbd49

Browse files
committed
serialization of matchers
1 parent 4e10467 commit 4fcbd49

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

Framework/Core/include/Framework/DataSpecUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ struct DataSpecUtils {
214214
/// Create an InputSpec from metadata string
215215
static InputSpec fromMetadataString(std::string s);
216216

217+
/// Create a concrete data matcher from serialized string
218+
static ConcreteDataMatcher fromString(std::string s);
219+
217220
/// Get the origin, if available
218221
static std::optional<header::DataOrigin> getOptionalOrigin(InputSpec const& spec);
219222

Framework/Core/include/Framework/DataSpecViews.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ static auto filter_string_params_with(std::string match)
4343
});
4444
}
4545

46+
static auto filter_string_params_starts_with(std::string match)
47+
{
48+
return std::views::filter([match](auto const& param) {
49+
return (param.type == VariantType::String) && (param.name.starts_with(match));
50+
});
51+
}
52+
4653
static auto input_to_output_specs()
4754
{
4855
return std::views::transform([](auto const& input) {

Framework/Core/include/Framework/InputRecord.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,22 @@ class InputRecord
513513
}
514514

515515
template <typename T>
516-
requires(std::same_as<T, TableConsumer>)
516+
requires(std::same_as<T, DataRef>)
517517
decltype(auto) get(ConcreteDataMatcher matcher, int part = 0)
518518
{
519519
auto pos = getPos(matcher);
520520
if (pos < 0) {
521521
auto msg = describeAvailableInputs();
522-
throw runtime_error_f("InputRecord::get: no input with binding %s found. %s", DataSpecUtils::describe(matcher), msg.c_str());
522+
throw runtime_error_f("InputRecord::get: no input with binding %s found. %s", DataSpecUtils::describe(matcher).c_str(), msg.c_str());
523523
}
524-
auto ref = getByPos(pos, part);
524+
return getByPos(pos, part);
525+
}
526+
527+
template <typename T>
528+
requires(std::same_as<T, TableConsumer>)
529+
decltype(auto) get(ConcreteDataMatcher matcher, int part = 0)
530+
{
531+
auto ref = get<DataRef>(matcher, part);
525532
auto data = reinterpret_cast<uint8_t const*>(ref.payload);
526533
return std::make_unique<TableConsumer>(data, DataRefUtils::getPayloadSize(ref));
527534
}

Framework/Core/src/DataSpecUtils.cxx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,39 @@ InputSpec DataSpecUtils::fromMetadataString(std::string s)
669669
if (std::distance(words, std::sregex_iterator()) != 4) {
670670
throw runtime_error_f("Malformed input spec metadata: %s", s.c_str());
671671
}
672-
std::vector<std::string> data;
672+
std::array<std::string, 4> data;
673+
auto pos = 0;
673674
for (auto i = words; i != std::sregex_iterator(); ++i) {
674-
data.emplace_back(i->str());
675+
data[pos] = i->str();
676+
++pos;
675677
}
676678
char origin[4];
677679
char description[16];
678680
std::memcpy(&origin, data[1].c_str(), 4);
679681
std::memcpy(&description, data[2].c_str(), 16);
680682
auto version = static_cast<o2::header::DataHeader::SubSpecificationType>(std::atoi(data[3].c_str()));
681-
return InputSpec{data[0], header::DataOrigin{origin}, header::DataDescription{description}, version, Lifetime::Timeframe};
683+
return {data[0], header::DataOrigin{origin}, header::DataDescription{description}, version, Lifetime::Timeframe};
684+
}
685+
686+
ConcreteDataMatcher DataSpecUtils::fromString(std::string s)
687+
{
688+
std::regex word_regex("(\\w+)");
689+
auto words = std::sregex_iterator(s.begin(), s.end(), word_regex);
690+
if (std::distance(words, std::sregex_iterator()) != 3) {
691+
throw runtime_error_f("Malformed serialized matcher: %s", s.c_str());
692+
}
693+
std::array<std::string, 3> data;
694+
auto pos = 0;
695+
for (auto i = words; i != std::sregex_iterator(); ++i) {
696+
data[pos] = i->str();
697+
++pos;
698+
}
699+
char origin[4];
700+
char description[16];
701+
std::memcpy(&origin, data[0].c_str(), 4);
702+
std::memcpy(&description, data[1].c_str(), 16);
703+
auto version = static_cast<o2::header::DataHeader::SubSpecificationType>(std::atoi(data[2].c_str()));
704+
return {header::DataOrigin{origin}, header::DataDescription{description}, version};
682705
}
683706

684707
std::optional<header::DataOrigin> DataSpecUtils::getOptionalOrigin(InputSpec const& spec)

Framework/Core/src/IndexJSONHelpers.cxx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct IndexRecordsReader : public rapidjson::BaseReaderHandler<rapidjson::UTF8<
4141
std::string currentKey;
4242
std::string label;
4343
std::string columnLabel;
44+
std::string matcherStr;
4445
o2::soa::IndexKind kind;
4546
int pos;
4647

@@ -87,6 +88,9 @@ struct IndexRecordsReader : public rapidjson::BaseReaderHandler<rapidjson::UTF8<
8788
if (currentKey.compare("label") == 0) {
8889
return true;
8990
}
91+
if (currentKey.compare("matcher") == 0) {
92+
return true;
93+
}
9094
if (currentKey.compare("column") == 0) {
9195
return true;
9296
}
@@ -127,7 +131,7 @@ struct IndexRecordsReader : public rapidjson::BaseReaderHandler<rapidjson::UTF8<
127131
if (states.top() == State::IN_RECORD) {
128132
states.pop();
129133
// add a record
130-
records.emplace_back(label, columnLabel, kind, pos);
134+
records.emplace_back(label, DataSpecUtils::fromString(matcherStr), columnLabel, kind, pos);
131135
return true;
132136
}
133137

@@ -175,6 +179,10 @@ struct IndexRecordsReader : public rapidjson::BaseReaderHandler<rapidjson::UTF8<
175179
columnLabel = str;
176180
return true;
177181
}
182+
if (currentKey.compare("matcher") == 0) {
183+
matcherStr = str;
184+
return true;
185+
}
178186
}
179187

180188
states.push(State::IN_ERROR);
@@ -205,6 +213,8 @@ void writeRecords(rapidjson::Writer<rapidjson::OStreamWrapper>& w, std::vector<o
205213
w.StartObject();
206214
w.Key("label");
207215
w.String(r.label.c_str());
216+
w.Key("matcher");
217+
w.String(DataSpecUtils::describe(r.matcher).c_str());
208218
w.Key("column");
209219
w.String(r.columnLabel.c_str());
210220
w.Key("kind");

0 commit comments

Comments
 (0)