summaryrefslogtreecommitdiffstats
path: root/common/query.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-27 00:28:40 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-27 00:28:40 +0200
commitfd532607ef29aac49b52c861e5aecda6dfa19e82 (patch)
tree3687ad63516a344f298c014c87a6f6209379a811 /common/query.h
parent47b9f2109f57c1121b760ea6d885ab08f12c46b3 (diff)
downloadsink-fd532607ef29aac49b52c861e5aecda6dfa19e82.tar.gz
sink-fd532607ef29aac49b52c861e5aecda6dfa19e82.zip
New query api
Diffstat (limited to 'common/query.h')
-rw-r--r--common/query.h157
1 files changed, 132 insertions, 25 deletions
diff --git a/common/query.h b/common/query.h
index 1b909e5..3021fe2 100644
--- a/common/query.h
+++ b/common/query.h
@@ -174,34 +174,13 @@ public:
174 return *this; 174 return *this;
175 } 175 }
176 176
177 template <typename T> 177 Query(const ApplicationDomain::Entity &value) : limit(0), liveQuery(false), synchronousQuery(false)
178 Query &filter(const QVariant &value)
179 {
180 propertyFilter.insert(T::name, value);
181 return *this;
182 }
183
184 template <typename T>
185 Query &filter(const Comparator &comparator)
186 {
187 propertyFilter.insert(T::name, comparator);
188 return *this;
189 }
190
191 template <typename T>
192 Query &filter(const ApplicationDomain::Entity &value)
193 {
194 propertyFilter.insert(T::name, QVariant::fromValue(value.identifier()));
195 return *this;
196 }
197
198 Query(const ApplicationDomain::Entity &value) : limit(0), liveQuery(false), synchronousQuery(false), threadLeaderOnly(false), bloomThread(false)
199 { 178 {
200 ids << value.identifier(); 179 ids << value.identifier();
201 resources << value.resourceInstanceIdentifier(); 180 resources << value.resourceInstanceIdentifier();
202 } 181 }
203 182
204 Query(Flags flags = Flags()) : limit(0), liveQuery(false), synchronousQuery(false), threadLeaderOnly(false), bloomThread(false) 183 Query(Flags flags = Flags()) : limit(0), liveQuery(false), synchronousQuery(false)
205 { 184 {
206 } 185 }
207 186
@@ -236,9 +215,137 @@ public:
236 int limit; 215 int limit;
237 bool liveQuery; 216 bool liveQuery;
238 bool synchronousQuery; 217 bool synchronousQuery;
239 bool threadLeaderOnly; 218
240 bool bloomThread; 219 class FilterStage {
220 public:
221 virtual ~FilterStage(){};
222 };
223
224 QList<QSharedPointer<FilterStage>> filterStages;
225
226 /*
227 * Filters
228 */
229 class Filter : public FilterStage {
230 QByteArrayList ids;
231 QHash<QByteArray, Comparator> propertyFilter;
232 QByteArray sortProperty;
233 };
234
235 template <typename T>
236 Query &filter(const QVariant &value)
237 {
238 propertyFilter.insert(T::name, value);
239 return *this;
240 }
241
242 template <typename T>
243 Query &filter(const Comparator &comparator)
244 {
245 propertyFilter.insert(T::name, comparator);
246 return *this;
247 }
248
249 template <typename T>
250 Query &filter(const ApplicationDomain::Entity &value)
251 {
252 propertyFilter.insert(T::name, QVariant::fromValue(value.identifier()));
253 return *this;
254 }
255
256 Query &filter(const ApplicationDomain::SinkResource &resource)
257 {
258 resources << resource.identifier();
259 return *this;
260 }
261
262 Query &filter(const ApplicationDomain::SinkAccount &account)
263 {
264 accounts << account.identifier();
265 return *this;
266 }
267
268 class Reduce : public FilterStage {
269 public:
270
271 class Selector {
272 public:
273 enum Comparator {
274 Min, //get the minimum value
275 Max, //get the maximum value
276 First //Get the first result we get
277 };
278
279 template <typename SelectionProperty>
280 static Selector max()
281 {
282 return Selector(SelectionProperty::name, Max);
283 }
284
285 Selector(const QByteArray &p, Comparator c)
286 : property(p),
287 comparator(c)
288 {
289 }
290
291 QByteArray property;
292 Comparator comparator;
293 };
294
295 Reduce(const QByteArray &p, const Selector &s)
296 : property(p),
297 selector(s)
298 {
299 }
300
301 //Reduce on property
302 QByteArray property;
303 Selector selector;
304
305 //TODO add aggregate functions like:
306 //.count()
307 //.collect<Mail::sender>();
308 //...
309 //
310 //Potentially pass-in an identifier under which the result will be available in the result set.
311 };
312
313 template <typename T>
314 Reduce &reduce(const Reduce::Selector &s)
315 {
316 auto reduction = QSharedPointer<Reduce>::create(T::name, s);
317 filterStages << reduction;
318 return *reduction;
319 }
320
321 /**
322 * "Bloom" on a property.
323 *
324 * For every encountered value of a property,
325 * a result set is generated containing all entries with the same value.
326 *
327 * Example:
328 * For an input result set of one mail; return all emails with the same threadId.
329 */
330 class Bloom : public FilterStage {
331 public:
332 //Property to bloom on
333 QByteArray property;
334 Bloom(const QByteArray &p)
335 : property(p)
336 {
337 }
338 };
339
340 template <typename T>
341 void bloom()
342 {
343 auto bloom = QSharedPointer<Bloom>::create(T::name);
344 filterStages << bloom;
345 }
346
241}; 347};
348
242} 349}
243 350
244QDebug operator<<(QDebug dbg, const Sink::Query::Comparator &c); 351QDebug operator<<(QDebug dbg, const Sink::Query::Comparator &c);