Caches: method to save values given a list of keys.

This commit is contained in:
Mathew Sutcliffe
2016-06-30 22:20:32 +01:00
parent 242e041ceb
commit 843620ca3c
4 changed files with 50 additions and 0 deletions

View File

@@ -38,6 +38,11 @@ namespace BlackMisc
return saveToFiles(persistentStore(), keyPrefix); return saveToFiles(persistentStore(), keyPrefix);
} }
BlackMisc::CStatusMessage CSettingsCache::saveToStore(const QStringList &keys)
{
return saveToFiles(persistentStore(), keys);
}
void CSettingsCache::enableLocalSave() void CSettingsCache::enableLocalSave()
{ {
connect(CSettingsCache::instance(), &CSettingsCache::valuesSaveRequested, CSettingsCache::instance(), &CSettingsCache::saveToStoreByPacket); connect(CSettingsCache::instance(), &CSettingsCache::valuesSaveRequested, CSettingsCache::instance(), &CSettingsCache::saveToStoreByPacket);

View File

@@ -40,6 +40,9 @@ namespace BlackMisc
//! Save core settings to disk. //! Save core settings to disk.
BlackMisc::CStatusMessage saveToStore(const QString &keyPrefix = {}); BlackMisc::CStatusMessage saveToStore(const QString &keyPrefix = {});
//! Save core settings to disk.
BlackMisc::CStatusMessage saveToStore(const QStringList &keys);
//! Connects signal CValueCache::valuesSaveRequested to a private slot that saves the values. //! Connects signal CValueCache::valuesSaveRequested to a private slot that saves the values.
//! In a dbus distributed scenario, only call this method in the core process. //! In a dbus distributed scenario, only call this method in the core process.
void enableLocalSave(); void enableLocalSave();

View File

@@ -181,6 +181,18 @@ namespace BlackMisc
return map; return map;
} }
CVariantMap CValueCache::getAllValues(const QStringList &keys) const
{
QMutexLocker lock(&m_mutex);
CVariantMap map;
for (const auto &key : keys)
{
auto it = m_elements.constFind(key);
if (it != m_elements.cend()) { map.insert(key, (*it)->m_value); }
}
return map;
}
CValueCachePacket CValueCache::getAllValuesWithTimestamps(const QString &keyPrefix) const CValueCachePacket CValueCache::getAllValuesWithTimestamps(const QString &keyPrefix) const
{ {
QMutexLocker lock(&m_mutex); QMutexLocker lock(&m_mutex);
@@ -299,6 +311,15 @@ namespace BlackMisc
return status; return status;
} }
CStatusMessage CValueCache::saveToFiles(const QString &dir, const QStringList &keys)
{
QMutexLocker lock(&m_mutex);
auto values = getAllValues(keys);
auto status = saveToFiles(dir, values);
if (status.isSuccess()) { markAllAsSaved(keys); }
return status;
}
CStatusMessage CValueCache::saveToFiles(const QString &dir, const CVariantMap &values, const QString &keysMessage) const CStatusMessage CValueCache::saveToFiles(const QString &dir, const CVariantMap &values, const QString &keysMessage) const
{ {
QMap<QString, CVariantMap> namespaces; QMap<QString, CVariantMap> namespaces;
@@ -398,6 +419,15 @@ namespace BlackMisc
} }
} }
void CValueCache::markAllAsSaved(const QStringList &keys)
{
QMutexLocker lock(&m_mutex);
for (const auto &key : keys)
{
getElement(key).m_saved = true;
}
}
QString CValueCache::filenameForKey(const QString &key) QString CValueCache::filenameForKey(const QString &key)
{ {
return key.section('/', 0, 0) + ".json"; return key.section('/', 0, 0) + ".json";

View File

@@ -158,6 +158,10 @@ namespace BlackMisc
//! \threadsafe //! \threadsafe
BlackMisc::CVariantMap getAllValues(const QString &keyPrefix = {}) const; BlackMisc::CVariantMap getAllValues(const QString &keyPrefix = {}) const;
//! Return map containing all given values in the cache.
//! \threadsafe
BlackMisc::CVariantMap getAllValues(const QStringList &keys) const;
//! Return map containing all values in the cache, and timestamps when they were modified. //! Return map containing all values in the cache, and timestamps when they were modified.
//! \threadsafe //! \threadsafe
BlackMisc::CValueCachePacket getAllValuesWithTimestamps(const QString &keyPrefix = {}) const; BlackMisc::CValueCachePacket getAllValuesWithTimestamps(const QString &keyPrefix = {}) const;
@@ -186,6 +190,10 @@ namespace BlackMisc
//! \threadsafe //! \threadsafe
CStatusMessage saveToFiles(const QString &directory, const QString &keyPrefix = {}); CStatusMessage saveToFiles(const QString &directory, const QString &keyPrefix = {});
//! Save values to Json files in a given directory.
//! \threadsafe
CStatusMessage saveToFiles(const QString &directory, const QStringList &keys);
//! Load all values from Json files in a given directory. //! Load all values from Json files in a given directory.
//! Values already in the cache will remain in the cache unless they are overwritten. //! Values already in the cache will remain in the cache unless they are overwritten.
//! \threadsafe //! \threadsafe
@@ -256,6 +264,10 @@ namespace BlackMisc
//! \threadsafe //! \threadsafe
void markAllAsSaved(const QString &keyPrefix); void markAllAsSaved(const QString &keyPrefix);
//! Mark all values with given keys as having been saved.
//! \threadsafe
void markAllAsSaved(const QStringList &keys);
//! Mutex protecting operations which are critical on m_elements. //! Mutex protecting operations which are critical on m_elements.
mutable QMutex m_mutex { QMutex::Recursive }; mutable QMutex m_mutex { QMutex::Recursive };