mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-26 13:08:48 +03:00
parent
199a4eaf51
commit
345dca3544
3 changed files with 77 additions and 2 deletions
|
@ -41,6 +41,9 @@ static const lmdb::val CACHE_FORMAT_VERSION_KEY("cache_format_version");
|
||||||
|
|
||||||
constexpr size_t MAX_RESTORED_MESSAGES = 30;
|
constexpr size_t MAX_RESTORED_MESSAGES = 30;
|
||||||
|
|
||||||
|
constexpr auto DB_SIZE = 256UL * 1024UL * 1024UL; // 256 MB
|
||||||
|
constexpr auto MAX_DBS = 1024UL;
|
||||||
|
|
||||||
//! Cache databases and their format.
|
//! Cache databases and their format.
|
||||||
//!
|
//!
|
||||||
//! Contains UI information for the joined rooms. (i.e name, topic, avatar url etc).
|
//! Contains UI information for the joined rooms. (i.e name, topic, avatar url etc).
|
||||||
|
@ -134,8 +137,8 @@ Cache::setup()
|
||||||
bool isInitial = !QFile::exists(statePath);
|
bool isInitial = !QFile::exists(statePath);
|
||||||
|
|
||||||
env_ = lmdb::env::create();
|
env_ = lmdb::env::create();
|
||||||
env_.set_mapsize(256UL * 1024UL * 1024UL); /* 256 MB */
|
env_.set_mapsize(DB_SIZE);
|
||||||
env_.set_max_dbs(1024UL);
|
env_.set_max_dbs(MAX_DBS);
|
||||||
|
|
||||||
if (isInitial) {
|
if (isInitial) {
|
||||||
nhlog::db()->info("initializing LMDB");
|
nhlog::db()->info("initializing LMDB");
|
||||||
|
@ -1786,6 +1789,67 @@ Cache::isNotificationSent(const std::string &event_id)
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string>
|
||||||
|
Cache::getRoomIds(lmdb::txn &txn)
|
||||||
|
{
|
||||||
|
auto db = lmdb::dbi::open(txn, ROOMS_DB, MDB_CREATE);
|
||||||
|
auto cursor = lmdb::cursor::open(txn, db);
|
||||||
|
|
||||||
|
std::vector<std::string> rooms;
|
||||||
|
|
||||||
|
std::string room_id, _unused;
|
||||||
|
while (cursor.get(room_id, _unused, MDB_NEXT))
|
||||||
|
rooms.emplace_back(room_id);
|
||||||
|
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
|
return rooms;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Cache::deleteOldMessages()
|
||||||
|
{
|
||||||
|
auto txn = lmdb::txn::begin(env_);
|
||||||
|
auto room_ids = getRoomIds(txn);
|
||||||
|
|
||||||
|
for (const auto &id : room_ids) {
|
||||||
|
auto msg_db = getMessagesDb(txn, id);
|
||||||
|
|
||||||
|
std::string ts, event;
|
||||||
|
uint64_t idx = 0;
|
||||||
|
|
||||||
|
const auto db_size = msg_db.size(txn);
|
||||||
|
if (db_size <= 3 * MAX_RESTORED_MESSAGES)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
nhlog::db()->info("[{}] message count: {}", id, db_size);
|
||||||
|
|
||||||
|
auto cursor = lmdb::cursor::open(txn, msg_db);
|
||||||
|
while (cursor.get(ts, event, MDB_NEXT)) {
|
||||||
|
idx += 1;
|
||||||
|
|
||||||
|
if (idx > MAX_RESTORED_MESSAGES)
|
||||||
|
lmdb::cursor_del(cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
|
nhlog::db()->info("[{}] updated message count: {}", id, msg_db.size(txn));
|
||||||
|
}
|
||||||
|
|
||||||
|
txn.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
Cache::deleteOldData() noexcept
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
deleteOldMessages();
|
||||||
|
} catch (const lmdb::error &e) {
|
||||||
|
nhlog::db()->error("failed to delete old messages: {}", e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
Cache::hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
|
Cache::hasEnoughPowerLevel(const std::vector<mtx::events::EventType> &eventTypes,
|
||||||
const std::string &room_id,
|
const std::string &room_id,
|
||||||
|
|
|
@ -388,6 +388,12 @@ public:
|
||||||
//! Check if we have sent a desktop notification for the given event id.
|
//! Check if we have sent a desktop notification for the given event id.
|
||||||
bool isNotificationSent(const std::string &event_id);
|
bool isNotificationSent(const std::string &event_id);
|
||||||
|
|
||||||
|
//! Remove old unused data.
|
||||||
|
void deleteOldMessages();
|
||||||
|
void deleteOldData() noexcept;
|
||||||
|
//! Retrieve all saved room ids.
|
||||||
|
std::vector<std::string> getRoomIds(lmdb::txn &txn);
|
||||||
|
|
||||||
//! Mark a room that uses e2e encryption.
|
//! Mark a room that uses e2e encryption.
|
||||||
void setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
|
void setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
|
||||||
bool isRoomEncrypted(const std::string &room_id);
|
bool isRoomEncrypted(const std::string &room_id);
|
||||||
|
|
|
@ -1070,6 +1070,11 @@ ChatPage::trySync()
|
||||||
|
|
||||||
emit syncTopBar(updates);
|
emit syncTopBar(updates);
|
||||||
emit syncRoomlist(updates);
|
emit syncRoomlist(updates);
|
||||||
|
|
||||||
|
cache::client()->deleteOldData();
|
||||||
|
} catch (const lmdb::map_full_error &e) {
|
||||||
|
nhlog::db()->error("lmdb is full: {}", e.what());
|
||||||
|
cache::client()->deleteOldData();
|
||||||
} catch (const lmdb::error &e) {
|
} catch (const lmdb::error &e) {
|
||||||
nhlog::db()->error("saving sync response: {}", e.what());
|
nhlog::db()->error("saving sync response: {}", e.what());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue