MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
FlowOptionsParser.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
5#include <picojson/picojson.h>
6#include <mxl/mxl.h>
9
10namespace mxl::lib
11{
12 FlowOptionsParser::FlowOptionsParser(std::string const& in_options)
13 {
14 if (in_options.empty())
15 {
16 return;
17 }
18
19 //
20 // Parse the json options
21 //
22 auto jsonValue = picojson::value{};
23 auto const err = parseJson(jsonValue, in_options);
24 if (!err.empty())
25 {
26 throw std::invalid_argument{"Invalid JSON options. " + err};
27 }
28
29 // Confirm that the root is a json object
30 if (!jsonValue.is<picojson::object>())
31 {
32 throw std::invalid_argument{"Expected a JSON object"};
33 }
34 _root = jsonValue.get<picojson::object>();
35
36 auto maxCommitBatchSizeHintIt = _root.find("maxCommitBatchSizeHint");
37 if (maxCommitBatchSizeHintIt != _root.end())
38 {
39 if (!maxCommitBatchSizeHintIt->second.is<double>())
40 {
41 throw std::invalid_argument{"maxCommitBatchSizeHint must be a number."};
42 }
43
44 auto const v = jsonToInteger<std::uint32_t>(maxCommitBatchSizeHintIt->second, "maxCommitBatchSizeHint");
45 if (v < 1U)
46 {
47 throw std::invalid_argument{"maxCommitBatchSizeHint must be greater or equal to 1."};
48 }
50 }
51
52 auto maxSyncBatchSizeHintIt = _root.find("maxSyncBatchSizeHint");
53 if (maxSyncBatchSizeHintIt != _root.end())
54 {
55 if (!maxSyncBatchSizeHintIt->second.is<double>())
56 {
57 throw std::invalid_argument{"maxSyncBatchSizeHint must be a number."};
58 }
59
60 auto const v = jsonToInteger<std::uint32_t>(maxSyncBatchSizeHintIt->second, "maxSyncBatchSizeHint");
61 if (v < 1U)
62 {
63 throw std::invalid_argument{"maxSyncBatchSizeHint must be greater or equal to 1."};
64 }
66 if ((_maxSyncBatchSizeHint.value() % _maxCommitBatchSizeHint.value_or(1) != 0))
67 {
68 throw std::invalid_argument{"maxSyncBatchSizeHint must be a multiple of maxCommitBatchSizeHint."};
69 }
70 }
71 }
72
73 std::optional<std::uint32_t> FlowOptionsParser::getMaxCommitBatchSizeHint() const
74 {
76 }
77
78 std::optional<std::uint32_t> FlowOptionsParser::getMaxSyncBatchSizeHint() const
79 {
81 }
82} // namespace mxl::lib
std::optional< std::uint32_t > getMaxCommitBatchSizeHint() const
std::optional< std::uint32_t > getMaxSyncBatchSizeHint() const
std::optional< std::uint32_t > _maxSyncBatchSizeHint
std::optional< std::uint32_t > _maxCommitBatchSizeHint
T jsonToInteger(picojson::value const &value, std::string_view name)
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})