swift
Loading...
Searching...
No Matches
datastoreobjectlist.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_DB_DATABASEOBJECTLIST_H
7#define SWIFT_MISC_DB_DATABASEOBJECTLIST_H
8
9#include <QJsonArray>
10#include <QMap>
11#include <QSet>
12#include <QString>
13
14#include "misc/db/datastore.h"
15#include "misc/jsonexception.h"
16#include "misc/mapbuilder.h"
17#include "misc/setbuilder.h"
19
20namespace swift::misc::db
21{
25 template <class OBJ, class CONTAINER, typename KEYTYPE>
26 class IDatastoreObjectList : public ITimestampObjectList<OBJ, CONTAINER>
27 {
28 static constexpr bool hasIntegerKey =
29 std::is_base_of_v<IDatastoreObjectWithIntegerKey, OBJ> && std::is_same_v<int, KEYTYPE>;
30 static constexpr bool hasStringKey =
31 std::is_base_of_v<IDatastoreObjectWithStringKey, OBJ> && std::is_base_of_v<QString, KEYTYPE>;
32 static_assert(hasIntegerKey || hasStringKey,
33 "ObjectType needs to implement IDatastoreObjectWithXXXXKey and have appropriate KeyType");
34
35 public:
37 OBJ findByKey(KEYTYPE key, const OBJ &notFound = OBJ()) const
38 {
39 return this->container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound);
40 }
41
43 CONTAINER findByKeys(const QSet<KEYTYPE> &keys) const
44 {
45 CONTAINER objects;
46 if (keys.isEmpty())
47 {
48 return objects;
49 }
51 {
52 if (!keys.contains(obj.getDbKey()))
53 {
54 continue;
55 }
56 objects.push_back(obj);
57 }
58 return objects;
59 }
60
62 CONTAINER findObjectsWithDbKey() const
63 {
64 CONTAINER objects;
66 {
67 if (!obj.hasValidDbKey())
68 {
69 continue;
70 }
71 objects.push_back(obj);
72 }
73 return objects;
74 }
75
77 CONTAINER findObjectsWithoutDbKey() const
78 {
79 CONTAINER objects;
81 {
82 if (obj.hasValidDbKey())
83 {
84 continue;
85 }
86 objects.push_back(obj);
87 }
88 return objects;
89 }
90
92 OBJ maxKeyObject() const
93 {
94 if (this->container().isEmpty())
95 {
96 return OBJ();
97 }
98 const OBJ max = *std::max_element(this->container().begin(), this->container().end(),
99 [](const OBJ &obj1, const OBJ &obj2) {
100 bool v1 = obj1.hasValidDbKey();
101 bool v2 = obj2.hasValidDbKey();
102 if (v1 && v2)
103 {
104 return obj1.getDbKey() < obj2.getDbKey();
105 }
106 return v2;
107 });
108 return max;
109 }
110
112 void sortByKey() { this->container().sort(swift::misc::predicates::MemberLess(&OBJ::getDbKey)); }
113
116 {
119 {
120 if (!obj.hasValidDbKey())
121 {
122 continue;
123 }
124 keys.insert(obj.getDbKey());
125 }
126 return keys;
127 }
128
131 {
134 {
135 if (!obj.hasValidDbKey())
136 {
137 continue;
138 }
139 map.insert(obj.getDbKey(), obj);
140 }
141 return map;
142 }
143
146 {
149 {
150 if (!obj.hasValidDbKey())
151 {
152 continue;
153 }
154 keys.insert(obj.getDbKeyAsString());
155 }
156 return keys;
157 }
158
160 QString dbKeysAsString(const QString &separator) const
161 {
163 {
164 return {};
165 }
167 QString s;
168 for (const QString &k : keys)
169 {
170 if (s.isEmpty())
171 {
172 s += k;
173 }
174 else
175 {
176 s += separator + k;
177 }
178 }
179 return s;
180 }
181
183 KEYTYPE getMaxKey(bool *ok = nullptr) const
184 {
185 QSet<KEYTYPE> keys(this->toDbKeySet());
186 if (keys.isEmpty())
187 {
188 if (ok)
189 {
190 *ok = false;
191 }
192 return KEYTYPE();
193 }
194 KEYTYPE max = *std::max_element(keys.begin(), keys.end());
195 if (ok)
196 {
197 *ok = true;
198 }
199 return max;
200 }
201
204 {
205 if (keys.isEmpty())
206 {
207 return 0;
208 }
209 if (this->container().isEmpty())
210 {
211 return 0;
212 }
213 CONTAINER newValues;
215 {
216 if (keys.contains(obj.getDbKey()))
217 {
218 continue;
219 }
220 newValues.push_back(obj);
221 }
222 const int delta = this->container().size() - newValues.size();
223 if (delta > 0)
224 {
225 this->container() = newValues;
226 }
227 return delta;
228 }
229
232 {
233 if (this->container().isEmpty())
234 {
235 return 0;
236 }
237 CONTAINER newValues;
239 {
240 if (!obj.hasValidDbKey())
241 {
242 continue;
243 }
244 newValues.push_back(obj);
245 }
246 int delta = this->container().size() - newValues.size();
247 this->container() = newValues;
248 return delta;
249 }
250
253 {
254 if (container.isEmpty())
255 {
256 return 0;
257 }
258 if (this->container().isEmpty())
259 {
260 this->container() = container;
261 return this->container().size();
262 }
263 CONTAINER newValues(this->container());
264 const QSet<KEYTYPE> keys(container.toDbKeySet());
265 newValues.removeObjectsWithKeys(keys);
266 int removeSize = newValues.size(); // size after removing data
267 newValues.push_back(container);
268 this->container() = newValues;
269 return newValues.size() - removeSize;
270 }
271
274 {
275 CONTAINER objs(this->container());
276 objs.removeObjectsWithoutDbKey();
277 if (objs.isEmpty())
278 {
279 return {};
280 }
281 return objs.latestTimestamp();
282 }
283
286 {
287 CONTAINER objs(this->container());
288 objs.removeObjectsWithoutDbKey();
289 if (objs.isEmpty())
290 {
291 return {};
292 }
293 return objs.oldestDbTimestamp();
294 }
295
297 int countWithValidDbKey(bool withKey) const
298 {
299 int count = 0;
301 {
302 if (obj.hasValidDbKey() && withKey)
303 {
304 count++;
305 }
306 }
307 return count;
308 }
309
311 int countWithValidDbKey() const { return this->countWithValidDbKey(true); }
312
315 {
316 return std::any_of(ITimestampObjectList<OBJ, CONTAINER>::container().cbegin(),
318 [](const OBJ &obj) { return !obj.hasValidDbKey(); });
319 }
320
322 bool containsDbKey(KEYTYPE key) const
323 {
324 return std::any_of(ITimestampObjectList<OBJ, CONTAINER>::container().cbegin(),
326 [&](const OBJ &obj) { return obj.hasValidDbKey() && obj.getDbKey() == key; });
327 }
328
331 static CONTAINER fromMultipleJsonFormats(const QJsonObject &jsonObject)
332 {
333 // also accept cache format
334 if (jsonObject.isEmpty())
335 {
336 const CONTAINER c;
337 return c;
338 }
339
340 // cache or settings format?
341 if (json::looksLikeSwiftDataObjectJson(jsonObject))
342 {
343 const QJsonObject cacheObj = json::swiftDataObjectValue(jsonObject);
344 CONTAINER container;
345 private_ns::CValueObjectMetaInfoHelper::convertFromMemoizedJson(
346 cacheObj, container, true, 0); // handles both, memoized or "normal" convertFromJson
347 return container;
348 }
349
350 // plain vanilla container, does not match memoized objects
351 if (json::looksLikeSwiftContainerJson(jsonObject))
352 {
353 CONTAINER container;
354 container.convertFromJson(jsonObject);
355 return container;
356 }
357
358 // still as type/value pair
359 if (json::looksLikeSwiftTypeValuePairJson(jsonObject))
360 {
361 const QJsonObject valueObject = jsonObject.value("value").toObject();
362 CONTAINER container;
363 private_ns::CValueObjectMetaInfoHelper::convertFromMemoizedJson(
364 valueObject, container, true, 0); // handles both, memoized or "normal" convertFromJson
365 return container;
366 }
367
368 // DB format, as array
369 if (jsonObject.contains("data"))
370 {
371 return IDatastoreObjectList::fromDatabaseJson(jsonObject.value("data").toArray());
372 }
373
374 // no idea what this is
375 throw CJsonException("Unsupported JSON format");
376 }
377
380 static CONTAINER fromMultipleJsonFormats(const QString &jsonString)
381 {
382 // also accept cache format
383 if (jsonString.isEmpty())
384 {
385 const CONTAINER c;
386 return c;
387 }
388
389 const QJsonObject jo = json::jsonObjectFromString(jsonString, false);
391 }
392
395 static CONTAINER fromDatabaseJson(const QJsonArray &array)
396 {
397 CONTAINER container;
398 for (const QJsonValue &value : array)
399 {
400 container.push_back(OBJ::fromDatabaseJson(value.toObject()));
401 }
402 return container;
403 }
404
407 static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix)
408 {
409 CONTAINER container;
410 for (const QJsonValue &value : array)
411 {
412 container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix));
413 }
414 return container;
415 }
416
417 protected:
420 };
421} // namespace swift::misc::db
422
423#endif // SWIFT_MISC_DB_DATABASEOBJECTLIST_H
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
Build a QMap more efficiently when calling insert() in a for loop.
Definition mapbuilder.h:26
Build a QSet more efficiently when calling insert() in a for loop.
Definition setbuilder.h:25
void insert(const T &value)
Add an element to the set. Runs in amortized constant time.
Definition setbuilder.h:29
ITimestampObjectList()=default
Constructor.
const CONTAINER & container() const
Container.
int replaceOrAddObjectsByKey(const CONTAINER &container)
Update or insert data (based on DB key).
QDateTime oldestDbTimestamp() const
Latest DB timestamp (means objects with DB key).
int removeObjectsWithKeys(const QSet< KEYTYPE > &keys)
Remove objects with keys.
int removeObjectsWithoutDbKey()
Remove objects without key.
QSet< KEYTYPE > toDbKeySet() const
All keys as set.
OBJ findByKey(KEYTYPE key, const OBJ &notFound=OBJ()) const
Object with key, notFound otherwise.
static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix)
From DB JSON.
OBJ maxKeyObject() const
Object with max.key.
KEYTYPE getMaxKey(bool *ok=nullptr) const
Max.key value (making sense with integer key).
QDateTime latestDbTimestamp() const
Latest DB timestamp (means objects with DB key).
static CONTAINER fromDatabaseJson(const QJsonArray &array)
From DB JSON with default prefixes.
QString dbKeysAsString(const QString &separator) const
The DB keys as string.
QSet< QString > toDbKeyStringSet() const
All keys as string set (also integer keys will be converted to string).
IDatastoreObjectList()=default
Constructor.
static CONTAINER fromMultipleJsonFormats(const QJsonObject &jsonObject)
From multiple JSON formats.
int countWithValidDbKey() const
Number of entries with valid DB key.
CONTAINER findObjectsWithoutDbKey() const
Objects without DB key.
CONTAINER findByKeys(const QSet< KEYTYPE > &keys) const
Object with key, notFound otherwise.
static CONTAINER fromMultipleJsonFormats(const QString &jsonString)
From multiple JSON formats.
bool containsAnyObjectWithoutKey() const
Any object without key?
QMap< KEYTYPE, OBJ > toDbKeyValueMap() const
As map with DB key/object.
int countWithValidDbKey(bool withKey) const
Number of objects with/without key.
bool containsDbKey(KEYTYPE key) const
Contains object with key?
CONTAINER findObjectsWithDbKey() const
Objects with DB key.
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
JSON Object from string.
Definition json.cpp:419
auto MemberLess(Ts... vs)
Predicate which compares the return values of some member functions of two objects.
Definition predicates.h:43
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:258
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:264
bool contains(QLatin1StringView key) const const
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
QJsonObject toObject() const const
QSet< T >::iterator begin()
bool contains(const QSet< T > &other) const const
QSet< T >::iterator end()
bool isEmpty() const const
bool isEmpty() const const