refs #646 Added timestamps in data cache load/save log messages.

This commit is contained in:
Mathew Sutcliffe
2016-05-19 22:42:30 +01:00
parent 74f577eec9
commit 9da53bd58b
4 changed files with 39 additions and 8 deletions

View File

@@ -234,7 +234,7 @@ namespace BlackMisc
if (! lock) { return; }
m_cache->m_revision.writeNewRevision(baseline.toTimestampMap());
auto msg = m_cache->saveToFiles(persistentStore(), values);
auto msg = m_cache->saveToFiles(persistentStore(), values, baseline.toTimestampMapString());
msg.setCategories(this);
CLogMessage::preformatted(msg);
@@ -247,7 +247,7 @@ namespace BlackMisc
if (lock && m_cache->m_revision.isPendingRead())
{
CValueCachePacket newValues;
auto msg = m_cache->loadFromFiles(persistentStore(), m_cache->m_revision.keysWithNewerTimestamps(), baseline.toVariantMap(), newValues);
auto msg = m_cache->loadFromFiles(persistentStore(), m_cache->m_revision.keysWithNewerTimestamps(), baseline.toVariantMap(), newValues, m_cache->m_revision.timestampsAsString());
msg.setCategories(this);
CLogMessage::preformatted(msg);
m_deferredChanges.insert(newValues);
@@ -451,6 +451,18 @@ namespace BlackMisc
return std::move(m_promises); // move into the return value, so m_promises becomes empty
}
QString CDataCacheRevision::timestampsAsString() const
{
QMutexLocker lock(&m_mutex);
QStringList result;
for (auto it = m_timestamps.cbegin(); it != m_timestamps.cend(); ++it)
{
result.push_back(it.key() + "(" + QDateTime::fromMSecsSinceEpoch(it.value(), Qt::UTC).toString(Qt::ISODate) + ")");
}
return result.join(",");
}
void CDataCacheRevision::setTimeToLive(const QString &key, int ttl)
{
Q_ASSERT(! m_updateInProgress);

View File

@@ -126,6 +126,9 @@ namespace BlackMisc
//! Returns (by move) the container of promises to load values.
std::vector<std::promise<void>> loadedValuePromises();
//! Keys with timestamps.
QString timestampsAsString() const;
//! Set TTL value that will be written to the revision file.
void setTimeToLive(const QString &key, int ttl);

View File

@@ -91,6 +91,17 @@ namespace BlackMisc
return result;
}
QString CValueCachePacket::toTimestampMapString() const
{
auto map = toTimestampMap();
QStringList result;
for (auto it = map.cbegin(); it != map.cend(); ++it)
{
result.push_back(it.key() + "(" + QDateTime::fromMSecsSinceEpoch(it.value(), Qt::UTC).toString(Qt::ISODate) + ")");
}
return result.join(",");
}
CValueCachePacket CValueCachePacket::takeByKey(const QString &key)
{
auto copy = *this;
@@ -269,7 +280,7 @@ namespace BlackMisc
return status;
}
CStatusMessage CValueCache::saveToFiles(const QString &dir, const CVariantMap &values) const
CStatusMessage CValueCache::saveToFiles(const QString &dir, const CVariantMap &values, const QString &keysMessage) const
{
QMap<QString, CVariantMap> namespaces;
for (auto it = values.cbegin(); it != values.cend(); ++it)
@@ -302,7 +313,8 @@ namespace BlackMisc
return CStatusMessage(this).error("Failed to write to %1: %2") << file.fileName() << file.errorString();
}
}
return CStatusMessage(this).info("Written %1 to value cache in %2") << values.keys().to<QStringList>().join(",") << dir;
return CStatusMessage(this).info("Written %1 to value cache in %2") <<
(keysMessage.isEmpty() ? values.keys().to<QStringList>().join(",") : keysMessage) << dir;
}
CStatusMessage CValueCache::loadFromFiles(const QString &dir)
@@ -315,7 +327,7 @@ namespace BlackMisc
return status;
}
CStatusMessage CValueCache::loadFromFiles(const QString &dir, const QSet<QString> &keys, const CVariantMap &currentValues, CValueCachePacket &o_values) const
CStatusMessage CValueCache::loadFromFiles(const QString &dir, const QSet<QString> &keys, const CVariantMap &currentValues, CValueCachePacket &o_values, const QString &keysMessage) const
{
if (! QDir(dir).isReadable())
{
@@ -344,7 +356,8 @@ namespace BlackMisc
temp.removeDuplicates(currentValues);
o_values.insert(temp, QFileInfo(file).lastModified().toMSecsSinceEpoch());
}
return CStatusMessage(this).info("Loaded cache values %1 from %2") << o_values.keys().to<QStringList>().join(",") << dir;
return CStatusMessage(this).info("Loaded cache values %1 from %2") <<
(keysMessage.isEmpty() ? o_values.keys().to<QStringList>().join(",") : keysMessage) << dir;
}
void CValueCache::markAllAsSaved(const QString &keyPrefix)

View File

@@ -93,6 +93,9 @@ namespace BlackMisc
//! Discard values and return as map of timestamps.
QMap<QString, qint64> toTimestampMap() const;
//! Return map of timestamps converted to string.
QString toTimestampMapString() const;
//! Remove value matching the given key, and return it in a separate packet.
CValueCachePacket takeByKey(const QString &key);
@@ -245,11 +248,11 @@ namespace BlackMisc
//! Save specific values to Json files in a given directory.
//! \threadsafe
CStatusMessage saveToFiles(const QString &directory, const CVariantMap &values) const;
CStatusMessage saveToFiles(const QString &directory, const CVariantMap &values, const QString &keysMessage = {}) const;
//! Load from Json files in a given directory any values which differ from the current ones, and insert them in o_values.
//! \threadsafe
CStatusMessage loadFromFiles(const QString &directory, const QSet<QString> &keys, const CVariantMap &current, CValueCachePacket &o_values) const;
CStatusMessage loadFromFiles(const QString &directory, const QSet<QString> &keys, const CVariantMap &current, CValueCachePacket &o_values, const QString &keysMessage = {}) const;
//! Mark all values with keys that start with the given prefix as having been saved.
//! \threadsafe