Update the cache state at once to avoid being in an invalid state

This commit is contained in:
Konstantinos Sideris 2017-08-26 13:49:16 +03:00
parent 43b1bdfe63
commit 7fbfe3af15
6 changed files with 844 additions and 831 deletions

View file

@ -26,17 +26,19 @@ class Cache
public:
Cache(const QString &userId);
void insertRoomState(const QString &roomid, const RoomState &state);
void setNextBatchToken(const QString &token);
bool isInitialized();
void setState(const QString &nextBatchToken, const QMap<QString, RoomState> &states);
bool isInitialized() const;
QString nextBatchToken();
QString nextBatchToken() const;
QMap<QString, RoomState> states();
inline void unmount();
inline QString memberDbName(const QString &roomid);
private:
void setNextBatchToken(lmdb::txn &txn, const QString &token);
void insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state);
lmdb::env env_;
lmdb::dbi stateDb_;
lmdb::dbi roomDb_;

View file

@ -67,7 +67,6 @@ protected:
private:
void updateDisplayNames(const RoomState &state);
void updateRoomState(RoomState &room_state, const QJsonArray &events);
void loadStateFromCache();
void showQuickSwitcher();

View file

@ -54,6 +54,7 @@ public:
void removeLeaveMemberships();
void update(const RoomState &state);
void updateFromEvents(const QJsonArray &events);
QJsonObject serialize() const;

View file

@ -60,7 +60,8 @@ Cache::Cache(const QString &userId)
env_.open(statePath.toStdString().c_str());
} catch (const lmdb::error &e) {
if (e.code() != MDB_VERSION_MISMATCH && e.code() != MDB_INVALID) {
throw std::runtime_error("LMDB initialization failed" + std::string(e.what()));
throw std::runtime_error("LMDB initialization failed" +
std::string(e.what()));
}
qWarning() << "Resetting cache due to LMDB version mismatch:" << e.what();
@ -69,7 +70,8 @@ Cache::Cache(const QString &userId)
for (const auto &file : stateDir.entryList(QDir::NoDotAndDotDot)) {
if (!stateDir.remove(file))
throw std::runtime_error(("Unable to delete file " + file).toStdString().c_str());
throw std::runtime_error(
("Unable to delete file " + file).toStdString().c_str());
}
env_.open(statePath.toStdString().c_str());
@ -85,20 +87,35 @@ Cache::Cache(const QString &userId)
}
void
Cache::insertRoomState(const QString &roomid, const RoomState &state)
Cache::setState(const QString &nextBatchToken, const QMap<QString, RoomState> &states)
{
if (!isMounted_)
return;
auto txn = lmdb::txn::begin(env_);
setNextBatchToken(txn, nextBatchToken);
for (auto it = states.constBegin(); it != states.constEnd(); it++)
insertRoomState(txn, it.key(), it.value());
txn.commit();
}
void
Cache::insertRoomState(lmdb::txn &txn, const QString &roomid, const RoomState &state)
{
auto stateEvents = QJsonDocument(state.serialize()).toBinaryData();
auto id = roomid.toUtf8();
lmdb::dbi_put(txn, roomDb_, lmdb::val(id.data(), id.size()), lmdb::val(stateEvents.data(), stateEvents.size()));
lmdb::dbi_put(txn,
roomDb_,
lmdb::val(id.data(), id.size()),
lmdb::val(stateEvents.data(), stateEvents.size()));
for (const auto &membership : state.memberships) {
lmdb::dbi membersDb = lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
lmdb::dbi membersDb =
lmdb::dbi::open(txn, roomid.toStdString().c_str(), MDB_CREATE);
// The user_id this membership event relates to, is used
// as the index on the membership database.
@ -130,8 +147,6 @@ Cache::insertRoomState(const QString &roomid, const RoomState &state)
}
}
}
txn.commit();
}
QMap<QString, RoomState>
@ -148,7 +163,8 @@ Cache::states()
// Retrieve all the room names.
while (cursor.get(room, stateData, MDB_NEXT)) {
auto roomid = QString::fromUtf8(room.data(), room.size());
auto json = QJsonDocument::fromBinaryData(QByteArray(stateData.data(), stateData.size()));
auto json =
QJsonDocument::fromBinaryData(QByteArray(stateData.data(), stateData.size()));
RoomState state;
state.parse(json.object());
@ -163,8 +179,8 @@ Cache::states()
while (memberCursor.get(memberId, memberContent, MDB_NEXT)) {
auto userid = QString::fromUtf8(memberId.data(), memberId.size());
auto data =
QJsonDocument::fromBinaryData(QByteArray(memberContent.data(), memberContent.size()));
auto data = QJsonDocument::fromBinaryData(
QByteArray(memberContent.data(), memberContent.size()));
try {
events::StateEvent<events::MemberEventContent> member;
@ -193,18 +209,15 @@ Cache::states()
}
void
Cache::setNextBatchToken(const QString &token)
Cache::setNextBatchToken(lmdb::txn &txn, const QString &token)
{
auto txn = lmdb::txn::begin(env_);
auto value = token.toUtf8();
lmdb::dbi_put(txn, stateDb_, NEXT_BATCH_KEY, lmdb::val(value.data(), value.size()));
txn.commit();
}
bool
Cache::isInitialized()
Cache::isInitialized() const
{
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
lmdb::val token;
@ -217,7 +230,7 @@ Cache::isInitialized()
}
QString
Cache::nextBatchToken()
Cache::nextBatchToken() const
{
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
lmdb::val token;

View file

@ -18,26 +18,15 @@
#include <QDebug>
#include <QSettings>
#include "AvatarProvider.h"
#include "ChatPage.h"
#include "MainWindow.h"
#include "Splitter.h"
#include "Sync.h"
#include "Theme.h"
#include "TimelineViewManager.h"
#include "UserInfoWidget.h"
#include "AliasesEventContent.h"
#include "AvatarEventContent.h"
#include "AvatarProvider.h"
#include "CanonicalAliasEventContent.h"
#include "CreateEventContent.h"
#include "HistoryVisibilityEventContent.h"
#include "JoinRulesEventContent.h"
#include "MainWindow.h"
#include "MemberEventContent.h"
#include "NameEventContent.h"
#include "PowerLevelsEventContent.h"
#include "TopicEventContent.h"
#include "StateEvent.h"
namespace events = matrix::events;
@ -127,9 +116,13 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
connect(room_list_, &RoomList::roomChanged, this, &ChatPage::changeTopRoomInfo);
connect(room_list_, &RoomList::roomChanged, text_input_, &TextInputWidget::focusLineEdit);
connect(room_list_, &RoomList::roomChanged, view_manager_, &TimelineViewManager::setHistoryView);
connect(
room_list_, &RoomList::roomChanged, view_manager_, &TimelineViewManager::setHistoryView);
connect(view_manager_, &TimelineViewManager::unreadMessages, this, [=](const QString &roomid, int count) {
connect(view_manager_,
&TimelineViewManager::unreadMessages,
this,
[=](const QString &roomid, int count) {
if (!settingsManager_.contains(roomid)) {
qWarning() << "RoomId does not have settings" << roomid;
room_list_->updateUnreadMessageCount(roomid, count);
@ -168,12 +161,18 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
SIGNAL(syncCompleted(const SyncResponse &)),
this,
SLOT(syncCompleted(const SyncResponse &)));
connect(client_.data(), SIGNAL(syncFailed(const QString &)), this, SLOT(syncFailed(const QString &)));
connect(client_.data(),
SIGNAL(syncFailed(const QString &)),
this,
SLOT(syncFailed(const QString &)));
connect(client_.data(),
SIGNAL(getOwnProfileResponse(const QUrl &, const QString &)),
this,
SLOT(updateOwnProfileInfo(const QUrl &, const QString &)));
connect(client_.data(), SIGNAL(ownAvatarRetrieved(const QPixmap &)), this, SLOT(setOwnAvatar(const QPixmap &)));
connect(client_.data(),
SIGNAL(ownAvatarRetrieved(const QPixmap &)),
this,
SLOT(setOwnAvatar(const QPixmap &)));
AvatarProvider::init(client);
}
@ -265,10 +264,6 @@ ChatPage::updateDisplayNames(const RoomState &state)
void
ChatPage::syncCompleted(const SyncResponse &response)
{
// TODO: Catch exception
cache_->setNextBatchToken(response.nextBatch());
client_->setNextBatchToken(response.nextBatch());
auto joined = response.rooms().join();
for (auto it = joined.constBegin(); it != joined.constEnd(); it++) {
@ -278,18 +273,10 @@ ChatPage::syncCompleted(const SyncResponse &response)
if (state_manager_.contains(it.key()))
room_state = state_manager_[it.key()];
updateRoomState(room_state, it.value().state().events());
updateRoomState(room_state, it.value().timeline().events());
updateDisplayNames(room_state);
room_state.updateFromEvents(it.value().state().events());
room_state.updateFromEvents(it.value().timeline().events());
try {
cache_->insertRoomState(it.key(), room_state);
} catch (const lmdb::error &e) {
qCritical() << e.what();
// Stop using the cache if an errors occurs.
// TODO: Should also be marked as invalid and be deleted.
cache_->unmount();
}
updateDisplayNames(room_state);
if (state_manager_.contains(it.key())) {
// TODO: Use pointers instead of copying.
@ -304,6 +291,16 @@ ChatPage::syncCompleted(const SyncResponse &response)
changeTopRoomInfo(it.key());
}
try {
cache_->setState(response.nextBatch(), state_manager_);
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be updated: " << e.what();
// TODO: Notify the user.
cache_->unmount();
}
client_->setNextBatchToken(response.nextBatch());
room_list_->sync(state_manager_);
view_manager_->sync(response.rooms());
@ -313,20 +310,14 @@ ChatPage::syncCompleted(const SyncResponse &response)
void
ChatPage::initialSyncCompleted(const SyncResponse &response)
{
if (!response.nextBatch().isEmpty())
client_->setNextBatchToken(response.nextBatch());
auto joined = response.rooms().join();
// TODO: Catch exception
cache_->setNextBatchToken(response.nextBatch());
for (auto it = joined.constBegin(); it != joined.constEnd(); it++) {
RoomState room_state;
// Build the current state from the timeline and state events.
updateRoomState(room_state, it.value().state().events());
updateRoomState(room_state, it.value().timeline().events());
room_state.updateFromEvents(it.value().state().events());
room_state.updateFromEvents(it.value().timeline().events());
// Remove redundant memberships.
room_state.removeLeaveMemberships();
@ -335,19 +326,11 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
room_state.resolveName();
room_state.resolveAvatar();
try {
cache_->insertRoomState(it.key(), room_state);
} catch (const lmdb::error &e) {
qCritical() << e.what();
// Stop using the cache if an errors occurs.
// TODO: Should also be marked as invalid and be deleted.
cache_->unmount();
}
updateDisplayNames(room_state);
state_manager_.insert(it.key(), room_state);
settingsManager_.insert(it.key(), QSharedPointer<RoomSettings>(new RoomSettings(it.key())));
settingsManager_.insert(it.key(),
QSharedPointer<RoomSettings>(new RoomSettings(it.key())));
for (const auto membership : room_state.memberships) {
auto uid = membership.sender();
@ -358,6 +341,15 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
}
}
try {
cache_->setState(response.nextBatch(), state_manager_);
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be initialized: " << e.what();
cache_->unmount();
}
client_->setNextBatchToken(response.nextBatch());
// Populate timelines with messages.
view_manager_->initialize(response.rooms());
@ -425,97 +417,6 @@ ChatPage::showUnreadMessageNotification(int count)
emit changeWindowTitle(QString("nheko (%1)").arg(count));
}
void
ChatPage::updateRoomState(RoomState &room_state, const QJsonArray &events)
{
events::EventType ty;
for (const auto &event : events) {
try {
ty = events::extractEventType(event.toObject());
} catch (const DeserializationException &e) {
qWarning() << e.what() << event;
continue;
}
if (!events::isStateEvent(ty))
continue;
try {
switch (ty) {
case events::EventType::RoomAliases: {
events::StateEvent<events::AliasesEventContent> aliases;
aliases.deserialize(event);
room_state.aliases = aliases;
break;
}
case events::EventType::RoomAvatar: {
events::StateEvent<events::AvatarEventContent> avatar;
avatar.deserialize(event);
room_state.avatar = avatar;
break;
}
case events::EventType::RoomCanonicalAlias: {
events::StateEvent<events::CanonicalAliasEventContent> canonical_alias;
canonical_alias.deserialize(event);
room_state.canonical_alias = canonical_alias;
break;
}
case events::EventType::RoomCreate: {
events::StateEvent<events::CreateEventContent> create;
create.deserialize(event);
room_state.create = create;
break;
}
case events::EventType::RoomHistoryVisibility: {
events::StateEvent<events::HistoryVisibilityEventContent> history_visibility;
history_visibility.deserialize(event);
room_state.history_visibility = history_visibility;
break;
}
case events::EventType::RoomJoinRules: {
events::StateEvent<events::JoinRulesEventContent> join_rules;
join_rules.deserialize(event);
room_state.join_rules = join_rules;
break;
}
case events::EventType::RoomName: {
events::StateEvent<events::NameEventContent> name;
name.deserialize(event);
room_state.name = name;
break;
}
case events::EventType::RoomMember: {
events::StateEvent<events::MemberEventContent> member;
member.deserialize(event);
room_state.memberships.insert(member.stateKey(), member);
break;
}
case events::EventType::RoomPowerLevels: {
events::StateEvent<events::PowerLevelsEventContent> power_levels;
power_levels.deserialize(event);
room_state.power_levels = power_levels;
break;
}
case events::EventType::RoomTopic: {
events::StateEvent<events::TopicEventContent> topic;
topic.deserialize(event);
room_state.topic = topic;
break;
}
default: {
continue;
}
}
} catch (const DeserializationException &e) {
qWarning() << e.what() << event;
continue;
}
}
}
void
ChatPage::loadStateFromCache()
{
@ -548,7 +449,8 @@ ChatPage::loadStateFromCache()
state_manager_.insert(it.key(), room_state);
// Create or restore the settings for this room.
settingsManager_.insert(it.key(), QSharedPointer<RoomSettings>(new RoomSettings(it.key())));
settingsManager_.insert(it.key(),
QSharedPointer<RoomSettings>(new RoomSettings(it.key())));
// Resolve user avatars.
for (const auto membership : room_state.memberships) {
@ -587,7 +489,10 @@ ChatPage::showQuickSwitcher()
if (quickSwitcher_ == nullptr) {
quickSwitcher_ = new QuickSwitcher(this);
connect(quickSwitcher_, &QuickSwitcher::roomSelected, room_list_, &RoomList::highlightSelectedRoom);
connect(quickSwitcher_,
&QuickSwitcher::roomSelected,
room_list_,
&RoomList::highlightSelectedRoom);
connect(quickSwitcher_, &QuickSwitcher::closing, this, [=]() {
if (this->quickSwitcherModal_ != nullptr)
this->quickSwitcherModal_->fadeOut();

View file

@ -295,3 +295,96 @@ RoomState::parse(const QJsonObject &object)
}
}
}
void
RoomState::updateFromEvents(const QJsonArray &events)
{
events::EventType ty;
for (const auto &event : events) {
try {
ty = events::extractEventType(event.toObject());
} catch (const DeserializationException &e) {
qWarning() << e.what() << event;
continue;
}
if (!events::isStateEvent(ty))
continue;
try {
switch (ty) {
case events::EventType::RoomAliases: {
events::StateEvent<events::AliasesEventContent> aliases;
aliases.deserialize(event);
this->aliases = aliases;
break;
}
case events::EventType::RoomAvatar: {
events::StateEvent<events::AvatarEventContent> avatar;
avatar.deserialize(event);
this->avatar = avatar;
break;
}
case events::EventType::RoomCanonicalAlias: {
events::StateEvent<events::CanonicalAliasEventContent>
canonical_alias;
canonical_alias.deserialize(event);
this->canonical_alias = canonical_alias;
break;
}
case events::EventType::RoomCreate: {
events::StateEvent<events::CreateEventContent> create;
create.deserialize(event);
this->create = create;
break;
}
case events::EventType::RoomHistoryVisibility: {
events::StateEvent<events::HistoryVisibilityEventContent>
history_visibility;
history_visibility.deserialize(event);
this->history_visibility = history_visibility;
break;
}
case events::EventType::RoomJoinRules: {
events::StateEvent<events::JoinRulesEventContent> join_rules;
join_rules.deserialize(event);
this->join_rules = join_rules;
break;
}
case events::EventType::RoomName: {
events::StateEvent<events::NameEventContent> name;
name.deserialize(event);
this->name = name;
break;
}
case events::EventType::RoomMember: {
events::StateEvent<events::MemberEventContent> member;
member.deserialize(event);
this->memberships.insert(member.stateKey(), member);
break;
}
case events::EventType::RoomPowerLevels: {
events::StateEvent<events::PowerLevelsEventContent> power_levels;
power_levels.deserialize(event);
this->power_levels = power_levels;
break;
}
case events::EventType::RoomTopic: {
events::StateEvent<events::TopicEventContent> topic;
topic.deserialize(event);
this->topic = topic;
break;
}
default: {
continue;
}
}
} catch (const DeserializationException &e) {
qWarning() << e.what() << event;
continue;
}
}
}