QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
datastore.h
1 #ifndef QTDATASYNC_DATASTORE_H
2 #define QTDATASYNC_DATASTORE_H
3 
4 #include <functional>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qscopedpointer.h>
8 #include <QtCore/qvariant.h>
9 
10 #include "QtDataSync/qtdatasync_global.h"
11 #include "QtDataSync/objectkey.h"
12 #include "QtDataSync/exception.h"
13 #include "QtDataSync/qtdatasync_helpertypes.h"
14 
15 namespace QtDataSync {
16 
17 class Defaults;
18 
19 class DataStorePrivate;
21 class Q_DATASYNC_EXPORT DataStore : public QObject
22 {
23  Q_OBJECT
24  friend class DataStoreModel;
25 
26 public:
29  {
34  EndsWithMode
35  };
36  Q_ENUM(SearchMode)
37 
38 
39  explicit DataStore(QObject *parent = nullptr);
41  explicit DataStore(const QString &setupName, QObject *parent = nullptr);
42  ~DataStore() override;
43 
45  QString setupName() const;
46 
48  qint64 count(int metaTypeId) const;
50  QStringList keys(int metaTypeId) const;
52  QVariantList loadAll(int metaTypeId) const;
54  bool contains(int metaTypeId, const QString &key) const;
56  inline bool contains(int metaTypeId, const QVariant &key) const {
57  return contains(metaTypeId, key.toString());
58  }
60  QVariant load(int metaTypeId, const QString &key) const;
62  inline QVariant load(int metaTypeId, const QVariant &key) const {
63  return load(metaTypeId, key.toString());
64  }
66  void save(int metaTypeId, QVariant value);
68  bool remove(int metaTypeId, const QString &key);
70  inline bool remove(int metaTypeId, const QVariant &key) {
71  return remove(metaTypeId, key.toString());
72  }
74  void update(int metaTypeId, QObject *object) const;
76  QVariantList search(int metaTypeId, const QString &query, SearchMode mode = RegexpMode) const;
78  void iterate(int metaTypeId,
79  const std::function<bool(QVariant)> &iterator) const;
81  void iterate(int metaTypeId,
82  const std::function<bool(QVariant)> &iterator,
83  bool skipBroken) const; //MAJOR merge overloads
85  void clear(int metaTypeId);
86 
88  template<typename T>
89  quint64 count() const;
91  template<typename T>
92  QStringList keys() const;
98  template<typename T, typename K>
99  QList<K> keys() const;
101  template<typename T>
102  QList<T> loadAll() const;
104  template<typename T>
105  bool contains(const QString &key) const;
107  template<typename T, typename K>
108  bool contains(const K &key) const;
110  template<typename T>
111  T load(const QString &key) const;
113  template<typename T, typename K>
114  T load(const K &key) const;
116  template<typename T>
117  void save(const T &value);
119  template<typename T>
120  bool remove(const QString &key);
122  template<typename T, typename K>
123  bool remove(const K &key);
125  template<typename T>
126  void update(T object) const;
128  template<typename T>
129  QList<T> search(const QString &query, SearchMode mode = RegexpMode) const;
131  template<typename T>
132  void iterate(const std::function<bool(T)> &iterator, bool skipBroken = false) const;
134  template<typename T>
135  void clear();
136 
137 Q_SIGNALS:
139  void dataChanged(int metaTypeId, const QString &key, bool deleted, QPrivateSignal);
141  Q_DECL_DEPRECATED void dataCleared(int metaTypeId, QPrivateSignal);
143  void dataResetted(QPrivateSignal);
144 
145 protected:
147  explicit DataStore(QObject *parent, void*);
149  void initStore(const QString &setupName);
150 
151 private:
153 };
154 
155 
156 
158 class Q_DATASYNC_EXPORT DataStoreException : public Exception
159 {
160 protected:
162  DataStoreException(const Defaults &defaults, const QString &message);
164  DataStoreException(const DataStoreException * const other);
165 };
166 
168 class Q_DATASYNC_EXPORT LocalStoreException : public DataStoreException
169 {
170 public:
172  LocalStoreException(const Defaults &defaults, const ObjectKey &key, const QString &context, const QString &message);
173 
175  ObjectKey key() const;
177  QString context() const;
178 
179  QByteArray className() const noexcept override;
180  QString qWhat() const override;
181  void raise() const override;
182  QException *clone() const override;
183 
184 protected:
186  LocalStoreException(const LocalStoreException * const other);
187 
189  ObjectKey _key;
191  QString _context;
192 };
193 
195 class Q_DATASYNC_EXPORT NoDataException : public DataStoreException
196 {
197 public:
199  NoDataException(const Defaults &defaults, const ObjectKey &key);
200 
202  ObjectKey key() const;
203 
204  QByteArray className() const noexcept override;
205  QString qWhat() const override;
206  void raise() const override;
207  QException *clone() const override;
208 
209 protected:
211  NoDataException(const NoDataException * const other);
212 
214  ObjectKey _key;
215 };
216 
218 class Q_DATASYNC_EXPORT InvalidDataException : public DataStoreException
219 {
220 public:
222  InvalidDataException(const Defaults &defaults, const QByteArray &typeName, const QString &message);
223 
225  QByteArray typeName() const;
226 
227  QByteArray className() const noexcept override;
228  QString qWhat() const override;
229  void raise() const override;
230  QException *clone() const override;
231 
232 protected:
234  InvalidDataException(const InvalidDataException * const other);
235 
237  QByteArray _typeName;
238 };
239 
240 // ------------- GENERIC IMPLEMENTATION -------------
241 
242 template<typename T>
243 quint64 DataStore::count() const
244 {
245  QTDATASYNC_STORE_ASSERT(T);
246  return count(qMetaTypeId<T>());
247 }
248 
249 template<typename T>
251 {
252  QTDATASYNC_STORE_ASSERT(T);
253  return keys(qMetaTypeId<T>());
254 }
255 
256 template<typename T, typename K>
258 {
259  QTDATASYNC_STORE_ASSERT(T);
260  QList<K> rList;
261  for(auto k : keys<T>())
262  rList.append(QVariant(k).template value<K>());
263  return rList;
264 }
265 
266 template<typename T>
268 {
269  QTDATASYNC_STORE_ASSERT(T);
270  QList<T> rList;
271  for(auto v : loadAll(qMetaTypeId<T>()))
272  rList.append(v.template value<T>());
273  return rList;
274 }
275 
276 template<typename T>
277 bool DataStore::contains(const QString &key) const
278 {
279  QTDATASYNC_STORE_ASSERT(T);
280  return contains(qMetaTypeId<T>(), key);
281 }
282 
283 template<typename T, typename K>
284 bool DataStore::contains(const K &key) const
285 {
286  QTDATASYNC_STORE_ASSERT(T);
287  return contains(qMetaTypeId<T>(), QVariant::fromValue(key));
288 }
289 
290 template<typename T>
291 T DataStore::load(const QString &key) const
292 {
293  QTDATASYNC_STORE_ASSERT(T);
294  return load(qMetaTypeId<T>(), key).template value<T>();
295 }
296 
297 template<typename T, typename K>
298 T DataStore::load(const K &key) const
299 {
300  QTDATASYNC_STORE_ASSERT(T);
301  return load(qMetaTypeId<T>(), QVariant::fromValue(key)).template value<T>();
302 }
303 
304 template<typename T>
305 void DataStore::save(const T &value)
306 {
307  QTDATASYNC_STORE_ASSERT(T);
308  save(qMetaTypeId<T>(), QVariant::fromValue(value));
309 }
310 
311 template<typename T>
312 bool DataStore::remove(const QString &key)
313 {
314  QTDATASYNC_STORE_ASSERT(T);
315  return remove(qMetaTypeId<T>(), key);
316 }
317 
318 template<typename T, typename K>
319 bool DataStore::remove(const K &key)
320 {
321  QTDATASYNC_STORE_ASSERT(T);
322  return remove(qMetaTypeId<T>(), QVariant::fromValue(key));
323 }
324 
325 template<typename T>
326 void DataStore::update(T object) const
327 {
328  static_assert(__helpertypes::is_object<T>::value, "loadInto can only be used for pointers to QObject extending classes");
329  update(qMetaTypeId<T>(), object);
330 }
331 
332 template<typename T>
333 QList<T> DataStore::search(const QString &query, SearchMode mode) const
334 {
335  QTDATASYNC_STORE_ASSERT(T);
336  QList<T> rList;
337  for(auto v : search(qMetaTypeId<T>(), query, mode))
338  rList.append(v.template value<T>());
339  return rList;
340 }
341 
342 template<typename T>
343 void DataStore::iterate(const std::function<bool (T)> &iterator, bool skipBroken) const
344 {
345  QTDATASYNC_STORE_ASSERT(T);
346  iterate(qMetaTypeId<T>(), [iterator](const QVariant &v) {
347  return iterator(v.template value<T>());
348  }, skipBroken);
349 }
350 
351 template<typename T>
353 {
354  QTDATASYNC_STORE_ASSERT(T);
355  clear(qMetaTypeId<T>());
356 }
357 
358 }
359 
360 #endif // QTDATASYNC_DATASTORE_H
void update(int metaTypeId, QObject *object) const
Loads the dataset with the given key for the given type into the existing object by updating it&#39;s pro...
bool contains(int metaTypeId, const QVariant &key) const
Checks if a dataset exists in the store for the given key.
Definition: datastore.h:56
The data key must contain the search string.
Definition: datastore.h:32
Main store to generically access all stored data synchronously.
Definition: datastore.h:21
QStringList keys() const
Returns all saved keys for the given type.
Definition: datastore.h:250
The base class for all exceptions of QtDataSync.
Definition: exception.h:13
QVariant load(int metaTypeId, const QString &key) const
Loads the dataset with the given key for the given type.
Exception that is thrown in case no data can be found for a key.
Definition: datastore.h:195
Interpret the search string as a wildcard string (with * and ?)
Definition: datastore.h:31
void append(const T &value)
void clear()
Removes all datasets of the given type from the store.
Definition: datastore.h:352
bool remove(int metaTypeId, const QString &key)
Removes the dataset with the given key for the given type.
QVariantList search(int metaTypeId, const QString &query, SearchMode mode=RegexpMode) const
Searches the store for datasets of the given type where the key matches the query.
void save(int metaTypeId, QVariant value)
Saves the given dataset in the store.
Exception that is thrown when an internal (critical) error occurs.
Definition: datastore.h:168
Defines a unique key to identify a dataset globally.
Definition: objectkey.h:11
The primary namespace of the QtDataSync library.
Exception that is thrown from DataStore operations in case of an error.
Definition: datastore.h:158
quint64 count() const
Counts the number of datasets for the given type.
Definition: datastore.h:243
bool contains(int metaTypeId, const QString &key) const
Checks if a dataset exists in the store for the given key.
QVariant fromValue(const T &value)
void iterate(int metaTypeId, const std::function< bool(QVariant)> &iterator) const
Iterates over all existing datasets of the given types.
QVariant load(int metaTypeId, const QVariant &key) const
Loads the dataset with the given key for the given type.
Definition: datastore.h:62
SearchMode
Possible pattern modes for the search mechanism.
Definition: datastore.h:28
The data key must start with the search string.
Definition: datastore.h:33
A helper class to get defaults per datasync instance (threadsafe)
Definition: defaults.h:61
QList< T > loadAll() const
Loads all existing datasets for the given type.
Definition: datastore.h:267
Interpret the search string as a regular expression. See QRegularExpression.
Definition: datastore.h:30
Exception that is thrown when unsaveable/unloadable data is passed to the store.
Definition: datastore.h:218
QString toString() const const
A passive item model for a datasync data store.