summary refs log tree commit diff
path: root/src/Cache.cc
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-10 12:51:44 +0200
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-12-10 12:51:44 +0200
commitd872b1060bb362d439f971fa5f9f04e1907d7410 (patch)
tree224a02cb0305bd82bf34705eda4ce312dc077073 /src/Cache.cc
parentHandle malformed state events (diff)
downloadnheko-d872b1060bb362d439f971fa5f9f04e1907d7410.tar.xz
Reset cache on breaking changes
fixes #154
Diffstat (limited to 'src/Cache.cc')
-rw-r--r--src/Cache.cc42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/Cache.cc b/src/Cache.cc

index 087dd4bc..7d044e8f 100644 --- a/src/Cache.cc +++ b/src/Cache.cc
@@ -24,8 +24,10 @@ #include "Cache.h" #include "RoomState.h" +static const std::string CURRENT_CACHE_FORMAT_VERSION("2017.12.10"); + static const lmdb::val NEXT_BATCH_KEY("next_batch"); -static const lmdb::val transactionID("transaction_id"); +static const lmdb::val CACHE_FORMAT_VERSION_KEY("cache_format_version"); Cache::Cache(const QString &userId) : env_{nullptr} @@ -270,3 +272,41 @@ Cache::deleteData() if (!cacheDirectory_.isEmpty()) QDir(cacheDirectory_).removeRecursively(); } + +bool +Cache::isFormatValid() +{ + auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); + + lmdb::val current_version; + bool res = lmdb::dbi_get(txn, stateDb_, CACHE_FORMAT_VERSION_KEY, current_version); + + txn.commit(); + + if (!res) + return false; + + std::string stored_version(current_version.data(), current_version.size()); + + if (stored_version != CURRENT_CACHE_FORMAT_VERSION) { + qWarning() << "Stored format version" << QString::fromStdString(stored_version); + qWarning() << "There are breaking changes in the cache format."; + return false; + } + + return true; +} + +void +Cache::setCurrentFormat() +{ + auto txn = lmdb::txn::begin(env_); + + lmdb::dbi_put( + txn, + stateDb_, + CACHE_FORMAT_VERSION_KEY, + lmdb::val(CURRENT_CACHE_FORMAT_VERSION.data(), CURRENT_CACHE_FORMAT_VERSION.size())); + + txn.commit(); +}