MQTTSuite
Loading...
Searching...
No Matches
mqtt::mqttstore::lib::StoragePlan Class Reference

#include <StoragePlan.h>

Collaboration diagram for mqtt::mqttstore::lib::StoragePlan:

Classes

struct  ColumnMapping
struct  Projection

Public Member Functions

const std::vector< Projection > & getProjections () const
std::vector< const Projection * > match (const std::string &topic) const

Static Public Member Functions

static StoragePlan fromFile (const std::string &fileName)
static StoragePlan fromJson (const nlohmann::json &json)

Static Private Member Functions

static bool topicMatches (const std::string &filter, const std::string &topic)

Private Attributes

std::vector< Projectionprojections

Detailed Description

Definition at line 28 of file StoragePlan.h.

Member Function Documentation

◆ fromFile()

StoragePlan mqtt::mqttstore::lib::StoragePlan::fromFile ( const std::string & fileName)
staticnodiscard

Definition at line 106 of file StoragePlan.cpp.

106 {
107 if (fileName.empty()) {
108 return {};
109 }
110
111 std::ifstream planFile(fileName);
112 if (!planFile.is_open()) {
113 throw std::runtime_error("Cannot open mqttstore projection file '" + fileName + "'");
114 }
115
116 nlohmann::json planJson;
117 try {
118 planJson = nlohmann::json::parse(std::string(std::istreambuf_iterator<char>(planFile), std::istreambuf_iterator<char>()));
119 } catch (const nlohmann::json::parse_error& exception) {
120 throw std::runtime_error("Cannot parse mqttstore projection file '" + fileName + "': " + exception.what());
121 }
122
123 try {
124 return fromJson(planJson);
125 } catch (const std::exception& exception) {
126 throw std::runtime_error("Cannot load mqttstore projection file '" + fileName + "': " + exception.what());
127 }
128 }
static StoragePlan fromJson(const nlohmann::json &json)

References fromJson().

Referenced by mqtt::mqttstore::SocketContextFactory::create(), and mqtt::mqttstore::websocket::SubProtocolFactory::create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fromJson()

StoragePlan mqtt::mqttstore::lib::StoragePlan::fromJson ( const nlohmann::json & json)
staticnodiscard

Definition at line 130 of file StoragePlan.cpp.

130 {
132
133 StoragePlan plan;
134
135 const nlohmann::json& projectionsJson = json.contains("projections") ? json.at("projections") : json;
136 if (!projectionsJson.is_array()) {
137 throw std::runtime_error("mqttstore projection configuration must be an array or contain a projections array");
138 }
139
140 for (const nlohmann::json& projectionJson : projectionsJson) {
141 Projection projection;
142 projection.name = projectionJson.value("name", "");
143 projection.topic = projectionJson.at("topic").get<std::string>();
144 projection.table = projectionJson.at("table").get<std::string>();
145
146 const nlohmann::json& columnsJson = projectionJson.at("columns");
147 if (!columnsJson.is_object()) {
148 throw std::runtime_error("mqttstore projection columns must be an object");
149 }
150
151 for (auto columnIterator = columnsJson.begin(); columnIterator != columnsJson.end(); ++columnIterator) {
152 ColumnMapping mapping;
153 mapping.column = columnIterator.key();
154
155 if (columnIterator.value().is_string()) {
156 mapping.jsonPointer = columnIterator.value().get<std::string>();
157 } else {
158 const nlohmann::json& mappingJson = columnIterator.value();
159 mapping.jsonPointer = mappingJson.value("json_pointer", "");
160 mapping.required = mappingJson.value("required", false);
161
162 if (mappingJson.contains("topic_level")) {
163 mapping.topicLevel = mappingJson.at("topic_level").get<std::size_t>();
164 }
165 if (mappingJson.contains("literal")) {
166 mapping.literal = mappingJson.at("literal").get<std::string>();
167 }
168 }
169
170 projection.columns.push_back(mapping);
171 }
172
173 plan.projections.push_back(projection);
174 }
175
176 return plan;
177 }
nlohmann::json json
void validateProjectionConfiguration(const nlohmann::json &json)

References mqtt::mqttstore::lib::anonymous_namespace{StoragePlan.cpp}::validateProjectionConfiguration().

Referenced by fromFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getProjections()

const std::vector< StoragePlan::Projection > & mqtt::mqttstore::lib::StoragePlan::getProjections ( ) const
nodiscard

Definition at line 179 of file StoragePlan.cpp.

179 {
180 return projections;
181 }
std::vector< Projection > projections
Definition StoragePlan.h:53

References projections.

◆ match()

std::vector< const StoragePlan::Projection * > mqtt::mqttstore::lib::StoragePlan::match ( const std::string & topic) const
nodiscard

Definition at line 183 of file StoragePlan.cpp.

183 {
184 std::vector<const Projection*> matches;
185
186 for (const Projection& projection : projections) {
187 if (topicMatches(projection.topic, topic)) {
188 matches.push_back(&projection);
189 }
190 }
191
192 return matches;
193 }
static bool topicMatches(const std::string &filter, const std::string &topic)

◆ topicMatches()

bool mqtt::mqttstore::lib::StoragePlan::topicMatches ( const std::string & filter,
const std::string & topic )
staticprivate

Definition at line 195 of file StoragePlan.cpp.

195 {
196 const std::vector<std::string> filterLevels = splitTopic(filter);
197 const std::vector<std::string> topicLevels = splitTopic(topic);
198
199 for (std::size_t level = 0; level < filterLevels.size(); ++level) {
200 if (filterLevels[level] == "#") {
201 return level + 1 == filterLevels.size();
202 }
203
204 if (level >= topicLevels.size()) {
205 return false;
206 }
207
208 if (filterLevels[level] != "+" && filterLevels[level] != topicLevels[level]) {
209 return false;
210 }
211 }
212
213 return filterLevels.size() == topicLevels.size();
214 }
std::vector< std::string > splitTopic(const std::string &topic)

Member Data Documentation

◆ projections

std::vector<Projection> mqtt::mqttstore::lib::StoragePlan::projections
private

Definition at line 53 of file StoragePlan.h.

Referenced by getProjections().


The documentation for this class was generated from the following files: