userver: userver/utils/encoding/tskv_parser_read.hpp File Reference
Loading...
Searching...
No Matches
tskv_parser_read.hpp File Reference

Detailed Description

Definition in file tskv_parser_read.hpp.

Go to the source code of this file.

Namespaces

namespace  utils
 Utilities.
 

Functions

template<typename TagConsumer >
TskvParser::RecordStatus utils::encoding::TskvReadRecord (TskvParser &parser, TagConsumer consumer)
 Read all keys-values for 1 TSKV record.
 

Function Documentation

◆ TskvReadRecord()

template<typename TagConsumer >
TskvParser::RecordStatus utils::encoding::TskvReadRecord ( TskvParser & parser,
TagConsumer consumer )

Read all keys-values for 1 TSKV record.

Parameters
parserparser that should have already found the start of the TSKV record using TskvParser::SkipToRecordBegin
consumera lambda with the signature (const std::string&, const std::string&) -> bool; the strings are temporaries, references to them should not be stored; can return false to skip the record and return immediately

Usage example:

struct Record {
std::vector<std::pair<std::string, std::string>> tags;
};
struct Junk {
std::string data;
};
struct Incomplete {
std::string data;
};
using SingleParsedRecord = std::variant<Record, Junk, Incomplete>;
using ParsedRecords = std::vector<SingleParsedRecord>;
ParsedRecords ParseTskv(std::string_view in) {
ParsedRecords result;
const auto account_incomplete = [&](const char* incomplete_begin) {
const std::string_view remaining(incomplete_begin, in.data() + in.size() - incomplete_begin);
if (!remaining.empty()) {
result.push_back(Incomplete{std::string(remaining)});
}
};
while (true) {
const char* const junk_begin = parser.GetStreamPosition();
const char* const record_begin = parser.SkipToRecordBegin();
if (!record_begin) {
// Note: production code should stop and wait for more data here.
account_incomplete(junk_begin);
break;
}
// Note: accounting for junk in-between records is supported, but is
// typically not important in production.
if (record_begin != junk_begin) {
result.push_back(Junk{std::string(junk_begin, record_begin)});
}
std::vector<std::pair<std::string, std::string>> tags;
const auto status =
utils::encoding::TskvReadRecord(parser, [&](const std::string& key, const std::string& value) {
tags.emplace_back(key, value);
return true;
});
// Note: production code should stop and wait for more data here.
account_incomplete(record_begin);
break;
}
result.push_back(Record{std::move(tags)});
}
return result;
}

Definition at line 38 of file tskv_parser_read.hpp.