mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-23 03:18:49 +03:00
parent
bbe69a4fb6
commit
d872b1060b
3 changed files with 50 additions and 1 deletions
|
@ -39,6 +39,9 @@ public:
|
||||||
void removeRoom(const QString &roomid);
|
void removeRoom(const QString &roomid);
|
||||||
void setup();
|
void setup();
|
||||||
|
|
||||||
|
bool isFormatValid();
|
||||||
|
void setCurrentFormat();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setNextBatchToken(lmdb::txn &txn, const QString &token);
|
void setNextBatchToken(lmdb::txn &txn, const QString &token);
|
||||||
void insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state);
|
void insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state);
|
||||||
|
|
42
src/Cache.cc
42
src/Cache.cc
|
@ -24,8 +24,10 @@
|
||||||
#include "Cache.h"
|
#include "Cache.h"
|
||||||
#include "RoomState.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 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)
|
Cache::Cache(const QString &userId)
|
||||||
: env_{nullptr}
|
: env_{nullptr}
|
||||||
|
@ -270,3 +272,41 @@ Cache::deleteData()
|
||||||
if (!cacheDirectory_.isEmpty())
|
if (!cacheDirectory_.isEmpty())
|
||||||
QDir(cacheDirectory_).removeRecursively();
|
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();
|
||||||
|
}
|
||||||
|
|
|
@ -334,6 +334,12 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
|
||||||
try {
|
try {
|
||||||
cache_->setup();
|
cache_->setup();
|
||||||
|
|
||||||
|
if (!cache_->isFormatValid()) {
|
||||||
|
cache_->deleteData();
|
||||||
|
cache_->setup();
|
||||||
|
cache_->setCurrentFormat();
|
||||||
|
}
|
||||||
|
|
||||||
if (cache_->isInitialized()) {
|
if (cache_->isInitialized()) {
|
||||||
loadStateFromCache();
|
loadStateFromCache();
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue