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

Refactoring MetadataStore class II

- Now is header-only to make SensorBase happy
parent bbeff154
......@@ -45,16 +45,41 @@ class SensorMetadata {
public:
bool isVirtual = false;
bool integrable = false;
bool monotonic = false;
string publicName = "";
string unit = "";
uint64_t scale = 1;
uint64_t ttl = 0;
uint64_t interval = 1000000000;
vector<string> operations;
SensorMetadata() :
isVirtual(false),
integrable(false),
monotonic(false),
publicName(""),
unit(""),
scale(1),
ttl(0),
interval(1000000000) {}
SensorMetadata(const SensorMetadata& other) {
this->isVirtual = other.isVirtual;
this->integrable = other.integrable;
this->monotonic = other.monotonic;
this->publicName = other.publicName;
this->unit = other.unit;
this->scale = other.scale;
this->ttl = other.ttl;
this->interval = other.interval;
this->operations = other.operations;
}
SensorMetadata& operator=(const SensorMetadata& other) {
this->isVirtual = other.isVirtual;
this->integrable = other.integrable;
this->monotonic = other.monotonic;
this->publicName = other.publicName;
this->unit = other.unit;
this->scale = other.scale;
this->ttl = other.ttl;
this->interval = other.interval;
this->operations = other.operations;
return *this;
}
/**
* @brief Parses a JSON string and stores the content in this object.
*
......@@ -62,7 +87,12 @@ public:
*
* @param payload JSON-encoded string containing metadata information
*/
void parseJSON(const string& payload);
void parseJSON(const string& payload) {
boost::property_tree::iptree config;
std::istringstream str(payload);
boost::property_tree::read_json(str, config);
parsePTREE(config);
}
/**
* @brief Parses a PTREE INFO block and stores the content in this object.
......@@ -71,30 +101,102 @@ public:
*
* @param config PTREE block containing metadata
*/
void parsePTREE(boost::property_tree::iptree& config);
void parsePTREE(boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "monotonic")) {
this->monotonic = to_bool(val.second.data());
} else if (boost::iequals(val.first, "isVirtual")) {
this->isVirtual = to_bool(val.second.data());
} else if (boost::iequals(val.first, "integrable")) {
this->integrable = to_bool(val.second.data());
} else if (boost::iequals(val.first, "unit")) {
this->unit = val.second.data();
} else if (boost::iequals(val.first, "publicName")) {
this->publicName = val.second.data();
} else if (boost::iequals(val.first, "scale")) {
this->scale = stoull(val.second.data());
} else if (boost::iequals(val.first, "interval")) {
this->interval = stoull(val.second.data()) * 1000000;
} else if (boost::iequals(val.first, "operations")) {
_parseVector(val.second.data(), this->operations);
}
}
}
/**
* @brief Converts the content of this object into JSON format.
*
* @return String containing the JSON representation of this object
*/
string getJSON() const;
string getJSON() const {
boost::property_tree::ptree config;
std::ostringstream output;
_dumpPTREE(config);
boost::property_tree::write_json(output, config, true);
return output.str();
}
/**
* @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;
boost::property_tree::ptree getPTREE() const {
boost::property_tree::ptree config;
_dumpPTREE(config);
return config;
}
// Public class members
bool isVirtual;
bool integrable;
bool monotonic;
string publicName;
string unit;
uint64_t scale;
uint64_t ttl;
uint64_t interval;
vector<string> operations;
protected:
// Parses a string and splits it according to a separator
void _parseVector(const string& str, vector<string>& v, const char sep=',');
void _parseVector(const string& str, vector<string>& v, const char sep=',') {
v.clear();
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, sep)) {
if(!token.empty()) {
boost::algorithm::trim(token);
v.push_back(token);
}
}
}
// Dumps the contents of "s" in "config"
void _dumpPTREE(boost::property_tree::ptree& config) const;
void _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)));
}
// 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;
void _dumpVector(string& str, const vector<string>& v, const char sep=',') const {
str = "";
string sepStr = string(sep,1);
for(const auto& el : v)
str += el + sepStr;
if(!str.empty() && str.back() == sep)
str.erase(str.size()-1, 1);
}
};
......@@ -119,14 +221,18 @@ public:
/**
* @brief Clears the internal metadata map.
*/
void clear() { _metadata.clear(); }
void clear() {
_metadata.clear();
}
/**
* @brief Returns the internal metadata map.
*
* @return A string to Metadata_t map
*/
const map<string, SensorMetadata>& getMap() { return _metadata; }
const map<string, SensorMetadata>& getMap() {
return _metadata;
}
/**
* @brief Stores a sensorMetadata_t object in the internal map.
......@@ -137,7 +243,11 @@ 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& s);
bool store(const string& key, const SensorMetadata& s) {
bool overwritten = !_metadata.count(key);
_metadata[key] = s;
return overwritten;
}
/**
* @brief Stores a sensorMetadata_t object in the internal map, after parsing it from a JSON string.
......@@ -149,7 +259,11 @@ public:
* @param payload JSON-encoded string containing metadata information
* @return True if "key" is unique, False if there was a collision
*/
bool storeFromJSON(const string& key, const string& payload);
bool storeFromJSON(const string& key, const string& payload) {
SensorMetadata metadata;
metadata.parseJSON(payload);
return store(key, metadata);
}
/**
* @brief Stores a sensorMetadata_t object in the internal map, after parsing it from a INFO block.
......@@ -161,7 +275,11 @@ public:
* @param config PTREE block containing metadata
* @return True if "key" is unique, False if there was a collision
*/
bool storeFromPTREE(const string& key, boost::property_tree::iptree& config);
bool storeFromPTREE(const string& key, boost::property_tree::iptree& config) {
SensorMetadata metadata;
metadata.parsePTREE(config);
return store(key, metadata);
}
/**
* @brief Returns a sensorMetadata_t object from the internal map.
......@@ -171,7 +289,11 @@ public:
* @param key Sensor key to be queried
* @return A reference to a sensorMetadata_t object
*/
const SensorMetadata& get(const string& key);
const SensorMetadata& get(const string& key) {
if(!_metadata.count(key))
throw invalid_argument("MetadataStore: key " + key + " does not exist!");
return _metadata[key];
}
/**
* @brief Returns a sensorMetadata_t object from the internal map, converted into JSON format.
......@@ -181,7 +303,9 @@ public:
* @param key Sensor key to be queried
* @return String containing the JSON representation of the sensorMetadata_t object
*/
string getJSON(const string& key);
string getJSON(const string& key) {
return this->get(key).getJSON();
}
/**
* @brief Returns a sensorMetadata_t object from the internal map, converted into PTREE format.
......@@ -191,7 +315,9 @@ public:
* @param key Sensor key to be queried
* @return A PTREE object representing the sensorMetadata_t object
*/
boost::property_tree::ptree getPTREE(const string& key);
boost::property_tree::ptree getPTREE(const string& key) {
return this->get(key).getPTREE();
}
protected:
......
//================================================================================
// Name : metadatastore.cpp
// Author : Alessio Netti
// Contact : info@dcdb.it
// Copyright : Leibniz Supercomputing Centre
// Description : A meta-data store for sensors
//================================================================================
//================================================================================
// This file is part of DCDB (DataCenter DataBase)
// Copyright (C) 2011-2019 Leibniz Supercomputing Centre
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//================================================================================
#include "metadatastore.h"
void SensorMetadata::parseJSON(const string& payload) {
boost::property_tree::iptree config;
std::istringstream str(payload);
boost::property_tree::read_json(str, config);
parsePTREE(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")) {
this->monotonic = to_bool(val.second.data());
} else if (boost::iequals(val.first, "isVirtual")) {
this->isVirtual = to_bool(val.second.data());
} else if (boost::iequals(val.first, "integrable")) {
this->integrable = to_bool(val.second.data());
} else if (boost::iequals(val.first, "unit")) {
this->unit = val.second.data();
} else if (boost::iequals(val.first, "publicName")) {
this->publicName = val.second.data();
} else if (boost::iequals(val.first, "scale")) {
this->scale = stoull(val.second.data());
} else if (boost::iequals(val.first, "interval")) {
this->interval = stoull(val.second.data()) * 1000000;
} else if (boost::iequals(val.first, "operations")) {
_parseVector(val.second.data(), this->operations);
}
}
}
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();
}
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;
while (std::getline(ss, token, sep)) {
if(!token.empty()) {
boost::algorithm::trim(token);
v.push_back(token);
}
}
}
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)
str += el + sepStr;
if(!str.empty() && str.back() == sep)
str.erase(str.size()-1, 1);
}
// ---------------------------------------------------------------------------
// --------------------------- 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