Commit 398683b3 authored by Michael Ott's avatar Michael Ott
Browse files

Add SensorConfig::loadCache() that loads all published sensors into a list and...

Add SensorConfig::loadCache() that loads all published sensors into a list and creates a map for faster lookup of sensor names
parent 336107da
......@@ -101,6 +101,13 @@ protected:
SensorConfigImpl* impl;
public:
/**
* @brief Load all published sensors into cache.
*
* @return See SCError.
*/
SCError loadCache();
/**
* @brief Makes a physical sensor public.
*
......
......@@ -27,6 +27,7 @@
#include <list>
#include <string>
#include <unordered_map>
#include <functional>
#include "cassandra.h"
......@@ -47,13 +48,15 @@ protected:
Connection* connection;
CassSession* session;
typedef std::unordered_map<std::string, PublicSensor> SensorPropertyCache_t;
static SensorPropertyCache_t sensorPropertyCache;
typedef std::unordered_map<std::string, std::reference_wrapper<PublicSensor>> SensorMap_t;
SensorMap_t sensorMapByName;
std::list<PublicSensor> sensorList;
bool validateSensorPattern(const char* sensorPattern);
bool validateSensorPublicName(std::string publicName);
public:
SCError loadCache();
SCError publishSensor(std::string publicName, std::string sensorPattern);
SCError publishVirtualSensor(std::string publicName, std::string vSensorExpression, std::string vSensorId, TimeStamp tZero, uint64_t frequency);
SCError unPublishSensor(std::string publicName);
......
......@@ -77,6 +77,12 @@ PublicSensor::PublicSensor (const PublicSensor &copy)
/*
* SensorConfig functions
*/
SCError SensorConfig::loadCache()
{
return impl->loadCache();
}
SCError SensorConfig::publishSensor(const char* publicName, const char* sensorPattern)
{
return impl->publishSensor(publicName, sensorPattern);
......@@ -178,7 +184,6 @@ SensorConfig::~SensorConfig()
/*
* SensorConfigImpl protected members and functions
*/
SensorConfigImpl::SensorPropertyCache_t SensorConfigImpl::sensorPropertyCache;
/*
* Validate the pattern for a Sensor to be published
......@@ -266,6 +271,23 @@ bool SensorConfigImpl::validateSensorPublicName(std::string publicName)
/*
* SensorConfigImpl public functions
*/
SCError SensorConfigImpl::loadCache()
{
sensorList.clear();
sensorMapByName.clear();
SCError rc = getPublicSensorsVerbose(sensorList);
if (rc != SC_OK) {
return rc;
} else {
for (auto& s: sensorList) {
sensorMapByName.insert(std::make_pair(s.name, std::ref(s)));
}
return SC_OK;
}
}
SCError SensorConfigImpl::publishSensor(std::string publicName, std::string sensorPattern)
{
/* Check if the pattern matches the requirements */
......@@ -628,8 +650,8 @@ SCError SensorConfigImpl::getPublicSensorsVerbose(std::list<PublicSensor>& publi
SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char* publicName)
{
/* Check if the sensor definition is already in the cache */
SensorPropertyCache_t::const_iterator got = sensorPropertyCache.find(publicName);
if (got != sensorPropertyCache.end()) {
SensorMap_t::const_iterator got = sensorMapByName.find(publicName);
if (got != sensorMapByName.end()) {
sensor = got->second;
return SC_OK;
}
......@@ -738,7 +760,7 @@ SCError SensorConfigImpl::getPublicSensorByName(PublicSensor& sensor, const char
sensor.frequency = frequency;
/* Add to sensorPropertyCache for later use */
sensorPropertyCache.insert(std::make_pair(publicName, sensor));
sensorMapByName.insert(std::make_pair(publicName, std::ref(sensor)));
}
else {
cass_result_free(result);
......
Supports Markdown
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