Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 68b29ec

Browse files
committed
added iterator (untested)
1 parent 1479767 commit 68b29ec

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

src/datasync/asyncdatastore.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ Task AsyncDataStore::search(int dataMetaTypeId, int listMetaTypeId, const QStrin
6969
return internalSearch(dataMetaTypeId, listMetaTypeId, searchQuery);
7070
}
7171

72+
void AsyncDataStore::iterate(int metaTypeId, const std::function<bool(QVariant)> &iterator, const std::function<void(const QException &)> &onExcept)
73+
{
74+
keys(metaTypeId).onResult([=](QStringList keys) {
75+
if(!keys.isEmpty()) {
76+
load(metaTypeId, keys[0]).onResult([=](QVariant result) {
77+
internalIterate(metaTypeId, keys, 0, result, iterator, onExcept);
78+
}, onExcept);
79+
}
80+
}, onExcept);
81+
}
82+
7283
QFutureInterface<QVariant> AsyncDataStore::internalCount(int metaTypeId)
7384
{
7485
QFutureInterface<QVariant> interface;
@@ -160,3 +171,14 @@ QFutureInterface<QVariant> AsyncDataStore::internalSearch(int dataMetaTypeId, in
160171
Q_ARG(QVariant, data));
161172
return interface;
162173
}
174+
175+
void AsyncDataStore::internalIterate(int metaTypeId, const QStringList &keys, int index, QVariant res, const std::function<bool (QVariant)> &iterator, const std::function<void (const QException &)> &onExcept)
176+
{
177+
if(iterator(res)) {
178+
if(++index < keys.size()) {
179+
load(metaTypeId, keys[index]).onResult([=](QVariant result) {
180+
internalIterate(metaTypeId, keys, index, result, iterator, onExcept);
181+
}, onExcept);
182+
}
183+
}
184+
}

src/datasync/asyncdatastore.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QtCore/qobject.h>
88
#include <QtCore/qfuture.h>
99
#include <QtCore/qmetaobject.h>
10+
#include <functional>
1011

1112
namespace QtDataSync {
1213

@@ -44,6 +45,10 @@ class Q_DATASYNC_EXPORT AsyncDataStore : public QObject
4445
Task remove(int metaTypeId, const QVariant &key);
4546
//! @copybrief AsyncDataStore::search(const QString &)
4647
Task search(int dataMetaTypeId, int listMetaTypeId, const QString &query);
48+
//! @copybrief AsyncDataStore::iterate(const std::function<bool(T)> &, const std::function<void(const QException &)> &)
49+
void iterate(int metaTypeId,
50+
const std::function<bool(QVariant)> &iterator,
51+
const std::function<void(const QException &)> &onExcept);
4752

4853
//! Counts the number of datasets for the given type
4954
template<typename T>
@@ -72,6 +77,10 @@ class Q_DATASYNC_EXPORT AsyncDataStore : public QObject
7277
//! Searches the store for datasets of the given type where the key matches the query
7378
template<typename T>
7479
GenericTask<QList<T>> search(const QString &query);
80+
//! Asynchronously iterates over all existing datasets of the given types
81+
template<typename T>
82+
void iterate(const std::function<bool(T)> &iterator,
83+
const std::function<void(const QException &)> &onExcept);
7584

7685
//! Loads the dataset with the given key for the given type into the existing object by updating it's properties
7786
template<typename T>
@@ -96,6 +105,13 @@ class Q_DATASYNC_EXPORT AsyncDataStore : public QObject
96105
QFutureInterface<QVariant> internalSave(int metaTypeId, const QVariant &value);
97106
QFutureInterface<QVariant> internalRemove(int metaTypeId, const QString &key);
98107
QFutureInterface<QVariant> internalSearch(int dataMetaTypeId, int listMetaTypeId, const QString &query);
108+
109+
void internalIterate(int metaTypeId,
110+
const QStringList &keys,
111+
int index,
112+
QVariant res,
113+
const std::function<bool(QVariant)> &iterator,
114+
const std::function<void(const QException &)> &onExcept);
99115
};
100116

101117
template<typename T>
@@ -152,6 +168,14 @@ GenericTask<QList<T>> AsyncDataStore::search(const QString &query)
152168
return internalSearch(qMetaTypeId<T>(), qMetaTypeId<QList<T>>(), query);
153169
}
154170

171+
template<typename T>
172+
void AsyncDataStore::iterate(const std::function<bool(T)> &iterator, const std::function<void(const QException &)> &onExcept)
173+
{
174+
return iterate(qMetaTypeId<T>(), [iterator](QVariant variant) {
175+
return iterator(variant.template value<T>());
176+
}, onExcept);
177+
}
178+
155179
template<typename T>
156180
UpdateTask<T> AsyncDataStore::loadInto(const QString &key, const T &object)
157181
{

src/datasync/storageengine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ void StorageEngine::clearAuthError()
231231
{
232232
if(!currentAuthError.isNull()) {
233233
currentAuthError.clear();
234-
emit authenticationErrorChanged(currentAuthError);
234+
emit authenticationErrorChanged({});
235235
}
236236
}
237237

0 commit comments

Comments
 (0)