summaryrefslogtreecommitdiffstats
path: root/common/query.h
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-04 15:07:35 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-11-04 15:07:35 +0100
commitc04755a772cbc6b2cf3a80e9c3c17b718e153c55 (patch)
tree9284c7c1bcd58912d19a5d2a614b5953443556ff /common/query.h
parentba94c4300c52dd80774ed7affc2ef9b4508cb7be (diff)
downloadsink-c04755a772cbc6b2cf3a80e9c3c17b718e153c55.tar.gz
sink-c04755a772cbc6b2cf3a80e9c3c17b718e153c55.zip
User querybase
Diffstat (limited to 'common/query.h')
-rw-r--r--common/query.h292
1 files changed, 177 insertions, 115 deletions
diff --git a/common/query.h b/common/query.h
index c9d52b7..b858610 100644
--- a/common/query.h
+++ b/common/query.h
@@ -73,6 +73,11 @@ public:
73 return mBaseFilterStage.propertyFilter; 73 return mBaseFilterStage.propertyFilter;
74 } 74 }
75 75
76 Filter getFilter() const
77 {
78 return mBaseFilterStage;
79 }
80
76 QByteArrayList ids() const 81 QByteArrayList ids() const
77 { 82 {
78 return mBaseFilterStage.ids; 83 return mBaseFilterStage.ids;
@@ -103,9 +108,143 @@ public:
103 return mType; 108 return mType;
104 } 109 }
105 110
111 void setSortProperty(const QByteArray &property)
112 {
113 mSortPorperty = property;
114 }
115
116 QByteArray sortProperty() const
117 {
118 return mSortPorperty;
119 }
120
121 class FilterStage {
122 public:
123 virtual ~FilterStage(){};
124 };
125
126 QList<QSharedPointer<FilterStage>> getFilterStages()
127 {
128 return mFilterStages;
129 }
130
131 class Reduce : public FilterStage {
132 public:
133
134 class Selector {
135 public:
136 enum Comparator {
137 Min, //get the minimum value
138 Max, //get the maximum value
139 First //Get the first result we get
140 };
141
142 template <typename SelectionProperty>
143 static Selector max()
144 {
145 return Selector(SelectionProperty::name, Max);
146 }
147
148 Selector(const QByteArray &p, Comparator c)
149 : property(p),
150 comparator(c)
151 {
152 }
153
154 QByteArray property;
155 Comparator comparator;
156 };
157
158 class Aggregator {
159 public:
160 enum Operation {
161 Count,
162 Collect
163 };
164
165 Aggregator(const QByteArray &p, Operation o, const QByteArray &c = QByteArray())
166 : resultProperty(p),
167 operation(o),
168 propertyToCollect(c)
169 {
170 }
171
172 QByteArray resultProperty;
173 Operation operation;
174 QByteArray propertyToCollect;
175 };
176
177 Reduce(const QByteArray &p, const Selector &s)
178 : property(p),
179 selector(s)
180 {
181 }
182
183 Reduce &count(const QByteArray &propertyName = "count")
184 {
185 aggregators << Aggregator(propertyName, Aggregator::Count);
186 return *this;
187 }
188
189 template <typename T>
190 Reduce &collect(const QByteArray &propertyName)
191 {
192 aggregators << Aggregator(propertyName, Aggregator::Collect, T::name);
193 return *this;
194 }
195
196 //Reduce on property
197 QByteArray property;
198 Selector selector;
199 QList<Aggregator> aggregators;
200
201 //TODO add aggregate functions like:
202 //.count()
203 //.collect<Mail::sender>();
204 //...
205 //
206 //Potentially pass-in an identifier under which the result will be available in the result set.
207 };
208
209 template <typename T>
210 Reduce &reduce(const Reduce::Selector &s)
211 {
212 auto reduction = QSharedPointer<Reduce>::create(T::name, s);
213 mFilterStages << reduction;
214 return *reduction;
215 }
216
217 /**
218 * "Bloom" on a property.
219 *
220 * For every encountered value of a property,
221 * a result set is generated containing all entries with the same value.
222 *
223 * Example:
224 * For an input set of one mail; return all emails with the same threadId.
225 */
226 class Bloom : public FilterStage {
227 public:
228 //Property to bloom on
229 QByteArray property;
230 Bloom(const QByteArray &p)
231 : property(p)
232 {
233 }
234 };
235
236 template <typename T>
237 void bloom()
238 {
239 auto bloom = QSharedPointer<Bloom>::create(T::name);
240 mFilterStages << bloom;
241 }
242
106private: 243private:
107 Filter mBaseFilterStage; 244 Filter mBaseFilterStage;
245 QList<QSharedPointer<FilterStage>> mFilterStages;
108 QByteArray mType; 246 QByteArray mType;
247 QByteArray mSortPorperty;
109}; 248};
110 249
111/** 250/**
@@ -140,7 +279,7 @@ public:
140 template <typename T> 279 template <typename T>
141 Query &sort() 280 Query &sort()
142 { 281 {
143 sortProperty = T::name; 282 setSortProperty(T::name);
144 return *this; 283 return *this;
145 } 284 }
146 285
@@ -212,7 +351,6 @@ public:
212 351
213 QByteArrayList requestedProperties; 352 QByteArrayList requestedProperties;
214 QByteArray parentProperty; 353 QByteArray parentProperty;
215 QByteArray sortProperty;
216 int limit; 354 int limit;
217 355
218 void setFlags(Flags flags) 356 void setFlags(Flags flags)
@@ -230,16 +368,6 @@ public:
230 return mFlags & SynchronousQuery; 368 return mFlags & SynchronousQuery;
231 } 369 }
232 370
233 class FilterStage {
234 public:
235 virtual ~FilterStage(){};
236 };
237
238 QList<QSharedPointer<FilterStage>> getFilterStages()
239 {
240 return mFilterStages;
241 }
242
243 Filter getResourceFilter() const 371 Filter getResourceFilter() const
244 { 372 {
245 return mResourceFilter; 373 return mResourceFilter;
@@ -276,122 +404,56 @@ public:
276 return resourceFilter(T::name, value); 404 return resourceFilter(T::name, value);
277 } 405 }
278 406
279 class Reduce : public FilterStage { 407private:
280 public: 408 Flags mFlags;
281 409 Filter mResourceFilter;
282 class Selector { 410};
283 public:
284 enum Comparator {
285 Min, //get the minimum value
286 Max, //get the maximum value
287 First //Get the first result we get
288 };
289
290 template <typename SelectionProperty>
291 static Selector max()
292 {
293 return Selector(SelectionProperty::name, Max);
294 }
295
296 Selector(const QByteArray &p, Comparator c)
297 : property(p),
298 comparator(c)
299 {
300 }
301
302 QByteArray property;
303 Comparator comparator;
304 };
305
306 class Aggregator {
307 public:
308 enum Operation {
309 Count,
310 Collect
311 };
312
313 Aggregator(const QByteArray &p, Operation o, const QByteArray &c = QByteArray())
314 : resultProperty(p),
315 operation(o),
316 propertyToCollect(c)
317 {
318 }
319
320 QByteArray resultProperty;
321 Operation operation;
322 QByteArray propertyToCollect;
323 };
324
325 Reduce(const QByteArray &p, const Selector &s)
326 : property(p),
327 selector(s)
328 {
329 }
330
331 Reduce &count(const QByteArray &propertyName = "count")
332 {
333 aggregators << Aggregator(propertyName, Aggregator::Count);
334 return *this;
335 }
336 411
337 template <typename T> 412class SyncScope : public QueryBase {
338 Reduce &collect(const QByteArray &propertyName) 413public:
339 { 414 template<typename T>
340 aggregators << Aggregator(propertyName, Aggregator::Collect, T::name); 415 SyncScope()
341 return *this; 416 {
342 } 417 setType(ApplicationDomain::getTypeName<T>());
418 }
343 419
344 //Reduce on property 420 Query::Filter getResourceFilter() const
345 QByteArray property; 421 {
346 Selector selector; 422 return mResourceFilter;
347 QList<Aggregator> aggregators; 423 }
348 424
349 //TODO add aggregate functions like: 425 SyncScope &resourceFilter(const QByteArray &id)
350 //.count() 426 {
351 //.collect<Mail::sender>(); 427 mResourceFilter.ids << id;
352 //... 428 return *this;
353 // 429 }
354 //Potentially pass-in an identifier under which the result will be available in the result set.
355 };
356 430
357 template <typename T> 431 template <typename T>
358 Reduce &reduce(const Reduce::Selector &s) 432 SyncScope &filter(const Query::Comparator &comparator)
359 { 433 {
360 auto reduction = QSharedPointer<Reduce>::create(T::name, s); 434 return filter(T::name, comparator);
361 mFilterStages << reduction;
362 return *reduction;
363 } 435 }
364 436
365 /** 437 SyncScope &filter(const QByteArray &id)
366 * "Bloom" on a property. 438 {
367 * 439 QueryBase::filter(id);
368 * For every encountered value of a property, 440 return *this;
369 * a result set is generated containing all entries with the same value. 441 }
370 *
371 * Example:
372 * For an input set of one mail; return all emails with the same threadId.
373 */
374 class Bloom : public FilterStage {
375 public:
376 //Property to bloom on
377 QByteArray property;
378 Bloom(const QByteArray &p)
379 : property(p)
380 {
381 }
382 };
383 442
384 template <typename T> 443 SyncScope &filter(const QByteArrayList &ids)
385 void bloom()
386 { 444 {
387 auto bloom = QSharedPointer<Bloom>::create(T::name); 445 QueryBase::filter(ids);
388 mFilterStages << bloom; 446 return *this;
447 }
448
449 SyncScope &filter(const QByteArray &property, const Query::Comparator &comparator)
450 {
451 QueryBase::filter(property, comparator);
452 return *this;
389 } 453 }
390 454
391private: 455private:
392 Flags mFlags; 456 Query::Filter mResourceFilter;
393 Filter mResourceFilter;
394 QList<QSharedPointer<FilterStage>> mFilterStages;
395}; 457};
396 458
397} 459}