QtDataSync  4.2.0
A simple offline-first synchronisation framework, to synchronize data of Qt applications between devices
Public Types | Public Member Functions | Static Public Member Functions | List of all members
QtDataSync::CachingDataTypeStore< TType, TKey > Class Template Reference

A DataTypeStore that caches all loaded data internally for faster access. More...

#include <datatypestore.h>

+ Inheritance diagram for QtDataSync::CachingDataTypeStore< TType, TKey >:

Public Types

using const_iterator = typename QHash< TKey, TType >::const_iterator
 Typedef for QHash::const_iterator.
 
using iterator = const_iterator
 Typedef for QHash::const_iterator.
 

Public Member Functions

 CachingDataTypeStore (QObject *parent=nullptr)
 Constructs a store for the default setup. More...
 
 CachingDataTypeStore (const QString &setupName, QObject *parent=nullptr)
 Constructs a store for the given setup. More...
 
 CachingDataTypeStore (DataStore *store, QObject *parent=nullptr)
 Constructs a store for the given setup. More...
 
DataStorestore () const override
 Returns a reference to the internally used DataStore. More...
 
qint64 count () const
 Counts the number of datasets for the given type. More...
 
QList< TKey > keys () const
 Returns all saved keys for the given type. More...
 
bool contains (const TKey &key) const
 Checks if a dataset exists in the store for the given key.
 
QList< TType > loadAll () const
 Loads all existing datasets for the given type. More...
 
TType load (const TKey &key) const
 Loads the dataset with the given key for the given type.
 
void save (const TType &value)
 Saves the given dataset in the store. More...
 
bool remove (const TKey &key)
 Removes the dataset with the given key for the given type. More...
 
TType take (const TKey &key)
 Returns the dataset for the given key and removes it from the store. More...
 
void clear ()
 Removes all datasets of the given type from the store. More...
 
const_iterator begin () const
 Returns the begin iterator of the internal hash.
 
const_iterator end () const
 Returns the end iterator of the internal hash.
 
- Public Member Functions inherited from QtDataSync::DataTypeStoreBase
 DataTypeStoreBase (QObject *parent=nullptr)
 Default constructor.
 
QString setupName () const
 Returns the name of the setup this class operates on. More...
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Static Public Member Functions

static TKey toKey (const QString &key)
 Shortcut to convert a string to the stores key type. More...
 
- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 

Additional Inherited Members

- Signals inherited from QtDataSync::DataTypeStoreBase
void dataChanged (const QString &key, const QVariant &value)
 Will be emitted when a dataset in the store has changed. More...
 
void dataResetted ()
 Will be emitted when the store was reset or cleared. More...
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

template<typename TType, typename TKey = QString>
class QtDataSync::CachingDataTypeStore< TType, TKey >

A DataTypeStore that caches all loaded data internally for faster access.

The caching store is an extension to the DataTypeStore. Instead of making requests to the store for any operation, the caching store loads all datasets for one type once on it's creation. All changes you make are directly passed to the store, which means they can trigger exceptions and are permanent, if successful. Changes in the main store are automatically detected and applied to the cached data. If a change operation fails, the cache will not be changed as well.

The main advantage here is that read operations won't access the store but instead used the cached data and thus are much faster and cannot fail. This store is most useful when doing much work with the data with frequent accesses.

Attention
The constructors will call DataStore::loadAll to load all data of the stores type initially. This can be a potentially long operation, and thus you should only use this store if the number of datasets does not get extremly big.

One additional feature of the store is that it provides read-only STL iterators for easy access. Using it with for/foreach however is currently not possible, as the store is not a value type.

See also
DataStore, DataStore::loadAll, DataTypeStore

Definition at line 86 of file datatypestore.h.

Constructor & Destructor Documentation

◆ CachingDataTypeStore() [1/3]

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::CachingDataTypeStore ( QObject parent = nullptr)
explicit

Constructs a store for the default setup.

Parameters
parentThe parent object
Exceptions
SetupDoesNotExistExceptionThrown if the default setup was not created yet

Definition at line 319 of file datatypestore.h.

◆ CachingDataTypeStore() [2/3]

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::CachingDataTypeStore ( const QString setupName,
QObject parent = nullptr 
)
explicit

Constructs a store for the given setup.

Parameters
setupNameThe name of the setup to connect to
parentThe parent object
Exceptions
SetupDoesNotExistExceptionThrown if the given setup was not created yet

Definition at line 324 of file datatypestore.h.

◆ CachingDataTypeStore() [3/3]

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::CachingDataTypeStore ( DataStore store,
QObject parent = nullptr 
)
explicit

Constructs a store for the given setup.

Parameters
storeThe store to be used by the model
parentThe parent object
Attention
The type store does not take ownership of the passed store. Thus, the store must stay valid as long as the model exists.

Definition at line 331 of file datatypestore.h.

Member Function Documentation

◆ clear()

template<typename TType , typename TKey >
void QtDataSync::CachingDataTypeStore< TType, TKey >::clear ( )

Removes all datasets of the given type from the store.

Exceptions
LocalStoreExceptionIn case of an internal error
See also
DataTypeStore::dataResetted, DataTypeStore::remove

Definition at line 406 of file datatypestore.h.

◆ count()

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::count ( ) const

Counts the number of datasets for the given type.

Returns
The number of datasets available in the cache
See also
CachingDataTypeStore::keys

Definition at line 354 of file datatypestore.h.

◆ keys()

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::keys ( ) const

Returns all saved keys for the given type.

Returns
A list of all keys of all cached datasets
See also
CachingDataTypeStore::count, CachingDataTypeStore::loadAll, CachingDataTypeStore::contains, CachingDataTypeStore::load

Definition at line 360 of file datatypestore.h.

◆ loadAll()

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::loadAll ( ) const

Loads all existing datasets for the given type.

Returns
A list of all cached datasets

Unlike with the DataStore or DataTypeStore, this method will not take extremly long blocking the store, as it only needs to pass the cached values.

See also
CachingDataTypeStore::begin, CachingDataTypeStore::end, CachingDataTypeStore::load, CachingDataTypeStore::keys

Definition at line 372 of file datatypestore.h.

◆ remove()

template<typename TType , typename TKey >
bool QtDataSync::CachingDataTypeStore< TType, TKey >::remove ( const TKey &  key)

Removes the dataset with the given key for the given type.

Parameters
keyThe key of the dataset to be removed
Returns
true in case the dataset was removed, false if it did not exist
Exceptions
LocalStoreExceptionIn case of an internal error
See also
DataTypeStore::save, DataTypeStore::clear, DataTypeStore::load, DataTypeStore::dataChanged

Definition at line 390 of file datatypestore.h.

◆ save()

template<typename TType , typename TKey >
void QtDataSync::CachingDataTypeStore< TType, TKey >::save ( const TType &  value)

Saves the given dataset in the store.

Parameters
valueThe dataset to be stored
Exceptions
InvalidDataExceptionIn case the given type cannot be stored
LocalStoreExceptionIn case of an internal error
See also
DataTypeStore::remove, DataTypeStore::load, DataTypeStore::dataChanged

Definition at line 384 of file datatypestore.h.

◆ store()

template<typename TType , typename TKey >
DataStore * QtDataSync::CachingDataTypeStore< TType, TKey >::store ( ) const
overridevirtual

Returns a reference to the internally used DataStore.

Returns
A reference to the internally used store
Note
Most models do own their own datastore (only not the case if the store was passed via the constructor). The most returned stores are thus deleted as soon as the model gets destroyed.

Implements QtDataSync::DataTypeStoreBase.

Definition at line 348 of file datatypestore.h.

◆ take()

template<typename TType , typename TKey >
QtDataSync::CachingDataTypeStore< TType, TKey >::take ( const TKey &  key)

Returns the dataset for the given key and removes it from the store.

Parameters
keyThe key of the dataset to be taken
Returns
The dataset for the given type and key if currently cached, a default constructed value otherwise
Exceptions
LocalStoreExceptionIn case of an internal error
Note
If the data you are trying to take is not cached, nothing will be done. If it is cached, it is removed from the cache and the permanent store. If that remove operation fails with an exception, neither happens.
See also
CachingDataTypeStore::loadAll, CachingDataTypeStore::keys, CachingDataTypeStore::contains

Definition at line 396 of file datatypestore.h.

◆ toKey()

template<typename TType , typename TKey >
TKey QtDataSync::CachingDataTypeStore< TType, TKey >::toKey ( const QString key)
static

Shortcut to convert a string to the stores key type.

Parameters
keyThe key as a string
Returns
The key as a TKey type

The conversion is performed by using QVariant::convert

Definition at line 424 of file datatypestore.h.


The documentation for this class was generated from the following files: