JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
IAggregator.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 10/6/17.
3 //
4 
5 #ifndef JODA_IAGGREGATOR_H
6 #define JODA_IAGGREGATOR_H
9 #include <rapidjson/allocators.h>
10 #include <rapidjson/fwd.h>
11 
12 #include <memory>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
18 
19 namespace joda::query {
25 class IAggregator {
26  public:
34  IAggregator(const std::string &toPointer,
35  std::vector<std::unique_ptr<IValueProvider>> &&params)
36  : toPointer(toPointer), params(std::move(params)) {
37  for (auto &param : this->params) {
39  }
40  }
41  IAggregator(IAggregator const &) = delete;
42  void operator=(IAggregator const &x) = delete;
43 
44  virtual ~IAggregator() = default;
45 
52  virtual void accumulate(const RapidJsonDocument &json,
53  RJMemoryPoolAlloc &alloc) = 0;
54 
55  /*
56  * Merge (Merges two instances)
57  */
62  virtual void merge(IAggregator *other) = 0;
63 
64  /*
65  * Terminators (Terminate and collect data. Only called once)
66  */
71  virtual RJValue terminate(RJMemoryPoolAlloc &alloc) = 0;
72 
77  virtual std::unique_ptr<IAggregator> duplicate() const = 0;
78 
79  virtual
83  const std::string &
84  getDestPointer() const {
85  return toPointer;
86  }
87 
88  virtual
92  const std::string
93  getName() const = 0;
94 
95  virtual std::vector<std::string> getAttributes() const {
96  std::vector<std::string> ret;
97  for (const auto &param : params) {
98  param->getAttributes(ret);
99  }
100  return ret;
101  };
102 
103  std::string getParameterStringRepresentation() const {
104  std::vector<std::string> pars;
105  for (auto &&param : params) {
106  pars.push_back(param->toString());
107  }
108  std::stringstream ss;
109  std::move(std::begin(pars), std::end(pars),
111  return ss.str();
112  }
113 
114  /*
115  * Returns a string representation of the aggregator
116  */
117  virtual std::string toString() const {
118  return "'" + toPointer + "':" + getName() + "(" +
120  };
121 
122  protected:
123  std::string toPointer;
124  std::vector<std::unique_ptr<IValueProvider>> params;
125 
126  void checkParamSize(unsigned int expected) {
127  if (params.size() != expected) {
128  throw WrongParameterCountException(params.size(), expected, getName());
129  }
130  }
131 
132  void checkMinParamSize(unsigned int expected) {
133  if (params.size() < expected) {
134  throw WrongParameterCountException(params.size(), expected, getName(),
135  true);
136  }
137  }
138 
139  void checkParamType(unsigned int i, IValueType expected) {
140  if (!(params[i]->isAny() || params[i]->getReturnType() == expected)) {
141  throw WrongParameterTypeException(i, expected, getName());
142  }
143  }
144 
145  void checkOptionalParamType(unsigned int i, IValueType expected) {
146  if(params.size() < i+1) {
147  return;
148  }
149  if (!(params[i]->isAny() || params[i]->getReturnType() == expected)) {
150  throw WrongParameterTypeException(i, expected, getName());
151  }
152  }
153 
154  std::vector<std::unique_ptr<IValueProvider>> duplicateParameters() const {
155  std::vector<std::unique_ptr<IValueProvider>> ret;
156  for (auto &&param : params) {
157  ret.push_back(param->duplicate());
158  }
159  return ret;
160  }
161 };
162 
163 // Aggregator Queue
165  typedef std::unique_ptr<IAggregator> payload_t;
167 
168  static std::unique_ptr<queue_t> getQueue() {
169  return std::make_unique<queue_t>();
170  }
171  static std::unique_ptr<queue_t> getQueue(size_t minCapacity,
172  size_t maxProducers) {
173  return std::make_unique<queue_t>(minCapacity, maxProducers);
174  }
175 };
176 
178 } // namespace joda::query
179 
180 #endif // JODA_IAGGREGATOR_H
rapidjson::MemoryPoolAllocator< RJBaseAlloc > RJMemoryPoolAlloc
Definition: RJFwd.h:26
rapidjson::GenericValue< RJChar, RJMemoryPoolAlloc > RJValue
Definition: RJFwd.h:29
Definition: RapidJsonDocument.h:22
Definition: infix_iterator.h:11
Definition: IAggregator.h:25
void checkParamType(unsigned int i, IValueType expected)
Definition: IAggregator.h:139
std::vector< std::unique_ptr< IValueProvider > > params
Definition: IAggregator.h:124
void checkMinParamSize(unsigned int expected)
Definition: IAggregator.h:132
IAggregator(IAggregator const &)=delete
virtual ~IAggregator()=default
virtual const std::string & getDestPointer() const
Definition: IAggregator.h:84
void operator=(IAggregator const &x)=delete
std::vector< std::unique_ptr< IValueProvider > > duplicateParameters() const
Definition: IAggregator.h:154
IAggregator(const std::string &toPointer, std::vector< std::unique_ptr< IValueProvider >> &&params)
Definition: IAggregator.h:34
std::string toPointer
Definition: IAggregator.h:120
void checkParamSize(unsigned int expected)
Definition: IAggregator.h:126
void checkOptionalParamType(unsigned int i, IValueType expected)
Definition: IAggregator.h:145
virtual std::vector< std::string > getAttributes() const
Definition: IAggregator.h:95
virtual std::unique_ptr< IAggregator > duplicate() const =0
virtual void merge(IAggregator *other)=0
virtual void accumulate(const RapidJsonDocument &json, RJMemoryPoolAlloc &alloc)=0
virtual RJValue terminate(RJMemoryPoolAlloc &alloc)=0
virtual std::string toString() const
Definition: IAggregator.h:117
virtual const std::string getName() const =0
std::string getParameterStringRepresentation() const
Definition: IAggregator.h:103
static void replaceConstSubexpressions(std::unique_ptr< IValueProvider > &val)
Definition: IValueProvider.cpp:11
Definition: IValueProvider.h:96
Definition: IValueProvider.h:60
Definition: AttributeStatAggregator.h:12
JODA_AGGREGATOR_QUEUE AggregatorQueue
Definition: IAggregator.h:177
IValueType
Definition: IValueProvider.h:33
Definition: Queue.h:19
Definition: IAggregator.h:164
static std::unique_ptr< queue_t > getQueue()
Definition: IAggregator.h:168
static std::unique_ptr< queue_t > getQueue(size_t minCapacity, size_t maxProducers)
Definition: IAggregator.h:171
std::unique_ptr< IAggregator > payload_t
Definition: IAggregator.h:165
JODA_SHARED_QUEUE< payload_t > queue_t
Definition: IAggregator.h:166