Module: check_mk
Branch: master
Commit: 6b43ee4731e7f8a6b7f1226e3ec3f6c339ee9692
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6b43ee4731e7f8…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Nov 10 15:12:43 2017 +0100
Make the current stack-like nature of AndingFilter more explicit.
Change-Id: I93da3f8bf96eca069f79371c437cfdcba5e524b7
---
livestatus/src/AndingFilter.cc | 11 -----------
livestatus/src/AndingFilter.h | 15 +++++++++++----
livestatus/src/Query.cc | 43 ++++++++++++++++++++++--------------------
3 files changed, 34 insertions(+), 35 deletions(-)
diff --git a/livestatus/src/AndingFilter.cc b/livestatus/src/AndingFilter.cc
index b48b75a..aa657d1 100644
--- a/livestatus/src/AndingFilter.cc
+++ b/livestatus/src/AndingFilter.cc
@@ -23,9 +23,7 @@
// Boston, MA 02110-1301 USA.
#include "AndingFilter.h"
-#include <algorithm>
#include <memory>
-#include <vector>
#include "Filter.h"
#include "FilterVisitor.h"
#include "OringFilter.h"
@@ -88,12 +86,3 @@ const std::string *AndingFilter::findValueForIndexing(
}
return nullptr;
}
-
-std::unique_ptr<Filter> AndingFilter::stealLastSubFilter() {
- if (_subfilters.empty()) {
- return nullptr;
- }
- std::unique_ptr<Filter> l = move(_subfilters.back());
- _subfilters.pop_back();
- return l;
-}
diff --git a/livestatus/src/AndingFilter.h b/livestatus/src/AndingFilter.h
index 8e4133a..68b8c3a 100644
--- a/livestatus/src/AndingFilter.h
+++ b/livestatus/src/AndingFilter.h
@@ -26,15 +26,15 @@
#define AndingFilter_h
#include "config.h" // IWYU pragma: keep
+#include <algorithm>
#include <chrono>
-#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
+#include "Filter.h"
#include "VariadicFilter.h"
#include "contact_fwd.h"
-class Filter;
class FilterVisitor;
class Row;
@@ -51,8 +51,15 @@ public:
std::unique_ptr<Filter> negate() const override;
const std::string *findValueForIndexing(
const std::string &column_name) const;
- std::unique_ptr<Filter> stealLastSubFilter();
- size_t size() const { return _subfilters.size(); }
+
+ // TODO(sp): stack-like interface, doesn't really belong here.
+ auto empty() const { return _subfilters.empty(); }
+ auto top() { return std::move(_subfilters.back()); }
+ void push(std::unique_ptr<Filter> f) {
+ _subfilters.push_back(std::move(f));
+ }
+ void pop() { return _subfilters.pop_back(); }
+
auto begin() const { return _subfilters.begin(); }
auto end() const { return _subfilters.end(); }
};
diff --git a/livestatus/src/Query.cc b/livestatus/src/Query.cc
index c96997c..7bdbea7 100644
--- a/livestatus/src/Query.cc
+++ b/livestatus/src/Query.cc
@@ -225,20 +225,21 @@ void Query::parseAndOrLine(const std::string &header, char
*line,
return;
}
- auto num_filters = filter.size();
- if (number > static_cast<int>(num_filters)) {
- invalidHeader("error combining filters for table " + _table->name()
+
- " with '" + header + "': expected " +
- std::to_string(number) + " filters, but only " +
- std::to_string(num_filters) + " " +
- (num_filters == 1 ? "is" : "are") + " on
stack");
- return;
- }
-
for (auto i = 0; i < number; ++i) {
- variadic->addSubfilter(filter.stealLastSubFilter());
+ if (filter.empty()) {
+ invalidHeader("error combining filters for table " +
+ _table->name() + " with '" + header +
"': expected " +
+ std::to_string(number) + " filters, but only " +
+ std::to_string(i) + " " + (i == 1 ? "is" :
"are") +
+ " on stack");
+ return;
+ }
+
+ auto top = filter.top();
+ filter.pop();
+ variadic->addSubfilter(std::move(top));
}
- filter.addSubfilter(std::move(variadic));
+ filter.push(std::move(variadic));
}
void Query::parseNegateLine(const std::string &header, char *line,
@@ -248,13 +249,16 @@ void Query::parseNegateLine(const std::string &header, char
*line,
return;
}
- auto to_negate = filter.stealLastSubFilter();
- if (!to_negate) {
- invalidHeader(header + " nothing to negate");
+ if (filter.empty()) {
+ invalidHeader("error combining filters for table " + _table->name()
+
+ " with '" + header +
+ "': expected 1 filters, but only 0 are on stack");
return;
}
- filter.addSubfilter(to_negate->negate());
+ auto top = filter.top();
+ filter.pop();
+ filter.push(top->negate());
}
void Query::parseStatsAndOrLine(const std::string &header, char *line,
@@ -429,7 +433,7 @@ void Query::parseFilterLine(char *line, AndingFilter &filter) {
}
if (auto sub_filter = createFilter(*column, relOp, value)) {
- filter.addSubfilter(std::move(sub_filter));
+ filter.push(std::move(sub_filter));
_all_columns.insert(column);
}
}
@@ -828,14 +832,13 @@ const std::vector<std::unique_ptr<Aggregator>>
&Query::getAggregatorsFor(
void Query::doWait() {
// If no wait condition and no trigger is set,
// we do not wait at all.
- auto num_filters = _wait_condition.size();
- if (num_filters == 0 && _wait_trigger == nullptr) {
+ if (_wait_condition.empty() && _wait_trigger == nullptr) {
return;
}
// If a condition is set, we check the condition. If it
// is already true, we do not need to way
- if (num_filters > 0 &&
+ if (!_wait_condition.empty() &&
_wait_condition.accepts(_wait_object, _auth_user, timezoneOffset())) {
Debug(_logger) << "Wait condition true, no waiting neccessary";
return;