Commit bbeff154 authored by Alessio Netti's avatar Alessio Netti
Browse files

Refactoring MetadataStore class

- Implementation has been split into a SensorMetadata class and a
MetadataStore one to simplify integration in DCDBPusher and Collect Agent
parent 333452ee
......@@ -41,8 +41,10 @@
using namespace std;
class sensorMetadata_t {
class SensorMetadata {
public:
bool isVirtual = false;
bool integrable = false;
bool monotonic = false;
......@@ -52,8 +54,54 @@ public:
uint64_t ttl = 0;
uint64_t interval = 1000000000;
vector<string> operations;
/**
* @brief Parses a JSON string and stores the content in this object.
*
* If parsing fails, a InvalidArgument exception is thrown.
*
* @param payload JSON-encoded string containing metadata information
*/
void parseJSON(const string& payload);
/**
* @brief Parses a PTREE INFO block and stores the content in this object.
*
* If parsing fails, a InvalidArgument exception is thrown.
*
* @param config PTREE block containing metadata
*/
void parsePTREE(boost::property_tree::iptree& config);
/**
* @brief Converts the content of this object into JSON format.
*
* @return String containing the JSON representation of this object
*/
string getJSON() const;
/**
* @brief Returns a sensorMetadata_t object from the internal map, converted into PTREE format.
*
* @return A PTREE object representing this SensorMetadata object
*/
boost::property_tree::ptree getPTREE() const;
protected:
// Parses a string and splits it according to a separator
void _parseVector(const string& str, vector<string>& v, const char sep=',');
// Dumps the contents of "s" in "config"
void _dumpPTREE(boost::property_tree::ptree& config) const;
// Dumps the content of a vector into a string with an arbitrary separator
void _dumpVector(string& str, const vector<string>& v, const char sep=',') const;
};
// ---------------------------------------------------------------------------
// --------------------------- METADATASTORE CLASS ---------------------------
// ---------------------------------------------------------------------------
class MetadataStore {
public:
......@@ -78,7 +126,7 @@ public:
*
* @return A string to Metadata_t map
*/
const map<string, sensorMetadata_t>& getMap() { return _metadata; }
const map<string, SensorMetadata>& getMap() { return _metadata; }
/**
* @brief Stores a sensorMetadata_t object in the internal map.
......@@ -89,7 +137,7 @@ public:
* @param s Object containing sensor metadata
* @return True if "key" is unique, False if there was a collision
*/
bool store(const string& key, const sensorMetadata_t& s);
bool store(const string& key, const SensorMetadata& s);
/**
* @brief Stores a sensorMetadata_t object in the internal map, after parsing it from a JSON string.
......@@ -123,7 +171,7 @@ public:
* @param key Sensor key to be queried
* @return A reference to a sensorMetadata_t object
*/
const sensorMetadata_t& get(const string& key);
const SensorMetadata& get(const string& key);
/**
* @brief Returns a sensorMetadata_t object from the internal map, converted into JSON format.
......@@ -147,16 +195,7 @@ public:
protected:
map<string, sensorMetadata_t> _metadata;
// Parses the "config" iptree block and stores the result in "s"
void _parsePTREE(boost::property_tree::iptree& config, sensorMetadata_t& s);
// Dumps the contents of "s" in "config"
void _dumpPTREE(boost::property_tree::ptree& config, const sensorMetadata_t& s);
// Parses a string and splits it according to a separator
void _parseVector(const string& str, vector<string>& v, const char sep=',');
// Dumps the content of a vector into a string with an arbitrary separator
void _dumpVector(string& str, const vector<string>& v, const char sep=',');
map<string, SensorMetadata> _metadata;
};
......
......@@ -27,83 +27,52 @@
#include "metadatastore.h"
const sensorMetadata_t& MetadataStore::get(const string& key) {
if(!_metadata.count(key))
throw invalid_argument("MetadataStore: key " + key + " does not exist!");
return _metadata[key];
}
bool MetadataStore::store(const string& key, const sensorMetadata_t& s) {
bool overwritten = !_metadata.count(key);
_metadata[key] = s;
return overwritten;
}
bool MetadataStore::storeFromPTREE(const string& key, boost::property_tree::iptree& config) {
sensorMetadata_t metadata;
_parsePTREE(config, metadata);
return store(key, metadata);
}
boost::property_tree::ptree MetadataStore::getPTREE(const string& key) {
boost::property_tree::ptree config;
_dumpPTREE(config, get(key));
return config;
}
bool MetadataStore::storeFromJSON(const string& key, const string& payload) {
void SensorMetadata::parseJSON(const string& payload) {
boost::property_tree::iptree config;
std::istringstream str(payload);
boost::property_tree::read_json(str, config);
return storeFromPTREE(key, config);
parsePTREE(config);
}
string MetadataStore::getJSON(const string& key) {
boost::property_tree::ptree config;
std::ostringstream output;
_dumpPTREE(config, get(key));
boost::property_tree::write_json(output, config, true);
return output.str();
}
void MetadataStore::_parsePTREE(boost::property_tree::iptree& config, sensorMetadata_t& s) {
// The isVirtual and publicName properties are NOT parsed
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
void SensorMetadata::parsePTREE(boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "monotonic")) {
s.monotonic = to_bool(val.second.data());
this->monotonic = to_bool(val.second.data());
} else if (boost::iequals(val.first, "isVirtual")) {
s.isVirtual = to_bool(val.second.data());
this->isVirtual = to_bool(val.second.data());
} else if (boost::iequals(val.first, "integrable")) {
s.integrable = to_bool(val.second.data());
this->integrable = to_bool(val.second.data());
} else if (boost::iequals(val.first, "unit")) {
s.unit = val.second.data();
this->unit = val.second.data();
} else if (boost::iequals(val.first, "publicName")) {
s.publicName = val.second.data();
this->publicName = val.second.data();
} else if (boost::iequals(val.first, "scale")) {
s.scale = stoull(val.second.data());
this->scale = stoull(val.second.data());
} else if (boost::iequals(val.first, "interval")) {
s.interval = stoull(val.second.data()) * 1000000;
this->interval = stoull(val.second.data()) * 1000000;
} else if (boost::iequals(val.first, "operations")) {
_parseVector(val.second.data(), s.operations);
_parseVector(val.second.data(), this->operations);
}
}
}
void MetadataStore::_dumpPTREE(boost::property_tree::ptree& config, const sensorMetadata_t& s) {
string ops="";
_dumpVector(ops, s.operations);
config.clear();
config.push_back(boost::property_tree::ptree::value_type("isVirtual", boost::property_tree::ptree(s.isVirtual ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("monotonic", boost::property_tree::ptree(s.monotonic ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("integrable", boost::property_tree::ptree(s.integrable ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("unit", boost::property_tree::ptree(s.unit)));
config.push_back(boost::property_tree::ptree::value_type("publicName", boost::property_tree::ptree(s.publicName)));
config.push_back(boost::property_tree::ptree::value_type("scale", boost::property_tree::ptree(to_string(s.scale))));
config.push_back(boost::property_tree::ptree::value_type("interval", boost::property_tree::ptree(to_string(s.interval / 1000000))));
config.push_back(boost::property_tree::ptree::value_type("operations", boost::property_tree::ptree(ops)));
string SensorMetadata::getJSON() const {
boost::property_tree::ptree config;
std::ostringstream output;
_dumpPTREE(config);
boost::property_tree::write_json(output, config, true);
return output.str();
}
void MetadataStore::_parseVector(const string& str, vector<string>& v, const char sep) {
boost::property_tree::ptree SensorMetadata::getPTREE() const {
boost::property_tree::ptree config;
_dumpPTREE(config);
return config;
}
void SensorMetadata::_parseVector(const string& str, vector<string>& v, const char sep) {
v.clear();
std::stringstream ss(str);
std::string token;
......@@ -115,11 +84,61 @@ void MetadataStore::_parseVector(const string& str, vector<string>& v, const cha
}
}
void MetadataStore::_dumpVector(string& str, const vector<string>& v, const char sep) {
void SensorMetadata::_dumpPTREE(boost::property_tree::ptree& config) const {
string ops="";
_dumpVector(ops, this->operations);
config.clear();
config.push_back(boost::property_tree::ptree::value_type("isVirtual", boost::property_tree::ptree(this->isVirtual ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("monotonic", boost::property_tree::ptree(this->monotonic ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("integrable", boost::property_tree::ptree(this->integrable ? "true" : "false")));
config.push_back(boost::property_tree::ptree::value_type("unit", boost::property_tree::ptree(this->unit)));
config.push_back(boost::property_tree::ptree::value_type("publicName", boost::property_tree::ptree(this->publicName)));
config.push_back(boost::property_tree::ptree::value_type("scale", boost::property_tree::ptree(to_string(this->scale))));
config.push_back(boost::property_tree::ptree::value_type("interval", boost::property_tree::ptree(to_string(this->interval / 1000000))));
config.push_back(boost::property_tree::ptree::value_type("operations", boost::property_tree::ptree(ops)));
}
void SensorMetadata::_dumpVector(string& str, const vector<string>& v, const char sep) const {
str = "";
string sepStr = string(sep,1);
for(const auto& el : v)
for(const auto& el : v)
str += el + sepStr;
if(!str.empty() && str.back() == sep)
if(!str.empty() && str.back() == sep)
str.erase(str.size()-1, 1);
}
\ No newline at end of file
}
// ---------------------------------------------------------------------------
// --------------------------- METADATASTORE CLASS ---------------------------
// ---------------------------------------------------------------------------
const SensorMetadata& MetadataStore::get(const string& key) {
if(!_metadata.count(key))
throw invalid_argument("MetadataStore: key " + key + " does not exist!");
return _metadata[key];
}
bool MetadataStore::store(const string& key, const SensorMetadata& s) {
bool overwritten = !_metadata.count(key);
_metadata[key] = s;
return overwritten;
}
bool MetadataStore::storeFromPTREE(const string& key, boost::property_tree::iptree& config) {
SensorMetadata metadata;
metadata.parsePTREE(config);
return store(key, metadata);
}
boost::property_tree::ptree MetadataStore::getPTREE(const string& key) {
return this->get(key).getPTREE();
}
bool MetadataStore::storeFromJSON(const string& key, const string& payload) {
SensorMetadata metadata;
metadata.parseJSON(payload);
return store(key, metadata);
}
string MetadataStore::getJSON(const string& key) {
return this->get(key).getJSON();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment