Akumuli
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
queryprocessor_framework.h
1 #pragma once
2 #include <memory>
3 #include <stdexcept>
4 
5 #include "akumuli.h"
6 #include "seriesparser.h"
7 
8 #include <boost/property_tree/ptree.hpp>
9 
10 namespace Akumuli {
11 namespace QP {
12 
14 struct QueryParserError : std::runtime_error {
15  QueryParserError(const char* parser_message)
16  : std::runtime_error(parser_message) {}
17 };
18 
19 static const aku_Sample NO_DATA = { 0u, 0u, { 0.0, sizeof(aku_Sample), aku_PData::EMPTY } };
20 
21 static const aku_Sample SAMPLING_LO_MARGIN = { 0u,
22  0u,
23  { 0.0, sizeof(aku_Sample), aku_PData::LO_MARGIN } };
24 static const aku_Sample SAMPLING_HI_MARGIN = { 0u,
25  0u,
26  { 0.0, sizeof(aku_Sample), aku_PData::HI_MARGIN } };
27 
28 struct Node {
29 
30  virtual ~Node() = default;
31 
33  virtual void complete() = 0;
34 
38  virtual bool put(aku_Sample const& sample) = 0;
39 
40  virtual void set_error(aku_Status status) = 0;
41 
42  // Query validation
43 
44  enum QueryFlags {
45  EMPTY = 0,
46  GROUP_BY_REQUIRED = 1,
47  TERMINAL = 2,
48  };
49 
52  virtual int get_requirements() const = 0;
53 };
54 
55 
56 struct NodeException : std::runtime_error {
57  NodeException(const char* msg)
58  : std::runtime_error(msg) {}
59 };
60 
61 
62 struct IQueryFilter {
63  enum FilterResult {
64  SKIP_THIS,
65  SKIP_ALL,
66  PROCESS,
67  };
68  virtual ~IQueryFilter() = default;
69  virtual FilterResult apply(aku_ParamId id) = 0;
70  virtual std::vector<aku_ParamId> get_ids() = 0;
71 };
72 
73 
76  std::vector<uint64_t> allids;
77 
78  BypassFilter(std::vector<uint64_t> allids = std::vector<uint64_t>())
79  : allids(allids) {}
80 
81  virtual FilterResult apply(aku_ParamId id) { return PROCESS; }
82 
83  std::vector<aku_ParamId> get_ids() { return allids; }
84 };
85 
86 
87 struct QueryRange {
88 
89  enum QueryRangeType {
90  INSTANT, // If upperbound is in the future - query should be executed untill most recent data were reached
91  CONTINUOUS, // If upperbound is in the future - query should wait
92  };
93 
94  aku_Timestamp lowerbound;
95  aku_Timestamp upperbound;
96  int direction;
97  QueryRangeType type;
98 
100  bool is_backward() const { return direction == AKU_CURSOR_DIR_BACKWARD; }
101 
103  aku_Timestamp begin() const {
104  return direction == AKU_CURSOR_DIR_BACKWARD ? upperbound : lowerbound;
105  }
106 
108  aku_Timestamp end() const {
109  return direction == AKU_CURSOR_DIR_BACKWARD ? lowerbound : upperbound;
110  }
111 };
112 
113 
114 std::ostream& operator<<(std::ostream& str, QueryRange const& range);
115 
116 
119 
120  // Query information
121  virtual ~IQueryProcessor() = default;
122 
124  virtual QueryRange range() const = 0;
125 
127  virtual IQueryFilter& filter() = 0;
128 
132  virtual SeriesMatcher* matcher() = 0;
133 
134  // Execution control
135 
141  virtual bool start() = 0;
142 
144  virtual bool put(const aku_Sample& sample) = 0;
145 
147  virtual void stop() = 0;
148 
150  virtual void set_error(aku_Status error) = 0;
151 };
152 
153 
155  virtual std::shared_ptr<Node> create(boost::property_tree::ptree const& ptree,
156  std::shared_ptr<Node> next) const = 0;
157  virtual std::string get_tag() const = 0;
158 };
159 
161 void add_queryparsertoken_to_registry(BaseQueryParserToken const* ptr);
162 
164 std::shared_ptr<Node> create_node(std::string tag, boost::property_tree::ptree const& ptree,
165  std::shared_ptr<Node> next);
166 
170 template <class Target> struct QueryParserToken : BaseQueryParserToken {
171  std::string tag;
172  QueryParserToken(const char* tag)
173  : tag(tag) {
174  add_queryparsertoken_to_registry(this);
175  }
176  virtual std::string get_tag() const { return tag; }
177  virtual std::shared_ptr<Node> create(boost::property_tree::ptree const& ptree,
178  std::shared_ptr<Node> next) const {
179  return std::make_shared<Target>(ptree, next);
180  }
181 };
182 }
183 } // namespaces
aku_Timestamp begin() const
Return timestamp from wich scan starts.
Definition: queryprocessor_framework.h:103
aku_Timestamp end() const
Return timestamp at wich scan stops.
Definition: queryprocessor_framework.h:108
virtual bool put(aku_Sample const &sample)=0
bool is_backward() const
Return true if query should scan data backward in time.
Definition: queryprocessor_framework.h:100
virtual void stop()=0
Will be called when processing completed without errors.
Definition: queryprocessor_framework.h:56
virtual QueryRange range() const =0
Lowerbound.
virtual void set_error(aku_Status error)=0
Will be called on error.
virtual bool put(const aku_Sample &sample)=0
Get new value.
Definition: queryprocessor_framework.h:87
Definition: queryprocessor_framework.h:170
Definition: queryprocessor_framework.h:154
virtual void complete()=0
Complete adding values.
Definition: queryprocessor_framework.h:62
virtual IQueryFilter & filter()=0
Return query filter.
Definition: queryprocessor_framework.h:28
virtual int get_requirements() const =0
virtual SeriesMatcher * matcher()=0
Query filter that doesn't block anything (for testing purposes)
Definition: queryprocessor_framework.h:75
Query processor interface.
Definition: queryprocessor_framework.h:118
Exception triggered by query parser.
Definition: queryprocessor_framework.h:14
Definition: seriesparser.h:38