sensorcache.cpp 3.98 KB
Newer Older
Alessio Netti's avatar
Alessio Netti committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * SensorCache.cpp
 *
 *  Created on: 3 Aug 2016
 *      Author: ottmi
 */

#include "sensorcache.h"

SensorCache::SensorCache(uint64_t maxHistory) {
    this->_maxHistory = maxHistory;
}

SensorCache::~SensorCache() {
    sensorCache.clear();
}

18
sensorCache_t& SensorCache::getSensorMap() {
Alessio Netti's avatar
Alessio Netti committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    return sensorCache;
}

void SensorCache::storeSensor(SensorId sid, uint64_t ts, int64_t val) {
  reading_t s = { val, ts };
  /* Remove the reserved bytes to leverage the standard find function */
  sid.setRsvd(0);
  //TODO: Check for thread-safety
  if(sensorCache.find(sid) == sensorCache.end())
      sensorCache[sid] = CacheEntry(_maxHistory);
  sensorCache[sid].store(s);
}

int64_t SensorCache::getSensor(SensorId sid, uint64_t avg) {
  /* Remove the reserved bytes to leverage the standard find function */
  sid.setRsvd(0);
  sensorCache_t::iterator it = sensorCache.find(sid);

  if (it == sensorCache.end()) {
    throw std::invalid_argument("Sid not found");
  }

  if (!it->second.checkValid())
  {
    throw std::out_of_range("Sid outdated");
  }

  if (avg) {
    return it->second.getAverage(avg);
  } else {
    return it->second.getLatest().value;
  }
}

int64_t SensorCache::getSensor(std::string topic, uint64_t avg) {
  topic.erase(std::remove(topic.begin(), topic.end(), '/'), topic.end());

  size_t wp = topic.find("*");
  if (wp == std::string::npos) {
    return getSensor(DCDB::SensorId(topic), avg);
  }

  int wl = 29 - topic.length();

  /* Create SensorIds with the lowest and highest values matching the wildcard */
  DCDB::SensorId sidLow(std::string(topic).replace(wp, 1, std::string(wl, '0')));
  DCDB::SensorId sidHi(std::string(topic).replace(wp, 1, std::string(wl, 'f')));
  DCDB::SensorId sidMask(std::string(wp, 'f') + std::string(wl, '0') + std::string(topic.length()-wp-1, 'f'));
  sidLow.setRsvd(0);
  sidHi.setRsvd(0);

  /* See whether there's a SensorId in the cache >= sidLow */
  sensorCache_t::iterator it = sensorCache.lower_bound(sidLow);
  sensorCache_t::iterator mostRecentSidIt = sensorCache.end();

  bool foundOne = false;
  /* Iterate over the cache until the current entry is > sidHi */
  while ((it != sensorCache.end()) && (it->first <= sidHi)) {
    if ((it->first & sidMask) == sidLow) {
      foundOne = true;
      /* We only return one value, even if multiple SensorIds would match.
       * At least make sure it's the most recent value
       */
      if (it->second.checkValid() && ((mostRecentSidIt == sensorCache.end()) || mostRecentSidIt->second.getLatest().timestamp < it->second.getLatest().timestamp)) {
        mostRecentSidIt = it;
      }
    }
    it++;
  }

  if (mostRecentSidIt == sensorCache.end()) {
    /* Check whether we actually found at least an outdated entry */
    if (foundOne) {
      throw std::out_of_range("Sid outdated");
    } else {
      throw std::invalid_argument("Sid not found");
    }
  }

  if (avg) {
    return mostRecentSidIt->second.getAverage(avg);
  } else {
    return mostRecentSidIt->second.getLatest().value;
  }
}

void SensorCache::dump() {
  std::cout << "SensorCache Dump:" << std::endl;
  for (sensorCache_t::iterator sit = sensorCache.begin(); sit != sensorCache.end(); sit++) {
    std::cout << "  id=" << sit->first.toString() << " data=[";
    for (std::vector<reading_t>::const_iterator eit = sit->second.getRaw()->begin(); eit != sit->second.getRaw()->end(); eit++) {
      if (eit != sit->second.getRaw()->begin()) {
        std::cout << ",";
      }
      std::cout << "(" <<  eit->value << "," << eit->timestamp/NS_PER_S << "." << std::setfill ('0') << std::setw (9) << eit->timestamp%NS_PER_S << ")";
    }
    std::cout << "]" << std::endl;
  }
}

uint64_t SensorCache::clean(uint64_t t) {
    uint64_t thresh = getTimestamp() - t;
    uint64_t ctr = 0;
    for (auto it = sensorCache.cbegin(); it != sensorCache.cend();) {
        uint64_t latestTs = it->second.getLatest().timestamp;
        if (latestTs!=0 && latestTs < thresh) {
            it = sensorCache.erase(it);
            ctr++;
        } else
            ++it;
    }
    return ctr;
}