6#ifndef SWIFT_MISC_DB_DATABASEOBJECTLIST_H
7#define SWIFT_MISC_DB_DATABASEOBJECTLIST_H
20namespace swift::misc::db
25 template <
class OBJ,
class CONTAINER,
typename KEYTYPE>
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");
37 OBJ
findByKey(KEYTYPE key,
const OBJ ¬Found = OBJ())
const
39 return this->
container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound);
56 objects.push_back(obj);
67 if (!obj.hasValidDbKey())
71 objects.push_back(obj);
82 if (obj.hasValidDbKey())
86 objects.push_back(obj);
99 [](
const OBJ &obj1,
const OBJ &obj2) {
100 bool v1 = obj1.hasValidDbKey();
101 bool v2 = obj2.hasValidDbKey();
104 return obj1.getDbKey() < obj2.getDbKey();
120 if (!obj.hasValidDbKey())
124 keys.
insert(obj.getDbKey());
135 if (!obj.hasValidDbKey())
139 map.insert(obj.getDbKey(), obj);
150 if (!obj.hasValidDbKey())
154 keys.
insert(obj.getDbKeyAsString());
194 KEYTYPE max = *std::max_element(keys.
begin(), keys.
end());
220 newValues.push_back(obj);
222 const int delta = this->
container().size() - newValues.size();
240 if (!obj.hasValidDbKey())
244 newValues.push_back(obj);
246 int delta = this->
container().size() - newValues.size();
265 newValues.removeObjectsWithKeys(keys);
266 int removeSize = newValues.size();
269 return newValues.size() - removeSize;
276 objs.removeObjectsWithoutDbKey();
281 return objs.latestTimestamp();
288 objs.removeObjectsWithoutDbKey();
293 return objs.oldestDbTimestamp();
302 if (obj.hasValidDbKey() && withKey)
318 [](
const OBJ &obj) {
return !obj.hasValidDbKey(); });
326 [&](
const OBJ &obj) {
return obj.hasValidDbKey() && obj.getDbKey() == key; });
341 if (json::looksLikeSwiftDataObjectJson(jsonObject))
343 const QJsonObject cacheObj = json::swiftDataObjectValue(jsonObject);
345 private_ns::CValueObjectMetaInfoHelper::convertFromMemoizedJson(
351 if (json::looksLikeSwiftContainerJson(jsonObject))
359 if (json::looksLikeSwiftTypeValuePairJson(jsonObject))
363 private_ns::CValueObjectMetaInfoHelper::convertFromMemoizedJson(
400 container.push_back(OBJ::fromDatabaseJson(value.toObject()));
412 container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix));
Thrown when a convertFromJson method encounters an unrecoverable error in JSON data.
Build a QMap more efficiently when calling insert() in a for loop.
Build a QSet more efficiently when calling insert() in a for loop.
void insert(const T &value)
Add an element to the set. Runs in amortized constant time.
ITimestampObjectList()=default
Constructor.
const CONTAINER & container() const
Container.
int replaceOrAddObjectsByKey(const CONTAINER &container)
Update or insert data (based on DB key).
void sortByKey()
Sort by timestamp.
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 ¬Found=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.
auto MemberLess(Ts... vs)
Predicate which compares the return values of some member functions of two objects.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
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